changeset 41:c934505fa098

sketch out quick + dirty solution; tests do not pass
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 01 Mar 2012 10:43:28 -0800
parents bb69b82ddd17
children 6ea806412fd1
files simpypi/wsgi.py
diffstat 1 files changed, 44 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/simpypi/wsgi.py	Thu Mar 01 10:16:12 2012 -0800
+++ b/simpypi/wsgi.py	Thu Mar 01 10:43:28 2012 -0800
@@ -59,21 +59,60 @@
         if not hasattr(package, 'file') or not hasattr(package, 'filename'):
             return exc.HTTPBadRequest()
 
-        # successful response
+        # successful response: redirect to the main page
         response = exc.HTTPSeeOther(add_slash=True)
 
         # make a temporary copy for pkginfo
         # (or PaInt?)
         tmpdir = tempfile.mkdtemp()
         try:
-            path = os.path.join(tmpdir, filename)
+            path = os.path.join(tmpdir, package.filename)
             f = file(path, 'w')
-            import pdb; pdb.set_trace()
+            f.write(package.file.read())
             f.close()
-        # put the package in the right place
+
+            # get package data
+            sdist = pkginfo.sdist.SDist(path)
+
+            # put the package in the right place
+            self.add_package(sdist)
+        except:
+            # something bad happen
+            response = exc.HTTPBadRequest()
         finally:
             # cleanup
             shutil.rmtree(tmpdir)
 
-        # redirect to the main page
         return response
+
+    ### API
+
+    def add_package(self, sdist, move=True):
+        """
+        add a package to the directory
+        """
+
+        # make a directory for the package
+        directory = os.path.join(self.directory, sdist.name)
+        if not os.path.exists(directory):
+            os.makedir(directory)
+        assert os.path.isdir(directory)
+
+        # determine the extension (XXX hacky)
+        extensions = ('.tar.gz', '.zip', '.tar.bz2')
+        for ext in extensions:
+            if sdist.filename.endswith(ext):
+                break
+        else:
+            raise Exception("Extension %s not found: %s" % (extensions, sdist.filename))
+
+        # get the filename destination
+        filename = '%s-%s%s' % (sdist.name, sdist.version, ext)
+        filename = os.path.join(directory, filename)
+
+        if move:
+            # move the file
+            shutil.move(sdist.filename, filename)
+        else:
+            # copy the file
+            shutil.copy(sdist.filename, filename)