comparison mozillatry.py @ 0:df6a8049e9a4

initial port from http://k0s.org/mozilla/try.py
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 30 Nov 2012 15:51:06 -0800
parents
children 0f8e4a3b4e1c
comparison
equal deleted inserted replaced
-1:000000000000 0:df6a8049e9a4
1 #!/usr/bin/env python
2
3 """
4 push patches to try
5 """
6
7 import optparse
8 import os
9 import sys
10
11 from subprocess import check_call as call
12
13 def config(filename):
14 """read .mozutils.ini config file"""
15 # XXX stub; this should really use
16 # e.g. http://k0s.org/mozilla/hg/configuration/
17 from ConfigParser import ConfigParser
18 parser = ConfigParser()
19 parser.read(filename)
20 return os.path.expanduser(parser.get('hg', 'mozilla-central'))
21
22 def reset(directory):
23 """reset an hg directory to a good state"""
24 assert os.path.exists(directory) and os.path.isdir(directory)
25 hg_dir = os.path.join(directory, '.hg')
26 assert os.path.exists(hg_dir) and os.path.isdir(hg_dir)
27 call(['hg', 'revert', '--no-backup', '--all'], cwd=directory)
28 call(['hg', 'qpop', '--all'], cwd=directory)
29 try:
30 shutil.rmtree(os.path.join(hg_dir, 'patches')) # remove patches
31 except:
32 pass
33
34 def update(directory):
35 """update a mozilla-central checkout"""
36 assert os.path.exists(directory) and os.path.isdir(directory)
37 reset(directory)
38 call(['hg', 'pull'], cwd=directory)
39 call(['hg', 'update'], cwd=directory)
40 call(['hg', 'qinit'], cwd=directory)
41
42 def push_to_try(patches, repo, commit, _try='ssh://hg.mozilla.org/try/'):
43 """push a series of patches to try repository"""
44
45 # ensure the repo is in a good state
46 update(repo)
47
48 try:
49 # apply patches
50 for patch in patches:
51 call(['hg', 'qimport', patch], cwd=repo)
52 call(['hg', 'qpush', '--all'], cwd=repo)
53 call(['hg', 'qseries', '-v'], cwd=repo)
54
55 # push to try
56 call(['hg', 'qref', '--message', commit], cwd=repo)
57 call(['hg', 'push', '-f', _try], cwd=repo)
58 finally:
59 reset(repo)
60
61 def try_syntax(opt=True, debug=True, unittests=('all'), talos=('all'), bug=None):
62 """
63 return try syntax; see also:
64 - https://github.com/pbiggar/trychooser
65 - http://trychooser.pub.build.mozilla.org/
66 """
67
68 assert opt or debug
69 message = ['try:']
70 message += ['-b', '%s%s' % (('d' if debug else ''), ('o' if opt else ''))]
71 message += ['-u', (','.join(unittests) if unittests else 'none')]
72 message += ['-t', (','.join(talos) if talos else 'none')]
73 if bug:
74 message += ['--post-to-bugzilla', str(bug)]
75 return ' '.join(message)
76
77 def main(args=sys.argv[1:]):
78
79 # parse command line arguments
80 usage = '%prog [options] patch <patch2> <...>'
81 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
82 """description formatter"""
83 def format_description(self, description):
84 description = description.strip()
85 if description:
86 return description + '\n'
87 else:
88 return ''
89 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
90 parser.add_option('--no-opt', dest='opt',
91 action='store_false', default=True,
92 help='no opt builds')
93 parser.add_option('--no-debug', dest='debug',
94 action='store_false', default=True,
95 help='no debug builds')
96 parser.add_option('-u', dest='unittests', action='append',
97 help='unittests')
98 parser.add_option('-t', dest='talos', action='append',
99 help='talos tests')
100 parser.add_option('--bug', dest='bug', type='int',
101 help='post to bugzilla bug #')
102 parser.add_option('-c', '--config', dest='config',
103 default=os.path.join(os.environ['HOME'], '.mozutils.ini'),
104 help='location of config file')
105 options, args = parser.parse_args(args)
106 if not args:
107 parser.print_help()
108 parser.exit()
109 if (not options.opt) and (not options.debug):
110 parser.error("Must enable opt or debug builds")
111
112 # get mozilla-central repository directory
113 config_file = options.__dict__.pop('config')
114 if not os.path.exists(config_file):
115 parser.error("You need a config file at ~/.mozutils.ini")
116 try_directory = config(config_file) # XXX temporary hack
117 if not os.path.exists(try_directory):
118 parser.error("mozilla-central try directory does not exist: %s" % try_directory)
119
120 # build try syntax
121 commit = try_syntax(**options.__dict__)
122 print commit
123
124 # push to try
125 push_to_try(patches=args, repo=try_directory, commit=commit)
126
127 if __name__ == '__main__':
128 main()
129