From 67906c3c8404e55f57a25dd06acd6ddeb9c5e6c5 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 16 Feb 2024 13:11:32 -0800 Subject: [PATCH] MAINT Make search_pyodide_root return None instead of raising (#4520) Every spot that we call it we have a try block to catch this exception, so returning None makes the code more succinct --- pyodide-build/pyodide_build/build_env.py | 11 ++++------- pyodide-build/pyodide_build/cli/build_recipes.py | 7 ++----- pyodide-build/pyodide_build/cli/skeleton.py | 7 ++----- pyodide-build/pyodide_build/tests/test_build_env.py | 3 +-- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/pyodide-build/pyodide_build/build_env.py b/pyodide-build/pyodide_build/build_env.py index 15539ba12..22ae03392 100644 --- a/pyodide-build/pyodide_build/build_env.py +++ b/pyodide-build/pyodide_build/build_env.py @@ -99,9 +99,8 @@ def init_environment(*, quiet: bool = False) -> None: if "PYODIDE_ROOT" in os.environ: return - try: - root = search_pyodide_root(Path.cwd()) - except FileNotFoundError: # Not in Pyodide tree + root = search_pyodide_root(Path.cwd()) + if not root: # Not in Pyodide tree root = _init_xbuild_env(quiet=quiet) os.environ["PYODIDE_ROOT"] = str(root) @@ -137,7 +136,7 @@ def get_pyodide_root() -> Path: return Path(os.environ["PYODIDE_ROOT"]) -def search_pyodide_root(curdir: str | Path, *, max_depth: int = 10) -> Path: +def search_pyodide_root(curdir: str | Path, *, max_depth: int = 10) -> Path | None: """ Recursively search for the root of the Pyodide repository, by looking for the pyproject.toml file in the parent directories @@ -162,9 +161,7 @@ def search_pyodide_root(curdir: str | Path, *, max_depth: int = 10) -> Path: if "tool" in configs and "pyodide" in configs["tool"]: return base - raise FileNotFoundError( - "Could not find Pyodide root directory. If you are not in the Pyodide directory, set `PYODIDE_ROOT=`." - ) + return None def in_xbuildenv() -> bool: diff --git a/pyodide-build/pyodide_build/cli/build_recipes.py b/pyodide-build/pyodide_build/cli/build_recipes.py index 9aed5977a..8bf7f173d 100644 --- a/pyodide-build/pyodide_build/cli/build_recipes.py +++ b/pyodide-build/pyodide_build/cli/build_recipes.py @@ -29,11 +29,8 @@ class Args: force_rebuild: bool, n_jobs: int | None = None, ): - root = Path.cwd() - try: - root = build_env.search_pyodide_root(root) - except FileNotFoundError: - pass + cwd = Path.cwd() + root = build_env.search_pyodide_root(cwd) or cwd self.recipe_dir = ( root / "packages" if not recipe_dir else Path(recipe_dir).resolve() ) diff --git a/pyodide-build/pyodide_build/cli/skeleton.py b/pyodide-build/pyodide_build/cli/skeleton.py index 1696fe690..0797e5db1 100644 --- a/pyodide-build/pyodide_build/cli/skeleton.py +++ b/pyodide-build/pyodide_build/cli/skeleton.py @@ -56,13 +56,10 @@ def new_recipe_pypi( else: cwd = Path.cwd() - try: - root = build_env.search_pyodide_root(cwd) - except FileNotFoundError: - root = cwd - if build_env.in_xbuildenv(): root = cwd + else: + root = build_env.search_pyodide_root(cwd) or cwd recipe_dir_ = root / "packages" diff --git a/pyodide-build/pyodide_build/tests/test_build_env.py b/pyodide-build/pyodide_build/tests/test_build_env.py index 1ace9b4a1..d02427da3 100644 --- a/pyodide-build/pyodide_build/tests/test_build_env.py +++ b/pyodide-build/pyodide_build/tests/test_build_env.py @@ -52,8 +52,7 @@ class TestInTree: assert build_env.search_pyodide_root(tmp_path / "subdir" / "subdir") == tmp_path pyproject_file.unlink() - with pytest.raises(FileNotFoundError): - build_env.search_pyodide_root(tmp_path) + assert build_env.search_pyodide_root(tmp_path) is None def test_in_xbuildenv(self, reset_env_vars, reset_cache): assert not build_env.in_xbuildenv()