openmedialibrary_platform_l.../lib/python3.4/site-packages/pip/operations/freeze.py

133 lines
5.1 KiB
Python
Raw Normal View History

2016-02-22 11:13:36 +00:00
from __future__ import absolute_import
import logging
import re
import pip
from pip.req import InstallRequirement
2017-10-13 12:49:49 +00:00
from pip.req.req_file import COMMENT_RE
2016-02-22 11:13:36 +00:00
from pip.utils import get_installed_distributions
from pip._vendor import pkg_resources
2016-03-23 11:41:42 +00:00
from pip._vendor.packaging.utils import canonicalize_name
2017-10-13 12:49:49 +00:00
from pip._vendor.pkg_resources import RequirementParseError
2016-02-22 11:13:36 +00:00
logger = logging.getLogger(__name__)
def freeze(
requirement=None,
find_links=None, local_only=None, user_only=None, skip_regex=None,
default_vcs=None,
isolated=False,
2016-03-23 11:41:42 +00:00
wheel_cache=None,
skip=()):
2016-02-22 11:13:36 +00:00
find_links = find_links or []
skip_match = None
if skip_regex:
2016-03-23 11:41:42 +00:00
skip_match = re.compile(skip_regex).search
2016-02-22 11:13:36 +00:00
dependency_links = []
for dist in pkg_resources.working_set:
if dist.has_metadata('dependency_links.txt'):
dependency_links.extend(
dist.get_metadata_lines('dependency_links.txt')
)
for link in find_links:
if '#egg=' in link:
dependency_links.append(link)
for link in find_links:
yield '-f %s' % link
installations = {}
for dist in get_installed_distributions(local_only=local_only,
2016-03-23 11:41:42 +00:00
skip=(),
2016-02-22 11:13:36 +00:00
user_only=user_only):
2017-10-13 12:49:49 +00:00
try:
req = pip.FrozenRequirement.from_dist(
dist,
dependency_links
)
except RequirementParseError:
logger.warning(
"Could not parse requirement: %s",
dist.project_name
)
continue
2016-02-22 11:13:36 +00:00
installations[req.name] = req
if requirement:
2017-10-13 12:49:49 +00:00
# the options that don't get turned into an InstallRequirement
# should only be emitted once, even if the same option is in multiple
# requirements files, so we need to keep track of what has been emitted
# so that we don't emit it again if it's seen again
emitted_options = set()
for req_file_path in requirement:
with open(req_file_path) as req_file:
for line in req_file:
if (not line.strip() or
line.strip().startswith('#') or
(skip_match and skip_match(line)) or
line.startswith((
'-r', '--requirement',
'-Z', '--always-unzip',
'-f', '--find-links',
'-i', '--index-url',
'--pre',
'--trusted-host',
'--process-dependency-links',
'--extra-index-url'))):
line = line.rstrip()
if line not in emitted_options:
emitted_options.add(line)
yield line
continue
2016-02-22 11:13:36 +00:00
2017-10-13 12:49:49 +00:00
if line.startswith('-e') or line.startswith('--editable'):
if line.startswith('-e'):
line = line[2:].strip()
else:
line = line[len('--editable'):].strip().lstrip('=')
line_req = InstallRequirement.from_editable(
line,
default_vcs=default_vcs,
isolated=isolated,
wheel_cache=wheel_cache,
)
2016-02-22 11:13:36 +00:00
else:
2017-10-13 12:49:49 +00:00
line_req = InstallRequirement.from_line(
COMMENT_RE.sub('', line).strip(),
isolated=isolated,
wheel_cache=wheel_cache,
)
2016-02-22 11:13:36 +00:00
2017-10-13 12:49:49 +00:00
if not line_req.name:
logger.info(
"Skipping line in requirement file [%s] because "
"it's not clear what it would install: %s",
req_file_path, line.strip(),
)
logger.info(
" (add #egg=PackageName to the URL to avoid"
" this warning)"
)
elif line_req.name not in installations:
logger.warning(
"Requirement file [%s] contains %s, but that "
"package is not installed",
req_file_path, COMMENT_RE.sub('', line).strip(),
)
else:
yield str(installations[line_req.name]).rstrip()
del installations[line_req.name]
2016-02-22 11:13:36 +00:00
yield(
'## The following requirements were added by '
'pip freeze:'
)
for installation in sorted(
installations.values(), key=lambda x: x.name.lower()):
2016-03-23 11:41:42 +00:00
if canonicalize_name(installation.name) not in skip:
yield str(installation).rstrip()