Bug 1826982 - Provide a better error message when servers fail to start, r=Sasha
[gecko.git] / client.py
blobea16f95627c94e3a78db88b6ba28d9865b468d71
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 HG_EXCLUSIONS = [".hg", ".hgignore", ".hgtags"]
8 import glob
9 import os
10 import shutil
11 import sys
12 from optparse import OptionParser
13 from subprocess import check_call
15 topsrcdir = os.path.dirname(__file__)
16 if topsrcdir == "":
17 topsrcdir = "."
20 def check_call_noisy(cmd, *args, **kwargs):
21 print("Executing command:", cmd)
22 check_call(cmd, *args, **kwargs)
25 def do_hg_pull(dir, repository, hg):
26 fulldir = os.path.join(topsrcdir, dir)
27 # clone if the dir doesn't exist, pull if it does
28 if not os.path.exists(fulldir):
29 check_call_noisy([hg, "clone", repository, fulldir])
30 else:
31 cmd = [hg, "pull", "-u", "-R", fulldir]
32 if repository is not None:
33 cmd.append(repository)
34 check_call_noisy(cmd)
35 check_call(
36 [hg, "parent", "-R", fulldir, "--template=Updated to revision {node}.\n"]
40 def do_hg_replace(dir, repository, tag, exclusions, hg):
41 """
42 Replace the contents of dir with the contents of repository, except for
43 files matching exclusions.
44 """
45 fulldir = os.path.join(topsrcdir, dir)
46 if os.path.exists(fulldir):
47 shutil.rmtree(fulldir)
49 assert not os.path.exists(fulldir)
50 check_call_noisy([hg, "clone", "-u", tag, repository, fulldir])
52 for thing in exclusions:
53 for excluded in glob.iglob(os.path.join(fulldir, thing)):
54 if os.path.isdir(excluded):
55 shutil.rmtree(excluded)
56 else:
57 os.remove(excluded)
60 def toggle_trailing_blank_line(depname):
61 """If the trailing line is empty, then we'll delete it.
62 Otherwise we'll add a blank line."""
63 lines = open(depname, "rb").readlines()
64 if not lines:
65 print("unexpected short file", file=sys.stderr)
66 return
68 if not lines[-1].strip():
69 # trailing line is blank, removing it
70 open(depname, "wb").writelines(lines[:-1])
71 else:
72 # adding blank line
73 open(depname, "ab").write(b"\n")
76 def get_trailing_blank_line_state(depname):
77 lines = open(depname, "r").readlines()
78 if not lines:
79 print("unexpected short file", file=sys.stderr)
80 return "no blank line"
82 if not lines[-1].strip():
83 return "has blank line"
84 return "no blank line"
87 def update_nspr_or_nss(tag, depfile, destination, hgpath):
88 destination = destination.rstrip("/")
89 permanent_patch_dir = destination + "/patches"
90 temporary_patch_dir = destination + ".patches"
91 if os.path.exists(temporary_patch_dir):
92 print("please clean up leftover directory " + temporary_patch_dir)
93 sys.exit(2)
94 warn_if_patch_exists(permanent_patch_dir)
95 # protect patch directory from being removed by do_hg_replace
96 if os.path.exists(permanent_patch_dir):
97 shutil.move(permanent_patch_dir, temporary_patch_dir)
98 # now update the destination
99 print("reverting to HG version of %s to get its blank line state" % depfile)
100 check_call_noisy([options.hg, "revert", depfile])
101 old_state = get_trailing_blank_line_state(depfile)
102 print("old state of %s is: %s" % (depfile, old_state))
103 do_hg_replace(destination, hgpath, tag, HG_EXCLUSIONS, options.hg)
104 new_state = get_trailing_blank_line_state(depfile)
105 print("new state of %s is: %s" % (depfile, new_state))
106 if old_state == new_state:
107 print("toggling blank line in: ", depfile)
108 toggle_trailing_blank_line(depfile)
109 tag_file = destination + "/TAG-INFO"
110 with open(tag_file, "w") as f:
111 f.write(tag)
112 # move patch directory back to a subdirectory
113 if os.path.exists(temporary_patch_dir):
114 shutil.move(temporary_patch_dir, permanent_patch_dir)
117 def warn_if_patch_exists(path):
118 # If the given patch directory exists and contains at least one file,
119 # then print warning and wait for the user to acknowledge.
120 if os.path.isdir(path) and os.listdir(path):
121 print("========================================")
122 print("WARNING: At least one patch file exists")
123 print("in directory: " + path)
124 print("You must manually re-apply all patches")
125 print("after this script has completed!")
126 print("========================================")
127 input("Press Enter to continue...")
128 return
131 o = OptionParser(usage="client.py [options] update_nspr tagname | update_nss tagname")
132 o.add_option(
133 "--skip-mozilla",
134 dest="skip_mozilla",
135 action="store_true",
136 default=False,
137 help="Obsolete",
140 o.add_option(
141 "--hg",
142 dest="hg",
143 default=os.environ.get("HG", "hg"),
144 help="The location of the hg binary",
146 o.add_option(
147 "--repo", dest="repo", help="the repo to update from (default: upstream repo)"
150 try:
151 options, args = o.parse_args()
152 action = args[0]
153 except IndexError:
154 o.print_help()
155 sys.exit(2)
157 if action in ("checkout", "co"):
158 print("Warning: client.py checkout is obsolete.", file=sys.stderr)
159 pass
160 elif action in ("update_nspr"):
161 (tag,) = args[1:]
162 depfile = "nsprpub/config/prdepend.h"
163 if not options.repo:
164 options.repo = "https://hg.mozilla.org/projects/nspr"
165 update_nspr_or_nss(tag, depfile, "nsprpub", options.repo)
166 elif action in ("update_nss"):
167 (tag,) = args[1:]
168 depfile = "security/nss/coreconf/coreconf.dep"
169 if not options.repo:
170 options.repo = "https://hg.mozilla.org/projects/nss"
171 update_nspr_or_nss(tag, depfile, "security/nss", options.repo)
172 else:
173 o.print_help()
174 sys.exit(2)