Bug 1642579 [wpt PR 23909] - [COOP access reporting] Preliminary WPT tests., a=testonly
[gecko.git] / client.py
blob49fd6e20d363aba96ecf019c062d8427992cb2b4
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("\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 else:
111 return "no blank line"
114 def update_nspr_or_nss(tag, depfile, destination, hgpath):
115 destination = destination.rstrip('/')
116 permanent_patch_dir = destination + '/patches'
117 temporary_patch_dir = destination + '.patches'
118 if os.path.exists(temporary_patch_dir):
119 print("please clean up leftover directory " + temporary_patch_dir)
120 sys.exit(2)
121 warn_if_patch_exists(permanent_patch_dir)
122 # protect patch directory from being removed by do_hg_replace
123 if os.path.exists(permanent_patch_dir):
124 shutil.move(permanent_patch_dir, temporary_patch_dir)
125 # now update the destination
126 print("reverting to HG version of %s to get its blank line state" % depfile)
127 check_call_noisy([options.hg, 'revert', depfile])
128 old_state = get_trailing_blank_line_state(depfile)
129 print("old state of %s is: %s" % (depfile, old_state))
130 do_hg_replace(destination, hgpath, tag, HG_EXCLUSIONS, options.hg)
131 new_state = get_trailing_blank_line_state(depfile)
132 print("new state of %s is: %s" % (depfile, new_state))
133 if old_state == new_state:
134 print("toggling blank line in: ", depfile)
135 toggle_trailing_blank_line(depfile)
136 tag_file = destination + "/TAG-INFO"
137 with open(tag_file, 'w') as f:
138 f.write(tag)
139 # move patch directory back to a subdirectory
140 if os.path.exists(temporary_patch_dir):
141 shutil.move(temporary_patch_dir, permanent_patch_dir)
144 def warn_if_patch_exists(path):
145 # If the given patch directory exists and contains at least one file,
146 # then print warning and wait for the user to acknowledge.
147 if os.path.isdir(path) and os.listdir(path):
148 print("========================================")
149 print("WARNING: At least one patch file exists")
150 print("in directory: " + path)
151 print("You must manually re-apply all patches")
152 print("after this script has completed!")
153 print("========================================")
154 input("Press Enter to continue...")
155 return
158 o = OptionParser(
159 usage="client.py [options] update_nspr tagname | update_nss tagname | update_libffi tagname")
160 o.add_option("--skip-mozilla", dest="skip_mozilla",
161 action="store_true", default=False,
162 help="Obsolete")
164 o.add_option("--cvs", dest="cvs", default=os.environ.get('CVS', 'cvs'),
165 help="The location of the cvs binary")
166 o.add_option("--cvsroot", dest="cvsroot",
167 help="The CVSROOT for libffi (default : %s)" % CVSROOT_LIBFFI)
168 o.add_option("--hg", dest="hg", default=os.environ.get('HG', 'hg'),
169 help="The location of the hg binary")
170 o.add_option("--repo", dest="repo",
171 help="the repo to update from (default: upstream repo)")
173 try:
174 options, args = o.parse_args()
175 action = args[0]
176 except IndexError:
177 o.print_help()
178 sys.exit(2)
180 if action in ('checkout', 'co'):
181 print("Warning: client.py checkout is obsolete.", file=sys.stderr)
182 pass
183 elif action in ('update_nspr'):
184 tag, = args[1:]
185 depfile = "nsprpub/config/prdepend.h"
186 if not options.repo:
187 options.repo = 'https://hg.mozilla.org/projects/nspr'
188 update_nspr_or_nss(tag, depfile, 'nsprpub', options.repo)
189 elif action in ('update_nss'):
190 tag, = args[1:]
191 depfile = "security/nss/coreconf/coreconf.dep"
192 if not options.repo:
193 options.repo = 'https://hg.mozilla.org/projects/nss'
194 update_nspr_or_nss(tag, depfile, 'security/nss', options.repo)
195 elif action in ('update_libffi'):
196 tag, = args[1:]
197 if not options.cvsroot:
198 options.cvsroot = CVSROOT_LIBFFI
199 do_cvs_export(LIBFFI_DIRS, tag, options.cvsroot, options.cvs)
200 else:
201 o.print_help()
202 sys.exit(2)