changeset 7:d2daa42c1a56

add some more options
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 20 Apr 2010 15:56:29 -0700
parents 6f8f390ab0b4
children 8c3361ab1db2
files stampit/main.py
diffstat 1 files changed, 42 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/stampit/main.py	Tue Apr 20 15:20:06 2010 -0700
+++ b/stampit/main.py	Tue Apr 20 15:56:29 2010 -0700
@@ -10,6 +10,7 @@
 """
 
 import os
+import shutil
 import subprocess
 import sys
 import tempfile
@@ -45,6 +46,10 @@
                       help='directory to use as the virtualenv')
     parser.add_option('-o', '--output',
                       help='where to put the tarballs')
+    parser.add_option('--clean', default=False, action='store_true',
+                      help='remove all directories [BE CAREFUL!]')
+    parser.add_option('-q', '--quiet', default=False, action='store_true',
+                      help='make less noise')
     parser.add_option('--verbose', default=False, action='store_true',
                       help='more output')
     parser.add_option('--version',
@@ -53,6 +58,8 @@
                       help='path to virtualenv to use')
     options, args = parser.parse_args(args)
     callargs = options.verbose and {} or { 'stdout': subprocess.PIPE }
+    if options.quiet:
+        callargs['stderr'] = subprocess.PIPE
     if not args:
         parser.print_help()
         sys.exit(0)
@@ -63,11 +70,20 @@
             parser.error("'%s', specified by --virtualenv, does not exist")
     else:
         options.virtualenv = which('virtualenv')
+    virtualenv_dir = None
     if options.virtualenv is None:
-        # TODO: download virtualenv for them
-        parser.error("virtualenv cannot be found; please install virtualenv or specify its location with --virtualenv")
+        hg = which('hg')
+        if hg:
+            # download virtualenv for them
+            virtualenv_src = 'http://bitbucket.org/ianb/virtualenv'
+            virtualenv_dir = tempfile.mkdtemp()
+            call([hg,  'clone', virtualenv_src, virtualenv_dir], **callargs)
+            options.virtualenv = os.path.join(virtualenv_dir, 'virtualenv.py')
+        else:
+            parser.error("virtualenv cannot be found; please install virtualenv or specify its location with --virtualenv")
 
     # create a virtualenv
+    directory_exists = bool(options.directory and os.path.exists(options.directory))
     if not options.directory:
         options.directory = tempfile.mkdtemp(dir=os.getcwd())
     call([options.virtualenv, '--no-site-packages', options.directory], **callargs)
@@ -89,21 +105,34 @@
         os.chdir(os.path.join(builddir, package))
         call([python, 'setup.py', 'sdist', '--dist-dir', options.output],
              **callargs)
-    print 'Tarballs are in %s:' % options.output
     tarballs = os.listdir(options.output) # XXX should be just new ones maybe
-    print '\n'.join(tarballs)
+    quiet = options.quiet or options.command
+    if options.quiet:
+        if not options.command:
+            print '\n'.join([os.path.join(options.output, tarball)
+                             for tarball in tarballs])
+    else:
+        print 'Tarballs are in %s:' % options.output
+        print '\n'.join(tarballs)
 
     # do something with them (optionally)
-    if not options.command:
+    if options.command:
         sys.exit(0) # you're done!
-    options.command = options.command.strip()
-    os.chdir(options.output)
-    for tarball in tarballs:
-        if '{}' in options.command:
-            command = options.command.replace('{}', tarball)
-        else:
-            command = '%s %s' % (options.command, tarball)
-        subprocess.call(command, shell=True)
+        options.command = options.command.strip()
+        os.chdir(options.output)
+        for tarball in tarballs:
+            if '{}' in options.command:
+                command = options.command.replace('{}', tarball)
+            else:
+                command = '%s %s' % (options.command, tarball)
+            subprocess.call(command, shell=True)
+
+    # clean up (optionally)
+    if options.clean:
+        if virtualenv_dir:
+            shutil.rmtree(virtualenv_dir)
+        if not directory_exists:
+            shutil.rmtree(options.directory)
 
 if __name__ == '__main__':
     main()