2014-12-10 19:03:06 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-10-31 06:43:46 +01:00
|
|
|
import collections
|
2014-12-10 19:03:06 +01:00
|
|
|
import logging
|
2017-03-24 21:39:09 +01:00
|
|
|
import os
|
2014-12-10 19:03:06 +01:00
|
|
|
import re
|
|
|
|
|
2017-10-31 06:43:46 +01:00
|
|
|
from pip._vendor import pkg_resources, six
|
2016-03-03 14:48:18 +01:00
|
|
|
from pip._vendor.packaging.utils import canonicalize_name
|
2016-05-26 12:36:16 +02:00
|
|
|
from pip._vendor.pkg_resources import RequirementParseError
|
2014-12-10 19:03:06 +01:00
|
|
|
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.exceptions import InstallationError
|
2018-08-21 17:07:40 +02:00
|
|
|
from pip._internal.req.constructors import (
|
|
|
|
install_req_from_editable, install_req_from_line,
|
|
|
|
)
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.req.req_file import COMMENT_RE
|
|
|
|
from pip._internal.utils.misc import (
|
2018-10-09 07:19:00 +02:00
|
|
|
dist_is_editable, get_installed_distributions,
|
2017-08-31 17:48:18 +02:00
|
|
|
)
|
2014-12-10 19:03:06 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def freeze(
|
|
|
|
requirement=None,
|
|
|
|
find_links=None, local_only=None, user_only=None, skip_regex=None,
|
2015-03-30 23:44:02 +02:00
|
|
|
isolated=False,
|
2015-10-10 23:31:29 +02:00
|
|
|
wheel_cache=None,
|
2017-01-11 22:37:39 +01:00
|
|
|
exclude_editable=False,
|
2015-10-10 23:31:29 +02:00
|
|
|
skip=()):
|
2014-12-10 19:03:06 +01:00
|
|
|
find_links = find_links or []
|
|
|
|
skip_match = None
|
|
|
|
|
|
|
|
if skip_regex:
|
2015-10-09 22:44:21 +02:00
|
|
|
skip_match = re.compile(skip_regex).search
|
2014-12-10 19:03:06 +01: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,
|
2015-10-10 23:31:29 +02:00
|
|
|
skip=(),
|
2014-12-10 19:03:06 +01:00
|
|
|
user_only=user_only):
|
2016-05-26 12:36:16 +02:00
|
|
|
try:
|
2017-03-24 21:39:09 +01:00
|
|
|
req = FrozenRequirement.from_dist(
|
2016-05-26 12:36:16 +02:00
|
|
|
dist,
|
|
|
|
dependency_links
|
|
|
|
)
|
|
|
|
except RequirementParseError:
|
|
|
|
logger.warning(
|
|
|
|
"Could not parse requirement: %s",
|
|
|
|
dist.project_name
|
|
|
|
)
|
|
|
|
continue
|
2017-01-11 22:37:39 +01:00
|
|
|
if exclude_editable and req.editable:
|
|
|
|
continue
|
2014-12-10 19:03:06 +01:00
|
|
|
installations[req.name] = req
|
|
|
|
|
|
|
|
if requirement:
|
2016-05-26 12:45:46 +02: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()
|
2017-10-31 06:43:46 +01:00
|
|
|
# keep track of which files a requirement is in so that we can
|
|
|
|
# give an accurate warning if a requirement appears multiple times.
|
|
|
|
req_files = collections.defaultdict(list)
|
2016-05-26 12:45:46 +02:00
|
|
|
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
|
2014-12-10 19:03:06 +01:00
|
|
|
|
2016-05-26 12:45:46 +02:00
|
|
|
if line.startswith('-e') or line.startswith('--editable'):
|
|
|
|
if line.startswith('-e'):
|
|
|
|
line = line[2:].strip()
|
|
|
|
else:
|
|
|
|
line = line[len('--editable'):].strip().lstrip('=')
|
2018-08-21 16:32:25 +02:00
|
|
|
line_req = install_req_from_editable(
|
2016-05-26 12:45:46 +02:00
|
|
|
line,
|
|
|
|
isolated=isolated,
|
|
|
|
wheel_cache=wheel_cache,
|
|
|
|
)
|
2014-12-10 19:03:06 +01:00
|
|
|
else:
|
2018-08-21 17:07:40 +02:00
|
|
|
line_req = install_req_from_line(
|
2016-10-30 23:09:38 +01:00
|
|
|
COMMENT_RE.sub('', line).strip(),
|
2016-05-26 12:45:46 +02:00
|
|
|
isolated=isolated,
|
|
|
|
wheel_cache=wheel_cache,
|
|
|
|
)
|
2014-12-10 19:03:06 +01:00
|
|
|
|
2016-05-26 12:45:46 +02: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:
|
2017-10-31 06:43:46 +01:00
|
|
|
# either it's not installed, or it is installed
|
|
|
|
# but has been processed already
|
|
|
|
if not req_files[line_req.name]:
|
|
|
|
logger.warning(
|
|
|
|
"Requirement file [%s] contains %s, but that "
|
|
|
|
"package is not installed",
|
|
|
|
req_file_path,
|
|
|
|
COMMENT_RE.sub('', line).strip(),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
req_files[line_req.name].append(req_file_path)
|
2016-05-26 12:45:46 +02:00
|
|
|
else:
|
|
|
|
yield str(installations[line_req.name]).rstrip()
|
|
|
|
del installations[line_req.name]
|
2017-10-31 06:43:46 +01:00
|
|
|
req_files[line_req.name].append(req_file_path)
|
|
|
|
|
|
|
|
# Warn about requirements that were included multiple times (in a
|
|
|
|
# single requirements file or in different requirements files).
|
|
|
|
for name, files in six.iteritems(req_files):
|
|
|
|
if len(files) > 1:
|
|
|
|
logger.warning("Requirement %s included multiple times [%s]",
|
|
|
|
name, ', '.join(sorted(set(files))))
|
2014-12-10 19:03:06 +01:00
|
|
|
|
|
|
|
yield(
|
|
|
|
'## The following requirements were added by '
|
|
|
|
'pip freeze:'
|
|
|
|
)
|
|
|
|
for installation in sorted(
|
|
|
|
installations.values(), key=lambda x: x.name.lower()):
|
2015-10-10 23:31:29 +02:00
|
|
|
if canonicalize_name(installation.name) not in skip:
|
|
|
|
yield str(installation).rstrip()
|
2017-03-24 21:39:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FrozenRequirement(object):
|
|
|
|
def __init__(self, name, req, editable, comments=()):
|
|
|
|
self.name = name
|
|
|
|
self.req = req
|
|
|
|
self.editable = editable
|
|
|
|
self.comments = comments
|
|
|
|
|
|
|
|
@classmethod
|
2018-09-27 07:11:27 +02:00
|
|
|
def _init_args_from_dist(cls, dist, dependency_links):
|
|
|
|
"""
|
|
|
|
Compute and return arguments (req, editable, comments) to pass to
|
|
|
|
FrozenRequirement.__init__().
|
|
|
|
|
|
|
|
This method is for use in FrozenRequirement.from_dist().
|
|
|
|
"""
|
2017-03-24 21:39:09 +01:00
|
|
|
location = os.path.normcase(os.path.abspath(dist.location))
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.vcs import vcs, get_src_requirement
|
2018-10-10 11:16:00 +02:00
|
|
|
if not dist_is_editable(dist):
|
|
|
|
req = dist.as_requirement()
|
|
|
|
return (req, False, [])
|
|
|
|
|
2018-10-10 11:34:40 +02:00
|
|
|
vc_type = vcs.get_backend_type(location)
|
2018-10-10 11:16:00 +02:00
|
|
|
|
2018-10-10 11:34:40 +02:00
|
|
|
if not vc_type:
|
2018-10-09 07:21:45 +02:00
|
|
|
req = dist.as_requirement()
|
|
|
|
return (req, False, [])
|
2018-09-30 11:39:20 +02:00
|
|
|
|
2018-10-09 07:21:45 +02:00
|
|
|
try:
|
2018-10-10 11:34:40 +02:00
|
|
|
req = get_src_requirement(vc_type, dist, location)
|
2018-10-09 07:21:45 +02:00
|
|
|
except InstallationError as exc:
|
2018-09-30 11:39:20 +02:00
|
|
|
logger.warning(
|
2018-10-09 07:21:45 +02:00
|
|
|
"Error when trying to get requirement for VCS system %s, "
|
|
|
|
"falling back to uneditable format", exc
|
2018-09-30 11:39:20 +02:00
|
|
|
)
|
2018-10-09 07:21:45 +02:00
|
|
|
else:
|
|
|
|
if req is not None:
|
|
|
|
return (req, True, [])
|
2018-09-30 11:33:49 +02:00
|
|
|
|
2018-10-09 07:21:45 +02:00
|
|
|
logger.warning(
|
|
|
|
'Could not determine repository location of %s', location
|
|
|
|
)
|
|
|
|
comments = ['## !! Could not determine repository location']
|
2018-09-30 11:33:49 +02:00
|
|
|
req = dist.as_requirement()
|
2018-09-30 11:41:17 +02:00
|
|
|
|
2018-10-09 07:21:45 +02:00
|
|
|
return (req, False, comments)
|
2018-09-27 07:11:27 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_dist(cls, dist, dependency_links):
|
|
|
|
args = cls._init_args_from_dist(dist, dependency_links)
|
|
|
|
return cls(dist.project_name, *args)
|
2017-03-24 21:39:09 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
req = self.req
|
|
|
|
if self.editable:
|
|
|
|
req = '-e %s' % req
|
|
|
|
return '\n'.join(list(self.comments) + [str(req)]) + '\n'
|