From 0a939ccb3e83564ef5c6c1cf3576e8b58be9707d Mon Sep 17 00:00:00 2001 From: Mukilan Thiyagarajan Date: Sun, 15 Dec 2024 17:26:34 +0530 Subject: [PATCH] allow invoking mach from arbitary current working directory --- mach | 7 ++++--- mach.bat | 3 ++- python/mach_bootstrap.py | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mach b/mach index 0e28bab8d846a..25ec56882ce80 100755 --- a/mach +++ b/mach @@ -10,17 +10,18 @@ # given in `.python-version` and python/mach_bootstrap.py will provision a new # environment that will be used for the subsequent runs. ''':' && { + MACH_DIR=$(dirname "$0"); run_in_nix_if_needed() { if ([ -f /etc/NIXOS ] || [ -n "${MACH_USE_NIX}" ]) && [ -z "${IN_NIX_SHELL}" ]; then EXTRA_NIX_ARGS=${SERVO_ANDROID_BUILD:+'--arg buildAndroid true'} - echo "NOTE: Entering nix-shell $(pwd)/shell.nix" - exec nix-shell $EXTRA_NIX_ARGS --run "$*" + echo "NOTE: Entering nix-shell ${MACH_DIR}/shell.nix" + exec nix-shell ${MACH_DIR}/shell.nix $EXTRA_NIX_ARGS --run "$*" else exec $* fi } - run_in_nix_if_needed uv run python ./mach $* + run_in_nix_if_needed uv run python ${MACH_DIR}/mach $* } ''' diff --git a/mach.bat b/mach.bat index 1a90d493e4498..4e0d273439cf4 100644 --- a/mach.bat +++ b/mach.bat @@ -1,3 +1,4 @@ @echo off -uv run python .\mach %* +set workdir=%~dp0 +uv run python %workdir%mach %* diff --git a/python/mach_bootstrap.py b/python/mach_bootstrap.py index 88bced77f7424..ee99326d1dbd0 100644 --- a/python/mach_bootstrap.py +++ b/python/mach_bootstrap.py @@ -80,9 +80,9 @@ } -def _process_exec(args): +def _process_exec(args, cwd): try: - subprocess.check_output(args, stderr=subprocess.STDOUT) + subprocess.check_output(args, stderr=subprocess.STDOUT, cwd=cwd) except subprocess.CalledProcessError as exception: print(exception.output.decode(sys.stdout.encoding)) print(f"Process failed with return code: {exception.returncode}") @@ -115,7 +115,8 @@ def install_virtual_env_requirements(project_path: str, virtualenv_path: str): _process_exec(["uv", "pip", "install", "-r", requirements_paths[0], "-r", requirements_paths[1], - "-r", requirements_paths[2]]) + "-r", requirements_paths[2]], + cwd=project_path) with open(marker_path, "w") as marker_file: marker_file.write(requirements_hash) @@ -126,7 +127,7 @@ def _activate_virtualenv(topdir): if os.environ.get("VIRTUAL_ENV") != virtualenv_path: if not os.path.exists(virtualenv_path): print(" * Setting up virtual environment...") - _process_exec(["uv", "venv"]) + _process_exec(["uv", "venv"], cwd=topdir) script_dir = "Scripts" if _is_windows() else "bin" runpy.run_path(os.path.join(virtualenv_path, script_dir, 'activate_this.py'))