Adds remaining functions to cros_mark_as_stable and cleaned up error messaging.

TEST=Tested with following workflow:
cros_mark_as_stable -p 'crash-reporter' -i 'boogabooga' commit
cros_mark_as_stable push
cros_mark_as_stable clean

Also passed in some real options for push_options in a branch that had nothing in it.

Review URL: http://codereview.chromium.org/3017002
This commit is contained in:
Chris Sosa 2010-07-16 15:04:26 -07:00
parent 721d94f429
commit 16caaf0c7d

View File

@ -23,16 +23,16 @@ import generate_test_report
gflags.DEFINE_string('board', 'x86-generic', gflags.DEFINE_string('board', 'x86-generic',
'Board for which the package belongs.', short_name='b') 'Board for which the package belongs.', short_name='b')
gflags.DEFINE_string('commit_ids', '', gflags.DEFINE_string('commit_ids', '',
'''Optional list of commit ids for each package. """Optional list of commit ids for each package.
This list must either be empty or have the same length as This list must either be empty or have the same length as
the packages list. If not set all rev'd ebuilds will have the packages list. If not set all rev'd ebuilds will have
empty commit id's.''', empty commit id's.""",
short_name='i') short_name='i')
gflags.DEFINE_string('packages', '', gflags.DEFINE_string('packages', '',
'Space separated list of packages to mark as stable.', 'Space separated list of packages to mark as stable.',
short_name='p') short_name='p')
gflags.DEFINE_boolean('push', False, gflags.DEFINE_string('push_options', '',
'Creates, commits and pushes the stable ebuild.') 'Options to use with git-cl push using push command.')
gflags.DEFINE_boolean('verbose', False, gflags.DEFINE_boolean('verbose', False,
'Prints out verbose information about what is going on.', 'Prints out verbose information about what is going on.',
short_name='v') short_name='v')
@ -47,6 +47,18 @@ _CHROMIUMOS_OVERLAYS_DIRECTORY = \
_GIT_COMMIT_MESSAGE = \ _GIT_COMMIT_MESSAGE = \
'Marking 9999 ebuild for %s with commit %s as stable.' 'Marking 9999 ebuild for %s with commit %s as stable.'
# Dictionary of valid commands with usage information.
_COMMAND_DICTIONARY = {
'clean':
'Cleans up previous calls to either commit or push',
'commit':
'Marks given ebuilds as stable locally',
'push':
'Pushes previous marking of ebuilds to remote repo',
}
# Name used for stabilizing branch.
_STABLE_BRANCH_NAME = 'stabilizing_branch'
# ======================= Global Helper Functions ======================== # ======================= Global Helper Functions ========================
@ -56,23 +68,67 @@ def _Print(message):
if gflags.FLAGS.verbose: if gflags.FLAGS.verbose:
print message print message
def _CheckOnStabilizingBranch():
"""Returns true if the git branch is on the stabilizing branch."""
current_branch = _RunCommand('git branch | grep \*').split()[1]
return current_branch == _STABLE_BRANCH_NAME
def _CheckSaneArguments(package_list, commit_id_list): def _CheckSaneArguments(package_list, commit_id_list, command):
"""Checks to make sure the flags are sane. Dies if arguments are not sane""" """Checks to make sure the flags are sane. Dies if arguments are not sane."""
if not gflags.FLAGS.packages: if not command in _COMMAND_DICTIONARY.keys():
generate_test_report.Die('Please specify at least one package') _PrintUsageAndDie('%s is not a valid command' % command)
if not gflags.FLAGS.board: if not gflags.FLAGS.packages and command == 'commit':
generate_test_report.Die('Please specify a board') _PrintUsageAndDie('Please specify at least one package')
if not gflags.FLAGS.board and command == 'commit':
_PrintUsageAndDie('Please specify a board')
if commit_id_list and (len(package_list) != len(commit_id_list)): if commit_id_list and (len(package_list) != len(commit_id_list)):
print commit_id_list _PrintUsageAndDie(
print len(commit_id_list)
generate_test_report.Die(
'Package list is not the same length as the commit id list') 'Package list is not the same length as the commit id list')
def _PrintUsageAndDie(): def _Clean():
"""Prints the usage and returns an error exit code.""" """Cleans up uncommitted changes on either stabilizing branch or master."""
generate_test_report.Die('Usage: %s ARGS\n%s' % (sys.argv[0], gflags.FLAGS)) if _CheckOnStabilizingBranch():
_RunCommand('git reset HEAD --hard')
_RunCommand('git checkout master')
_RunCommand('git reset HEAD --hard')
def _PrintUsageAndDie(error_message=''):
"""Prints optional error_message the usage and returns an error exit code."""
command_usage = 'Commands: \n'
# Add keys and usage information from dictionary.
commands = sorted(_COMMAND_DICTIONARY.keys())
for command in commands:
command_usage += ' %s: %s\n' % (command, _COMMAND_DICTIONARY[command])
commands_str = '|'.join(commands)
print 'Usage: %s FLAGS [%s]\n\n%s\nFlags:%s' % (sys.argv[0], commands_str,
command_usage, gflags.FLAGS)
if error_message:
generate_test_report.Die(error_message)
else:
sys.exit(1)
def _PushChange():
"""Pushes changes to the git repository.
Pushes locals commits from calls to CommitChange to the remote git
repository specified by os.pwd.
Raises:
OSError: Error occurred while pushing.
"""
# TODO(sosa) - Add logic for buildbot to check whether other slaves have
# completed and push this change only if they have.
# Sanity check to make sure we're on a stabilizing branch before pushing.
if not _CheckOnStabilizingBranch():
generate_test_report.Die('Expected %s to be on branch "%s"' %
(_CHROMIUMOS_OVERLAYS_DIRECTORY,
_STABLE_BRANCH_NAME))
_RunCommand('git cl push %s' % gflags.FLAGS.push_options)
def _RunCommand(command): def _RunCommand(command):
@ -91,12 +147,6 @@ class _GitBranch(object):
def __init__(self, branch_name): def __init__(self, branch_name):
"""Sets up variables but does not create the branch.""" """Sets up variables but does not create the branch."""
self.branch_name = branch_name self.branch_name = branch_name
self._cleaned_up = False
def __del__(self):
"""Ensures we're checked back out to the master branch."""
if not self._cleaned_up:
self.CleanUp()
def CreateBranch(self): def CreateBranch(self):
"""Creates a new git branch or replaces an existing one.""" """Creates a new git branch or replaces an existing one."""
@ -104,11 +154,6 @@ class _GitBranch(object):
self.Delete() self.Delete()
self._Checkout(self.branch_name) self._Checkout(self.branch_name)
def CleanUp(self):
"""Does a git checkout back to the master branch."""
self._Checkout('master', create=False)
self._cleaned_up = True
def _Checkout(self, target, create=True): def _Checkout(self, target, create=True):
"""Function used internally to create and move between branches.""" """Function used internally to create and move between branches."""
if create: if create:
@ -259,43 +304,34 @@ class EBuildStableMarker(object):
git_commit_cmd = 'git commit -am "%s"' % message git_commit_cmd = 'git commit -am "%s"' % message
_RunCommand(git_commit_cmd) _RunCommand(git_commit_cmd)
# TODO(sosa): This doesn't work yet. Want to directly push without a prompt.
def PushChange(self):
"""Pushes changes to the git repository.
Pushes locals commits from calls to CommitChange to the remote git
repository specified by os.pwd.
Raises:
OSError: Error occurred while pushing.
"""
print 'Push currently not implemented'
# TODO(sosa): Un-comment once PushChange works.
# _Print('Pushing changes for %s' % self._ebuild.package)
# git_commit_cmd = 'git push'
# _RunCommand(git_commit_cmd)
def main(argv): def main(argv):
try: try:
argv = gflags.FLAGS(argv) argv = gflags.FLAGS(argv)
except gflags.FlagsError: if len(argv) != 2:
_PrintUsageAndDie() _PrintUsageAndDie('Must specify a valid command')
else:
command = argv[1]
except gflags.FlagsError, e :
_PrintUsageAndDie(str(e))
package_list = gflags.FLAGS.packages.split(' ') package_list = gflags.FLAGS.packages.split(' ')
if gflags.FLAGS.commit_ids: if gflags.FLAGS.commit_ids:
commit_id_list = gflags.FLAGS.commit_ids.split(' ') commit_id_list = gflags.FLAGS.commit_ids.split(' ')
else: else:
commit_id_list = None commit_id_list = None
_CheckSaneArguments(package_list, commit_id_list) _CheckSaneArguments(package_list, commit_id_list, command)
pwd = os.curdir
os.chdir(_CHROMIUMOS_OVERLAYS_DIRECTORY) os.chdir(_CHROMIUMOS_OVERLAYS_DIRECTORY)
work_branch = _GitBranch('stabilizing_branch') if command == 'clean':
_Clean()
elif command == 'commit':
work_branch = _GitBranch(_STABLE_BRANCH_NAME)
work_branch.CreateBranch() work_branch.CreateBranch()
if not work_branch.Exists(): if not work_branch.Exists():
generate_test_report.Die('Unable to create stabilizing branch') generate_test_report.Die('Unable to create stabilizing branch in %s' %
_CHROMIUMOS_OVERLAYS_DIRECTORY)
index = 0 index = 0
try: try:
for index in range(len(package_list)): for index in range(len(package_list)):
@ -309,19 +345,15 @@ def main(argv):
worker = EBuildStableMarker(_EBuild(package, commit_id)) worker = EBuildStableMarker(_EBuild(package, commit_id))
worker.RevEBuild(commit_id) worker.RevEBuild(commit_id)
worker.CommitChange(_GIT_COMMIT_MESSAGE % (package, commit_id)) worker.CommitChange(_GIT_COMMIT_MESSAGE % (package, commit_id))
if gflags.FLAGS.push:
worker.PushChange()
except (OSError, IOError): except (OSError, IOError), e:
print 'An exception occurred %s' % sys.exc_info()[0] print ('An exception occurred %s\n'
print 'Only the following packages were revved: %s' % package_list[:index] 'Only the following packages were revved: %s\n'
print '''Note you will have to go into the chromiumos-overlay directory and 'Note you will have to go into %s'
reset the git repo yourself. 'and reset the git repo yourself.' %
''' (e, package_list[:index], _CHROMIUMOS_OVERLAYS_DIRECTORY))
finally: elif command == 'push':
# Always run the last two cleanup functions. _PushChange()
work_branch.CleanUp()
os.chdir(pwd)
if __name__ == '__main__': if __name__ == '__main__':