1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-04-02 13:23:05 +02:00

- cleaned up the conf module (I hope I didn't break anything):

* basically get rid of the scheme and apisrv mess - instead directly use the apiurl (== <protocol>://<host>) for everything
  * some other minor cleanups here and there
  * everyone who has a scheme or apisrv entry in his ~/.oscrc will get a deprecation warning but it's still working (at some point in the future we might remove these code paths)
  * when writing a new ~/.oscrc store the apiurl in the conffile [fixes #478054]
- while being at it:
  * fixed #478054 ("osc asks for build.o.o credential even if -A<somelocalapi> is always used")
  * fixed #478052 ("osc backtrace on password entry (first startup)")
This commit is contained in:
Marcus Hüwe 2009-02-23 23:29:32 +00:00
parent b43d67093b
commit caae2bc8a1
4 changed files with 54 additions and 50 deletions

View File

@ -52,7 +52,7 @@ if hostarch == 'i686': # FIXME
class Buildinfo: class Buildinfo:
"""represent the contents of a buildinfo file""" """represent the contents of a buildinfo file"""
def __init__(self, filename, apiurl=config['apiurl']): def __init__(self, filename, apiurl):
try: try:
tree = ET.parse(filename) tree = ET.parse(filename)
@ -69,13 +69,8 @@ class Buildinfo:
sys.stderr.write(error + '\n') sys.stderr.write(error + '\n')
sys.exit(1) sys.exit(1)
if apiurl.startswith('https://') or apiurl.startswith('http://'): if not (apiurl.startswith('https://') or apiurl.startswith('http://')):
import urlparse raise urllib2.URLError('invalid protocol for the apiurl: \'%s\'' % apiurl)
scheme, netloc, path = urlparse.urlsplit(apiurl)[0:3]
apisrv = netloc+path
else:
print >>sys.stderr, 'invalid protocol for the apiurl: \'%s\'' % apiurl
sys.exit(1)
# are we building .rpm or .deb? # are we building .rpm or .deb?
# need the right suffix for downloading # need the right suffix for downloading
@ -89,7 +84,6 @@ class Buildinfo:
self.buildarch = root.find('arch').text self.buildarch = root.find('arch').text
self.downloadurl = root.get('downloadurl') self.downloadurl = root.get('downloadurl')
self.apiurl = apisrv
self.debuginfo = 0 self.debuginfo = 0
if root.find('debuginfo') != None: if root.find('debuginfo') != None:
try: try:
@ -102,8 +96,7 @@ class Buildinfo:
p = Pac(node, p = Pac(node,
self.buildarch, # buildarch is used only for the URL to access the full tree... self.buildarch, # buildarch is used only for the URL to access the full tree...
self.pacsuffix, self.pacsuffix,
scheme, apiurl)
apisrv)
self.deps.append(p) self.deps.append(p)
@ -131,9 +124,7 @@ class Pac:
We build a map that's later used to fill our URL templates We build a map that's later used to fill our URL templates
""" """
def __init__(self, node, def __init__(self, node, buildarch, pacsuffix, apiurl):
buildarch, pacsuffix,
scheme=config['scheme'], apisrv=config['apisrv']):
self.mp = {} self.mp = {}
for i in ['name', 'package', for i in ['name', 'package',
@ -156,8 +147,7 @@ class Pac:
"buildinfo for package %s/%s/%s is incomplete" "buildinfo for package %s/%s/%s is incomplete"
% (self.mp['name'], self.mp['arch'], self.mp['version'])) % (self.mp['name'], self.mp['arch'], self.mp['version']))
self.mp['scheme'] = scheme self.mp['apiurl'] = apiurl
self.mp['apisrv'] = apisrv
self.filename = '%(name)s-%(version)s-%(release)s.%(arch)s.%(pacsuffix)s' % self.mp self.filename = '%(name)s-%(version)s-%(release)s.%(arch)s.%(pacsuffix)s' % self.mp
@ -395,7 +385,7 @@ def main(opts, argv):
# OBS 1.5 and before has no downloadurl defined in buildinfo # OBS 1.5 and before has no downloadurl defined in buildinfo
if bi.downloadurl: if bi.downloadurl:
urllist.append( bi.downloadurl + '/%(project)s/%(repository)s/%(arch)s/%(filename)s' ) urllist.append( bi.downloadurl + '/%(project)s/%(repository)s/%(arch)s/%(filename)s' )
urllist.append( '%(scheme)s://%(apisrv)s/build/%(project)s/%(repository)s/%(buildarch)s/%(repopackage)s/%(repofilename)s' ) urllist.append( '%(apiurl)s/build/%(project)s/%(repository)s/%(buildarch)s/%(repopackage)s/%(repofilename)s' )
fetcher = Fetcher(cachedir = config['packagecachedir'], fetcher = Fetcher(cachedir = config['packagecachedir'],
urllist = urllist, urllist = urllist,

View File

@ -53,7 +53,7 @@ class Osc(cmdln.Cmdln):
help='debug HTTP traffic') help='debug HTTP traffic')
optparser.add_option('-d', '--debug', action='store_true', optparser.add_option('-d', '--debug', action='store_true',
help='print info useful for debugging') help='print info useful for debugging')
optparser.add_option('-A', '--apisrv', dest='apisrv', optparser.add_option('-A', '--apiurl', dest='apiurl',
metavar='URL', metavar='URL',
help='specify URL to access API server at') help='specify URL to access API server at')
optparser.add_option('-c', '--config', dest='conffile', optparser.add_option('-c', '--config', dest='conffile',
@ -66,7 +66,7 @@ class Osc(cmdln.Cmdln):
"""merge commandline options into the config""" """merge commandline options into the config"""
try: try:
conf.get_config(override_conffile = self.options.conffile, conf.get_config(override_conffile = self.options.conffile,
override_apisrv = self.options.apisrv, override_apiurl = self.options.apiurl,
override_debug = self.options.debug, override_debug = self.options.debug,
override_http_debug = self.options.http_debug, override_http_debug = self.options.http_debug,
override_traceback = self.options.traceback, override_traceback = self.options.traceback,
@ -78,6 +78,8 @@ class Osc(cmdln.Cmdln):
config = {} config = {}
config['user'] = raw_input('Username: ') config['user'] = raw_input('Username: ')
config['pass'] = getpass.getpass() config['pass'] = getpass.getpass()
if self.options.apiurl:
config['apiurl'] = self.options.apiurl
conf.write_initial_config(e.file, config) conf.write_initial_config(e.file, config)
print >>sys.stderr, 'done' print >>sys.stderr, 'done'

View File

@ -39,8 +39,7 @@ from osc import oscerr
# it will hold the parsed configuration # it will hold the parsed configuration
config = { } config = { }
DEFAULTS = { 'apisrv': 'https://api.opensuse.org/', DEFAULTS = { 'apiurl': 'https://api.opensuse.org',
'scheme': 'https',
'user': 'your_username', 'user': 'your_username',
'pass': 'your_password', 'pass': 'your_password',
'packagecachedir': '/var/tmp/osbuild-packagecache', 'packagecachedir': '/var/tmp/osbuild-packagecache',
@ -71,9 +70,9 @@ boolean_opts = ['debug', 'do_package_tracking', 'http_debug', 'post_mortem', 'tr
new_conf_template = """ new_conf_template = """
[general] [general]
# URL to access API server, e.g. %(apisrv)s # URL to access API server, e.g. %(apiurl)s
# you also need a section [%(apisrv)s] with the credentials # you also need a section [%(apiurl)s] with the credentials
#apisrv = %(apisrv)s apiurl = %(apiurl)s
# Downloaded packages are cached here. Must be writable by you. # Downloaded packages are cached here. Must be writable by you.
#packagecachedir = %(packagecachedir)s #packagecachedir = %(packagecachedir)s
@ -108,7 +107,7 @@ new_conf_template = """
# print call traces in case of errors # print call traces in case of errors
#traceback = 1 #traceback = 1
[%(apisrv)s] [%(apiurl)s]
user = %(user)s user = %(user)s
pass = %(pass)s pass = %(pass)s
# additional headers to pass to a request, e.g. for special authentication # additional headers to pass to a request, e.g. for special authentication
@ -145,6 +144,9 @@ def parse_apisrv_url(scheme, apisrv):
else: else:
return scheme, apisrv return scheme, apisrv
def urljoin(scheme, apisrv):
return '://'.join([scheme, apisrv])
def get_apiurl_api_host_options(apiurl): def get_apiurl_api_host_options(apiurl):
""" """
Returns all apihost specific options for the given apiurl, None if Returns all apihost specific options for the given apiurl, None if
@ -153,8 +155,13 @@ def get_apiurl_api_host_options(apiurl):
# FIXME: in A Better World (tm) there was a config object which # FIXME: in A Better World (tm) there was a config object which
# knows this instead of having to extract it from a url where it # knows this instead of having to extract it from a url where it
# had been mingled into before. But this works fine for now. # had been mingled into before. But this works fine for now.
scheme, apisrv = parse_apisrv_url(None, apiurl)
return config['api_host_options'][apisrv] apiurl = urljoin(*parse_apisrv_url(None, apiurl))
try:
return config['api_host_options'][apiurl]
except KeyError:
raise oscerr.ConfigMissingApiurl('missing credentials for apiurl: \'%s\'' % apiurl,
'', apiurl)
def get_apiurl_usr(apiurl): def get_apiurl_usr(apiurl):
""" """
@ -269,7 +276,7 @@ def write_initial_config(conffile, entries, custom_template = ''):
def get_config(override_conffile = None, def get_config(override_conffile = None,
override_apisrv = None, override_apiurl = None,
override_debug = None, override_debug = None,
override_http_debug = None, override_http_debug = None,
override_traceback = None, override_traceback = None,
@ -299,8 +306,14 @@ def get_config(override_conffile = None,
config = dict(cp.items('general', raw=1)) config = dict(cp.items('general', raw=1))
config['scheme'], config['apisrv'] = \ # backward compatibility
parse_apisrv_url(config['scheme'], config['apisrv']) if config.has_key('apisrv'):
scheme = config.get('scheme', 'https')
config['apiurl'] = urljoin(scheme, config['apisrv'])
if config.has_key('apisrv') or config.has_key('scheme'):
import warnings
warnings.warn("Use of the 'scheme' or 'apisrv' config option is deprecated!",
DeprecationWarning)
for i in boolean_opts: for i in boolean_opts:
try: try:
@ -316,7 +329,7 @@ def get_config(override_conffile = None,
config['extra-pkgs'] = None config['extra-pkgs'] = None
# collect the usernames, passwords and additional options for each api host # collect the usernames, passwords and additional options for each api host
api_host_options = { } api_host_options = {}
# Regexp to split extra http headers into a dictionary # Regexp to split extra http headers into a dictionary
# the text to be matched looks essentially looks this: # the text to be matched looks essentially looks this:
@ -326,8 +339,10 @@ def get_config(override_conffile = None,
http_header_regexp = re.compile(r"\s*(.*?)\s*:\s*(.*?)\s*(?:,\s*|\Z)") http_header_regexp = re.compile(r"\s*(.*?)\s*:\s*(.*?)\s*(?:,\s*|\Z)")
for url in [ x for x in cp.sections() if x != 'general' ]: for url in [ x for x in cp.sections() if x != 'general' ]:
dummy, host = \ # backward compatiblity
parse_apisrv_url(config['scheme'], url) scheme, host = \
parse_apisrv_url(config.get('scheme', 'https'), url)
apiurl = urljoin(scheme, host)
#FIXME: this could actually be the ideal spot to take defaults #FIXME: this could actually be the ideal spot to take defaults
#from the general section. #from the general section.
user = cp.get(url, 'user') user = cp.get(url, 'user')
@ -339,9 +354,9 @@ def get_config(override_conffile = None,
else: else:
http_headers = [] http_headers = []
api_host_options[host] = { 'user': user, api_host_options[apiurl] = { 'user': user,
'pass': password, 'pass': password,
'http_headers': http_headers}; 'http_headers': http_headers};
# add the auth data we collected to the config dict # add the auth data we collected to the config dict
config['api_host_options'] = api_host_options config['api_host_options'] = api_host_options
@ -349,27 +364,24 @@ def get_config(override_conffile = None,
# override values which we were called with # override values which we were called with
if override_debug: if override_debug:
config['debug'] = override_debug config['debug'] = override_debug
if override_http_debug: if override_http_debug:
config['http_debug'] = override_http_debug config['http_debug'] = override_http_debug
if override_traceback: if override_traceback:
config['traceback'] = override_traceback config['traceback'] = override_traceback
if override_post_mortem: if override_post_mortem:
config['post_mortem'] = override_post_mortem config['post_mortem'] = override_post_mortem
if override_apisrv: if override_apiurl:
config['scheme'], config['apisrv'] = \ config['apiurl'] = override_apiurl
parse_apisrv_url(config['scheme'], override_apisrv)
# to make the mess complete, set up the more convenient api url which we'll rather use
config['apiurl'] = config['scheme'] + '://' + config['apisrv']
# XXX unless config['user'] goes away (and is replaced with a handy function, or # XXX unless config['user'] goes away (and is replaced with a handy function, or
# config becomes an object, even better), set the global 'user' here as well, # config becomes an object, even better), set the global 'user' here as well,
# provided that there _are_ credentials for the chosen apisrv: # provided that there _are_ credentials for the chosen apiurl:
if config['apisrv'] in config['api_host_options'].keys(): try:
config['user'] = config['api_host_options'][config['apisrv']]['user'] config['user'] = get_apiurl_api_host_options(config['apiurl'])
else: except oscerr.ConfigMissingApiurl, e:
raise oscerr.ConfigMissingApiurl(config_missing_apiurl_text % config['apisrv'], e.msg = config_missing_apiurl_text % config['apiurl']
conffile, config['apiurl']) e.file = conffile
raise e
# finally, initialize urllib2 for to use the credentials for Basic Authentication # finally, initialize urllib2 for to use the credentials for Basic Authentication
init_basicauth(config) init_basicauth(config)

View File

@ -2936,7 +2936,7 @@ def store_read_apiurl(dir):
try: try:
apiurl = open(fname).readlines()[0].strip() apiurl = open(fname).readlines()[0].strip()
except: except:
apiurl = conf.config['scheme'] + '://' + conf.config['apisrv'] apiurl = conf.config['apiurl']
#store_write_apiurl(dir, apiurl) #store_write_apiurl(dir, apiurl)
return apiurl return apiurl