Bug 1665091: [devtools] Relax timing restrictions to test to prevent intermittency...
[gecko.git] / client.py
blobad6c1ab7fa7f230ddcf2beb0b7c96b82fbb0027f
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/.
6 from __future__ import absolute_import, print_function
8 LIBFFI_DIRS = (('js/ctypes/libffi', 'libffi'),)
9 HG_EXCLUSIONS = ['.hg', '.hgignore', '.hgtags']
11 CVSROOT_LIBFFI = ':pserver:anoncvs@sources.redhat.com:/cvs/libffi'
13 import os
14 import sys
15 import datetime
16 import shutil
17 import glob
18 from optparse import OptionParser
19 from subprocess import check_call
21 topsrcdir = os.path.dirname(__file__)
22 if topsrcdir == '':
23 topsrcdir = '.'
26 def check_call_noisy(cmd, *args, **kwargs):
27 print("Executing command:", cmd)
28 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'])
45 def do_hg_replace(dir, repository, tag, exclusions, hg):
46 """
47 Replace the contents of dir with the contents of repository, except for
48 files matching exclusions.
49 """
50 fulldir = os.path.join(topsrcdir, dir)
51 if os.path.exists(fulldir):
52 shutil.rmtree(fulldir)
54 assert not os.path.exists(fulldir)
55 check_call_noisy([hg, 'clone', '-u', tag, repository, fulldir])
57 for thing in exclusions:
58 for excluded in glob.iglob(os.path.join(fulldir, thing)):
59 if os.path.isdir(excluded):
60 shutil.rmtree(excluded)
61 else:
62 os.remove(excluded)
65 def do_cvs_export(modules, tag, cvsroot, cvs):
66 """Check out a CVS directory without CVS metadata, using "export"
67 modules is a list of directories to check out and the corresponding
68 cvs module, e.g. (('js/ctypes/libffi', 'libffi'),)
69 """
70 for module_tuple in modules:
71 module = module_tuple[0]
72 cvs_module = module_tuple[1]
73 fullpath = os.path.join(topsrcdir, module)
74 if os.path.exists(fullpath):
75 print("Removing '%s'" % fullpath)
76 shutil.rmtree(fullpath)
78 (parent, leaf) = os.path.split(module)
79 print("CVS export begin: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC"))
80 check_call_noisy([cvs, '-d', cvsroot,
81 'export', '-r', tag, '-d', leaf, cvs_module],
82 cwd=os.path.join(topsrcdir, parent))
83 print("CVS export end: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC"))
86 def toggle_trailing_blank_line(depname):
87 """If the trailing line is empty, then we'll delete it.
88 Otherwise we'll add a blank line."""
89 lines = open(depname, "r").readlines()
90 if not lines:
91 print("unexpected short file", file=sys.stderr)
92 return
94 if not lines[-1].strip():
95 # trailing line is blank, removing it
96 open(depname, "wb").writelines(lines[:-1])
97 else:
98 # adding blank line
99 open(depname, "ab").write(b'\n')
102 def get_trailing_blank_line_state(depname):
103 lines = open(depname, "r").readlines()
104 if not lines:
105 print("unexpected short file", file=sys.stderr)
106 return "no blank line"
108 if not lines[-1].strip():
109 return "has blank line"
110 return "no blank line"
113 def update_nspr_or_nss(tag, depfile, destination, hgpath):
114 destination = destination.rstrip('/')
115 permanent_patch_dir = destination + '/patches'
116 temporary_patch_dir = destination + '.patches'
117 if os.path.exists(temporary_patch_dir):
118 print("please clean up leftover directory " + temporary_patch_dir)
119 sys.exit(2)
120 warn_if_patch_exists(permanent_patch_dir)
121 # protect patch directory from being removed by do_hg_replace
122 if os.path.exists(permanent_patch_dir):
123 shutil.move(permanent_patch_dir, temporary_patch_dir)
124 # now update the destination
125 print("reverting to HG version of %s to get its blank line state" % depfile)
126 check_call_noisy([options.hg, 'revert', depfile])
127 old_state = get_trailing_blank_line_state(depfile)
128 print("old state of %s is: %s" % (depfile, old_state))
129 do_hg_replace(destination, hgpath, tag, HG_EXCLUSIONS, options.hg)
130 new_state = get_trailing_blank_line_state(depfile)
131 print("new state of %s is: %s" % (depfile, new_state))
132 if old_state == new_state:
133 print("toggling blank line in: ", depfile)
134 toggle_trailing_blank_line(depfile)
135 tag_file = destination + "/TAG-INFO"
136 with open(tag_file, 'w') as f:
137 f.write(tag)
138 # move patch directory back to a subdirectory
139 if os.path.exists(temporary_patch_dir):
140 shutil.move(temporary_patch_dir, permanent_patch_dir)
143 def warn_if_patch_exists(path):
144 # If the given patch directory exists and contains at least one file,
145 # then print warning and wait for the user to acknowledge.
146 if os.path.isdir(path) and os.listdir(path):
147 print("========================================")
148 print("WARNING: At least one patch file exists")
149 print("in directory: " + path)
150 print("You must manually re-apply all patches")
151 print("after this script has completed!")
152 print("========================================")
153 input("Press Enter to continue...")
154 return
157 o = OptionParser(
158 usage="client.py [options] update_nspr tagname | update_nss tagname | update_libffi tagname")
159 o.add_option("--skip-mozilla", dest="skip_mozilla",
160 action="store_true", default=False,
161 help="Obsolete")
163 o.add_option("--cvs", dest="cvs", default=os.environ.get('CVS', 'cvs'),
164 help="The location of the cvs binary")
165 o.add_option("--cvsroot", dest="cvsroot",
166 help="The CVSROOT for libffi (default : %s)" % CVSROOT_LIBFFI)
167 o.add_option("--hg", dest="hg", default=os.environ.get('HG', 'hg'),
168 help="The location of the hg binary")
169 o.add_option("--repo", dest="repo",
170 help="the repo to update from (default: upstream repo)")
172 try:
173 options, args = o.parse_args()
174 action = args[0]
175 except IndexError:
176 o.print_help()
177 sys.exit(2)
179 if action in ('checkout', 'co'):
180 print("Warning: client.py checkout is obsolete.", file=sys.stderr)
181 pass
182 elif action in ('update_nspr'):
183 tag, = args[1:]
184 depfile = "nsprpub/config/prdepend.h"
185 if not options.repo:
186 options.repo = 'https://hg.mozilla.org/projects/nspr'
187 update_nspr_or_nss(tag, depfile, 'nsprpub', options.repo)
188 elif action in ('update_nss'):
189 tag, = args[1:]
190 depfile = "security/nss/coreconf/coreconf.dep"
191 if not options.repo:
192 options.repo = 'https://hg.mozilla.org/projects/nss'
193 update_nspr_or_nss(tag, depfile, 'security/nss', options.repo)
194 elif action in ('update_libffi'):
195 tag, = args[1:]
196 if not options.cvsroot:
197 options.cvsroot = CVSROOT_LIBFFI
198 do_cvs_export(LIBFFI_DIRS, tag, options.cvsroot, options.cvs)
199 else:
200 o.print_help()
201 sys.exit(2)