Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Recursively look for the project root #2286

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ The following configuration items can be retrieved and modified by [`pdm config`
| `install.cache` | Enable caching of wheel installations | False | Yes | |
| `install.cache_method` | Specify how to create links to the caches(`symlink` or `pth`) | `symlink` | Yes | |
| `install.parallel` | Whether to perform installation and uninstallation in parallel | `True` | Yes | `PDM_PARALLEL_INSTALL` |
| `project_max_depth` | The max depth to search for a project through the parents | 5 | No | `PDM_PROJECT_MAX_DEPTH` |
| `python.use_pyenv` | Use the pyenv interpreter | `True` | Yes | |
| `python.use_venv` | Use virtual environments when available | `True` | Yes | `PDM_USE_VENV` |
| `python.providers` | List of python provider names for findpython | All providers supported by findpython | Yes | |
Expand Down
23 changes: 12 additions & 11 deletions src/pdm/pep582/sitecustomize.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import os
import site
import sys
from pathlib import Path


def get_pypackages_path():
def find_pypackage(path, version):
if not os.path.exists(path):
if not path.exists():
return None

packages_name = f"__pypackages__/{version}/lib"
for _ in range(int(os.getenv("PDM_PROJECT_MAX_DEPTH", "5"))):
if os.path.exists(os.path.join(path, packages_name)):
return os.path.join(path, packages_name)
if os.path.dirname(path) == path:
# Root path is reached
break
path = os.path.dirname(path)
return None
files = list(path.glob(packages_name))
if files:
return str(files[0])

if path == path.parent:
return None

return find_pypackage(path.parent, version)

if "PEP582_PACKAGES" in os.environ:
return os.path.join(os.getenv("PEP582_PACKAGES"), "lib")
Expand All @@ -30,13 +31,13 @@ def find_pypackage(path, version):
find_paths.insert(0, script_dir)

for path in find_paths:
result = find_pypackage(path, version)
result = find_pypackage(Path(path), version)
if result:
return result

if bare_version != version:
for path in find_paths:
result = find_pypackage(path, bare_version)
result = find_pypackage(Path(path), bare_version)
if result:
return result

Expand Down
7 changes: 0 additions & 7 deletions src/pdm/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ class Config(MutableMapping[str, str]):
True,
),
"global_project.user_site": ConfigItem("Whether to install to user site", False, True, coerce=ensure_boolean),
"project_max_depth": ConfigItem(
"The max depth to search for a project through the parents",
5,
True,
env_var="PDM_PROJECT_MAX_DEPTH",
coerce=int,
),
"strategy.update": ConfigItem("The default strategy for updating packages", "reuse", False),
"strategy.save": ConfigItem("Specify how to save versions when a package is added", "minimum", False),
"strategy.resolve_max_rounds": ConfigItem(
Expand Down
6 changes: 1 addition & 5 deletions src/pdm/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ def __init__(
global_project = Path(self.global_config["global_project.path"])

if root_path is None:
root_path = (
find_project_root(max_depth=self.global_config["project_max_depth"])
if not is_global
else global_project
)
root_path = find_project_root() if not is_global else global_project
if not is_global and root_path is None and self.global_config["global_project.fallback"]:
root_path = global_project
is_global = True
Expand Down
25 changes: 10 additions & 15 deletions src/pdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,16 @@ def join_list_with(items: list[Any], sep: Any) -> list[Any]:
return new_items[:-1]


def find_project_root(cwd: str = ".", max_depth: int = 5) -> str | None:
"""Recursively find a `pyproject.toml` at given path or current working directory.
If none if found, go to the parent directory, at most `max_depth` levels will be
looked for.
"""
original_path = Path(cwd).absolute()
path = original_path
for _ in range(max_depth):
if path.joinpath("pyproject.toml").exists():
return path.as_posix()
if path.parent == path:
# Root path is reached
break
path = path.parent
return None
def find_project_root(cwd: str = ".") -> str | None:
"""Recursively find a `pyproject.toml` at given path or current working directory."""
path = Path(cwd).absolute()
if list(path.glob("pyproject.toml")):
return path.as_posix()

if path == path.parent:
return None

return find_project_root(str(path.parent))


def convert_hashes(files: list[FileHash]) -> dict[str, list[str]]:
Expand Down
10 changes: 5 additions & 5 deletions tests/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ def test_config_project_global_precedence(project, pdm):


def test_specify_config_file(tmp_path, pdm, monkeypatch):
tmp_path.joinpath("global_config.toml").write_text("project_max_depth = 9\n")
tmp_path.joinpath("global_config.toml").write_text("strategy.resolve_max_rounds = 1000\n")
with cd(tmp_path):
result = pdm(["-c", "global_config.toml", "config", "project_max_depth"])
result = pdm(["-c", "global_config.toml", "config", "strategy.resolve_max_rounds"])
assert result.exit_code == 0
assert result.output.strip() == "9"
assert result.output.strip() == "1000"

monkeypatch.setenv("PDM_CONFIG_FILE", "global_config.toml")
result = pdm(["config", "project_max_depth"])
result = pdm(["config", "strategy.resolve_max_rounds"])
assert result.exit_code == 0
assert result.output.strip() == "9"
assert result.output.strip() == "1000"


def test_default_repository_setting(project):
Expand Down