Reported bug with utf-8 strings
[be.git] / becommands / target.py
1 # Copyright (C) 2005-2009 Aaron Bentley and Panometrics, Inc.
2 #                         Chris Ball <cjb@laptop.org>
3 #                         Gianluca Montecchi <gian@grys.it>
4 #                         Marien Zwart <marienz@gentoo.org>
5 #                         Thomas Gerigk <tgerigk@gmx.de>
6 #                         W. Trevor King <wking@drexel.edu>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 """Show or change a bug's target for fixing"""
22 from libbe import cmdutil, bugdir
23 __desc__ = __doc__
24
25 def execute(args, manipulate_encodings=True):
26     """
27     >>> import os
28     >>> bd = bugdir.SimpleBugDir()
29     >>> os.chdir(bd.root)
30     >>> execute(["a"], manipulate_encodings=False)
31     No target assigned.
32     >>> execute(["a", "tomorrow"], manipulate_encodings=False)
33     >>> execute(["a"], manipulate_encodings=False)
34     tomorrow
35     >>> execute(["--list"], manipulate_encodings=False)
36     tomorrow
37     >>> execute(["a", "none"], manipulate_encodings=False)
38     >>> execute(["a"], manipulate_encodings=False)
39     No target assigned.
40     >>> bd.cleanup()
41     """
42     parser = get_parser()
43     options, args = parser.parse_args(args)
44     cmdutil.default_complete(options, args, parser,
45                              bugid_args={0: lambda bug : bug.active==True})
46                              
47     if len(args) not in (1, 2):
48         if not (options.list == True and len(args) == 0):
49             raise cmdutil.UsageError
50     bd = bugdir.BugDir(from_disk=True,
51                        manipulate_encodings=manipulate_encodings)
52     if options.list:
53         ts = set([bd.bug_from_uuid(bug).target for bug in bd.list_uuids()])
54         for target in sorted(ts):
55             if target and isinstance(target,str):
56                 print target
57         return
58     bug = cmdutil.bug_from_shortname(bd, args[0])
59     if len(args) == 1:
60         if bug.target is None:
61             print "No target assigned."
62         else:
63             print bug.target
64     else:
65         assert len(args) == 2
66         if args[1] == "none":
67             bug.target = None
68         else:
69             bug.target = args[1]
70
71 def get_parser():
72     parser = cmdutil.CmdOptionParser("be target BUG-ID [TARGET]\nor:    be target --list")
73     parser.add_option("-l", "--list", action="store_true", dest="list",
74                       help="List all available targets and exit")
75     return parser
76
77 longhelp="""
78 Show or change a bug's target for fixing.  
79
80 If no target is specified, the current value is printed.  If a target
81 is specified, it will be assigned to the bug.
82
83 Targets are freeform; any text may be specified.  They will generally be
84 milestone names or release numbers.
85
86 The value "none" can be used to unset the target.
87
88 In the alternative `be target --list` form print a list of all
89 currently specified targets.  Note that bug status
90 (i.e. opened/closed) is ignored.  If you want to list all bugs
91 matching a current target, see `be list --target TARGET'.
92 """
93
94 def help():
95     return get_parser().help_str() + longhelp