Skip to content

Commit 0749004

Browse files
Merge pull request #57 from MrMino/skip_incompatible_flag
Add --skip-incompatible flag
2 parents 042862b + d672318 commit 0749004

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Release History
44

55
2.2.0
66

7+
- Added `--skip-incompatible` flag to `pip-extra-reqs`, which makes it ignore
8+
requirements with environment markers that are incompatible with the current
9+
environment.
710
- Added `--requirements-file` flag to `pip-extra-reqs` and `pip-missing-reqs`
811
commands. This flag makes it possible to specify a path to the requirements
912
file. Previously, `"requirements.txt"` was always used.

pip_check_reqs/common.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77

88
from packaging.utils import canonicalize_name
9+
from packaging.markers import Marker
910
# Between different versions of pip the location of PipSession has changed.
1011
try:
1112
from pip._internal.network.session import PipSession
@@ -142,12 +143,33 @@ def find_required_modules(options, requirements_filename: str):
142143

143144
if options.ignore_reqs(requirement):
144145
log.debug('ignoring requirement: %s', requirement_name)
146+
continue
147+
148+
if options.skip_incompatible:
149+
requirement_string = requirement.requirement
150+
if not has_compatible_markers(requirement_string):
151+
log.debug('ignoring requirement (incompatible environment '
152+
'marker): %s', requirement_string)
153+
continue
154+
145155
else:
146156
log.debug('found requirement: %s', requirement_name)
147157
explicit.add(canonicalize_name(requirement_name))
158+
148159
return explicit
149160

150161

162+
def has_compatible_markers(full_requirement: str) -> bool:
163+
if ';' not in full_requirement:
164+
return True # No environment marker.
165+
166+
enviroment_marker = full_requirement.split(';')[1]
167+
if not enviroment_marker:
168+
return True # Empty environment marker.
169+
170+
return Marker(enviroment_marker).evaluate()
171+
172+
151173
def is_package_file(path):
152174
'''Determines whether the path points to a Python package sentinel
153175
file - the __init__.py or its compiled variants.

pip_check_reqs/find_extra_reqs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def main():
8787
action="append",
8888
default=[],
8989
help="reqs in requirements to ignore")
90+
parser.add_option("-s",
91+
"--skip-incompatible",
92+
dest="skip_incompatible",
93+
action="store_true",
94+
default=False,
95+
help="skip requirements that have incompatible "
96+
"environment markers")
9097
parser.add_option("-v",
9198
"--verbose",
9299
dest="verbose",

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
packaging
1+
packaging >= 16.0
22
# Pinned pip versions are matched in the GitHub workflows CI matrix.
33
pip >= 10.0.1

tests/test_common.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_ignorer(monkeypatch, tmp_path: Path, ignore_cfg, candidate, result):
165165

166166
def test_find_required_modules(monkeypatch, tmp_path: Path):
167167
class options:
168-
pass
168+
skip_incompatible = False
169169

170170
options.ignore_reqs = common.ignorer(ignore_cfg=['barfoo'])
171171

@@ -177,3 +177,22 @@ class options:
177177
requirements_filename=str(fake_requirements_file),
178178
)
179179
assert reqs == set(['foobar'])
180+
181+
182+
def test_find_required_modules_env_markers(monkeypatch, tmp_path):
183+
class options:
184+
skip_incompatible = True
185+
186+
def ignore_reqs(self, modname):
187+
return False
188+
189+
fake_requirements_file = tmp_path / 'requirements.txt'
190+
fake_requirements_file.write_text('spam==1; python_version<"2.0"\n'
191+
'ham==2;\n'
192+
'eggs==3\n')
193+
194+
reqs = common.find_required_modules(
195+
options=options(),
196+
requirements_filename=str(fake_requirements_file),
197+
)
198+
assert not reqs

tests/test_find_extra_reqs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def test_find_extra_reqs(monkeypatch, tmp_path: Path):
7272
class options:
7373
def ignore_reqs(x, y):
7474
return False
75+
skip_incompatible = False
7576

7677
options = options()
7778

0 commit comments

Comments
 (0)