Skip to content

Commit

Permalink
Replace wheel cache cache_dir with an object.
Browse files Browse the repository at this point in the history
Wheel cache lookups become more complex when we wish to allow binary
blacklisting. Rather than passing more parameters around, replace
cache_root with wheel_cache, and create a wheel cache in all the
relevant command entry points.
  • Loading branch information
rbtcollins committed Apr 20, 2015
1 parent 3070609 commit a1a0147
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 42 deletions.
9 changes: 5 additions & 4 deletions pip/basecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ class RequirementCommand(Command):

@staticmethod
def populate_requirement_set(requirement_set, args, options, finder,
session, name):
session, name, wheel_cache):
"""
Marshal cmd line args into a requirement set.
"""
for name in args:
requirement_set.add_requirement(
InstallRequirement.from_line(
name, None, isolated=options.isolated_mode,
cache_root=options.cache_dir
wheel_cache=wheel_cache
)
)

Expand All @@ -299,14 +299,15 @@ def populate_requirement_set(requirement_set, args, options, finder,
name,
default_vcs=options.default_vcs,
isolated=options.isolated_mode,
cache_root=options.cache_dir
wheel_cache=wheel_cache
)
)

for filename in options.requirements:
for req in parse_requirements(
filename,
finder=finder, options=options, session=session):
finder=finder, options=options, session=session,
wheel_cache=wheel_cache):
requirement_set.add_requirement(req)

if not requirement_set.has_requirements:
Expand Down
4 changes: 3 additions & 1 deletion pip/commands/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pip.basecommand import Command
from pip.operations.freeze import freeze
from pip.wheel import Cache


class FreezeCommand(Command):
Expand Down Expand Up @@ -54,14 +55,15 @@ def __init__(self, *args, **kw):
self.parser.insert_option_group(0, self.cmd_opts)

def run(self, options, args):
wheel_cache = Cache(options.cache_dir)
freeze_kwargs = dict(
requirement=options.requirement,
find_links=options.find_links,
local_only=options.local,
user_only=options.user,
skip_regex=options.skip_requirements_regex,
isolated=options.isolated_mode,
cache_root=options.cache_dir)
wheel_cache=wheel_cache)

for line in freeze(**freeze_kwargs):
sys.stdout.write(line + '\n')
6 changes: 4 additions & 2 deletions pip/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pip.utils import ensure_dir
from pip.utils.build import BuildDirectory
from pip.utils.deprecation import RemovedInPip8Warning
from pip.wheel import WheelBuilder
from pip.wheel import Cache, WheelBuilder


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -241,9 +241,9 @@ def run(self, options, args):
build_delete = (not (options.no_clean or options.build_dir))
with BuildDirectory(options.build_dir,
delete=build_delete) as build_dir:
wheel_cache = Cache(options.cache_dir)
requirement_set = RequirementSet(
build_dir=build_dir,
cache_root=options.cache_dir,
src_dir=options.src_dir,
download_dir=options.download_dir,
upgrade=options.upgrade,
Expand All @@ -256,10 +256,12 @@ def run(self, options, args):
session=session,
pycompile=options.compile,
isolated=options.isolated_mode,
wheel_cache=wheel_cache,
)

self.populate_requirement_set(
requirement_set, args, options, finder, session, self.name,
wheel_cache
)

if not requirement_set.has_requirements:
Expand Down
4 changes: 3 additions & 1 deletion pip/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pip.index import PackageFinder
from pip.req import InstallRequirement
from pip.utils import get_installed_distributions, dist_is_editable
from pip.wheel import Cache
from pip.cmdoptions import make_option_group, index_group


Expand Down Expand Up @@ -128,10 +129,11 @@ def find_packages_latest_versions(self, options):
user_only=options.user,
include_editables=False,
)
wheel_cache = Cache(options.cache_dir)
for dist in installed_packages:
req = InstallRequirement.from_line(
dist.key, None, isolated=options.isolated_mode,
cache_root=options.cache_dir,
wheel_cache=wheel_cache
)
typ = 'unknown'
try:
Expand Down
9 changes: 5 additions & 4 deletions pip/commands/uninstall.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

from pip.wheel import Cache
from pip.req import InstallRequirement, RequirementSet, parse_requirements
from pip.basecommand import Command
from pip.exceptions import InstallationError
Expand Down Expand Up @@ -42,28 +43,28 @@ def __init__(self, *args, **kw):

def run(self, options, args):
with self._build_session(options) as session:

wheel_cache = Cache(options.cache_dir)
requirement_set = RequirementSet(
build_dir=None,
cache_root=options.cache_dir,
src_dir=None,
download_dir=None,
isolated=options.isolated_mode,
session=session,
wheel_cache=wheel_cache,
)
for name in args:
requirement_set.add_requirement(
InstallRequirement.from_line(
name, isolated=options.isolated_mode,
cache_root=options.cache_dir,
wheel_cache=wheel_cache
)
)
for filename in options.requirements:
for req in parse_requirements(
filename,
options=options,
session=session,
cache_root=options.cache_dir):
wheel_cache=wheel_cache):
requirement_set.add_requirement(req)
if not requirement_set.has_requirements:
raise InstallationError(
Expand Down
6 changes: 4 additions & 2 deletions pip/commands/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pip.utils import import_or_raise, normalize_path
from pip.utils.build import BuildDirectory
from pip.utils.deprecation import RemovedInPip8Warning
from pip.wheel import WheelBuilder
from pip.wheel import Cache, WheelBuilder
from pip import cmdoptions

DEFAULT_WHEEL_DIR = os.path.join(normalize_path(os.curdir), 'wheelhouse')
Expand Down Expand Up @@ -157,20 +157,22 @@ def run(self, options, args):
build_delete = (not (options.no_clean or options.build_dir))
with BuildDirectory(options.build_dir,
delete=build_delete) as build_dir:
wheel_cache = Cache(options.cache_dir)
requirement_set = RequirementSet(
build_dir=build_dir,
cache_root=options.cache_dir,
src_dir=options.src_dir,
download_dir=None,
ignore_dependencies=options.ignore_dependencies,
ignore_installed=True,
isolated=options.isolated_mode,
session=session,
wheel_cache=wheel_cache,
wheel_download_dir=options.wheel_dir
)

self.populate_requirement_set(
requirement_set, args, options, finder, session, self.name,
wheel_cache
)

if not requirement_set.has_requirements:
Expand Down
6 changes: 3 additions & 3 deletions pip/operations/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def freeze(
find_tags=False,
default_vcs=None,
isolated=False,
cache_root=None):
wheel_cache=None):
find_links = find_links or []
skip_match = None

Expand Down Expand Up @@ -76,13 +76,13 @@ def freeze(
line,
default_vcs=default_vcs,
isolated=isolated,
cache_root=cache_root,
wheel_cache=wheel_cache,
)
else:
line_req = InstallRequirement.from_line(
line,
isolated=isolated,
cache_root=cache_root,
wheel_cache=wheel_cache,
)

if not line_req.name:
Expand Down
23 changes: 12 additions & 11 deletions pip/req/req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ def parser_exit(self, msg):


def parse_requirements(filename, finder=None, comes_from=None, options=None,
session=None, cache_root=None):
session=None, wheel_cache=None):
"""
Parse a requirements file and yield InstallRequirement instances.
:param filename: Path or url of requirements file.
:param finder: Instance of pip.index.PackageFinder.
:param comes_from: Origin description of requirements.
:param options: Global options.
:param session: Instance of pip.download.PipSession.
:param filename: Path or url of requirements file.
:param finder: Instance of pip.index.PackageFinder.
:param comes_from: Origin description of requirements.
:param options: Global options.
:param session: Instance of pip.download.PipSession.
:param wheel_cache: Instance of pip.wheel.Cache
"""
if session is None:
raise TypeError(
Expand All @@ -98,15 +99,15 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
)

parser = parse_content(
filename, content, finder, comes_from, options, session, cache_root
filename, content, finder, comes_from, options, session, wheel_cache
)

for item in parser:
yield item


def parse_content(filename, content, finder=None, comes_from=None,
options=None, session=None, cache_root=None):
options=None, session=None, wheel_cache=None):

# Split, sanitize and join lines with continuations.
content = content.splitlines()
Expand All @@ -129,7 +130,7 @@ def parse_content(filename, content, finder=None, comes_from=None,
isolated = options.isolated_mode if options else False
yield InstallRequirement.from_line(
req, comes_from, isolated=isolated, options=opts,
cache_root=cache_root)
wheel_cache=wheel_cache)

# ---------------------------------------------------------------------
elif linetype == REQUIREMENT_EDITABLE:
Expand All @@ -139,7 +140,7 @@ def parse_content(filename, content, finder=None, comes_from=None,
yield InstallRequirement.from_editable(
value, comes_from=comes_from,
default_vcs=default_vcs, isolated=isolated,
cache_root=cache_root)
wheel_cache=wheel_cache)

# ---------------------------------------------------------------------
elif linetype == REQUIREMENT_FILE:
Expand All @@ -152,7 +153,7 @@ def parse_content(filename, content, finder=None, comes_from=None,
# TODO: Why not use `comes_from='-r {} (line {})'` here as well?
parser = parse_requirements(
req_url, finder, comes_from, options, session,
cache_root=cache_root)
wheel_cache=wheel_cache)
for req in parser:
yield req

Expand Down
17 changes: 9 additions & 8 deletions pip/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class InstallRequirement(object):
def __init__(self, req, comes_from, source_dir=None, editable=False,
link=None, as_egg=False, update=True, editable_options=None,
pycompile=True, markers=None, isolated=False, options=None,
cache_root=None):
wheel_cache=None):
self.extras = ()
if isinstance(req, six.string_types):
req = pkg_resources.Requirement.parse(req)
Expand All @@ -89,7 +89,8 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,
editable_options = {}

self.editable_options = editable_options
self._cache_root = cache_root
assert wheel_cache is not None
self._wheel_cache = wheel_cache
self.link = link
self.as_egg = as_egg
self.markers = markers
Expand Down Expand Up @@ -120,7 +121,7 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,

@classmethod
def from_editable(cls, editable_req, comes_from=None, default_vcs=None,
isolated=False, options=None, cache_root=None):
isolated=False, options=None, wheel_cache=None):
from pip.index import Link

name, url, extras_override, editable_options = parse_editable(
Expand All @@ -136,7 +137,7 @@ def from_editable(cls, editable_req, comes_from=None, default_vcs=None,
editable_options=editable_options,
isolated=isolated,
options=options if options else {},
cache_root=cache_root)
wheel_cache=wheel_cache)

if extras_override is not None:
res.extras = extras_override
Expand All @@ -146,7 +147,7 @@ def from_editable(cls, editable_req, comes_from=None, default_vcs=None,
@classmethod
def from_line(
cls, name, comes_from=None, isolated=False, options=None,
cache_root=None):
wheel_cache=None):
"""Creates an InstallRequirement from a name, which might be a
requirement, directory containing 'setup.py', filename, or URL.
"""
Expand Down Expand Up @@ -213,7 +214,8 @@ def from_line(

options = options if options else {}
return cls(req, comes_from, link=link, markers=markers,
isolated=isolated, options=options, cache_root=cache_root)
isolated=isolated, options=options,
wheel_cache=wheel_cache)

def __str__(self):
if self.req:
Expand Down Expand Up @@ -253,8 +255,7 @@ def link(self):
@link.setter
def link(self, link):
# Lookup a cached wheel, if possible.
link = pip.wheel.cached_wheel(self._cache_root, link)
self._link = link
self._link = self._wheel_cache.cached_wheel(link)

@property
def specifier(self):
Expand Down
8 changes: 4 additions & 4 deletions pip/req/req_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self, build_dir, src_dir, download_dir, upgrade=False,
ignore_dependencies=False, force_reinstall=False,
use_user_site=False, session=None, pycompile=True,
isolated=False, wheel_download_dir=None,
cache_root=None):
wheel_cache=None):
"""Create a RequirementSet.
:param wheel_download_dir: Where still-packed .whl files should be
Expand All @@ -150,7 +150,7 @@ def __init__(self, build_dir, src_dir, download_dir, upgrade=False,
:param download_dir: Where still packed archives should be written to.
If None they are not saved, and are deleted immediately after
unpacking.
:param cache_root: The root of the pip cache, for passing to
:param wheel_cache: The pip wheel cache, for passing to
InstallRequirement.
"""
if session is None:
Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(self, build_dir, src_dir, download_dir, upgrade=False,
if wheel_download_dir:
wheel_download_dir = normalize_path(wheel_download_dir)
self.wheel_download_dir = wheel_download_dir
self._cache_root = cache_root
self._wheel_cache = wheel_cache
# Maps from install_req -> dependencies_of_install_req
self._dependencies = defaultdict(list)

Expand Down Expand Up @@ -517,7 +517,7 @@ def add_req(subreq):
str(subreq),
req_to_install,
isolated=self.isolated,
cache_root=self._cache_root,
wheel_cache=self._wheel_cache,
)
more_reqs.extend(self.add_requirement(
sub_install_req, req_to_install.name))
Expand Down
Loading

0 comments on commit a1a0147

Please sign in to comment.