[Python-checkins] distutils2: local files support

tarek.ziade python-checkins at python.org
Sun Jul 4 11:48:40 CEST 2010


tarek.ziade pushed 187764b18476 to distutils2:

http://hg.python.org/distutils2/rev/187764b18476
changeset:   331:187764b18476
parent:      324:f28c41acac7c
user:        Alexis Metaireau <ametaireau at gmail.com>
date:        Tue Jun 29 13:45:26 2010 +0200
summary:     local files support
files:       src/distutils2/pypi/simple.py, src/distutils2/tests/test_pypi_simple.py

diff --git a/src/distutils2/pypi/simple.py b/src/distutils2/pypi/simple.py
--- a/src/distutils2/pypi/simple.py
+++ b/src/distutils2/pypi/simple.py
@@ -75,6 +75,8 @@
                         working with the one given in "url"
         :param timeout: time in seconds to consider a url has timeouted.
         """
+        if not index_url.endswith("/"):
+            index_url += "/"
         self._index_urls = [index_url]
         self._index_urls.extend(mirrors)
         self._current_index_url = 0
@@ -169,10 +171,8 @@
         """
         # if _index_url is contained in the given URL, we are browsing the
         # index, and it's always "browsable".
-        # We asume here that if the url starts with "." or "..", it's browsable
-        # too. This is useful as the simple index make heavy use of relative
-        # URLS.
-        if self.index_url in url:
+        # local files are always considered browable resources
+        if self.index_url in url or urlparse.urlparse(url)[0] == "file":
             return True
         elif self.follow_externals is True:
             if self._allowed_hosts(urlparse.urlparse(url)[1]):  # 1 is netloc
@@ -287,7 +287,8 @@
 
     @socket_timeout()
     def _open_url(self, url):
-        """Open a urllib2 request, handling HTTP authentication.
+        """Open a urllib2 request, handling HTTP authentication, and local
+        files support.
 
         """
         try:
@@ -297,6 +298,11 @@
                 auth, host = urllib2.splituser(netloc)
             else:
                 auth = None
+            
+            # add index.html automatically for filesystem paths
+            if scheme == 'file':
+                if url.endswith('/'):
+                    url += "index.html"
 
             if auth:
                 auth = "Basic " + \
diff --git a/src/distutils2/tests/test_pypi_simple.py b/src/distutils2/tests/test_pypi_simple.py
--- a/src/distutils2/tests/test_pypi_simple.py
+++ b/src/distutils2/tests/test_pypi_simple.py
@@ -8,7 +8,8 @@
 import unittest2
 import urllib2
 
-from distutils2.tests.pypi_server import use_pypi_server, PyPIServer
+from distutils2.tests.pypi_server import use_pypi_server, PyPIServer, \
+                                         PYPI_DEFAULT_STATIC_PATH
 from distutils2.pypi import simple
 
 from distutils2.errors import DistutilsError
@@ -274,6 +275,14 @@
                          generator.next())
         self.assertRaises(StopIteration, generator.next)
 
+    def test_browse_local_files(self):
+        """Test that we can browse local files"""
+        index_path = os.sep.join(["file://" + PYPI_DEFAULT_STATIC_PATH,
+                                  "test_found_links", "simple"])
+        index = simple.SimpleIndex(index_path)
+        dists = index.find("foobar")
+        self.assertEqual(4, len(dists))
+
 def test_suite():
     return unittest2.makeSuite(PyPISimpleTestCase)
 

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list