From 969ef74126f602a084baae9c76d1e69b409fd01b Mon Sep 17 00:00:00 2001 From: JF Ding Date: Mon, 6 Feb 2012 11:39:51 +0800 Subject: [PATCH] enhance conf module to support multiple conf files --- gitbuildsys/conf.py | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/gitbuildsys/conf.py b/gitbuildsys/conf.py index a277bdd..d76d05d 100644 --- a/gitbuildsys/conf.py +++ b/gitbuildsys/conf.py @@ -31,11 +31,12 @@ class BrainConfigParser(SafeConfigParser): def read(self, filenames): """Limit the read() only support one input file. It's enough for current case. + If the input list has multiple values, use the last one. """ if len(filenames) > 1: - msger.warning('Will not support multiple config files, only read in the 1st one.') - filenames = filenames[:1] + msger.warning('Will not support multiple config files, only read in the last one.') + filenames = filenames[-1:] return SafeConfigParser.read(self, filenames) @@ -211,7 +212,7 @@ class ConfigMgr(object): 'user': 'my_user_id', 'passwd': '', 'passwdx': '', - 'build_server': 'https://build.saobs.jf.intel.com', + 'build_server': 'https://build.saobs.jf.intel.com', } DEFAULT_CONF_TEMPLATE="""[general] @@ -242,24 +243,50 @@ build_server = $build_server def __init__(self, fpath=None): self.cfgparser = BrainConfigParser() - if not fpath: - # use the default path - fpath = os.path.expanduser('~/.gbs.conf') + if fpath: + if not os.path.exists(fpath): + if not self._new_conf(fpath): + msger.error('No config file available') - if not os.path.exists(fpath): - if not self._new_conf(fpath): - msger.error('No config file available') + fpaths = [fpath] + else: + # use the default path + fpaths = self._lookfor_confs() + if not fpaths: + if not self._new_conf(): + msger.error('No config file available') - self.cfgparser.read([fpath]) + self.cfgparser.read(fpaths) self._check_passwd() + def _lookfor_confs(self): + """Look for available config files following the order: + > Global + > User + > Cwd + > Current git + """ + + paths = [] + for p in ('/etc/gbs.conf', + os.path.expanduser('~/.gbs.conf'), + '.gbs.conf', + '.git/gbs.conf'): + if os.path.exists(p): + paths.append(p) + + return paths + def get_default_conf(self, defaults=None): from string import Template if not defaults: defaults = self.DEFAULTS return Template(self.DEFAULT_CONF_TEMPLATE).safe_substitute(defaults) - def _new_conf(self, fpath): + def _new_conf(self, fpath=None): + if not fpath: + fpath = os.path.expanduser('~/.gbs.conf') + if msger.ask('Create config file %s using default values?' % fpath): import getpass -- 2.7.4