view stampit/main.py @ 6:6f8f390ab0b4

add option to run a command
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 20 Apr 2010 15:20:06 -0700
parents 3f9fac577d75
children d2daa42c1a56
line wrap: on
line source

#!/usr/bin/env python
"""
tar up a set of packages in a virtualenv per platform:

<package>-<version>-<platform>.tar.gz

Example:

mozmill-1.4.1-linux.tar.gz
"""

import os
import subprocess
import sys
import tempfile

from optparse import OptionParser, IndentedHelpFormatter, HelpFormatter
from utils import which

def call(*args, **kwargs):
    code = subprocess.call(*args, **kwargs)
    if code:
        sys.exit(code)

class UnformattedDescription(IndentedHelpFormatter):
    def format_description(self, description):
        return description.strip() or ''

def main(args=sys.argv[1:]):

    # XXX this is actually a very long way to work around just getting the
    # tarballs as pip magically does this for you!  ::sigh::
    # not sure of a better/easier way, though

    if 'PYTHONHOME' in os.environ:
        del os.environ['PYTHONHOME'] # just make sure this is killed

    # parse options
    usage = '%prog [options] <package> <platform>'
    parser = OptionParser(usage, description=globals()['__doc__'],
                          formatter=UnformattedDescription())
    parser.add_option('-c', '--command',
                      help="command to use on each tarball;  the tarball will be the last argument to the command, or '{}' may be used, find-style, to substittue for the argument")
    parser.add_option('-d', '--directory',
                      help='directory to use as the virtualenv')
    parser.add_option('-o', '--output',
                      help='where to put the tarballs')
    parser.add_option('--verbose', default=False, action='store_true',
                      help='more output')
    parser.add_option('--version',
                      help='version of the package to be installed (defaults to the current on the cheeseshop)')
    parser.add_option('--virtualenv',
                      help='path to virtualenv to use')
    options, args = parser.parse_args(args)
    callargs = options.verbose and {} or { 'stdout': subprocess.PIPE }
    if not args:
        parser.print_help()
        sys.exit(0)

    # locate virtualenv
    if options.virtualenv:
        if not os.path.exists(options.virtualenv):
            parser.error("'%s', specified by --virtualenv, does not exist")
    else:
        options.virtualenv = which('virtualenv')
    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")

    # create a virtualenv
    if not options.directory:
        options.directory = tempfile.mkdtemp(dir=os.getcwd())
    call([options.virtualenv, '--no-site-packages', options.directory], **callargs)

    # install the packages
    pip = os.path.join(options.directory, 'bin', 'pip')
    command = [ pip, 'install', '--no-install' ]
    command.extend(args)
    call(command, **callargs)

    # make the tarballs
    if not options.output:
        options.output = os.path.join(options.directory, 'dist')
    builddir = os.path.join(options.directory, 'build') # virtualenv creates
    if not os.path.exists(options.output):
        os.mkdir(options.output)
    python = os.path.join(options.directory, 'bin', 'python')
    for package in os.listdir(builddir):
        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)

    # do something with them (optionally)
    if not 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)

if __name__ == '__main__':
    main()