Mercurial > hg > stampit
annotate stampit/main.py @ 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 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 tar up a set of packages in a virtualenv per platform: | |
4 | |
5 <package>-<version>-<platform>.tar.gz | |
6 | |
7 Example: | |
8 | |
9 mozmill-1.4.1-linux.tar.gz | |
10 """ | |
11 | |
3 | 12 import os |
7 | 13 import shutil |
3 | 14 import subprocess |
0 | 15 import sys |
3 | 16 import tempfile |
0 | 17 |
2
164af2a4d291
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
18 from optparse import OptionParser, IndentedHelpFormatter, HelpFormatter |
3 | 19 from utils import which |
20 | |
21 def call(*args, **kwargs): | |
22 code = subprocess.call(*args, **kwargs) | |
23 if code: | |
24 sys.exit(code) | |
0 | 25 |
2
164af2a4d291
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
26 class UnformattedDescription(IndentedHelpFormatter): |
164af2a4d291
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
27 def format_description(self, description): |
164af2a4d291
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
28 return description.strip() or '' |
0 | 29 |
30 def main(args=sys.argv[1:]): | |
4
95b9f262d795
check for PYTHONHOME before deleting it
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
31 |
5
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
32 # XXX this is actually a very long way to work around just getting the |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
33 # tarballs as pip magically does this for you! ::sigh:: |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
34 # not sure of a better/easier way, though |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
35 |
4
95b9f262d795
check for PYTHONHOME before deleting it
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
36 if 'PYTHONHOME' in os.environ: |
95b9f262d795
check for PYTHONHOME before deleting it
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
37 del os.environ['PYTHONHOME'] # just make sure this is killed |
3 | 38 |
39 # parse options | |
0 | 40 usage = '%prog [options] <package> <platform>' |
2
164af2a4d291
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
41 parser = OptionParser(usage, description=globals()['__doc__'], |
164af2a4d291
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
42 formatter=UnformattedDescription()) |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
43 parser.add_option('-c', '--command', |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
44 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") |
3 | 45 parser.add_option('-d', '--directory', |
46 help='directory to use as the virtualenv') | |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
47 parser.add_option('-o', '--output', |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
48 help='where to put the tarballs') |
7 | 49 parser.add_option('--clean', default=False, action='store_true', |
50 help='remove all directories [BE CAREFUL!]') | |
51 parser.add_option('-q', '--quiet', default=False, action='store_true', | |
52 help='make less noise') | |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
53 parser.add_option('--verbose', default=False, action='store_true', |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
54 help='more output') |
0 | 55 parser.add_option('--version', |
56 help='version of the package to be installed (defaults to the current on the cheeseshop)') | |
3 | 57 parser.add_option('--virtualenv', |
58 help='path to virtualenv to use') | |
0 | 59 options, args = parser.parse_args(args) |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
60 callargs = options.verbose and {} or { 'stdout': subprocess.PIPE } |
7 | 61 if options.quiet: |
62 callargs['stderr'] = subprocess.PIPE | |
1
820beace15f2
get description from module docstring
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
63 if not args: |
820beace15f2
get description from module docstring
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
64 parser.print_help() |
820beace15f2
get description from module docstring
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
65 sys.exit(0) |
3 | 66 |
67 # locate virtualenv | |
5
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
68 if options.virtualenv: |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
69 if not os.path.exists(options.virtualenv): |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
70 parser.error("'%s', specified by --virtualenv, does not exist") |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
71 else: |
3 | 72 options.virtualenv = which('virtualenv') |
7 | 73 virtualenv_dir = None |
3 | 74 if options.virtualenv is None: |
7 | 75 hg = which('hg') |
76 if hg: | |
77 # download virtualenv for them | |
78 virtualenv_src = 'http://bitbucket.org/ianb/virtualenv' | |
79 virtualenv_dir = tempfile.mkdtemp() | |
80 call([hg, 'clone', virtualenv_src, virtualenv_dir], **callargs) | |
81 options.virtualenv = os.path.join(virtualenv_dir, 'virtualenv.py') | |
82 else: | |
83 parser.error("virtualenv cannot be found; please install virtualenv or specify its location with --virtualenv") | |
1
820beace15f2
get description from module docstring
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
84 |
0 | 85 # create a virtualenv |
7 | 86 directory_exists = bool(options.directory and os.path.exists(options.directory)) |
3 | 87 if not options.directory: |
88 options.directory = tempfile.mkdtemp(dir=os.getcwd()) | |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
89 call([options.virtualenv, '--no-site-packages', options.directory], **callargs) |
3 | 90 |
91 # install the packages | |
5
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
92 pip = os.path.join(options.directory, 'bin', 'pip') |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
93 command = [ pip, 'install', '--no-install' ] |
3 | 94 command.extend(args) |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
95 call(command, **callargs) |
3 | 96 |
5
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
97 # make the tarballs |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
98 if not options.output: |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
99 options.output = os.path.join(options.directory, 'dist') |
5
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
100 builddir = os.path.join(options.directory, 'build') # virtualenv creates |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
101 if not os.path.exists(options.output): |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
102 os.mkdir(options.output) |
5
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
103 python = os.path.join(options.directory, 'bin', 'python') |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
104 for package in os.listdir(builddir): |
3f9fac577d75
this now actually makes some tarballs for you
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
105 os.chdir(os.path.join(builddir, package)) |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
106 call([python, 'setup.py', 'sdist', '--dist-dir', options.output], |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
107 **callargs) |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
108 tarballs = os.listdir(options.output) # XXX should be just new ones maybe |
7 | 109 quiet = options.quiet or options.command |
110 if options.quiet: | |
111 if not options.command: | |
112 print '\n'.join([os.path.join(options.output, tarball) | |
113 for tarball in tarballs]) | |
114 else: | |
115 print 'Tarballs are in %s:' % options.output | |
116 print '\n'.join(tarballs) | |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
117 |
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
118 # do something with them (optionally) |
7 | 119 if options.command: |
6
6f8f390ab0b4
add option to run a command
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
120 sys.exit(0) # you're done! |
7 | 121 options.command = options.command.strip() |
122 os.chdir(options.output) | |
123 for tarball in tarballs: | |
124 if '{}' in options.command: | |
125 command = options.command.replace('{}', tarball) | |
126 else: | |
127 command = '%s %s' % (options.command, tarball) | |
128 subprocess.call(command, shell=True) | |
129 | |
130 # clean up (optionally) | |
131 if options.clean: | |
132 if virtualenv_dir: | |
133 shutil.rmtree(virtualenv_dir) | |
134 if not directory_exists: | |
135 shutil.rmtree(options.directory) | |
0 | 136 |
137 if __name__ == '__main__': | |
138 main() |