Commits

Paul Moore committed e8fec91

Move pkg_resources dependency into wheel.util

Comments (0)

Files changed (3)

 import csv
 
 try:
-    from pkg_resources import parse_version
-except ImportError:
-    from distutils.version import LooseVersion as parse_version
-
-try:
     import sysconfig
 except ImportError:
     import distutils.sysconfig as sysconfig
 
 from wheel.decorator import reify
 from wheel.util import (urlsafe_b64encode, from_json,
-    urlsafe_b64decode, native, binary, HashingFile)
+    urlsafe_b64decode, native, binary, HashingFile, parse_version)
 from wheel import signatures
 from wheel.pkginfo import read_pkg_info_bytes
 from wheel.util import open_for_csv

wheel/tool/__init__.py

 import sys
 import json
 from glob import iglob
-try:
-    from pkg_resources import Distribution, Requirement
-    have_pkgresources = True
-except ImportError:
-    have_pkgresources = False
 from .. import signatures
-from ..util import urlsafe_b64decode, urlsafe_b64encode, native, binary
+from ..util import (urlsafe_b64decode, urlsafe_b64encode, native, binary,
+        have_pkgresources, matches_requirement)
 from ..install import WheelFile
 
 if have_pkgresources:
     wf.zipfile.extractall(destination)
     wf.zipfile.close()    
 
-def matches_requirement(req, wheels):
-    """List of wheels matching a requirement.
-
-    :param req: The requirement to satisfy
-    :param wheels: List of wheels to search.
-    """
-    # If we don't have pkg_resources, raise an error
-    if not have_pkgresources:
-        raise RuntimeError("matches_requirement called without pkg_resources")
-
-    selected =  []
-    for wf in wheels:
-        f = wf.parsed_filename
-        dist = Distribution(project_name=f.group("name"), version=f.group("ver"))
-        if dist in req:
-            selected.append(wf)
-    return selected
-
 def install(requirements, requirements_file=None,
             wheel_dirs=None, force=False, list_files=False,
             dry_run=False):
             continue
 
         # We have a requirement spec
-        # We can only handle this if we have pkg_resources available
-        if have_pkgresources:
-            req = Requirement.parse(req)
-            matches = matches_requirement(req, all_wheels)
-            if not matches:
-                raise Exception("No match for requirement {}".format(req))
-            to_install.append(max(matches))
+        # If we don't have pkg_resources, this will raise an exception
+        matches = matches_requirement(req, all_wheels)
+        if not matches:
+            raise Exception("No match for requirement {}".format(req))
+        to_install.append(max(matches))
 
     # We now have a list of wheels to install
     if list_files:
 from .pep425tags import (get_abbr_impl, get_impl_ver, get_supported,
                          get_supported as generate_supported) # b/c
 
+try:
+    from pkg_resources import Distribution, Requirement, parse_version
+    have_pkgresources = True
+except ImportError:
+    from distutils.version import LooseVersion as parse_version
+    have_pkgresources = False
+
 __all__ = ['urlsafe_b64encode', 'urlsafe_b64decode', 'utf8', 'to_json',
            'from_json', 'generate_supported', 'get_abbr_impl', 'get_impl_ver',
-           'compatibility_match']
+           'compatibility_match', 'parse_version', 'matches_requirement',
+           'have_pkgresources']
 
 
 def urlsafe_b64encode(data):
         import dirspec.basedir
         return dirspec.basedir.load_config_paths(*resource)
 
+if have_pkgresources:
+    def matches_requirement(req, wheels):
+        """List of wheels matching a requirement.
+
+        :param req: The requirement to satisfy
+        :param wheels: List of wheels to search.
+        """
+        # If we don't have pkg_resources, raise an error
+        req = Requirement.parse(req)
+
+        selected =  []
+        for wf in wheels:
+            f = wf.parsed_filename
+            dist = Distribution(project_name=f.group("name"), version=f.group("ver"))
+            if dist in req:
+                selected.append(wf)
+        return selected
+else:
+    def matches_requirement(req, wheels):
+        raise RuntimeError("Cannot use requirements without pkg_resources")