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 cmd_line = "%s %s %s" % (prefix, cmd, args) |
| 74 print "Command: %s" % cmd_line |
| 75 try: |
| 76 if pipe: |
| 77 return subprocess.check_output(cmd_line, shell=True) |
| 78 else: |
| 79 return subprocess.check_call(cmd_line, shell=True) |
| 80 except subprocess.CalledProcessError: |
| 81 return None |
| 82 |
| 83 |
| 84 # Wrapper for side effects. |
| 85 class SideEffectHandler(object): |
| 86 def Command(self, cmd, args="", prefix="", pipe=True): |
| 87 return Command(cmd, args, prefix, pipe) |
| 88 |
| 89 def ReadLine(self): |
| 90 return sys.stdin.readline().strip() |
| 91 |
| 92 DEFAULT_SIDE_EFFECT_HANDLER = SideEffectHandler() |
| 93 |
| 94 |
| 95 class Step(object): |
| 96 def __init__(self, text="", requires=None): |
| 97 self._text = text |
| 98 self._number = -1 |
| 99 self._requires = requires |
| 100 self._side_effect_handler = DEFAULT_SIDE_EFFECT_HANDLER |
| 101 |
| 102 def SetNumber(self, number): |
| 103 self._number = number |
| 104 |
| 105 def SetConfig(self, config): |
| 106 self._config = config |
| 107 |
| 108 def SetState(self, state): |
| 109 self._state = state |
| 110 |
| 111 def SetOptions(self, options): |
| 112 self._options = options |
| 113 |
| 114 def SetSideEffectHandler(self, handler): |
| 115 self._side_effect_handler = handler |
| 116 |
| 117 def Config(self, key): |
| 118 return self._config[key] |
| 119 |
| 120 def Run(self): |
| 121 assert self._number >= 0 |
| 122 assert self._config is not None |
| 123 assert self._state is not None |
| 124 assert self._side_effect_handler is not None |
| 125 if self._requires: |
| 126 self.RestoreIfUnset(self._requires) |
| 127 if not self._state[self._requires]: |
| 128 return |
| 129 print ">>> Step %d: %s" % (self._number, self._text) |
| 130 self.RunStep() |
| 131 |
| 132 def RunStep(self): |
| 133 raise NotImplementedError |
| 134 |
| 135 def ReadLine(self): |
| 136 return self._side_effect_handler.ReadLine() |
| 137 |
| 138 def Git(self, args="", prefix="", pipe=True): |
| 139 return self._side_effect_handler.Command("git", args, prefix, pipe) |
| 140 |
| 141 def Editor(self, args): |
| 142 return self._side_effect_handler.Command(os.environ["EDITOR"], args, |
| 143 pipe=False) |
| 144 |
| 145 def Die(self, msg=""): |
| 146 if msg != "": |
| 147 print "Error: %s" % msg |
| 148 print "Exiting" |
| 149 raise Exception(msg) |
| 150 |
| 151 def Confirm(self, msg): |
| 152 print "%s [Y/n] " % msg, |
| 153 answer = self.ReadLine() |
| 154 return answer == "" or answer == "Y" or answer == "y" |
| 155 |
| 156 def DeleteBranch(self, name): |
| 157 git_result = self.Git("branch").strip() |
| 158 for line in git_result.splitlines(): |
| 159 if re.match(r".*\s+%s$" % name, line): |
| 160 msg = "Branch %s exists, do you want to delete it?" % name |
| 161 if self.Confirm(msg): |
| 162 if self.Git("branch -D %s" % name) is None: |
| 163 self.Die("Deleting branch '%s' failed." % name) |
| 164 print "Branch %s deleted." % name |
| 165 else: |
| 166 msg = "Can't continue. Please delete branch %s and try again." % name |
| 167 self.Die(msg) |
| 168 |
| 169 def Persist(self, var, value): |
| 170 value = value or "__EMPTY__" |
| 171 TextToFile(value, "%s-%s" % (self._config[PERSISTFILE_BASENAME], var)) |
| 172 |
| 173 def Restore(self, var): |
| 174 value = FileToText("%s-%s" % (self._config[PERSISTFILE_BASENAME], var)) |
| 175 value = value or self.Die("Variable '%s' could not be restored." % var) |
| 176 return "" if value == "__EMPTY__" else value |
| 177 |
| 178 def RestoreIfUnset(self, var_name): |
| 179 if self._state.get(var_name) is None: |
| 180 self._state[var_name] = self.Restore(var_name) |
| 181 |
| 182 def InitialEnvironmentChecks(self): |
| 183 # Cancel if this is not a git checkout. |
| 184 if not os.path.exists(self._config[DOT_GIT_LOCATION]): |
| 185 self.Die("This is not a git checkout, this script won't work for you.") |
| 186 |
| 187 # Cancel if EDITOR is unset or not executable. |
| 188 if (not os.environ.get("EDITOR") or |
| 189 Command("which", os.environ["EDITOR"]) is None): |
| 190 self.Die("Please set your EDITOR environment variable, you'll need it.") |
| 191 |
| 192 def CommonPrepare(self): |
| 193 # Check for a clean workdir. |
| 194 if self.Git("status -s -uno").strip() != "": |
| 195 self.Die("Workspace is not clean. Please commit or undo your changes.") |
| 196 |
| 197 # Persist current branch. |
| 198 current_branch = "" |
| 199 git_result = self.Git("status -s -b -uno").strip() |
| 200 for line in git_result.splitlines(): |
| 201 match = re.match(r"^## (.+)", line) |
| 202 if match: |
| 203 current_branch = match.group(1) |
| 204 break |
| 205 self.Persist("current_branch", current_branch) |
| 206 |
| 207 # Fetch unfetched revisions. |
| 208 if self.Git("svn fetch") is None: |
| 209 self.Die("'git svn fetch' failed.") |
| 210 |
| 211 # Get ahold of a safe temporary branch and check it out. |
| 212 if current_branch != self._config[TEMP_BRANCH]: |
| 213 self.DeleteBranch(self._config[TEMP_BRANCH]) |
| 214 self.Git("checkout -b %s" % self._config[TEMP_BRANCH]) |
| 215 |
| 216 # Delete the branch that will be created later if it exists already. |
| 217 self.DeleteBranch(self._config[BRANCHNAME]) |
| 218 |
| 219 def CommonCleanup(self): |
| 220 self.RestoreIfUnset("current_branch") |
| 221 self.Git("checkout -f %s" % self._state["current_branch"]) |
| 222 if self._config[TEMP_BRANCH] != self._state["current_branch"]: |
| 223 self.Git("branch -D %s" % self._config[TEMP_BRANCH]) |
| 224 if self._config[BRANCHNAME] != self._state["current_branch"]: |
| 225 self.Git("branch -D %s" % self._config[BRANCHNAME]) |
| 226 |
| 227 # Clean up all temporary files. |
| 228 Command("rm", "-f %s*" % self._config[PERSISTFILE_BASENAME]) |
| 229 |
| 230 def ReadAndPersistVersion(self, prefix=""): |
| 231 def ReadAndPersist(var_name, def_name): |
| 232 match = re.match(r"^#define %s\s+(\d*)" % def_name, line) |
| 233 if match: |
| 234 value = match.group(1) |
| 235 self.Persist("%s%s" % (prefix, var_name), value) |
| 236 self._state["%s%s" % (prefix, var_name)] = value |
| 237 for line in LinesInFile(self._config[VERSION_FILE]): |
| 238 for (var_name, def_name) in [("major", "MAJOR_VERSION"), |
| 239 ("minor", "MINOR_VERSION"), |
| 240 ("build", "BUILD_NUMBER"), |
| 241 ("patch", "PATCH_LEVEL")]: |
| 242 ReadAndPersist(var_name, def_name) |
| 243 |
| 244 def RestoreVersionIfUnset(self, prefix=""): |
| 245 for v in ["major", "minor", "build", "patch"]: |
| 246 self.RestoreIfUnset("%s%s" % (prefix, v)) |
| 247 |
| 248 def WaitForLGTM(self): |
| 249 print ("Please wait for an LGTM, then type \"LGTM<Return>\" to commit " |
| 250 "your change. (If you need to iterate on the patch or double check " |
| 251 "that it's sane, do so in another shell, but remember to not " |
| 252 "change the headline of the uploaded CL.") |
| 253 answer = "" |
| 254 while answer != "LGTM": |
| 255 print "> ", |
| 256 answer = self.ReadLine() |
| 257 if answer != "LGTM": |
| 258 print "That was not 'LGTM'." |
| 259 |
| 260 def WaitForResolvingConflicts(self, patch_file): |
| 261 print("Applying the patch \"%s\" failed. Either type \"ABORT<Return>\", " |
| 262 "or resolve the conflicts, stage *all* touched files with " |
| 263 "'git add', and type \"RESOLVED<Return>\"") |
| 264 answer = "" |
| 265 while answer != "RESOLVED": |
| 266 if answer == "ABORT": |
| 267 self.Die("Applying the patch failed.") |
| 268 if answer != "": |
| 269 print "That was not 'RESOLVED' or 'ABORT'." |
| 270 print "> ", |
| 271 answer = self.ReadLine() |
| 272 |
| 273 # Takes a file containing the patch to apply as first argument. |
| 274 def ApplyPatch(self, patch_file, reverse_patch=""): |
| 275 args = "apply --index --reject %s \"%s\"" % (reverse_patch, patch_file) |
| 276 if self.Git(args) is None: |
| 277 self.WaitForResolvingConflicts(patch_file) |
| 278 |
| 279 |
| 280 class UploadStep(Step): |
| 281 def __init__(self): |
| 282 Step.__init__(self, "Upload for code review.") |
| 283 |
| 284 def RunStep(self): |
| 285 print "Please enter the email address of a V8 reviewer for your patch: ", |
| 286 reviewer = self.ReadLine() |
| 287 args = "cl upload -r \"%s\" --send-mail" % reviewer |
| 288 if self.Git(args,pipe=False) is None: |
| 289 self.Die("'git cl upload' failed, please try again.") |
OLD | NEW |