From 2f16b88fb39e2ac269262b804aa2ce2cbeb91421 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Wed, 10 Oct 2018 02:16:00 -0700 Subject: [PATCH 1/2] Refactor away VcsSupport.get_backend_from_location(). --- news/D9A540D9-7665-48A5-A1C8-5141ED49E404.trivial | 0 src/pip/_internal/operations/freeze.py | 10 ++++++++-- src/pip/_internal/vcs/__init__.py | 10 ++-------- 3 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 news/D9A540D9-7665-48A5-A1C8-5141ED49E404.trivial diff --git a/news/D9A540D9-7665-48A5-A1C8-5141ED49E404.trivial b/news/D9A540D9-7665-48A5-A1C8-5141ED49E404.trivial new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/pip/_internal/operations/freeze.py b/src/pip/_internal/operations/freeze.py index b173875bf01..3911a0fcc16 100644 --- a/src/pip/_internal/operations/freeze.py +++ b/src/pip/_internal/operations/freeze.py @@ -173,12 +173,18 @@ def _init_args_from_dist(cls, dist, dependency_links): """ location = os.path.normcase(os.path.abspath(dist.location)) from pip._internal.vcs import vcs, get_src_requirement - if not dist_is_editable(dist) or not vcs.get_backend_name(location): + if not dist_is_editable(dist): + req = dist.as_requirement() + return (req, False, []) + + vc_name = vcs.get_backend_name(location) + + if not vc_name: req = dist.as_requirement() return (req, False, []) try: - req = get_src_requirement(dist, location) + req = get_src_requirement(vc_name, dist, location) except InstallationError as exc: logger.warning( "Error when trying to get requirement for VCS system %s, " diff --git a/src/pip/_internal/vcs/__init__.py b/src/pip/_internal/vcs/__init__.py index 794b35d6564..4249a7c0eb3 100644 --- a/src/pip/_internal/vcs/__init__.py +++ b/src/pip/_internal/vcs/__init__.py @@ -150,12 +150,6 @@ def get_backend(self, name): if name in self._registry: return self._registry[name] - def get_backend_from_location(self, location): - vc_type = self.get_backend_name(location) - if vc_type: - return self.get_backend(vc_type) - return None - vcs = VcsSupport() @@ -487,8 +481,8 @@ def controls_location(cls, location): return cls.is_repository_directory(location) -def get_src_requirement(dist, location): - version_control = vcs.get_backend_from_location(location) +def get_src_requirement(vc_name, dist, location): + version_control = vcs.get_backend(vc_name) if version_control: try: return version_control().get_src_requirement(dist, From 00a2ff198e178b440793820de78e06031c378252 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Wed, 10 Oct 2018 02:34:40 -0700 Subject: [PATCH 2/2] Remove unneeded if block in vcs.get_src_requirement(). --- src/pip/_internal/operations/freeze.py | 6 ++-- src/pip/_internal/vcs/__init__.py | 39 ++++++++++---------------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/pip/_internal/operations/freeze.py b/src/pip/_internal/operations/freeze.py index 3911a0fcc16..af484f2c19c 100644 --- a/src/pip/_internal/operations/freeze.py +++ b/src/pip/_internal/operations/freeze.py @@ -177,14 +177,14 @@ def _init_args_from_dist(cls, dist, dependency_links): req = dist.as_requirement() return (req, False, []) - vc_name = vcs.get_backend_name(location) + vc_type = vcs.get_backend_type(location) - if not vc_name: + if not vc_type: req = dist.as_requirement() return (req, False, []) try: - req = get_src_requirement(vc_name, dist, location) + req = get_src_requirement(vc_type, dist, location) except InstallationError as exc: logger.warning( "Error when trying to get requirement for VCS system %s, " diff --git a/src/pip/_internal/vcs/__init__.py b/src/pip/_internal/vcs/__init__.py index 4249a7c0eb3..d34c8be0b17 100644 --- a/src/pip/_internal/vcs/__init__.py +++ b/src/pip/_internal/vcs/__init__.py @@ -133,16 +133,16 @@ def unregister(self, cls=None, name=None): else: logger.warning('Cannot unregister because no class or name given') - def get_backend_name(self, location): + def get_backend_type(self, location): """ - Return the name of the version control backend if found at given - location, e.g. vcs.get_backend_name('/path/to/vcs/checkout') + Return the type of the version control backend if found at given + location, e.g. vcs.get_backend_type('/path/to/vcs/checkout') """ for vc_type in self._registry.values(): if vc_type.controls_location(location): logger.debug('Determine that %s uses VCS: %s', location, vc_type.name) - return vc_type.name + return vc_type return None def get_backend(self, name): @@ -481,23 +481,14 @@ def controls_location(cls, location): return cls.is_repository_directory(location) -def get_src_requirement(vc_name, dist, location): - version_control = vcs.get_backend(vc_name) - if version_control: - try: - return version_control().get_src_requirement(dist, - location) - except BadCommand: - logger.warning( - 'cannot determine version of editable source in %s ' - '(%s command not found in path)', - location, - version_control.name, - ) - return dist.as_requirement() - logger.warning( - 'cannot determine version of editable source in %s (is not SVN ' - 'checkout, Git clone, Mercurial clone or Bazaar branch)', - location, - ) - return dist.as_requirement() +def get_src_requirement(vc_type, dist, location): + try: + return vc_type().get_src_requirement(dist, location) + except BadCommand: + logger.warning( + 'cannot determine version of editable source in %s ' + '(%s command not found in path)', + location, + vc_type.name, + ) + return dist.as_requirement()