Skip to content

Commit

Permalink
Limit marker complexity a bit
Browse files Browse the repository at this point in the history
- Fix bugs related to previous marker complexity reduction

Signed-off-by: Dan Ryan <[email protected]>
  • Loading branch information
techalchemy committed Mar 11, 2020
1 parent 7621b2a commit d424bf7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 88 deletions.
90 changes: 10 additions & 80 deletions src/requirementslib/models/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..environment import MYPY_RUNNING
from ..utils import _ensure_dir, prepare_pip_source_args
from .cache import CACHE_DIR, DependencyCache
from .setup_info import SetupInfo
from .utils import (
clean_requires_python,
fix_requires_python_marker,
Expand Down Expand Up @@ -479,90 +480,19 @@ def get_dependencies_from_index(dep, sources=None, pip_options=None, wheel_cache
if not wheel_cache:
wheel_cache = WHEEL_CACHE
dep.is_direct = True
reqset = pip_shims.shims.RequirementSet()
reqset.add_requirement(dep)
requirements = None
setup_requires = {}
with temp_environ(), start_resolver(
finder=finder, session=session, wheel_cache=wheel_cache
) as resolver:
with temp_environ():
os.environ["PIP_EXISTS_ACTION"] = "i"
dist = None
if dep.editable and not dep.prepared and not dep.req:
with cd(dep.setup_py_dir):
from setuptools.dist import distutils

try:
dist = distutils.core.run_setup(dep.setup_py)
except (ImportError, TypeError, AttributeError):
dist = None
else:
setup_requires[dist.get_name()] = dist.setup_requires
if not dist:
try:
dist = dep.get_dist()
except (TypeError, ValueError, AttributeError):
pass
else:
setup_requires[dist.get_name()] = dist.setup_requires
resolver.require_hashes = False
try:
results = resolver._resolve_one(reqset, dep)
except Exception:
# FIXME: Needs to bubble the exception somehow to the user.
results = []
finally:
try:
wheel_cache.cleanup()
except AttributeError:
pass
resolver_requires_python = getattr(resolver, "requires_python", None)
requires_python = getattr(reqset, "requires_python", resolver_requires_python)
if requires_python:
add_marker = fix_requires_python_marker(requires_python)
reqset.remove(dep)
if dep.req.marker:
dep.req.marker._markers.extend(["and"].extend(add_marker._markers))
else:
dep.req.marker = add_marker
reqset.add(dep)
requirements = set()
for r in results:
if requires_python:
if r.req.marker:
r.req.marker._markers.extend(["and"].extend(add_marker._markers))
else:
r.req.marker = add_marker
requirements.add(format_requirement(r))
for section in setup_requires:
python_version = section
not_python = not is_python(section)

# This is for cleaning up :extras: formatted markers
# by adding them to the results of the resolver
# since any such extra would have been returned as a result anyway
for value in setup_requires[section]:

# This is a marker.
if is_python(section):
python_version = value[1:-1]
else:
not_python = True

if ":" not in value and not_python:
try:
requirement_str = "{0}{1}".format(value, python_version).replace(
":", ";"
)
requirements.add(
format_requirement(
make_install_requirement(requirement_str).ireq
)
)
# Anything could go wrong here -- can't be too careful.
except Exception:
pass

setup_info = SetupInfo.from_ireq(dep)
results = setup_info.get_info()
setup_requires.update(results["setup_requires"])
requirements = set(results["requires"].values())
else:
results = pip_shims.shims.resolve(dep)
requirements = [v for v in results.values() if v.name != dep.name]
requirements = set([format_requirement(r) for r in requirements])
if not dep.editable and is_pinned_requirement(dep) and requirements is not None:
DEPENDENCY_CACHE[dep] = list(requirements)
return requirements
Expand Down
14 changes: 7 additions & 7 deletions src/requirementslib/models/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ def _format_pyspec(specifier):
version = getattr(specifier, "version", specifier).rstrip()
if version and version.endswith("*"):
if version.endswith(".*"):
version = version.rstrip(".*")
else:
version = version.rstrip("*")
version = version[:-2]
version = version.rstrip("*")
specifier = Specifier("{0}{1}".format(specifier.operator, version))
try:
op = REPLACE_RANGES[specifier.operator]
Expand Down Expand Up @@ -228,10 +227,10 @@ def normalize_specifier_set(specs):
return {_format_pyspec(spec) for spec in specs}
spec_list = []
for spec in specs.split(","):
spec = spec.strip()
if spec.endswith(".*"):
spec = spec.rstrip(".*")
elif spec.endswith("*"):
spec = spec.rstrip("*")
spec = spec[:-2]
spec = spec.rstrip("*")
spec_list.append(spec)
return normalize_specifier_set(SpecifierSet(",".join(spec_list)))

Expand Down Expand Up @@ -582,9 +581,10 @@ def get_specset(marker_list):
specset = set()
_last_str = "and"
for marker_parts in marker_list:
specset.update(marker_parts)
if isinstance(marker_parts, str):
_last_str = marker_parts # noqa
else:
specset.update(_get_specifiers_from_markers(marker_parts))
specifiers = SpecifierSet()
specifiers._specs = frozenset(specset)
return specifiers
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_get_extras(marker, extras):
Marker(
"os_name == 'posix' and python_version >= '2.7' and python_version not in '3.0.*,3.1.*,3.2.*,3.3.*'"
),
SpecifierSet("!=3.0,!=3.1,!=3.2,!=3.3,>=2.7"),
SpecifierSet("!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7"),
),
],
)
Expand Down

0 comments on commit d424bf7

Please sign in to comment.