diff paint/package.py @ 28:c44fb4b6918b

CLI sorta works
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 30 Mar 2012 11:07:15 -0700
parents c54411c721cb
children 59524b6d25c0
line wrap: on
line diff
--- a/paint/package.py	Fri Mar 30 10:05:45 2012 -0700
+++ b/paint/package.py	Fri Mar 30 11:07:15 2012 -0700
@@ -3,6 +3,7 @@
 """
 
 import os
+import pip
 import shutil
 import subprocess
 import sys
@@ -20,14 +21,13 @@
 __all__ = ['Package']
 
 class Package(object):
-    # XXX much of this is generic resource stuff and should be split off
 
     def __init__(self, src):
         self.src = src
 
         # ephemeral data
         self._tmppath = None
-        self._egg_info = None
+        self._egg_info_path = None
 
     def path(self):
         """filesystem path to package"""
@@ -43,7 +43,7 @@
             tmpfile = src = self.fetch()
 
         # unpack if an archive
-        if self.is_archive(src):
+        if self._is_archive(src):
             try:
                 self.unpack(src)
             finally:
@@ -83,13 +83,13 @@
             tf.extract(member, path=self._tmppath)
         tf.close()
 
-    def is_archive(self, path):
+    def _is_archive(self, path):
         """returns if the filesystem path is an archive"""
         # TODO: should handle zipfile additionally at least
         # Ideally, this would be pluggable, etc
         return tarfile.is_tarfile(path)
 
-    def cleanup(self):
+    def _cleanup(self):
         if self._tmppath:
             shutil.rmtree(self._tmppath)
         self._tmppath = None
@@ -98,12 +98,12 @@
 
     ### python-package-specific functionality
 
-    def egg_info(self):
+    def _egg_info(self):
         """build the egg_info directory"""
 
-        if self._egg_info:
+        if self._egg_info_path:
             # return cached copy
-            return self._egg_info
+            return self._egg_info_path
 
         directory = self.path()
         setup_py = os.path.join(directory, 'setup.py')
@@ -121,14 +121,14 @@
         assert os.path.isdir(egg_info), "%s is not a directory" % egg_info
 
         # cache it
-        self._egg_info = egg_info
-        return self._egg_info
+        self._egg_info_path = egg_info
+        return self._egg_info_path
 
     def info(self):
         """return info dictionary for package"""
         # could use pkginfo
 
-        egg_info = self.egg_info()
+        egg_info = self._egg_info()
 
         # read the package information
         pkg_info = os.path.join(egg_info, 'PKG-INFO')
@@ -147,7 +147,7 @@
         """return the dependencies"""
 
         # get the egg_info directory
-        egg_info = self.egg_info()
+        egg_info = self._egg_info()
 
         # read the dependencies
         requires = os.path.join(egg_info, 'requires.txt')
@@ -172,5 +172,18 @@
 
     def download(self, directory):
         """download a package and all its dependencies using pip"""
-        import pip
-        
+        if not os.path.exists(directory):
+            os.makedirs(directory)
+        assert os.path.isdir(directory)
+        pip.main(['install', '--download', directory, self.src])
+
+    def pypi(self, directory):
+        """
+        download packages for a pypi directory structure
+        http://k0s.org/portfolio/pypi.html
+        """
+        tempdir = tempfile.mkdtemp()
+        try:
+            pass
+        finally:
+            shutil.rmtree(tempdir)