Skip to content

Commit c729831

Browse files
committed
Add support for reading the requirements from pyproject.toml
1 parent d04b82e commit c729831

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

dep_checker/__init__.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from consolekit.terminal_colours import Fore, resolve_color_default
4040
from domdf_python_tools.paths import PathPlus, in_directory
4141
from domdf_python_tools.typing import PathLike
42+
from packaging.requirements import Requirement
4243
from shippinglabel.requirements import read_requirements
4344

4445
# this package
@@ -315,18 +316,18 @@ def iter_files_to_check(basepath: PathLike, pkg_name: str) -> Iterator[PathPlus]
315316

316317
def check_imports(
317318
pkg_name: str,
318-
req_file: PathLike = "requirements.txt",
319+
*requirements: Requirement,
319320
allowed_unused: Optional[List[str]] = None,
320321
colour: Optional[bool] = None,
321322
name_mapping: Optional[Dict[str, str]] = None,
322323
namespace_packages: Optional[List[str]] = None,
323324
work_dir: PathLike = '.',
324325
) -> int:
325-
"""
326+
r"""
326327
Check imports for the given package, against the given requirements file.
327328
328329
:param pkg_name:
329-
:param req_file:
330+
:param \*requirements:
330331
:param allowed_unused: List of requirements which are allowed to be unused in the source code.
331332
:default allowed_unused: ``[]``
332333
:param colour: Whether to use coloured output.
@@ -346,6 +347,10 @@ def check_imports(
346347
347348
* Added the ``name_mapping`` option.
348349
* Added the ``work_dir`` option.
350+
351+
.. versionchanged:: 0.7.0 Replaced the ``req_file`` argument with the ``*requirements`` argument.
352+
Use :func:`shippinglabel.requirements.read_requirements(req_file)[0] <shippinglabel.requirements.read_requirements>`
353+
to get the original bevhaviour.
349354
"""
350355

351356
ret = 0
@@ -362,17 +367,11 @@ def check_imports(
362367
namespace_packages = NamespacePackages.get(config)
363368

364369
work_dir = PathPlus(work_dir)
365-
req_file = PathPlus(req_file)
366-
367-
if not req_file.is_absolute():
368-
req_file = work_dir / req_file
369-
370-
req_file = req_file.abspath()
371370
work_dir = work_dir.abspath()
372371

373372
checker = DepChecker(
374373
pkg_name,
375-
requirements=map(attrgetter("name"), read_requirements(req_file)[0]),
374+
requirements=map(attrgetter("name"), requirements),
376375
allowed_unused=allowed_unused,
377376
name_mapping=name_mapping,
378377
namespace_packages=namespace_packages,

dep_checker/__main__.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@
3232

3333
# 3rd party
3434
import click
35+
import dom_toml
3536
from consolekit import click_command
36-
from consolekit.options import colour_option
37+
from consolekit.options import auto_default_option, colour_option, flag_option
3738
from consolekit.utils import abort
3839

3940
# this package
41+
from domdf_python_tools.paths import PathPlus
42+
from shippinglabel.requirements import parse_pyproject_dependencies, read_requirements
43+
4044
from dep_checker import check_imports
4145

4246
__all__ = ("main", )
@@ -57,12 +61,16 @@
5761
multiple=True,
5862
help="Requirements which are allowed to be unused in the source code.",
5963
)
60-
@click.option(
64+
@auto_default_option(
6165
"--req-file",
6266
type=click.STRING,
6367
metavar="FILENAME",
64-
default="requirements.txt",
65-
help="The requirements file.",
68+
help="Parse the requirements from the given requirements file.",
69+
)
70+
@flag_option(
71+
"-P", "--pyproject",
72+
type=click.STRING,
73+
help="Parse the requirements from 'pyproject.toml'.",
6674
)
6775
@click.argument(
6876
"pkg-name",
@@ -71,10 +79,11 @@
7179
@click_command()
7280
def main(
7381
pkg_name: str,
74-
req_file: str,
7582
allowed_unused: Optional[List[str]],
76-
colour: Optional[bool],
83+
colour: Optional[bool] = None,
84+
req_file: str = "requirements.txt",
7785
work_dir: str = '.',
86+
pyproject: bool = False
7887
) -> None:
7988
"""
8089
Tool to check all requirements are actually required.
@@ -83,10 +92,32 @@ def main(
8392
if allowed_unused == ():
8493
allowed_unused = None
8594

95+
work_dir = PathPlus(work_dir)
96+
97+
def read_req_file(req_file):
98+
req_file = PathPlus(req_file)
99+
100+
if not req_file.is_absolute():
101+
req_file = work_dir / req_file
102+
103+
return read_requirements(req_file)[0]
104+
105+
if pyproject:
106+
pyproject_file = work_dir / "pyproject.toml"
107+
dynamic = dom_toml.load(pyproject_file)["project"].get("dynamic", ())
108+
109+
if "requirements" in dynamic:
110+
requirements = read_req_file(work_dir / "requirements.txt")
111+
else:
112+
requirements = parse_pyproject_dependencies(pyproject_file, flavour="pep621")
113+
114+
else:
115+
requirements = read_req_file(req_file)
116+
86117
try:
87118
ret = check_imports(
88119
pkg_name,
89-
req_file=req_file,
120+
*requirements,
90121
allowed_unused=allowed_unused,
91122
colour=colour,
92123
work_dir=work_dir,

doc-source/usage.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ Command Line
109109
:prog: dep-checker
110110

111111

112+
The :option:`-P / --pyproject <-P>` option takes precedence over the :option:`--req-file` option.
113+
114+
.. versionchanged:: 0.7.0
115+
116+
Added the :option:`-P / --pyproject <-P>` option.
117+
112118
As a ``pre-commit`` hook
113119
----------------------------
114120

0 commit comments

Comments
 (0)