Skip to content

Commit

Permalink
Add pkg_name function to 'dependencies' to fix #516
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Oct 9, 2021
1 parent 30d07da commit 5443761
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/pyscaffold/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from itertools import chain
from typing import Iterable, List

from packaging.requirements import Requirement
from packaging.requirements import InvalidRequirement, Requirement

# setuptools version is now enforced via `install_requires`

Expand Down Expand Up @@ -51,18 +51,30 @@ def deduplicate(requirements: Iterable[str]) -> List[str]:
"packaging>20.0"]``, remove the duplicated packages.
If a package is duplicated, the last occurrence stays.
"""
return list({Requirement(r).name: r for r in requirements}.values())
return list({pkg_name(r): r for r in requirements}.values())


def remove(requirements: Iterable[str], to_remove: Iterable[str]) -> List[str]:
"""Given a list of individual requirement strings, e.g. ``["appdirs>=1.4.4",
"packaging>20.0"]``, remove the requirements in ``to_remove``.
"""
removable = {Requirement(r).name for r in to_remove}
return [r for r in requirements if Requirement(r).name not in removable]
removable = {pkg_name(r) for r in to_remove}
return [r for r in requirements if pkg_name(r) not in removable]


def add(requirements: Iterable[str], to_add: Iterable[str] = BUILD) -> List[str]:
"""Given a sequence of individual requirement strings, add ``to_add`` to it.
By default adds :obj:`BUILD` if ``to_add`` is not given."""
return deduplicate(chain(requirements, to_add))


def pkg_name(requirement: str) -> str:
"""In the case the given string is a dependency specification (PEP 508/440),
it returns the "package name" part of dependency (without versions).
Otherwise, it returns the same string (removed the comment marks).
"""
req = requirement.strip("#").strip()
try:
return Requirement(req).name
except InvalidRequirement:
return req
22 changes: 22 additions & 0 deletions tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,25 @@ def test_add():
"setuptools_scm>=1.2.5,<2",
"django>=5.3.99999,<6",
]


def test_add_commented():
new_deps = [
"setuptools_scm>=1.2.5,<2",
"pyscaffold>=42.1.0,<43",
"django>=5.3.99999,<6",
"mypkg~=9.0",
"gitdep @ git+https://repo.com/gitdep@main#egg=gitdep",
"# a comment",
"# comment @ https://that.reminds.dep",
]
assert deps.add(["appdirs==1", "# a comment", "# mypkg~=2.0"], new_deps) == [
"appdirs==1",
"# a comment",
"mypkg~=9.0",
"setuptools_scm>=1.2.5,<2",
"pyscaffold>=42.1.0,<43",
"django>=5.3.99999,<6",
"gitdep @ git+https://repo.com/gitdep@main#egg=gitdep",
"# comment @ https://that.reminds.dep",
]
1 change: 1 addition & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ def test_add_dependencies_with_comments(tmpfolder):
# Adding some comments here that are perfectly valid.
some-other-dependency
gitdep @ git+https://repo.com/gitdep@main#egg=gitdep
packages = find_namespace:
"""
Path(tmpfolder, "setup.cfg").write_text(dedent(config))
# when we update it
Expand Down

0 comments on commit 5443761

Please sign in to comment.