From 664154a905e1abdcb8512a1f587e912a3d72d516 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 12 Feb 2025 20:50:25 -0800 Subject: [PATCH] ninja: Improve depot_tools directory detection Tweak the code in ninja.py so it would only skip directories in $PATH that are named "depot_tools". The existing detection logic skips "foo_depot_tools". Also rename functions in the file to follow Python Style and auto-format the file. Change-Id: I631958768168f0d673b824c8e14783d2c6f65563 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6259139 Reviewed-by: Junji Watanabe Commit-Queue: Lei Zhang --- ninja.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ninja.py b/ninja.py index 49aa71b80..a1dffe14a 100755 --- a/ninja.py +++ b/ninja.py @@ -15,7 +15,7 @@ import gclient_paths import gn_helper -def findNinjaInPath(): +def find_ninja_in_path(): env_path = os.getenv("PATH") if not env_path: return @@ -23,14 +23,16 @@ def findNinjaInPath(): if sys.platform in ("win32", "cygwin"): exe += ".exe" for bin_dir in env_path.split(os.pathsep): - if bin_dir.rstrip(os.sep).endswith("depot_tools"): + bin_dir = bin_dir.rstrip(os.sep) + if os.path.basename(bin_dir) == "depot_tools": # skip depot_tools to avoid calling ninja.py infinitely. continue ninja_path = os.path.join(bin_dir, exe) if os.path.isfile(ninja_path): return ninja_path -def checkOutdir(ninja_args): + +def check_out_dir(ninja_args): out_dir = "." tool = "" for i, arg in enumerate(ninja_args): @@ -65,11 +67,12 @@ def checkOutdir(ninja_args): file=sys.stderr) sys.exit(1) + def fallback(ninja_args): # Try to find ninja in PATH. - ninja_path = findNinjaInPath() + ninja_path = find_ninja_in_path() if ninja_path: - checkOutdir(ninja_args) + check_out_dir(ninja_args) return subprocess.call([ninja_path] + ninja_args) print( @@ -120,7 +123,7 @@ def main(args): "ninja" + gclient_paths.GetExeSuffix(), ) if os.path.isfile(ninja_path): - checkOutdir(args[1:]) + check_out_dir(args[1:]) return subprocess.call([ninja_path] + args[1:]) return fallback(args[1:])