OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2013 the V8 project authors. All rights reserved. | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following | |
11 # disclaimer in the documentation and/or other materials provided | |
12 # with the distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived | |
15 # from this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 import os | |
30 import re | |
31 import subprocess | |
32 import sys | |
33 | |
34 PERSISTFILE_BASENAME = "PERSISTFILE_BASENAME" | |
35 TEMP_BRANCH = "TEMP_BRANCH" | |
36 BRANCHNAME = "BRANCHNAME" | |
37 DOT_GIT_LOCATION = "DOT_GIT_LOCATION" | |
38 VERSION_FILE = "VERSION_FILE" | |
39 CHANGELOG_FILE = "CHANGELOG_FILE" | |
40 CHANGELOG_ENTRY_FILE = "CHANGELOG_ENTRY_FILE" | |
41 COMMITMSG_FILE = "COMMITMSG_FILE" | |
42 PATCH_FILE = "PATCH_FILE" | |
43 | |
44 | |
45 def TextToFile(text, file_name): | |
46 with open(file_name, "w") as f: | |
47 f.write(text) | |
48 | |
49 | |
50 def AppendToFile(text, file_name): | |
51 with open(file_name, "a") as f: | |
52 f.write(text) | |
53 | |
54 | |
55 def LinesInFile(file_name): | |
56 with open(file_name) as f: | |
57 for line in f: | |
58 yield line | |
59 | |
60 | |
61 def FileToText(file_name): | |
62 with open(file_name) as f: | |
63 return f.read() | |
64 | |
65 | |
66 def MSub(rexp, replacement, text): | |
67 return re.sub(rexp, replacement, text, flags=re.MULTILINE) | |
68 | |
69 | |
70 # Some commands don't like the pipe, e.g. calling vi from within the script or | |
71 # from subscripts like git cl upload. | |
72 def Command(cmd, args="", prefix="", pipe=True): | |
73 print "Command: %s %s %s" % (prefix, cmd, args) | |
74 try: | |
75 if pipe: | |
76 return subprocess.check_output("%s %s %s" % (prefix, cmd, args), shell=Tru e) | |
Jakob Kummerow
2013/11/08 14:01:18
nit: 80col (I'd break after '%')
Michael Achenbach
2013/11/08 14:18:14
Done.
| |
77 else: | |
78 return subprocess.check_call("%s %s %s" % (prefix, cmd, args), shell=True) | |
79 except subprocess.CalledProcessError: | |
80 return None | |
81 | |
82 | |
83 # Wrapper for side effects. | |
84 class SideEffectHandler(object): | |
85 def Command(self, cmd, args="", prefix="", pipe=True): | |
86 return Command(cmd, args, prefix, pipe) | |
87 | |
88 def ReadLine(self): | |
89 return sys.stdin.readline().strip() | |
90 | |
91 DEFAULT_SIDE_EFFECT_HANDLER = SideEffectHandler() | |
92 | |
93 | |
94 class Step(object): | |
95 def __init__(self, text="", requires=None): | |
96 self._text = text | |
97 self._number = -1 | |
98 self._requires = requires | |
99 self._side_effect_handler = DEFAULT_SIDE_EFFECT_HANDLER | |
100 | |
101 def SetNumber(self, number): | |
102 self._number = number | |
103 | |
104 def SetConfig(self, config): | |
105 self._config = config | |
106 | |
107 def SetState(self, state): | |
108 self._state = state | |
109 | |
110 def SetOptions(self, options): | |
111 self._options = options | |
112 | |
113 def SetSideEffectHandler(self, handler): | |
114 self._side_effect_handler = handler | |
115 | |
116 def Config(self, key): | |
117 return self._config[key] | |
118 | |
119 def Run(self): | |
120 assert self._number >= 0 | |
121 assert self._config is not None | |
122 assert self._state is not None | |
123 assert self._side_effect_handler is not None | |
124 if self._requires: | |
125 self.RestoreIfUnset(self._requires) | |
126 if not self._state[self._requires]: | |
127 return | |
128 print ">>> Step %d: %s" % (self._number, self._text) | |
129 self.RunStep() | |
130 | |
131 def RunStep(self): | |
132 raise NotImplementedError | |
133 | |
134 def ReadLine(self): | |
135 return self._side_effect_handler.ReadLine() | |
136 | |
137 def Git(self, args="", prefix="", pipe=True): | |
138 return self._side_effect_handler.Command("git", args, prefix, pipe) | |
139 | |
140 def Editor(self, args): | |
141 return self._side_effect_handler.Command(os.environ["EDITOR"], args, | |
142 pipe=False) | |
143 | |
144 def Die(self, msg=""): | |
145 if msg != "": | |
146 print "Error: %s" % msg | |
147 print "Exiting" | |
148 raise Exception(msg) | |
149 | |
150 def Confirm(self, msg): | |
151 print "%s [Y/n] " % msg, | |
152 answer = self.ReadLine() | |
153 return answer == "" or answer == "Y" or answer == "y" | |
154 | |
155 def DeleteBranch(self, name): | |
156 git_result = self.Git("branch").strip() | |
157 for line in git_result.splitlines(): | |
158 if re.match(r".*\s+%s$" % name, line): | |
159 msg = "Branch %s exists, do you want to delete it?" % name | |
160 if self.Confirm(msg): | |
161 if self.Git("branch -D %s" % name) is None: | |
162 self.Die("Deleting branch '%s' failed." % name) | |
163 print "Branch %s deleted." % name | |
164 else: | |
165 msg = "Can't continue. Please delete branch %s and try again." % name | |
166 self.Die(msg) | |
167 | |
168 def Persist(self, var, value): | |
169 value = value or "__EMPTY__" | |
170 TextToFile(value, "%s-%s" % (self._config[PERSISTFILE_BASENAME], var)) | |
171 | |
172 def Restore(self, var): | |
173 value = FileToText("%s-%s" % (self._config[PERSISTFILE_BASENAME], var)) | |
174 value = value or self.Die("Variable '%s' could not be restored." % var) | |
175 return "" if value == "__EMPTY__" else value | |
176 | |
177 def RestoreIfUnset(self, var_name): | |
178 if self._state.get(var_name) is None: | |
179 self._state[var_name] = self.Restore(var_name) | |
180 | |
181 def InitialEnvironmentChecks(self): | |
182 # Cancel if this is not a git checkout. | |
183 if not os.path.exists(self._config[DOT_GIT_LOCATION]): | |
184 self.Die("This is not a git checkout, this script won't work for you.") | |
185 | |
186 # Cancel if EDITOR is unset or not executable. | |
187 if (not os.environ.get("EDITOR") or | |
188 Command("which", os.environ["EDITOR"]) is None): | |
189 self.Die("Please set your EDITOR environment variable, you'll need it.") | |
190 | |
191 def CommonPrepare(self): | |
192 # Check for a clean workdir. | |
193 if self.Git("status -s -uno").strip() != "": | |
194 self.Die("Workspace is not clean. Please commit or undo your changes.") | |
195 | |
196 # Persist current branch. | |
197 current_branch = "" | |
198 git_result = self.Git("status -s -b -uno").strip() | |
199 for line in git_result.splitlines(): | |
200 match = re.match(r"^## (.+)", line) | |
201 if match: | |
202 current_branch = match.group(1) | |
203 break | |
204 self.Persist("current_branch", current_branch) | |
205 | |
206 # Fetch unfetched revisions. | |
207 if self.Git("svn fetch") is None: | |
208 self.Die("'git svn fetch' failed.") | |
209 | |
210 # Get ahold of a safe temporary branch and check it out. | |
211 if current_branch != self._config[TEMP_BRANCH]: | |
212 self.DeleteBranch(self._config[TEMP_BRANCH]) | |
213 self.Git("checkout -b %s" % self._config[TEMP_BRANCH]) | |
214 | |
215 # Delete the branch that will be created later if it exists already. | |
216 self.DeleteBranch(self._config[BRANCHNAME]) | |
217 | |
218 def CommonCleanup(self): | |
219 self.RestoreIfUnset("current_branch") | |
220 self.Git("checkout -f %s" % self._state["current_branch"]) | |
221 if self._config[TEMP_BRANCH] != self._state["current_branch"]: | |
222 self.Git("branch -D %s" % self._config[TEMP_BRANCH]) | |
223 if self._config[BRANCHNAME] != self._state["current_branch"]: | |
224 self.Git("branch -D %s" % self._config[BRANCHNAME]) | |
225 | |
226 # Clean up all temporary files. | |
227 Command("rm", "-f %s*" % self._config[PERSISTFILE_BASENAME]) | |
228 | |
229 def ReadAndPersistVersion(self, prefix=""): | |
230 def ReadAndPersist(var_name, def_name): | |
231 match = re.match(r"^#define %s\s+(\d*)" % def_name, line) | |
232 if match: | |
233 value = match.group(1) | |
234 self.Persist("%s%s" % (prefix, var_name), value) | |
235 self._state["%s%s" % (prefix, var_name)] = value | |
236 for line in LinesInFile(self._config[VERSION_FILE]): | |
237 for (var_name, def_name) in [("major", "MAJOR_VERSION"), | |
238 ("minor", "MINOR_VERSION"), | |
239 ("build", "BUILD_NUMBER"), | |
240 ("patch", "PATCH_LEVEL")]: | |
241 ReadAndPersist(var_name, def_name) | |
242 | |
243 def RestoreVersionIfUnset(self, prefix=""): | |
244 for v in ["major", "minor", "build", "patch"]: | |
245 self.RestoreIfUnset("%s%s" % (prefix, v)) | |
246 | |
247 def WaitForLGTM(self): | |
248 print ("Please wait for an LGTM, then type \"LGTM<Return>\" to commit " | |
249 "your change. (If you need to iterate on the patch or double check " | |
250 "that it's sane, do so in another shell, but remember to not " | |
251 "change the headline of the uploaded CL.") | |
252 answer = "" | |
253 while answer != "LGTM": | |
254 print "> ", | |
255 answer = self.ReadLine() | |
256 if answer != "LGTM": | |
257 print "That was not 'LGTM'." | |
258 | |
259 def WaitForResolvingConflicts(self, patch_file): | |
260 print("Applying the patch \"%s\" failed. Either type \"ABORT<Return>\", " | |
261 "or resolve the conflicts, stage *all* touched files with " | |
262 "'git add', and type \"RESOLVED<Return>\"") | |
263 answer = "" | |
264 while answer != "RESOLVED": | |
265 if answer == "ABORT": | |
266 self.Die("Applying the patch failed.") | |
267 if answer != "": | |
268 print "That was not 'RESOLVED' or 'ABORT'." | |
269 print "> ", | |
270 answer = self.ReadLine() | |
271 | |
272 # Takes a file containing the patch to apply as first argument. | |
273 def ApplyPatch(self, patch_file, reverse_patch=""): | |
274 args = "apply --index --reject %s \"%s\"" % (reverse_patch, patch_file) | |
275 if self.Git(args) is None: | |
276 self.WaitForResolvingConflicts(patch_file) | |
277 | |
278 | |
279 class UploadStep(Step): | |
280 def __init__(self): | |
281 Step.__init__(self, "Upload for code review.") | |
282 | |
283 def RunStep(self): | |
284 print "Please enter the email address of a V8 reviewer for your patch: ", | |
285 reviewer = self.ReadLine() | |
286 args = "cl upload -r \"%s\" --send-mail" % reviewer | |
287 if self.Git(args,pipe=False) is None: | |
288 self.Die("'git cl upload' failed, please try again.") | |
OLD | NEW |