From 2f16b88fb39e2ac269262b804aa2ce2cbeb91421 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Wed, 10 Oct 2018 02:16:00 -0700 Subject: 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 000000000..e69de29bb diff --git a/src/pip/_internal/operations/freeze.py b/src/pip/_internal/operations/freeze.py index b173875bf..3911a0fcc 100644 --- a/src/pip/_internal/operations/freeze.py +++ b/src/pip/_internal/operations/freeze.py @@ -173,12 +173,18 @@ class FrozenRequirement(object): """ 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 794b35d65..4249a7c0e 100644 --- a/src/pip/_internal/vcs/__init__.py +++ b/src/pip/_internal/vcs/__init__.py @@ -150,12 +150,6 @@ class VcsSupport(object): 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 @@ class VersionControl(object): 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, -- cgit v1.2.1 From 00a2ff198e178b440793820de78e06031c378252 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Wed, 10 Oct 2018 02:34:40 -0700 Subject: 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 3911a0fcc..af484f2c1 100644 --- a/src/pip/_internal/operations/freeze.py +++ b/src/pip/_internal/operations/freeze.py @@ -177,14 +177,14 @@ class FrozenRequirement(object): 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 4249a7c0e..d34c8be0b 100644 --- a/src/pip/_internal/vcs/__init__.py +++ b/src/pip/_internal/vcs/__init__.py @@ -133,16 +133,16 @@ class VcsSupport(object): 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 @@ class VersionControl(object): 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() -- cgit v1.2.1