From d6e6bb7dfbfdafe111e5442abc3c785c856ec1cf Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 18 Jul 2019 10:50:13 +0200 Subject: [PATCH] Allow specifying extras for build dependencies --- macros.pyproject | 2 +- pyproject_buildrequires.py | 23 ++++++++++++++++++----- test_pyproject_buildrequires.py | 1 + testcases.yaml | 30 ++++++++++++++++++++++++++++-- tests/python-pytest.spec | 8 +++++++- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/macros.pyproject b/macros.pyproject index 9ddd652..6e558b2 100644 --- a/macros.pyproject +++ b/macros.pyproject @@ -17,7 +17,7 @@ if [ -d %{buildroot}%{python3_sitearch} ]; then fi } -%pyproject_buildrequires(r) %{expand:\\\ +%pyproject_buildrequires(rx:) %{expand:\\\ echo 'python3-devel' echo 'python3dist(packaging)' echo 'python3dist(pip) >= 19' diff --git a/pyproject_buildrequires.py b/pyproject_buildrequires.py index a6ebc57..2a3fa63 100644 --- a/pyproject_buildrequires.py +++ b/pyproject_buildrequires.py @@ -42,7 +42,7 @@ def hook_call(): class Requirements: """Requirement printer""" - def __init__(self, freeze_output): + def __init__(self, freeze_output, extras=''): self.installed_packages = {} for line in freeze_output.splitlines(): line = line.strip() @@ -51,6 +51,8 @@ class Requirements: name, version = line.split('==') self.installed_packages[name.strip()] = Version(version) + self.marker_env = {'extra': extras} + self.missing_requirements = False def add(self, requirement_str, *, source=None): @@ -68,7 +70,7 @@ class Requirements: name = canonicalize_name(requirement.name) if (requirement.marker is not None - and not requirement.marker.evaluate(environment={'extra': ''}) + and not requirement.marker.evaluate(environment=self.marker_env) ): print_err(f'Ignoring alien requirement:', requirement_str) return @@ -181,8 +183,10 @@ def python3dist(name, op=None, version=None): return f'python3dist({name}) {op} {version}' -def generate_requires(freeze_output, *, include_runtime=False, toxenv=None): - requirements = Requirements(freeze_output) +def generate_requires( + freeze_output, *, include_runtime=False, toxenv=None, extras='', +): + requirements = Requirements(freeze_output, extras=extras) try: backend = get_backend(requirements) @@ -206,6 +210,11 @@ def main(argv): help='generate test tequirements from tox environment ' + '(not implemented; implies --runtime)', ) + parser.add_argument( + '-x', '--extras', metavar='EXTRAS', default='', + help='comma separated list of "extras" for runtime requirements ' + + '(e.g. -x testing,feature-x)', + ) args = parser.parse_args(argv) if args.toxenv: @@ -224,7 +233,11 @@ def main(argv): ).stdout try: - generate_requires(freeze_output, include_runtime=args.runtime) + generate_requires( + freeze_output, + include_runtime=args.runtime, + extras=args.extras, + ) except Exception as e: # Log the traceback explicitly (it's useful debug info) traceback.print_exc() diff --git a/test_pyproject_buildrequires.py b/test_pyproject_buildrequires.py index 225d1c3..b6462b4 100644 --- a/test_pyproject_buildrequires.py +++ b/test_pyproject_buildrequires.py @@ -29,6 +29,7 @@ def test_data(case_name, capsys, tmp_path, monkeypatch): generate_requires( case['freeze_output'], include_runtime=case.get('include_runtime', False), + extras=case.get('extras', ''), ) except SystemExit as e: assert e.code == case['result'] diff --git a/testcases.yaml b/testcases.yaml index 77cc782..956e78e 100644 --- a/testcases.yaml +++ b/testcases.yaml @@ -142,13 +142,13 @@ Default build system, run dependencies in setup.py: python3dist(inst2) < 3 result: 0 -Run dependencies with extras: +Run dependencies with extras (not selected): freeze_output: | setuptools==50 wheel==1 pyyaml==1 include_runtime: true - setup.py: | + setup.py: &pytest_setup_py | # slightly abriged copy of pytest's setup.py from setuptools import setup @@ -198,3 +198,29 @@ Run dependencies with extras: python3dist(pluggy) >= 0.11 python3dist(more-itertools) >= 4 result: 0 + +Run dependencies with extras (selected): + freeze_output: | + setuptools==50 + wheel==1 + pyyaml==1 + include_runtime: true + extras: testing + setup.py: *pytest_setup_py + expected: | + python3dist(setuptools) >= 40.8 + python3dist(wheel) + python3dist(wheel) + python3dist(setuptools) >= 40 + python3dist(py) >= 1.5 + python3dist(six) >= 1.10 + python3dist(setuptools) + python3dist(attrs) >= 17.4 + python3dist(atomicwrites) >= 1 + python3dist(pluggy) >= 0.11 + python3dist(more-itertools) >= 4 + python3dist(argcomplete) + python3dist(hypothesis) >= 3.56 + python3dist(nose) + python3dist(requests) + result: 0 diff --git a/tests/python-pytest.spec b/tests/python-pytest.spec index e427ccf..d6ad7ab 100644 --- a/tests/python-pytest.spec +++ b/tests/python-pytest.spec @@ -27,7 +27,7 @@ py.test provides simple, yet powerful testing for Python. %generate_buildrequires -%pyproject_buildrequires -r +%pyproject_buildrequires -r -x testing %build @@ -37,6 +37,12 @@ py.test provides simple, yet powerful testing for Python. %install %pyproject_install +%check +# Only run one test (which uses a test-only dependency, hypothesis). +# (Unfortunately, some other tests still fail.) +export PYTHONPATH=%{buildroot}%{python3_sitelib} +%{__python3} -m pytest -k metafunc + %files -n python3-%{pypi_name} %doc README.rst