Bumping gaia.json for 2 gaia-central revision(s) a=gaia-bump
[gecko.git] / client.py
blobaf760609cccdf8e3d68d5365335c223f2840898f
1 #!/usr/bin/python
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 LIBFFI_DIRS = (('js/ctypes/libffi', 'libffi'),)
8 WEBIDLPARSER_DIR = 'dom/bindings/parser'
9 WEBIDLPARSER_REPO = 'https://hg.mozilla.org/users/khuey_mozilla.com/webidl-parser'
10 HG_EXCLUSIONS = ['.hg', '.hgignore', '.hgtags']
11 WEBIDLPARSER_EXCLUSIONS = HG_EXCLUSIONS + ['.gitignore', 'ply']
13 CVSROOT_LIBFFI = ':pserver:anoncvs@sources.redhat.com:/cvs/libffi'
15 import os
16 import sys
17 import datetime
18 import shutil
19 import glob
20 from optparse import OptionParser
21 from subprocess import check_call
23 topsrcdir = os.path.dirname(__file__)
24 if topsrcdir == '':
25 topsrcdir = '.'
27 def check_call_noisy(cmd, *args, **kwargs):
28 print "Executing command:", cmd
29 check_call(cmd, *args, **kwargs)
31 def do_hg_pull(dir, repository, hg):
32 fulldir = os.path.join(topsrcdir, dir)
33 # clone if the dir doesn't exist, pull if it does
34 if not os.path.exists(fulldir):
35 check_call_noisy([hg, 'clone', repository, fulldir])
36 else:
37 cmd = [hg, 'pull', '-u', '-R', fulldir]
38 if repository is not None:
39 cmd.append(repository)
40 check_call_noisy(cmd)
41 check_call([hg, 'parent', '-R', fulldir,
42 '--template=Updated to revision {node}.\n'])
44 def do_hg_replace(dir, repository, tag, exclusions, hg):
45 """
46 Replace the contents of dir with the contents of repository, except for
47 files matching exclusions.
48 """
49 fulldir = os.path.join(topsrcdir, dir)
50 if os.path.exists(fulldir):
51 shutil.rmtree(fulldir)
53 assert not os.path.exists(fulldir)
54 check_call_noisy([hg, 'clone', '-u', tag, repository, fulldir])
56 for thing in exclusions:
57 for excluded in glob.iglob(os.path.join(fulldir, thing)):
58 if os.path.isdir(excluded):
59 shutil.rmtree(excluded)
60 else:
61 os.remove(excluded)
63 def do_cvs_export(modules, tag, cvsroot, cvs):
64 """Check out a CVS directory without CVS metadata, using "export"
65 modules is a list of directories to check out and the corresponding
66 cvs module, e.g. (('js/ctypes/libffi', 'libffi'),)
67 """
68 for module_tuple in modules:
69 module = module_tuple[0]
70 cvs_module = module_tuple[1]
71 fullpath = os.path.join(topsrcdir, module)
72 if os.path.exists(fullpath):
73 print "Removing '%s'" % fullpath
74 shutil.rmtree(fullpath)
76 (parent, leaf) = os.path.split(module)
77 print "CVS export begin: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
78 check_call_noisy([cvs, '-d', cvsroot,
79 'export', '-r', tag, '-d', leaf, cvs_module],
80 cwd=os.path.join(topsrcdir, parent))
81 print "CVS export end: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
83 def toggle_trailing_blank_line(depname):
84 """If the trailing line is empty, then we'll delete it.
85 Otherwise we'll add a blank line."""
86 lines = open(depname, "r").readlines()
87 if not lines:
88 print >>sys.stderr, "unexpected short file"
89 return
91 if not lines[-1].strip():
92 # trailing line is blank, removing it
93 open(depname, "wb").writelines(lines[:-1])
94 else:
95 # adding blank line
96 open(depname, "ab").write("\n")
98 def get_trailing_blank_line_state(depname):
99 lines = open(depname, "r").readlines()
100 if not lines:
101 print >>sys.stderr, "unexpected short file"
102 return "no blank line"
104 if not lines[-1].strip():
105 return "has blank line"
106 else:
107 return "no blank line"
109 def update_nspr_or_nss(tag, depfile, destination, hgpath):
110 print "reverting to HG version of %s to get its blank line state" % depfile
111 check_call_noisy([options.hg, 'revert', depfile])
112 old_state = get_trailing_blank_line_state(depfile)
113 print "old state of %s is: %s" % (depfile, old_state)
114 do_hg_replace(destination, hgpath, tag, HG_EXCLUSIONS, options.hg)
115 new_state = get_trailing_blank_line_state(depfile)
116 print "new state of %s is: %s" % (depfile, new_state)
117 if old_state == new_state:
118 print "toggling blank line in: ", depfile
119 toggle_trailing_blank_line(depfile)
120 tag_file = destination + "/TAG-INFO"
121 print >>file(tag_file, "w"), tag
123 o = OptionParser(usage="client.py [options] update_nspr tagname | update_nss tagname | update_libffi tagname | update_webidlparser tagname")
124 o.add_option("--skip-mozilla", dest="skip_mozilla",
125 action="store_true", default=False,
126 help="Obsolete")
128 o.add_option("--cvs", dest="cvs", default=os.environ.get('CVS', 'cvs'),
129 help="The location of the cvs binary")
130 o.add_option("--cvsroot", dest="cvsroot",
131 help="The CVSROOT for libffi (default : %s)" % CVSROOT_LIBFFI)
132 o.add_option("--hg", dest="hg", default=os.environ.get('HG', 'hg'),
133 help="The location of the hg binary")
135 try:
136 options, args = o.parse_args()
137 action = args[0]
138 except IndexError:
139 o.print_help()
140 sys.exit(2)
142 if action in ('checkout', 'co'):
143 print >>sys.stderr, "Warning: client.py checkout is obsolete."
144 pass
145 elif action in ('update_nspr'):
146 tag, = args[1:]
147 depfile = "nsprpub/config/prdepend.h"
148 update_nspr_or_nss(tag, depfile, 'nsprpub', 'https://hg.mozilla.org/projects/nspr')
149 elif action in ('update_nss'):
150 tag, = args[1:]
151 depfile = "security/nss/coreconf/coreconf.dep"
152 update_nspr_or_nss(tag, depfile, 'security/nss', 'https://hg.mozilla.org/projects/nss')
153 elif action in ('update_libffi'):
154 tag, = args[1:]
155 if not options.cvsroot:
156 options.cvsroot = CVSROOT_LIBFFI
157 do_cvs_export(LIBFFI_DIRS, tag, options.cvsroot, options.cvs)
158 elif action in ('update_webidlparser'):
159 tag, = args[1:]
160 do_hg_replace(WEBIDLPARSER_DIR, WEBIDLPARSER_REPO, tag, WEBIDLPARSER_EXCLUSIONS, options.hg)
161 else:
162 o.print_help()
163 sys.exit(2)