Support a finalize method for argument parsing
Allow for more complex manipulation of arguments to be contained within a separate method for clarity. Change-Id: I9d5f369511401591d3267a8654bfb871f8208820
This commit is contained in:

committed by
Darragh Bailey

parent
3d641bcbf9
commit
f0f03b4c91
@@ -51,15 +51,24 @@ class GitUpstreamCommand(object):
|
|||||||
|
|
||||||
def __init__(self, parser):
|
def __init__(self, parser):
|
||||||
self.parser = parser
|
self.parser = parser
|
||||||
|
self.args = None
|
||||||
|
|
||||||
def validate(self, args):
|
def validate(self):
|
||||||
"""Verify the arguments passed for this command"""
|
"""Verify the arguments passed for this command"""
|
||||||
return
|
|
||||||
|
def finalize(self):
|
||||||
|
"""Additional updating of the args to set values"""
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def run(self, args):
|
def execute(self, args):
|
||||||
"""Execute this command"""
|
"""Execute this command"""
|
||||||
return
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def run(self, args):
|
||||||
|
self.args = args
|
||||||
|
self.finalize()
|
||||||
|
self.validate()
|
||||||
|
return self.execute()
|
||||||
|
|
||||||
|
|
||||||
def get_subcommands(parser):
|
def get_subcommands(parser):
|
||||||
|
@@ -40,9 +40,9 @@ class DropCommand(LogDedentMixin, GitUpstreamCommand):
|
|||||||
'-a', '--author', metavar='<author>', dest='author', default=None,
|
'-a', '--author', metavar='<author>', dest='author', default=None,
|
||||||
help='Git author for the mark')
|
help='Git author for the mark')
|
||||||
|
|
||||||
def run(self, args):
|
def execute(self):
|
||||||
|
|
||||||
drop = Drop(git_object=args.commit, author=args.author)
|
drop = Drop(git_object=self.args.commit, author=self.args.author)
|
||||||
|
|
||||||
if drop.mark():
|
if drop.mark():
|
||||||
self.log.notice("Drop mark created successfully")
|
self.log.notice("Drop mark created successfully")
|
||||||
|
@@ -29,7 +29,7 @@ class HelpCommand(LogDedentMixin, GitUpstreamCommand):
|
|||||||
self.parser.add_argument('command', metavar='<command>', nargs='?',
|
self.parser.add_argument('command', metavar='<command>', nargs='?',
|
||||||
help="command to display help about")
|
help="command to display help about")
|
||||||
|
|
||||||
def run(self, args, parent_parser=None):
|
def execute(self, args, parent_parser=None):
|
||||||
if getattr(args, 'command', None):
|
if getattr(args, 'command', None):
|
||||||
if args.command in args.subcommands:
|
if args.command in args.subcommands:
|
||||||
args.subcommands[args.command].print_help()
|
args.subcommands[args.command].print_help()
|
||||||
|
@@ -102,44 +102,47 @@ class ImportCommand(LogDedentMixin, GitUpstreamCommand):
|
|||||||
help='Branches to additionally merge into the import branch using '
|
help='Branches to additionally merge into the import branch using '
|
||||||
'default git merging behaviour')
|
'default git merging behaviour')
|
||||||
|
|
||||||
def validate(self, args):
|
def validate(self):
|
||||||
"""Perform more complex validation of args that cannot be mixed"""
|
"""Perform more complex validation of args that cannot be mixed"""
|
||||||
|
|
||||||
# check if --finish set with --no-merge
|
# check if --finish set with --no-merge
|
||||||
if args.finish and args.merge is False:
|
if self.args.finish and self.args.merge is False:
|
||||||
self.parser.error(
|
self.parser.error(
|
||||||
"--finish cannot be used with '--no-merge'")
|
"--finish cannot be used with '--no-merge'")
|
||||||
|
|
||||||
def _finish(self, args, import_upstream):
|
def _finish(self, import_upstream):
|
||||||
self.log.notice("Merging import to requested branch '%s'", args.branch)
|
self.log.notice("Merging import to requested branch '%s'",
|
||||||
|
self.args.branch)
|
||||||
if import_upstream.finish():
|
if import_upstream.finish():
|
||||||
self.log.notice(
|
self.log.notice(
|
||||||
"""
|
"""
|
||||||
Successfully finished import:
|
Successfully finished import:
|
||||||
target branch: '%s'
|
target branch: '%s'
|
||||||
upstream branch: '%s'
|
upstream branch: '%s'
|
||||||
import branch: '%s'""", args.branch, args.upstream_branch,
|
import branch: '%s'""",
|
||||||
|
self.args.branch, self.args.upstream_branch,
|
||||||
import_upstream.import_branch)
|
import_upstream.import_branch)
|
||||||
if args.branches:
|
if self.args.branches:
|
||||||
for branch in args.branches:
|
for branch in self.args.branches:
|
||||||
self.log.notice(" extra branch: '%s'", branch,
|
self.log.notice(" extra branch: '%s'", branch,
|
||||||
dedent=False)
|
dedent=False)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def run(self, args):
|
def execute(self):
|
||||||
|
|
||||||
import_upstream = ImportUpstream(
|
import_upstream = ImportUpstream(
|
||||||
branch=args.branch,
|
branch=self.args.branch,
|
||||||
upstream=args.upstream_branch,
|
upstream=self.args.upstream_branch,
|
||||||
import_branch=args.import_branch,
|
import_branch=self.args.import_branch,
|
||||||
extra_branches=args.branches)
|
extra_branches=self.args.branches)
|
||||||
|
|
||||||
self.log.notice("Searching for previous import")
|
self.log.notice("Searching for previous import")
|
||||||
strategy = ImportStrategiesFactory.create_strategy(
|
strategy = ImportStrategiesFactory.create_strategy(
|
||||||
args.strategy, branch=args.branch, upstream=args.upstream_branch,
|
self.args.strategy, branch=self.args.branch,
|
||||||
search_refs=args.search_refs)
|
upstream=self.args.upstream_branch,
|
||||||
|
search_refs=self.args.search_refs)
|
||||||
|
|
||||||
if len(strategy) == 0:
|
if len(strategy) == 0:
|
||||||
raise ImportUpstreamError("Cannot find previous import")
|
raise ImportUpstreamError("Cannot find previous import")
|
||||||
@@ -155,14 +158,14 @@ class ImportCommand(LogDedentMixin, GitUpstreamCommand):
|
|||||||
if idxs:
|
if idxs:
|
||||||
additional_commits = [prev_import_merge.parents[i]
|
additional_commits = [prev_import_merge.parents[i]
|
||||||
for i in idxs]
|
for i in idxs]
|
||||||
if additional_commits and len(args.branches) == 0:
|
if additional_commits and len(self.args.branches) == 0:
|
||||||
self.log.warning("""
|
self.log.warning("""
|
||||||
**************** WARNING ****************
|
**************** WARNING ****************
|
||||||
Previous import merged additional branches but none
|
Previous import merged additional branches but none
|
||||||
have been specified on the command line for this
|
have been specified on the command line for this
|
||||||
import.\n""")
|
import.\n""")
|
||||||
|
|
||||||
if args.dry_run:
|
if self.args.dry_run:
|
||||||
commit_list = [c.hexsha[:6] + " - " + c.summary[:60] +
|
commit_list = [c.hexsha[:6] + " - " + c.summary[:60] +
|
||||||
(c.summary[60:] and "...")
|
(c.summary[60:] and "...")
|
||||||
for c in list(strategy.filtered_iter())]
|
for c in list(strategy.filtered_iter())]
|
||||||
@@ -175,26 +178,26 @@ class ImportCommand(LogDedentMixin, GitUpstreamCommand):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
# finish and return if thats all
|
# finish and return if thats all
|
||||||
if args.finish:
|
if self.args.finish:
|
||||||
return self._finish(args, import_upstream)
|
return self._finish(import_upstream)
|
||||||
|
|
||||||
# otherwise perform fresh import
|
# otherwise perform fresh import
|
||||||
self.log.notice("Starting import of upstream")
|
self.log.notice("Starting import of upstream")
|
||||||
import_upstream.create_import(force=args.force)
|
import_upstream.create_import(force=self.args.force)
|
||||||
self.log.notice("Successfully created import branch")
|
self.log.notice("Successfully created import branch")
|
||||||
|
|
||||||
if not import_upstream.apply(strategy, args.interactive):
|
if not import_upstream.apply(strategy, self.args.interactive):
|
||||||
self.log.notice("Import cancelled")
|
self.log.notice("Import cancelled")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not args.merge:
|
if not self.args.merge:
|
||||||
self.log.notice(
|
self.log.notice(
|
||||||
"""
|
"""
|
||||||
Import complete, not merging to target branch '%s' as
|
Import complete, not merging to target branch '%s' as
|
||||||
requested.
|
requested.
|
||||||
""", args.branch)
|
""", self.args.branch)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return self._finish(args, import_upstream)
|
return self._finish(import_upstream)
|
||||||
|
|
||||||
# vim:sw=4:sts=4:ts=4:et:
|
# vim:sw=4:sts=4:ts=4:et:
|
||||||
|
@@ -57,12 +57,12 @@ class SupersedeCommand(LogDedentMixin, GitUpstreamCommand):
|
|||||||
help='Search change ids values in <upstream-branch> branch '
|
help='Search change ids values in <upstream-branch> branch '
|
||||||
'(default: %(default)s)')
|
'(default: %(default)s)')
|
||||||
|
|
||||||
def run(self, args):
|
def execute(self):
|
||||||
|
|
||||||
supersede = Supersede(git_object=args.commit,
|
supersede = Supersede(git_object=self.args.commit,
|
||||||
change_ids=args.change_ids,
|
change_ids=self.args.change_ids,
|
||||||
upstream_branch=args.upstream_branch,
|
upstream_branch=self.args.upstream_branch,
|
||||||
force=args.force)
|
force=self.args.force)
|
||||||
|
|
||||||
if supersede.mark():
|
if supersede.mark():
|
||||||
self.logger.notice("Supersede mark created successfully")
|
self.logger.notice("Supersede mark created successfully")
|
||||||
|
@@ -139,9 +139,7 @@ def main(argv=None):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cmd = args.cmd
|
args.cmd.run(args)
|
||||||
cmd.validate(args)
|
|
||||||
cmd.run(args)
|
|
||||||
except GitUpstreamError as e:
|
except GitUpstreamError as e:
|
||||||
logger.fatal("%s", e[0])
|
logger.fatal("%s", e[0])
|
||||||
logger.debug("Git-Upstream: %s", e[0], exc_info=e)
|
logger.debug("Git-Upstream: %s", e[0], exc_info=e)
|
||||||
|
Reference in New Issue
Block a user