From: Zhang Qiang Date: Wed, 4 Sep 2013 05:03:43 +0000 (+0800) Subject: Just overwrite original config instead of clean up X-Git-Tag: 0.19~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=95c4c6c22b767b80e4b6b6276976ee6cee4c555a;p=tools%2Fgbs.git Just overwrite original config instead of clean up configmgr will load default config files, if -c specified new conf file, which should be added to configmgr and the original default keys should not removed, in some cases they are needed. Fixes: #1273 Change-Id: If4451bf60a2683a2c7c753a5044f25ec53898da4 --- diff --git a/gitbuildsys/conf.py b/gitbuildsys/conf.py index 7d219cb..5d8a060 100644 --- a/gitbuildsys/conf.py +++ b/gitbuildsys/conf.py @@ -228,8 +228,22 @@ url = http://download.tizen.org/releases/daily/trunk/ivi/latest/ return cls._instance def __init__(self, fpath=None): + self._cfgfiles = [] self._cfgparsers = [] - self.reset_from_conf(fpath) + if fpath: + if not os.path.exists(fpath): + raise errors.ConfigError('Configuration file %s does not '\ + 'exist' % fpath) + self._cfgfiles.append(fpath) + + # find the default path + fpaths = self._lookfor_confs() + if not fpaths: + self._new_conf() + fpaths = self._lookfor_confs() + self._cfgfiles.extend(fpaths) + + self.load_confs() def _create_default_parser(self): 'create a default parser that handle DEFAULTS values' @@ -240,22 +254,11 @@ url = http://download.tizen.org/releases/daily/trunk/ivi/latest/ parser.set(sec, key, val) return parser - def reset_from_conf(self, fpath): + def load_confs(self): 'reset all config values by files passed in' - if fpath: - if not os.path.exists(fpath): - raise errors.ConfigError('Configuration file %s does not '\ - 'exist' % fpath) - fpaths = [fpath] - else: - # use the default path - fpaths = self._lookfor_confs() - if not fpaths: - self._new_conf() - fpaths = self._lookfor_confs() self._cfgparsers = [] - for fpath in fpaths: + for fpath in self._cfgfiles: cfgparser = BrainConfigParser() try: cfgparser.read_one(fpath) @@ -271,6 +274,20 @@ url = http://download.tizen.org/releases/daily/trunk/ivi/latest/ self._check_passwd() + def add_conf(self, fpath): + """ Add new config to configmgr, and new added config file has + highest priority + """ + if not fpath: + return + if not os.path.exists(fpath): + raise errors.ConfigError('Configuration file %s does not '\ + 'exist' % fpath) + # new added conf has highest priority + self._cfgfiles.insert(0, fpath) + # reload config files + self.load_confs() + @staticmethod def _lookfor_confs(): """Look for available config files following the order: diff --git a/tests/test_config.py b/tests/test_config.py index f01ae82..6e3aedc 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -92,6 +92,15 @@ class ConfigGettingTest(unittest.TestCase): reload(gitbuildsys.conf) return gitbuildsys.conf.configmgr.get(option, section) + @staticmethod + def add_conf(fpath): + '''get section.option from config''' + # configmgr is a global variable, reload to recreate it + # otherwise fixtures only take effect in the first time + reload(gitbuildsys.conf) + return gitbuildsys.conf.configmgr.add_conf(fpath) + + @Fixture(project='project1.ini') def test_no_such_section(self): '''test no such section''' @@ -139,6 +148,12 @@ class ConfigGettingTest(unittest.TestCase): 'test interpolation is supported' self.assertEquals('abc/def', self.get('remote', 'target')) + @Fixture(home='home1.ini') + def test_addconf(self): + '''value can be inherit from two levels''' + self.add_conf(os.path.join(FILE_DIRNAME, 'testdata', 'ini', 'project1.ini')) + self.assertEqual('homev2', self.get('section', 'home_only_key')) + if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tools/gbs b/tools/gbs index 333b138..1e466e3 100755 --- a/tools/gbs +++ b/tools/gbs @@ -526,7 +526,7 @@ def main(argv): # Process configuration file if --conf is used if args.conf: from gitbuildsys.conf import configmgr - configmgr.reset_from_conf(args.conf) + configmgr.add_conf(args.conf) # Import target module and call 'main' from it module = __import__("gitbuildsys.%s" % args.module, fromlist=[args.module])