# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-"""Assorted bug target manipulations and queries"""
-from libbe import cmdutil, bugdir
-from becommands import depend
-__desc__ = __doc__
-def execute(args, manipulate_encodings=True, restrict_file_access=False,
- dir="."):
- """
+import libbe
+import libbe.command
+import libbe.command.util
+import libbe.command.depend
+
+
+
+class Target (libbe.command.Command):
+ """Assorted bug target manipulations and queries
+
>>> import os, StringIO, sys
- >>> bd = bugdir.SimpleBugDir()
- >>> os.chdir(bd.root)
- >>> execute(["a"], manipulate_encodings=False)
+ >>> import libbe.bugdir
+ >>> bd = libbe.bugdir.SimpleBugDir(memory=False)
+ >>> cmd = Target()
+ >>> cmd._storage = bd.storage
+ >>> cmd._setup_io = lambda i_enc,o_enc : None
+ >>> cmd.stdout = sys.stdout
+
+ >>> ret = cmd.run(args=['/a'])
No target assigned.
- >>> execute(["a", "tomorrow"], manipulate_encodings=False)
- >>> execute(["a"], manipulate_encodings=False)
+ >>> ret = cmd.run(args=['/a', 'tomorrow'])
+ >>> ret = cmd.run(args=['/a'])
tomorrow
- >>> orig_stdout = sys.stdout
- >>> tmp_stdout = StringIO.StringIO()
- >>> sys.stdout = tmp_stdout
- >>> execute(["--resolve", "tomorrow"], manipulate_encodings=False)
- >>> sys.stdout = orig_stdout
- >>> output = tmp_stdout.getvalue().strip()
+ >>> cmd.stdout = StringIO.StringIO()
+ >>> ret = cmd.run({'resolve':True}, ['tomorrow'])
+ >>> output = cmd.stdout.getvalue().strip()
>>> target = bd.bug_from_uuid(output)
>>> print target.summary
tomorrow
>>> print target.severity
target
- >>> execute(["a", "none"], manipulate_encodings=False)
- >>> execute(["a"], manipulate_encodings=False)
+ >>> cmd.stdout = sys.stdout
+ >>> ret = cmd.run(args=['/a', 'none'])
+ >>> ret = cmd.run(args=['/a'])
No target assigned.
>>> bd.cleanup()
"""
- parser = get_parser()
- options, args = parser.parse_args(args)
- cmdutil.default_complete(options, args, parser,
- bugid_args={0: lambda bug : bug.active==True})
-
- if (options.resolve == False and len(args) not in (1, 2)) \
- or (options.resolve == True and len(args) not in (0, 1)):
- raise cmdutil.UsageError('Incorrect number of arguments.')
- bd = bugdir.BugDir(from_disk=True,
- manipulate_encodings=manipulate_encodings,
- root=dir)
- if options.resolve == True:
- if len(args) == 0:
- summary = None
- else:
- summary = args[0]
- bug = bug_from_target_summary(bd, summary)
- if bug == None:
- print 'No target assigned.'
- else:
- print bug.uuid
- return
- bug = cmdutil.bug_from_id(bd, args[0])
- if len(args) == 1:
- target = bug_target(bd, bug)
- if target is None:
- print "No target assigned."
+ name = 'target'
+
+ def __init__(self, *args, **kwargs):
+ libbe.command.Command.__init__(self, *args, **kwargs)
+ self.options.extend([
+ libbe.command.Option(name='resolve', short_name='r',
+ help="Print the UUID for the target bug whose summary "
+ "matches TARGET. If TARGET is not given, print the UUID "
+ "of the current bugdir target."),
+ ])
+ self.args.extend([
+ libbe.command.Argument(
+ name='id', metavar='BUG-ID', default=None,
+ optional=True,
+ completion_callback=libbe.command.util.complete_bug_id),
+ libbe.command.Argument(
+ name='target', metavar='TARGET', default=None,
+ optional=True,
+ completion_callback=complete_target),
+ ])
+
+ def _run(self, **params):
+ if params['resolve'] == False:
+ if params['id'] == None:
+ raise libbe.command.UserError('Please specify a bug id.')
else:
- print target.summary
- else:
- if args[1] == "none":
- target = remove_target(bd, bug)
+ if params['target'] != None:
+ raise libbe.command.UserError('Too many arguments')
+ params['target'] = params.pop('id')
+ bugdir = self._get_bugdir()
+ if params['resolve'] == True:
+ bug = bug_from_target_summary(bugdir, params['target'])
+ if bug == None:
+ print >> self.stdout, 'No target assigned.'
+ else:
+ print >> self.stdout, bug.uuid
+ return 0
+ bug,dummy_comment = libbe.command.util.bug_comment_from_user_id(
+ bugdir, params['id'])
+ if params['target'] == None:
+ target = bug_target(bugdir, bug)
+ if target == None:
+ print >> self.stdout, 'No target assigned.'
+ else:
+ print >> self.stdout, target.summary
else:
- target = add_target(bd, bug, args[1])
+ if params['target'] == 'none':
+ target = remove_target(bugdir, bug)
+ else:
+ target = add_target(bugdir, bug, params['target'])
+ return 0
-def get_parser():
- parser = cmdutil.CmdOptionParser("be target BUG-ID [TARGET]\nor: be target --resolve [TARGET]")
- parser.add_option("-r", "--resolve", action="store_true", dest="resolve",
- help="Print the UUID for the target bug whose summary matches TARGET. If TARGET is not given, print the UUID of the current bugdir target. If that is not set, don't print anything.",
- default=False)
- return parser
+ def _usage(self):
+ return 'usage: be %(name)s BUG-ID [TARGET]\nor: be %(name)s --resolve [TARGET]' \
+ % vars(self)
-longhelp="""
+ def _long_help(self):
+ return """
Assorted bug target manipulations and queries.
If no target is specified, the bug's current target is printed. If
$ be set target $(be target --resolve SUMMARY)
"""
-def help():
- return get_parser().help_str() + longhelp
-
def bug_from_target_summary(bugdir, summary=None):
if summary == None:
if bugdir.target == None:
if bug.severity == 'target':
return bug
matched = []
- for blocked in depend.get_blocks(bugdir, bug):
+ for blocked in libbe.command.depend.get_blocks(bugdir, bug):
if blocked.severity == 'target':
matched.append(blocked)
if len(matched) == 0:
def remove_target(bugdir, bug):
target = bug_target(bugdir, bug)
- depend.remove_block(target, bug)
+ libbe.command.depend.remove_block(target, bug)
return target
def add_target(bugdir, bug, summary):
if target == None:
target = bugdir.new_bug(summary=summary)
target.severity = 'target'
- depend.add_block(target, bug)
+ libbe.command.depend.add_block(target, bug)
return target
+
+def targets(bugdir):
+ bugdir.load_all_bugs()
+ for bug in bugdir:
+ if bug.severity == 'target':
+ yield bug.summary
+
+def complete_target(command, argument, fragment=None):
+ """
+ List possible command completions for fragment.
+
+ argument argument is not used.
+ """
+ return targets(command._get_bugdir())