1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-09-20 09:16:16 +02:00

- fully implement accessing multiple API servers. "apisrv" in the config takes

a URL now, so the variable "scheme" which was needed in addition before
  becomes obsolete. For backward compatibility, a hostname (and scheme
  variable) are still accepted. Likewise, the auth sections in the config take
  a URL now, and a hostname:port to keep old config working.
  Furthermore, apisrv can be overridden by -A on the commandline. HTTP or HTTPS
  scheme is determined from the URL. Credentials must be configured in .oscrc.
This commit is contained in:
Dr. Peter Poeml 2007-04-25 10:24:51 +00:00
parent 36e4cc3140
commit b58b6e5a3f
2 changed files with 34 additions and 35 deletions

View File

@ -41,29 +41,24 @@ class Osc(cmdln.Cmdln):
default=conf.config['http_debug'],
help='debug HTTP traffic')
self.optparser.add_option('-A', '--apisrv', dest='apisrv',
default=conf.config['apisrv'],
metavar='host',
help='use HOST as API server')
self.optparser.add_option('-S', '--scheme', dest='scheme',
default=conf.config['scheme'],
metavar='(http|https)',
help='use this protocol (default: %s)' % conf.config['scheme'])
metavar='URL',
help='specify URL to access API server at')
(self.global_opts, self.myargs) = self.optparser.parse_args()
# XXX version is printed twice otherwise...
self.optparser.version = ''
# merge commandline options into the config
conf.config['http_debug'] = self.global_opts.http_debug
conf.config['apisrv'] = self.global_opts.apisrv
conf.config['scheme'] = self.global_opts.scheme
if self.global_opts.apisrv:
conf.config['scheme'], conf.config['apisrv'] = \
conf.parse_apisrv_url(conf.config['scheme'], self.global_opts.apisrv)
# finally, initialize urllib2 for to use the credentials for Basic Authentication
conf.init_basicauth(conf.config)
def do_init(self, subcmd, opts, *args):
"""${cmd_name}: Initialize a directory as working copy

View File

@ -17,8 +17,7 @@ After reading the config, urllib2 is initialized.
The configuration dictionary could look like this:
{'apisrv': 'api.opensuse.org',
'scheme': 'http',
{'apisrv': 'https://api.opensuse.org/',
'user': 'poeml',
'pass': 'secret',
'auth_dict': {'api.opensuse.org': {'user': 'poeml', 'pass': 'secret'},
@ -40,7 +39,7 @@ import ConfigParser
# it will hold the parsed configuration
config = { }
DEFAULTS = { 'apisrv': 'api.opensuse.org',
DEFAULTS = { 'apisrv': 'https://api.opensuse.org/',
'scheme': 'https',
'user': 'your_username',
'pass': 'your_password',
@ -68,6 +67,10 @@ boolean_opts = ['http_debug', 'do_commits']
new_conf_template = """
[general]
# URL to access API server, e.g. %(apisrv)s
# you also need a section [%(apisrv)s] with the credentials
#apisrv = %(apisrv)s
# Downloaded packages are cached here. Must be writable by you.
#packagecachedir = %(packagecachedir)s
@ -79,15 +82,8 @@ new_conf_template = """
# /srv/oscbuild/%%(repo)s-%%(arch)s
#build-root = %(build-root)s
# use this API server (hostname[:port])
# (it needs a section [%(apisrv)s] with the credentials)
#apisrv = %(apisrv)s
# use this protocol to access the API server (http or https)
#scheme = https
# show HTTP traffic useful for debugging
# http_debug = 1
#http_debug = 1
[%(apisrv)s]
user = %(user)s
@ -112,6 +108,13 @@ Make sure that it has a [general] section.
cookiejar = None
def parse_apisrv_url(scheme, apisrv):
import urlparse
if apisrv.startswith('http://') or apisrv.startswith('https://'):
return urlparse.urlsplit(apisrv)[0:2]
else:
return scheme, apisrv
def init_basicauth(config):
"""initialize urllib2 with the credentials for Basic Authentication"""
@ -175,8 +178,10 @@ def get_config():
# note that it is not suited for credentials containing spaces
import netrc
try:
# FIXME: apisrv is a URL now
netrc_host = parse_apisrv_url(None, DEFAULTS['apisrv'])[1]
config['user'], account, config['pass'] = \
netrc.netrc().authenticators(DEFAULTS['apisrv'])
netrc.netrc().authenticators(netrc_host)
print >>sys.stderr, 'Read credentials from %s.' % os.path.expanduser('~/.netrc')
except (IOError, TypeError, netrc.NetrcParseError):
#
@ -208,15 +213,11 @@ def get_config():
print >>sys.stderr, new_conf_template % DEFAULTS
sys.exit(1)
# holds multiple usernames and passwords
# it is used by urlgrabber's mirror fetcher
auth_dict = { }
for host in [ x for x in cp.sections() if x != 'general' ]:
auth_dict[host] = { 'user': cp.get(host, 'user'),
'pass': cp.get(host, 'pass') }
config = dict(cp.items('general', raw=1))
config['scheme'], config['apisrv'] = \
parse_apisrv_url(config['scheme'], config['apisrv'])
for i in boolean_opts:
try:
if int(config.get(i)):
@ -230,11 +231,14 @@ def get_config():
if type(config['urllist']) == str:
config['urllist'] = [ i.strip() for i in config['urllist'].split(',') ]
# holds multiple usernames and passwords
auth_dict = { }
for url in [ x for x in cp.sections() if x != 'general' ]:
dummy, host = \
parse_apisrv_url(config['scheme'], url)
auth_dict[host] = { 'user': cp.get(url, 'user'),
'pass': cp.get(url, 'pass') }
# add the auth data we collected to the config dict
config['auth_dict'] = auth_dict
# for easier access to the api server's credentials, copy them to the "top":
config['user'] = config['auth_dict'][config['apisrv']]['user']
config['pass'] = config['auth_dict'][config['apisrv']]['pass']