server: Create file paths as needed
authorW. Trevor King <wking@tremily.us>
Thu, 20 Feb 2014 20:48:22 +0000 (12:48 -0800)
committerW. Trevor King <wking@tremily.us>
Thu, 20 Feb 2014 20:48:22 +0000 (12:48 -0800)
Add support for non-flat source file layouts (e.g. relative paths that
contain directory parts).

Instead of creating the cache directory and possible per-file
subdirectories separately, just create per-file directories on the
fly.  This simplifies the code, but means that you won't die until the
first request if your server doesn't have permission to create these
directories.

package_cache/server.py

index 97deb5254e305ba02e0e9573f60bb75880bfb3e5..be34ee4960e9925eac38432144bb6244c60ebb77 100644 (file)
@@ -24,8 +24,6 @@ class Server (object):
         self.opener.addheaders = [
             ('User-agent', 'Package-cache/{}'.format(__version__)),
             ]
-        if not _os.path.isdir(self.cache):
-            _os.makedirs(self.cache, exist_ok=True)
 
     def __call__(self, environ, start_response):
         try:
@@ -54,6 +52,9 @@ class Server (object):
             path=cache_path, environ=environ, start_response=start_response)
 
     def _get_file_from_sources(self, url, path):
+        dirname = _os.path.dirname(path)
+        if not _os.path.isdir(dirname):
+            _os.makedirs(dirname, exist_ok=True)
         for i, source in enumerate(self.sources):
             source_url = source.rstrip('/') + url
             try: