Skip to content

Commit

Permalink
Make distribution finder compatible with python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Aug 10, 2021
1 parent cabc74c commit c051a00
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
10 changes: 6 additions & 4 deletions pdm/installers/synchronizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def update_candidate(self, key: str) -> Tuple[Distribution, Candidate]:
"""Update candidate"""
can = self.candidates[key]
dist = self.working_set[strip_extras(key)[0]]
dist_version = dist.version
with self.ui.open_spinner(
f"Updating {termui.green(key, bold=True)} {termui.yellow(dist.version)} "
f"-> {termui.yellow(can.version)}..."
Expand All @@ -187,21 +188,22 @@ def update_candidate(self, key: str) -> Tuple[Distribution, Candidate]:
except Exception:
spinner.fail(
f"Update {termui.green(key, bold=True)} "
f"{termui.yellow(dist.version)} -> "
f"{termui.yellow(dist_version)} -> "
f"{termui.yellow(can.version)} failed"
)
raise
else:
spinner.succeed(
f"Update {termui.green(key, bold=True)} "
f"{termui.yellow(dist.version)} -> "
f"{termui.yellow(dist_version)} -> "
f"{termui.yellow(can.version)} successful"
)
return dist, can

def remove_distribution(self, key: str) -> Distribution:
"""Remove distributions with given names."""
dist = self.working_set[key]
dist_version = dist.version
with self.ui.open_spinner(
f"Removing {termui.green(key, bold=True)} {termui.yellow(dist.version)}..."
) as spinner:
Expand All @@ -210,13 +212,13 @@ def remove_distribution(self, key: str) -> Distribution:
except Exception:
spinner.fail(
f"Remove {termui.green(key, bold=True)} "
f"{termui.yellow(dist.version)} failed"
f"{termui.yellow(dist_version)} failed"
)
raise
else:
spinner.succeed(
f"Remove {termui.green(key, bold=True)} "
f"{termui.yellow(dist.version)} successful"
f"{termui.yellow(dist_version)} successful"
)
return dist

Expand Down
4 changes: 3 additions & 1 deletion pdm/models/working_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ def find_distributions(
cls, context: im.DistributionFinder.Context = default_context
) -> Iterable[im.Distribution]:
found_links = cls._search_paths(context.name, context.path)
# For Py3.7 compatibility, handle both classmethod and instance method
meta_finder = im.MetadataPathFinder()
for link in found_links:
name = link.stem
link_pointer = Path(link.open("rb").readline().decode().strip())
dist = next(
iter(
im.MetadataPathFinder.find_distributions(
meta_finder.find_distributions(
im.DistributionFinder.Context(
name=name, path=[str(link_pointer)]
)
Expand Down
14 changes: 12 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import collections
import functools
import json
import os
import re
Expand Down Expand Up @@ -324,7 +323,18 @@ def is_dev(request):
@pytest.fixture()
def invoke():
runner = CliRunner(mix_stderr=False)
return functools.partial(runner.invoke, main, prog_name="pdm")

def caller(args, strict=False, **kwargs):
result = runner.invoke(
main, args, catch_exceptions=not strict, prog_name="pdm", **kwargs
)
if strict and result.exit_code != 0:
raise RuntimeError(
f"Call command {args} failed({result.exit_code}): {result.stderr}"
)
return result

return caller


@pytest.fixture()
Expand Down
27 changes: 8 additions & 19 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,22 @@
from pdm.utils import cd


@pytest.fixture
def strict_invoke(invoke):
def new_invoke(args, **kwargs):
result = invoke(args, **kwargs)
if result.exit_code != 0:
raise RuntimeError(f"Call command {args} fail: {result.stderr}")
return result

return new_invoke


@pytest.mark.integration
@pytest.mark.parametrize("python_version", ["2.7", "3.6", "3.7", "3.8", "3.9"])
def test_basic_integration(python_version, project_no_init, strict_invoke):
def test_basic_integration(python_version, project_no_init, invoke):
"""An e2e test case to ensure PDM works on all supported Python versions"""
project = project_no_init
project.root.joinpath("foo.py").write_text("import django\n")
strict_invoke(["init"], input="\ny\n\n\n\n\n\n>=2.7\n", obj=project)
strict_invoke(["use", "-f", python_version], obj=project)
invoke(["init"], input="\ny\n\n\n\n\n\n>=2.7\n", obj=project, strict=True)
invoke(["use", "-f", python_version], obj=project, strict=True)
project._environment = None
strict_invoke(["add", "django"], obj=project)
invoke(["add", "django"], obj=project, strict=True)
with cd(project.root):
strict_invoke(["run", "python", "foo.py"], obj=project)
invoke(["run", "python", "foo.py"], obj=project, strict=True)
if python_version != "2.7":
strict_invoke(["build", "-v"], obj=project)
strict_invoke(["remove", "django"], obj=project)
result = strict_invoke(["list"], obj=project)
invoke(["build", "-v"], obj=project, strict=True)
invoke(["remove", "-v", "django"], obj=project, strict=True)
result = invoke(["list"], obj=project, strict=True)
assert not any(
line.strip().lower().startswith("django") for line in result.output.splitlines()
)

0 comments on commit c051a00

Please sign in to comment.