[Python-checkins] distutils2: PyPIServer can now serve static files from multiple directories.

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


tarek.ziade pushed 7eaab6fefd5a to distutils2:

http://hg.python.org/distutils2/rev/7eaab6fefd5a
changeset:   291:7eaab6fefd5a
user:        Alexis Metaireau <ametaireau at gmail.com>
date:        Tue Jun 01 10:53:15 2010 +0200
summary:     PyPIServer can now serve static files from multiple directories.
files:       docs/source/test_framework.rst, src/distutils2/tests/pypi_server.py, src/distutils2/tests/test_pypi_server.py

diff --git a/docs/source/test_framework.rst b/docs/source/test_framework.rst
--- a/docs/source/test_framework.rst
+++ b/docs/source/test_framework.rst
@@ -14,9 +14,20 @@
 HTTP response can be overriden with the ``default_response_status``,
 ``default_response_headers`` and ``default_response_data`` attributes.
 
-When accessing the server with urls beginning with `/simple/`, the server also
-record your requests, but will look for files under the /tests/pypi_server/
-path.
+By default, when accessing the server with urls beginning with `/simple/`, 
+the server also record your requests, but will look for files under 
+the `/tests/pypiserver/simple/` path.
+
+You can tell the sever to serve static files for other paths. This could be 
+accomplished by using the `static_uri_paths` parameter, as below::
+
+    server = PyPIServer(static_uri_paths=["first_path", "second_path"])
+
+You need to create the content that will be served under the `/test/pypiserver`
+path. If you want to serve content from another place, you also can specify
+another filesystem path::
+
+    server = PyPIServer(static_filesystem_paths=["path/to/your/dir"])
 
 ``PyPIServerTestCase``
 ======================
diff --git a/src/distutils2/tests/pypi_server.py b/src/distutils2/tests/pypi_server.py
--- a/src/distutils2/tests/pypi_server.py
+++ b/src/distutils2/tests/pypi_server.py
@@ -19,7 +19,7 @@
     """Thread that wraps a wsgi app"""
     
     def __init__(self, static_uri_paths=["pypi"],
-            static_filesystem_path=PYPI_DEFAULT_STATIC_PATH):
+            static_filesystem_paths=[PYPI_DEFAULT_STATIC_PATH]):
         """Initialize the server.
 
         static_uri_paths and static_base_path are parameters used to provides
@@ -36,7 +36,7 @@
         self.default_response_headers = [('Content-type', 'text/plain')]
         self.default_response_data = ["hello"]
         self._static_uri_paths = static_uri_paths
-        self._static_filesystem_path = static_filesystem_path
+        self._static_filesystem_paths = static_filesystem_paths
 
     def run(self):
         self.httpd.serve_forever()
@@ -49,7 +49,7 @@
         """Serve the content.
 
         Also record the requests to be accessed later. If trying to access an
-        url matching `_static_paths`, serve static content, otherwise serve 
+        url matching a static uri, serve static content, otherwise serve 
         what is provided by the `get_next_response` method.
         """
         # record the request. Read the input only on PUT or POST requests
@@ -69,12 +69,16 @@
         relative_path = environ["PATH_INFO"].replace(self.full_address, '')
         url_parts = relative_path.split("/")
         if len(url_parts) > 1 and url_parts[1] in self._static_uri_paths:
-            file_to_serve = self._static_filesystem_path + relative_path
-            try:
-                file = open(file_to_serve)
-                data = file.read()
-                start_response("200 OK", [('Content-type', 'text/plain')])
-            except IOError:
+            data = None
+            for fs_path in self._static_filesystem_paths:
+                try:
+                    file = open(fs_path + relative_path)
+                    data = file.read()
+                    start_response("200 OK", [('Content-type', 'text/plain')])
+                except IOError:
+                    pass
+            
+            if data is None:
                 start_response("404 NOT FOUND", [('Content-type', 'text/plain')])
                 data = "Not Found"
             return data
diff --git a/src/distutils2/tests/test_pypi_server.py b/src/distutils2/tests/test_pypi_server.py
--- a/src/distutils2/tests/test_pypi_server.py
+++ b/src/distutils2/tests/test_pypi_server.py
@@ -1,6 +1,6 @@
 """Tests for distutils.command.bdist."""
 import unittest2, urllib, urllib2
-from distutils2.tests.pypi_server import PyPIServer
+from distutils2.tests.pypi_server import PyPIServer, PYPI_DEFAULT_STATIC_PATH
 import os.path
 
 class PyPIServerTest(unittest2.TestCase):
@@ -33,7 +33,7 @@
             url = server.full_address + url_path 
             request = urllib2.Request(url)
             response = urllib2.urlopen(request)
-            file = open(server._static_filesystem_path + url_path)
+            file = open(PYPI_DEFAULT_STATIC_PATH + url_path)
             return response.read() == file.read()
 
         server = PyPIServer(static_uri_paths=["simple", "external"])

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


More information about the Python-checkins mailing list