Skip to content

Commit f0d78ec

Browse files
committed
bin: Grab default cli url from bugzillarc
bugzillarc can now have a value like: [DEFAULT] url=X And 'X' will be used as the default URL from the bugzilla command line tool Resolves: #27
1 parent f3fd8a5 commit f0d78ec

2 files changed

Lines changed: 55 additions & 24 deletions

File tree

bin/bugzilla

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import requests.exceptions
3535

3636
import bugzilla
3737

38-
default_bz = 'https://bugzilla.redhat.com/xmlrpc.cgi'
38+
DEFAULT_BZ = 'https://bugzilla.redhat.com/xmlrpc.cgi'
3939

4040
_is_unittest = bool(os.getenv("__BUGZILLA_UNITTEST"))
4141
_is_unittest_debug = bool(os.getenv("__BUGZILLA_UNITTEST_DEBUG"))
@@ -91,6 +91,20 @@ def open_without_clobber(name, *args):
9191
return fobj
9292

9393

94+
def get_default_url():
95+
"""
96+
Grab a default URL from bugzillarc [DEFAULT] url=X
97+
"""
98+
from bugzilla.base import _open_bugzillarc
99+
cfg = _open_bugzillarc()
100+
if cfg:
101+
cfgurl = cfg.defaults().get("url", None)
102+
if cfgurl is not None:
103+
log.debug("bugzillarc: found cli url=%s", cfgurl)
104+
return cfgurl
105+
return DEFAULT_BZ
106+
107+
94108
##################
95109
# Option parsing #
96110
##################
@@ -99,9 +113,11 @@ def _setup_root_parser():
99113
epilog = 'Try "bugzilla COMMAND --help" for command-specific help.'
100114
p = argparse.ArgumentParser(epilog=epilog)
101115

116+
default_url = get_default_url()
117+
102118
# General bugzilla connection options
103-
p.add_argument('--bugzilla', default=default_bz,
104-
help="bugzilla XMLRPC URI. default: %s" % default_bz)
119+
p.add_argument('--bugzilla', default=default_url,
120+
help="bugzilla XMLRPC URI. default: %s" % default_url)
105121
p.add_argument("--nosslverify", dest="sslverify",
106122
action="store_false", default=True,
107123
help="Don't error on invalid bugzilla SSL certificate")

bugzilla/base.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,28 @@ def _build_cookiejar(cookiefile):
109109
cookiefile)
110110

111111

112+
_default_configpaths = [
113+
'/etc/bugzillarc',
114+
'~/.bugzillarc',
115+
'~/.config/python-bugzilla/bugzillarc',
116+
]
117+
118+
119+
def _open_bugzillarc(configpaths=-1):
120+
if configpaths == -1:
121+
configpaths = _default_configpaths[:]
122+
123+
configpaths = [os.path.expanduser(p) for p in
124+
Bugzilla._listify(configpaths)]
125+
cfg = SafeConfigParser()
126+
read_files = cfg.read(configpaths)
127+
if not read_files:
128+
return
129+
130+
log.info("Found bugzillarc files: %s", read_files)
131+
return cfg
132+
133+
112134
class _FieldAlias(object):
113135
"""
114136
Track API attribute names that differ from what we expose in users.
@@ -220,6 +242,15 @@ def fix_url(url):
220242
url = url + '/xmlrpc.cgi'
221243
return url
222244

245+
@staticmethod
246+
def _listify(val):
247+
if val is None:
248+
return val
249+
if isinstance(val, list):
250+
return val
251+
return [val]
252+
253+
223254
def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
224255
sslverify=True, tokenfile=-1, use_creds=True, api_key=None):
225256
"""
@@ -267,8 +298,7 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
267298
self._field_aliases = []
268299
self._init_field_aliases()
269300

270-
self.configpath = ['/etc/bugzillarc', '~/.bugzillarc',
271-
'~/.config/python-bugzilla/bugzillarc']
301+
self.configpath = _default_configpaths[:]
272302
if not use_creds:
273303
cookiefile = None
274304
tokenfile = None
@@ -360,13 +390,6 @@ def _check_version(self, major, minor):
360390
return True
361391
return False
362392

363-
def _listify(self, val):
364-
if val is None:
365-
return val
366-
if isinstance(val, list):
367-
return val
368-
return [val]
369-
370393
def _product_id_to_name(self, productid):
371394
'''Convert a product ID (int) to a product name (str).'''
372395
for p in self.products:
@@ -448,24 +471,16 @@ def readconfig(self, configpath=None):
448471
api_key = key
449472
450473
The file can have multiple sections for different bugzilla instances.
451-
You can also use the [DEFAULT] section to set defaults that apply to
452-
any site without a specific section of its own.
474+
A 'url' field in the [DEFAULT] section can be used to set a default
475+
URL for the bugzilla command line tool.
453476
454477
Be sure to set appropriate permissions on bugzillarc if you choose to
455478
store your password in it!
456479
"""
457-
if not configpath:
458-
configpath = self.configpath
459-
460-
configpath = [os.path.expanduser(p) for p in
461-
self._listify(configpath)]
462-
cfg = SafeConfigParser()
463-
read_files = cfg.read(configpath)
464-
if not read_files:
480+
cfg = _open_bugzillarc(configpath or self.configpath)
481+
if not cfg:
465482
return
466483

467-
log.info("Found bugzillarc files: %s", read_files)
468-
469484
section = ""
470485
log.debug("bugzillarc: Searching for config section matching %s",
471486
self.url)

0 commit comments

Comments
 (0)