diff carton.py @ 18:c6a03199d4bf

stub out package creation; next: to test this
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 08 Jul 2011 17:19:25 -0700
parents b05f5f1ec26e
children 46882eaebb59
line wrap: on
line diff
--- a/carton.py	Fri Jul 08 16:03:09 2011 -0700
+++ b/carton.py	Fri Jul 08 17:19:25 2011 -0700
@@ -15,6 +15,7 @@
 # imports
 import os
 import sys
+import subprocess
 import tarfile
 import tempfile
 import urllib2
@@ -73,7 +74,6 @@
         break
 else:
     raise Exception("virtualenv.py not found in " + tempdir)
-print virtualenv
         
 # create the virtualenv
 os.environ.pop('PYTHONHOME', None)
@@ -128,12 +128,21 @@
 def isURL(path):
     return path.startswith('http://') or path.startswith('https://')
 
+try:
+    call = subprocess.check_call
+except AttributeError:
+    # old python; boo :(
+    call = subprocess.call
+
 def main(args=sys.argv[1:]):
 
     # parse CLI arguments
     parser = OptionParser(usage=usage, description=__doc__)
     parser.add_option('-o', dest='outfile',
                       help="specify outfile; otherwise it will come from environment_name")
+    parser.add_option('-p', '--package', dest='package',
+                      action='store_true', default=False,
+                      help="create python packages from sources; do not take entire subdirectory")
     parser.add_option('--virtualenv', dest='virtualenv',
                       help="use this virtualenv URL or file tarball")
     options, args = parser.parse_args(args)
@@ -155,9 +164,22 @@
             # remote tarball or resource
             buffer = urllib2.urlopen(source).read()
         else:
+            # local directory or tarball
             assert os.path.exists(source), "%s does not exist" % source
+
+            # package up the source if applicable
+            if options.package and os.path.exists(os.path.join(source, 'setup.py')):
+                call([sys.executable, 'setup.py', 'sdist'], cwd=source)
+                dist_dir = os.path.join(source, 'dist')
+                assert os.path.isdir(dist_dir), "dist directory not created in %s" % source
+                tarfiles = [i for i in os.listdir(dist_dir)
+                            if i.endswith('.tar.gz')]
+                assert tarfiles, "no .tar.gz files found in %s" % dist_dir
+                def last_modified(filename):
+                    return os.path.getmtime(os.path.join(dist_dir, filename))
+                tarfiles.sort(key=last_modified)
+                source = os.path.join(dist_dir, tarfiles[-1])
             
-            # local directory or tarball
             if (not os.path.isdir(source)) and tarfile.is_tarfile(source):
                 # check for a tarball
                 buffer = file(source).read()