| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- python -*- |
| 3 |
# querybts - Examine the state of a debbugs server |
| 4 |
# Written by Chris Lawrence <[email protected]> |
| 5 |
# (C) 1999-2008 Chris Lawrence |
| 6 |
# Copyright (C) 2008-2023 Sandro Tosi <[email protected]> |
| 7 |
# |
| 8 |
# This program is freely distributable per the following license: |
| 9 |
# |
| 10 |
# Permission to use, copy, modify, and distribute this software and its |
| 11 |
# documentation for any purpose and without fee is hereby granted, |
| 12 |
# provided that the above copyright notice appears in all copies and that |
| 13 |
# both that copyright notice and this permission notice appear in |
| 14 |
# supporting documentation. |
| 15 |
# |
| 16 |
# I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL |
| 17 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I |
| 18 |
# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY |
| 19 |
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 21 |
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 22 |
# SOFTWARE. |
| 23 |
|
| 24 |
import sys |
| 25 |
import optparse |
| 26 |
import re |
| 27 |
|
| 28 |
from reportbug import utils |
| 29 |
from reportbug.exceptions import ( |
| 30 |
UINotImportable, |
| 31 |
NoPackage, NoBugs, NoReport, NoNetwork, |
| 32 |
) |
| 33 |
from reportbug import debbugs |
| 34 |
from reportbug import urlutils |
| 35 |
|
| 36 |
from reportbug.ui import AVAILABLE_UIS |
| 37 |
|
| 38 |
import reportbug.ui.text_ui as ui # noqa: F401 |
| 39 |
|
| 40 |
from reportbug import VERSION_NUMBER |
| 41 |
|
| 42 |
VERSION = "querybts %s" % VERSION_NUMBER |
| 43 |
|
| 44 |
ui_mode = 'text' |
| 45 |
|
| 46 |
|
| 47 |
def main(): |
| 48 |
# default values for cli options |
| 49 |
defaults = dict(system='debian', archived=False, |
| 50 |
http_proxy='', interface='text', |
| 51 |
use_browser=False, source=False, |
| 52 |
mirrors=None, mbox=False, buglist=False, |
| 53 |
mbox_reader_cmd=None) |
| 54 |
|
| 55 |
# parse config file to update default options |
| 56 |
args = utils.parse_config_files() |
| 57 |
for option, arg in list(args.items()): |
| 58 |
if option in ('system', 'mirrors', 'interface', 'http_proxy', |
| 59 |
'mbox_reader_cmd'): |
| 60 |
defaults[option] = arg |
| 61 |
|
| 62 |
# define the cli options parser |
| 63 |
parser = optparse.OptionParser( |
| 64 |
description='%prog - Examine the state of a debbugs server.', |
| 65 |
usage='%prog [options] {<package> | <report number> [report2] ...}', |
| 66 |
version=VERSION) |
| 67 |
|
| 68 |
# set the defaults |
| 69 |
parser.set_defaults(**defaults) |
| 70 |
|
| 71 |
# add the cli options |
| 72 |
parser.add_option('-A', '--archive', action='store_true', dest='archived', |
| 73 |
help='Browse archived bugs.') |
| 74 |
parser.add_option('-b', '--buglist', action='store_true', dest='buglist', |
| 75 |
help='Display a bugs list for the given package.') |
| 76 |
parser.add_option('-B', '--bts', dest='system', |
| 77 |
help='Specify an alternate debbugs BTS; available values: %s ' % |
| 78 |
', '.join([k for k in debbugs.SYSTEMS if debbugs.SYSTEMS[k].get('btsroot')])) |
| 79 |
parser.add_option('-m', '--mbox', action='store_true', dest='mbox', |
| 80 |
help='generate mbox') |
| 81 |
parser.add_option('--proxy', '--http_proxy', dest='http_proxy', |
| 82 |
help='define the proxy to use') |
| 83 |
parser.add_option('-s', '--source', action='store_true', dest='source', |
| 84 |
help='Query for source packages rather than binary packages.') |
| 85 |
parser.add_option('--timeout', type="int", dest='timeout', default=60, |
| 86 |
help='Specify the network timeout, in seconds [default: %default].') |
| 87 |
parser.add_option('-u', '--ui', '--interface', dest='interface', |
| 88 |
help='Specify the user interface to use; available values: %s ' % ', '.join(list(AVAILABLE_UIS.keys()))) |
| 89 |
parser.add_option('-w', '--web', action='store_true', dest='use_browser', |
| 90 |
help='Use a web browser instead of the internal interface.') |
| 91 |
parser.add_option('--mbox-reader-cmd', dest='mbox_reader_cmd', |
| 92 |
help="Specify the program to open the reports mbox.") |
| 93 |
parser.add_option('--latest-first', action='store_true', dest='latest_first', default=False, |
| 94 |
help='Order bugs to show the latest first') |
| 95 |
|
| 96 |
# parse cli options |
| 97 |
(options, args) = parser.parse_args() |
| 98 |
|
| 99 |
# check options for consistency |
| 100 |
|
| 101 |
# the gtk2 ui has been renamed to gtk, stay compatible |
| 102 |
if options.interface and options.interface == 'gtk2': |
| 103 |
options.interface = 'gtk' |
| 104 |
|
| 105 |
# interface must be one of those supported |
| 106 |
if options.interface not in AVAILABLE_UIS: |
| 107 |
parser.error('Allowed arguments to --ui: \n' |
| 108 |
+ '\n'.join([' %s (%s)' % (key, value) for key, value in AVAILABLE_UIS.items()])) |
| 109 |
else: |
| 110 |
# prepare the UI and import it |
| 111 |
global ui, ui_mode |
| 112 |
iface = '%s_ui' % options.interface |
| 113 |
try: |
| 114 |
lib_package = __import__('reportbug.ui', fromlist=[iface]) |
| 115 |
ui = getattr(lib_package, iface) |
| 116 |
ui_mode = options.interface |
| 117 |
except UINotImportable as msg: |
| 118 |
ui.long_message('*** Unable to import %s interface: %s ' |
| 119 |
'Falling back to %s interface.\n', |
| 120 |
options.interface, msg, ui_mode) |
| 121 |
print() |
| 122 |
|
| 123 |
# initialize the selected UI |
| 124 |
ui.initialize() |
| 125 |
|
| 126 |
# system must be one of those supported |
| 127 |
if options.system not in [k for k in debbugs.SYSTEMS if debbugs.SYSTEMS[k].get('btsroot')]: |
| 128 |
parser.error('Allowed arguments to --bts: \n' |
| 129 |
+ '\n'.join([' %s (%s)' % (k, debbugs.SYSTEMS[k]['name']) |
| 130 |
for k in debbugs.SYSTEMS if debbugs.SYSTEMS[k].get('btsroot')])) |
| 131 |
|
| 132 |
# there should be at least one argument |
| 133 |
if len(args) == 0: |
| 134 |
parser.error('Please specify a package or one or more bug numbers. ' |
| 135 |
'Note: most shells consider # a comment character; however, a ' |
| 136 |
'leading # is not needed to specify a bug by number.') |
| 137 |
|
| 138 |
if options.use_browser: |
| 139 |
if options.buglist: |
| 140 |
parser.error("--web and --buglist can't work together, exiting.") |
| 141 |
|
| 142 |
package = args[0] |
| 143 |
|
| 144 |
m = re.match(r'^#?(\d+)$', package) |
| 145 |
if m: |
| 146 |
num = int(m.group(1)) |
| 147 |
url = debbugs.get_report_url(options.system, num, options.mirrors, options.archived) |
| 148 |
else: |
| 149 |
url = debbugs.get_package_url(options.system, package, options.mirrors, options.source, options.archived) |
| 150 |
|
| 151 |
# launch the browser and exit |
| 152 |
urlutils.launch_browser(url) |
| 153 |
sys.exit() |
| 154 |
|
| 155 |
if options.mbox: |
| 156 |
if options.buglist: |
| 157 |
parser.error("--mbox and --buglist can't work together, exiting.") |
| 158 |
|
| 159 |
for bugnum in args: |
| 160 |
package = bugnum |
| 161 |
m = re.match(r'^#?(\d+)$', bugnum) |
| 162 |
if not m: |
| 163 |
mboxbuglist = ui.handle_bts_query(package, options.system, options.timeout, options.mirrors, |
| 164 |
options.http_proxy, queryonly=True, title=VERSION, |
| 165 |
archived=options.archived, source=options.source, mbox=options.mbox, |
| 166 |
latest_first=options.latest_first) |
| 167 |
for num in mboxbuglist: |
| 168 |
url = debbugs.get_report_url(options.system, num, options.archived, mbox=True) |
| 169 |
try: |
| 170 |
report = urlutils.open_url(url, options.http_proxy, timeout=options.timeout) |
| 171 |
sys.stdout.write(report) |
| 172 |
except NoNetwork as ex: |
| 173 |
print("Error while accessing mbox report (%s)." % ex, file=sys.stderr) |
| 174 |
else: |
| 175 |
num = int(m.group(1)) |
| 176 |
url = debbugs.get_report_url(options.system, num, options.archived, mbox=True) |
| 177 |
try: |
| 178 |
report = urlutils.open_url(url, options.http_proxy, timeout=options.timeout) |
| 179 |
sys.stdout.write(report) |
| 180 |
except NoNetwork as ex: |
| 181 |
print("Error while accessing mbox report (%s)." % ex, file=sys.stderr) |
| 182 |
sys.exit(1) |
| 183 |
return |
| 184 |
|
| 185 |
reportre = re.compile(r'^#?(\d+)$') |
| 186 |
try: |
| 187 |
if len(args) > 1: |
| 188 |
bugs = [] |
| 189 |
for report in args: |
| 190 |
match = reportre.match(report) |
| 191 |
if match: |
| 192 |
bugs.append(int(match.group(1))) |
| 193 |
package = bugs |
| 194 |
if not bugs: |
| 195 |
raise ui.NoBugs |
| 196 |
else: |
| 197 |
package = args[0] |
| 198 |
match = reportre.match(package) |
| 199 |
if match: |
| 200 |
report = int(match.group(1)) |
| 201 |
while 1: |
| 202 |
_ = ui.show_report(report, options.system, options.mirrors, |
| 203 |
options.http_proxy, options.timeout, |
| 204 |
queryonly=True, |
| 205 |
title=VERSION, |
| 206 |
archived=options.archived, |
| 207 |
mbox_reader_cmd=options.mbox_reader_cmd) |
| 208 |
ui.long_message('This option is not available while using querybts alone.\n') |
| 209 |
x = ui.select_options('What do you want to do now?', 'Qb', |
| 210 |
{'q': 'Exit querybts.', |
| 211 |
'b': 'Query BTS one more time.'}) |
| 212 |
if x == 'q': |
| 213 |
ui.long_message('Exiting.\n') |
| 214 |
sys.exit(0) |
| 215 |
|
| 216 |
while 1: |
| 217 |
ui.handle_bts_query(package, options.system, options.timeout, options.mirrors, options.http_proxy, |
| 218 |
queryonly=True, title=VERSION, archived=options.archived, |
| 219 |
source=options.source, buglist=options.buglist, |
| 220 |
mbox_reader_cmd=options.mbox_reader_cmd, latest_first=options.latest_first) |
| 221 |
ui.long_message('This option is not available while using querybts alone.\n') |
| 222 |
x = ui.select_options('What do you want to do now?', 'Qb', |
| 223 |
{'q': 'Exit querybts.', |
| 224 |
'b': 'Query BTS one more time.'}) |
| 225 |
if x == 'q': |
| 226 |
ui.long_message('Exiting.\n') |
| 227 |
sys.exit(0) |
| 228 |
|
| 229 |
except NoPackage: |
| 230 |
ui.long_message('Package appears not to exist in the BTS.\n') |
| 231 |
except NoBugs: |
| 232 |
ui.long_message('No bug reports found.\n') |
| 233 |
except NoReport: |
| 234 |
ui.long_message('Exiting.\n') |
| 235 |
except NoNetwork: |
| 236 |
ui.long_message('Cannot connect to network.\n') |
| 237 |
|
| 238 |
|
| 239 |
if __name__ == '__main__': |
| 240 |
try: |
| 241 |
main() |
| 242 |
except KeyboardInterrupt: |
| 243 |
print("querybts: exiting due to user interrupt.") |
| 244 |
except debbugs.Error as x: |
| 245 |
print('error accessing BTS: ' + str(x)) |
| 246 |
except SystemExit: |
| 247 |
pass |
| 248 |
except Exception: |
| 249 |
raise |