Commits

Vinay Sajip committed 61ce7c1

Added DistPathLocator.

  • Participants
  • Parent commits 2ac3de5

Comments (0)

Files changed (2)

File distlib/locators.py

                      url2pathname, pathname2url, queue, quote,
                      unescape, string_types,
                      Request, HTTPError, URLError)
-from .database import Distribution
+from .database import Distribution, DistributionPath
 from .metadata import Metadata
 from .util import (cached_property, parse_credentials, ensure_slash,
                    split_filename, get_release_data)
                 result[version] = dist
         return result
 
+class DistPathLocator(Locator):
+    def __init__(self, path, **kwargs):
+        super(DistPathLocator, self).__init__(**kwargs)
+        assert isinstance(path, DistributionPath)
+        self.path = path
+
+    def _get_project(self, name):
+        dist = self.path.get_distribution(name)
+        if dist is None:
+            result = None
+        else:
+            result = { dist.version: dist }
+        return result
+
+
 class AggregatingLocator(Locator):
     """
     Chain and/or merge a list of locators.
             result = True
         return result
 
-    def find(self, dist):
+    def find(self, requirement):
+        dist = self.locator.locate(requirement)
+        if dist is None:
+            raise ValueError('Unable to locate %r' % requirement)
         problems = set()
         todo = set([dist])
         while todo:

File tests/test_locators.py

 #
 from __future__ import unicode_literals
 import os
+import sys
 
 from compat import unittest
 
 from distlib.compat import url2pathname, urlparse
+from distlib.database import DistributionPath
 from distlib.locators import (SimpleScrapingLocator, PyPIRPCLocator,
                               PyPIJSONLocator, DirectoryLocator,
-                              AggregatingLocator)
+                              DistPathLocator, AggregatingLocator)
 
 HERE = os.path.abspath(os.path.dirname(__file__))
 
         expected = set(['Flask', 'python-gnupg', 'coverage', 'Django'])
         self.assertEqual(names, expected)
 
+    def test_path(self):
+        fakes = os.path.join(HERE, 'fake_dists')
+        sys.path.insert(0, fakes)
+        try:
+            edp = DistributionPath(include_egg=True)
+            locator = DistPathLocator(edp)
+            cases = ('babar', 'choxie', 'strawberry', 'towel-stuff',
+                     'coconuts-aster', 'bacon', 'grammar', 'truffles',
+                     'banana', 'cheese')
+            for name in cases:
+                d = locator.locate(name)
+                r = locator.get_project(name)
+                self.assertIsNotNone(d)
+                self.assertEqual(r, { d.version: d })
+        finally:
+            sys.path.pop(0)
+
     def test_aggregation(self):
         d = os.path.join(HERE, 'fake_archives')
         loc1 = DirectoryLocator(d)