Skip to content

Commit

Permalink
fix: path to uri expansion in dependency string
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Aug 29, 2023
1 parent abd5537 commit dfd22b3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
17 changes: 16 additions & 1 deletion src/pdm/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pdm.backend.hooks import BuildHookInterface, Context
from pdm.backend.hooks.version import DynamicVersionBuildHook
from pdm.backend.structures import FileMap
from pdm.backend.utils import import_module_at_path, is_python_package
from pdm.backend.utils import expand_vars, import_module_at_path, is_python_package

if TYPE_CHECKING:
from typing import SupportsIndex
Expand Down Expand Up @@ -160,8 +160,23 @@ def clean(self, context: Context) -> None:
if context.build_dir.exists():
shutil.rmtree(context.build_dir)

def _fix_dependencies(self) -> None:
"""Fix the dependencies and remove dynamic variables from the metadata"""
metadata = self.config.metadata
root = self.location.as_posix()
if metadata.get("dependencies"):
metadata["dependencies"] = [
expand_vars(dep, root) for dep in metadata["dependencies"]
]
if metadata.get("optional-dependencies"):
for name, deps in metadata["optional-dependencies"].items():
metadata["optional-dependencies"][name] = [
expand_vars(dep, root) for dep in deps
]

def initialize(self, context: Context) -> None:
"""Initialize the build context."""
self._fix_dependencies()
self.call_hook("pdm_build_initialize", context)

def get_files(self, context: Context) -> Iterable[tuple[str, Path]]:
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def expand_vars(line: str, root: str) -> str:
quote: Callable[[str], str] = urllib.parse.quote
else:
quote = str
line = line.replace("${PROJECT_ROOT}", root.lstrip("/"))
line = line.replace("file:///${PROJECT_ROOT}", Path(root).as_uri())

def replace_func(match: Match[str]) -> str:
rv = os.getenv(match.group(1))
Expand Down
19 changes: 0 additions & 19 deletions src/pdm/backend/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from pdm.backend.hooks.setuptools import SetuptoolsBuildHook
from pdm.backend.structures import FileMap
from pdm.backend.utils import (
expand_vars,
get_abi_tag,
get_platform,
normalize_file_permissions,
Expand Down Expand Up @@ -99,24 +98,6 @@ def _get_platform_tags(self) -> tuple[str | None, str | None, str | None]:
plat_name = self.config_settings["--plat-name"]
return python_tag, py_limited_api, plat_name

def _fix_dependencies(self) -> None:
"""Fix the dependencies and remove dynamic variables from the metadata"""
metadata = self.config.metadata
root = self.location.as_posix()
if metadata.get("dependencies"):
metadata["dependencies"] = [
expand_vars(dep, root) for dep in metadata["dependencies"]
]
if metadata.get("optional-dependencies"):
for name, deps in metadata["optional-dependencies"].items():
metadata["optional-dependencies"][name] = [
expand_vars(dep, root) for dep in deps
]

def initialize(self, context: Context) -> None:
self._fix_dependencies()
return super().initialize(context)

def prepare_metadata(self, metadata_directory: str) -> Path:
"""Write the dist-info files under the given directory"""
context = self.build_context(Path(metadata_directory))
Expand Down
19 changes: 18 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import os

import pytest

from pdm.backend.utils import expand_vars

is_nt = os.name == "nt"


def test_expand_vars(monkeypatch):
@pytest.mark.skipif(is_nt, reason="Posix path")
def test_expand_vars_posix(monkeypatch):
monkeypatch.setenv("FOO", "foo=a")
monkeypatch.setenv("BAR", "bar")
root = "/abc/def"

line = "file:///${PROJECT_ROOT}/${FOO}:${BAR}:${BAZ}"
assert expand_vars(line, root) == "file:///abc/def/foo%3Da:bar:${BAZ}"


@pytest.mark.skipif(not is_nt, reason="Windows path")
def test_expand_vars_win(monkeypatch):
monkeypatch.setenv("FOO", "foo=a")
monkeypatch.setenv("BAR", "bar")
root = "C:/abc/def"

line = "file:///${PROJECT_ROOT}/${FOO}:${BAR}:${BAZ}"
assert expand_vars(line, root) == "file:///C:/abc/def/foo%3Da:bar:${BAZ}"

0 comments on commit dfd22b3

Please sign in to comment.