| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """This module uprevs a given package's ebuild to the next revision.""" | 7 """This module uprevs a given package's ebuild to the next revision.""" |
| 8 | 8 |
| 9 | 9 |
| 10 import fileinput | 10 import fileinput |
| 11 import gflags | 11 import gflags |
| 12 import os | 12 import os |
| 13 import re | 13 import re |
| 14 import shutil | 14 import shutil |
| 15 import subprocess | 15 import subprocess |
| 16 import sys | 16 import sys |
| 17 | 17 |
| 18 # TODO(sosa): Refactor Die into common library. | 18 # TODO(sosa): Refactor Die into common library. |
| 19 sys.path.append(os.path.dirname(__file__)) | 19 sys.path.append(os.path.dirname(__file__)) |
| 20 import generate_test_report | 20 import generate_test_report |
| 21 | 21 |
| 22 | 22 |
| 23 gflags.DEFINE_string('board', 'x86-generic', | 23 gflags.DEFINE_string('board', 'x86-generic', |
| 24 'Board for which the package belongs.', short_name='b') | 24 'Board for which the package belongs.', short_name='b') |
| 25 gflags.DEFINE_string('commit_ids', '', | 25 gflags.DEFINE_string('commit_ids', '', |
| 26 '''Optional list of commit ids for each package. | 26 """Optional list of commit ids for each package. |
| 27 This list must either be empty or have the same length as | 27 This list must either be empty or have the same length as |
| 28 the packages list. If not set all rev'd ebuilds will have | 28 the packages list. If not set all rev'd ebuilds will have |
| 29 empty commit id's.''', | 29 empty commit id's.""", |
| 30 short_name='i') | 30 short_name='i') |
| 31 gflags.DEFINE_string('packages', '', | 31 gflags.DEFINE_string('packages', '', |
| 32 'Space separated list of packages to mark as stable.', | 32 'Space separated list of packages to mark as stable.', |
| 33 short_name='p') | 33 short_name='p') |
| 34 gflags.DEFINE_boolean('push', False, | 34 gflags.DEFINE_string('push_options', '', |
| 35 'Creates, commits and pushes the stable ebuild.') | 35 'Options to use with git-cl push using push command.') |
| 36 gflags.DEFINE_boolean('verbose', False, | 36 gflags.DEFINE_boolean('verbose', False, |
| 37 'Prints out verbose information about what is going on.', | 37 'Prints out verbose information about what is going on.', |
| 38 short_name='v') | 38 short_name='v') |
| 39 | 39 |
| 40 | 40 |
| 41 # TODO(sosa): Remove hard-coding of overlays directory once there is a better | 41 # TODO(sosa): Remove hard-coding of overlays directory once there is a better |
| 42 # way. | 42 # way. |
| 43 _CHROMIUMOS_OVERLAYS_DIRECTORY = \ | 43 _CHROMIUMOS_OVERLAYS_DIRECTORY = \ |
| 44 '%s/trunk/src/third_party/chromiumos-overlay' % os.environ['HOME'] | 44 '%s/trunk/src/third_party/chromiumos-overlay' % os.environ['HOME'] |
| 45 | 45 |
| 46 # Takes two strings, package_name and commit_id. | 46 # Takes two strings, package_name and commit_id. |
| 47 _GIT_COMMIT_MESSAGE = \ | 47 _GIT_COMMIT_MESSAGE = \ |
| 48 'Marking 9999 ebuild for %s with commit %s as stable.' | 48 'Marking 9999 ebuild for %s with commit %s as stable.' |
| 49 | 49 |
| 50 # Dictionary of valid commands with usage information. |
| 51 _COMMAND_DICTIONARY = { |
| 52 'clean': |
| 53 'Cleans up previous calls to either commit or push', |
| 54 'commit': |
| 55 'Marks given ebuilds as stable locally', |
| 56 'push': |
| 57 'Pushes previous marking of ebuilds to remote repo', |
| 58 } |
| 59 |
| 60 # Name used for stabilizing branch. |
| 61 _STABLE_BRANCH_NAME = 'stabilizing_branch' |
| 50 | 62 |
| 51 # ======================= Global Helper Functions ======================== | 63 # ======================= Global Helper Functions ======================== |
| 52 | 64 |
| 53 | 65 |
| 54 def _Print(message): | 66 def _Print(message): |
| 55 """Verbose print function.""" | 67 """Verbose print function.""" |
| 56 if gflags.FLAGS.verbose: | 68 if gflags.FLAGS.verbose: |
| 57 print message | 69 print message |
| 58 | 70 |
| 71 def _CheckOnStabilizingBranch(): |
| 72 """Returns true if the git branch is on the stabilizing branch.""" |
| 73 current_branch = _RunCommand('git branch | grep \*').split()[1] |
| 74 return current_branch == _STABLE_BRANCH_NAME |
| 59 | 75 |
| 60 def _CheckSaneArguments(package_list, commit_id_list): | 76 def _CheckSaneArguments(package_list, commit_id_list, command): |
| 61 """Checks to make sure the flags are sane. Dies if arguments are not sane""" | 77 """Checks to make sure the flags are sane. Dies if arguments are not sane.""" |
| 62 if not gflags.FLAGS.packages: | 78 if not command in _COMMAND_DICTIONARY.keys(): |
| 63 generate_test_report.Die('Please specify at least one package') | 79 _PrintUsageAndDie('%s is not a valid command' % command) |
| 64 if not gflags.FLAGS.board: | 80 if not gflags.FLAGS.packages and command == 'commit': |
| 65 generate_test_report.Die('Please specify a board') | 81 _PrintUsageAndDie('Please specify at least one package') |
| 82 if not gflags.FLAGS.board and command == 'commit': |
| 83 _PrintUsageAndDie('Please specify a board') |
| 66 if commit_id_list and (len(package_list) != len(commit_id_list)): | 84 if commit_id_list and (len(package_list) != len(commit_id_list)): |
| 67 print commit_id_list | 85 _PrintUsageAndDie( |
| 68 print len(commit_id_list) | |
| 69 generate_test_report.Die( | |
| 70 'Package list is not the same length as the commit id list') | 86 'Package list is not the same length as the commit id list') |
| 71 | 87 |
| 72 | 88 |
| 73 def _PrintUsageAndDie(): | 89 def _Clean(): |
| 74 """Prints the usage and returns an error exit code.""" | 90 """Cleans up uncommitted changes on either stabilizing branch or master.""" |
| 75 generate_test_report.Die('Usage: %s ARGS\n%s' % (sys.argv[0], gflags.FLAGS)) | 91 if _CheckOnStabilizingBranch(): |
| 92 _RunCommand('git reset HEAD --hard') |
| 93 _RunCommand('git checkout master') |
| 94 _RunCommand('git reset HEAD --hard') |
| 95 |
| 96 |
| 97 def _PrintUsageAndDie(error_message=''): |
| 98 """Prints optional error_message the usage and returns an error exit code.""" |
| 99 command_usage = 'Commands: \n' |
| 100 # Add keys and usage information from dictionary. |
| 101 commands = sorted(_COMMAND_DICTIONARY.keys()) |
| 102 for command in commands: |
| 103 command_usage += ' %s: %s\n' % (command, _COMMAND_DICTIONARY[command]) |
| 104 commands_str = '|'.join(commands) |
| 105 print 'Usage: %s FLAGS [%s]\n\n%s\nFlags:%s' % (sys.argv[0], commands_str, |
| 106 command_usage, gflags.FLAGS) |
| 107 if error_message: |
| 108 generate_test_report.Die(error_message) |
| 109 else: |
| 110 sys.exit(1) |
| 111 |
| 112 |
| 113 def _PushChange(): |
| 114 """Pushes changes to the git repository. |
| 115 |
| 116 Pushes locals commits from calls to CommitChange to the remote git |
| 117 repository specified by os.pwd. |
| 118 |
| 119 Raises: |
| 120 OSError: Error occurred while pushing. |
| 121 """ |
| 122 |
| 123 # TODO(sosa) - Add logic for buildbot to check whether other slaves have |
| 124 # completed and push this change only if they have. |
| 125 |
| 126 # Sanity check to make sure we're on a stabilizing branch before pushing. |
| 127 if not _CheckOnStabilizingBranch(): |
| 128 generate_test_report.Die('Expected %s to be on branch "%s"' % |
| 129 (_CHROMIUMOS_OVERLAYS_DIRECTORY, |
| 130 _STABLE_BRANCH_NAME)) |
| 131 _RunCommand('git cl push %s' % gflags.FLAGS.push_options) |
| 76 | 132 |
| 77 | 133 |
| 78 def _RunCommand(command): | 134 def _RunCommand(command): |
| 79 """Runs a shell command and returns stdout back to caller.""" | 135 """Runs a shell command and returns stdout back to caller.""" |
| 80 _Print(' + %s' % command) | 136 _Print(' + %s' % command) |
| 81 proc_handle = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) | 137 proc_handle = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) |
| 82 return proc_handle.communicate()[0] | 138 return proc_handle.communicate()[0] |
| 83 | 139 |
| 84 | 140 |
| 85 # ======================= End Global Helper Functions ======================== | 141 # ======================= End Global Helper Functions ======================== |
| 86 | 142 |
| 87 | 143 |
| 88 class _GitBranch(object): | 144 class _GitBranch(object): |
| 89 """Wrapper class for a git branch.""" | 145 """Wrapper class for a git branch.""" |
| 90 | 146 |
| 91 def __init__(self, branch_name): | 147 def __init__(self, branch_name): |
| 92 """Sets up variables but does not create the branch.""" | 148 """Sets up variables but does not create the branch.""" |
| 93 self.branch_name = branch_name | 149 self.branch_name = branch_name |
| 94 self._cleaned_up = False | |
| 95 | |
| 96 def __del__(self): | |
| 97 """Ensures we're checked back out to the master branch.""" | |
| 98 if not self._cleaned_up: | |
| 99 self.CleanUp() | |
| 100 | 150 |
| 101 def CreateBranch(self): | 151 def CreateBranch(self): |
| 102 """Creates a new git branch or replaces an existing one.""" | 152 """Creates a new git branch or replaces an existing one.""" |
| 103 if self.Exists(): | 153 if self.Exists(): |
| 104 self.Delete() | 154 self.Delete() |
| 105 self._Checkout(self.branch_name) | 155 self._Checkout(self.branch_name) |
| 106 | 156 |
| 107 def CleanUp(self): | |
| 108 """Does a git checkout back to the master branch.""" | |
| 109 self._Checkout('master', create=False) | |
| 110 self._cleaned_up = True | |
| 111 | |
| 112 def _Checkout(self, target, create=True): | 157 def _Checkout(self, target, create=True): |
| 113 """Function used internally to create and move between branches.""" | 158 """Function used internally to create and move between branches.""" |
| 114 if create: | 159 if create: |
| 115 git_cmd = 'git checkout -b %s origin' % target | 160 git_cmd = 'git checkout -b %s origin' % target |
| 116 else: | 161 else: |
| 117 git_cmd = 'git checkout %s' % target | 162 git_cmd = 'git checkout %s' % target |
| 118 _RunCommand(git_cmd) | 163 _RunCommand(git_cmd) |
| 119 | 164 |
| 120 def Exists(self): | 165 def Exists(self): |
| 121 """Returns True if the branch exists.""" | 166 """Returns True if the branch exists.""" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 message: the commit string to write when committing to git. | 297 message: the commit string to write when committing to git. |
| 253 | 298 |
| 254 Raises: | 299 Raises: |
| 255 OSError: Error occurred while committing. | 300 OSError: Error occurred while committing. |
| 256 """ | 301 """ |
| 257 _Print('Committing changes for %s with commit message %s' % \ | 302 _Print('Committing changes for %s with commit message %s' % \ |
| 258 (self._ebuild.package, message)) | 303 (self._ebuild.package, message)) |
| 259 git_commit_cmd = 'git commit -am "%s"' % message | 304 git_commit_cmd = 'git commit -am "%s"' % message |
| 260 _RunCommand(git_commit_cmd) | 305 _RunCommand(git_commit_cmd) |
| 261 | 306 |
| 262 # TODO(sosa): This doesn't work yet. Want to directly push without a prompt. | |
| 263 def PushChange(self): | |
| 264 """Pushes changes to the git repository. | |
| 265 | |
| 266 Pushes locals commits from calls to CommitChange to the remote git | |
| 267 repository specified by os.pwd. | |
| 268 | |
| 269 Raises: | |
| 270 OSError: Error occurred while pushing. | |
| 271 """ | |
| 272 print 'Push currently not implemented' | |
| 273 # TODO(sosa): Un-comment once PushChange works. | |
| 274 # _Print('Pushing changes for %s' % self._ebuild.package) | |
| 275 # git_commit_cmd = 'git push' | |
| 276 # _RunCommand(git_commit_cmd) | |
| 277 | |
| 278 | 307 |
| 279 def main(argv): | 308 def main(argv): |
| 280 try: | 309 try: |
| 281 argv = gflags.FLAGS(argv) | 310 argv = gflags.FLAGS(argv) |
| 282 except gflags.FlagsError: | 311 if len(argv) != 2: |
| 283 _PrintUsageAndDie() | 312 _PrintUsageAndDie('Must specify a valid command') |
| 313 else: |
| 314 command = argv[1] |
| 315 except gflags.FlagsError, e : |
| 316 _PrintUsageAndDie(str(e)) |
| 284 | 317 |
| 285 package_list = gflags.FLAGS.packages.split(' ') | 318 package_list = gflags.FLAGS.packages.split(' ') |
| 286 if gflags.FLAGS.commit_ids: | 319 if gflags.FLAGS.commit_ids: |
| 287 commit_id_list = gflags.FLAGS.commit_ids.split(' ') | 320 commit_id_list = gflags.FLAGS.commit_ids.split(' ') |
| 288 else: | 321 else: |
| 289 commit_id_list = None | 322 commit_id_list = None |
| 290 _CheckSaneArguments(package_list, commit_id_list) | 323 _CheckSaneArguments(package_list, commit_id_list, command) |
| 291 | 324 |
| 292 pwd = os.curdir | |
| 293 os.chdir(_CHROMIUMOS_OVERLAYS_DIRECTORY) | 325 os.chdir(_CHROMIUMOS_OVERLAYS_DIRECTORY) |
| 294 | 326 |
| 295 work_branch = _GitBranch('stabilizing_branch') | 327 if command == 'clean': |
| 296 work_branch.CreateBranch() | 328 _Clean() |
| 297 if not work_branch.Exists(): | 329 elif command == 'commit': |
| 298 generate_test_report.Die('Unable to create stabilizing branch') | 330 work_branch = _GitBranch(_STABLE_BRANCH_NAME) |
| 299 index = 0 | 331 work_branch.CreateBranch() |
| 300 try: | 332 if not work_branch.Exists(): |
| 301 for index in range(len(package_list)): | 333 generate_test_report.Die('Unable to create stabilizing branch in %s' % |
| 302 # Gather the package and optional commit id to work on. | 334 _CHROMIUMOS_OVERLAYS_DIRECTORY) |
| 303 package = package_list[index] | 335 index = 0 |
| 304 commit_id = "" | 336 try: |
| 305 if commit_id_list: | 337 for index in range(len(package_list)): |
| 306 commit_id = commit_id_list[index] | 338 # Gather the package and optional commit id to work on. |
| 339 package = package_list[index] |
| 340 commit_id = "" |
| 341 if commit_id_list: |
| 342 commit_id = commit_id_list[index] |
| 307 | 343 |
| 308 _Print('Working on %s' % package) | 344 _Print('Working on %s' % package) |
| 309 worker = EBuildStableMarker(_EBuild(package, commit_id)) | 345 worker = EBuildStableMarker(_EBuild(package, commit_id)) |
| 310 worker.RevEBuild(commit_id) | 346 worker.RevEBuild(commit_id) |
| 311 worker.CommitChange(_GIT_COMMIT_MESSAGE % (package, commit_id)) | 347 worker.CommitChange(_GIT_COMMIT_MESSAGE % (package, commit_id)) |
| 312 if gflags.FLAGS.push: | |
| 313 worker.PushChange() | |
| 314 | 348 |
| 315 except (OSError, IOError): | 349 except (OSError, IOError), e: |
| 316 print 'An exception occurred %s' % sys.exc_info()[0] | 350 print ('An exception occurred %s\n' |
| 317 print 'Only the following packages were revved: %s' % package_list[:index] | 351 'Only the following packages were revved: %s\n' |
| 318 print '''Note you will have to go into the chromiumos-overlay directory and | 352 'Note you will have to go into %s' |
| 319 reset the git repo yourself. | 353 'and reset the git repo yourself.' % |
| 320 ''' | 354 (e, package_list[:index], _CHROMIUMOS_OVERLAYS_DIRECTORY)) |
| 321 finally: | 355 elif command == 'push': |
| 322 # Always run the last two cleanup functions. | 356 _PushChange() |
| 323 work_branch.CleanUp() | |
| 324 os.chdir(pwd) | |
| 325 | 357 |
| 326 | 358 |
| 327 if __name__ == '__main__': | 359 if __name__ == '__main__': |
| 328 main(sys.argv) | 360 main(sys.argv) |
| 329 | 361 |
| OLD | NEW |