Skip to content

Commit 85dc25c

Browse files
committed
Add --skip-incompatible flag
This flag makes it possible to skip requirements that are incompatible with the current environment, i.e. those whose PEP-496 environment markers do not evaluate thruthly. For example, when running Python 3.5, previously there would be no way of processing the following requirement properly: ``` enum34; python_version<"3.4" ``` Ignoring it via `-r` is a bit heavy handed, as it means that it would no longer get checked when running e.g. Python 2.7. By using `-s` or `--skip-incompatible`, this requirement gets ignored when running python versions 3.4 and above, but evaluated when running on earlier versions.
1 parent ac5a463 commit 85dc25c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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",

0 commit comments

Comments
 (0)