Just overwrite original config instead of clean up
authorZhang Qiang <qiang.z.zhang@intel.com>
Wed, 4 Sep 2013 05:03:43 +0000 (13:03 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Wed, 4 Sep 2013 07:54:51 +0000 (15:54 +0800)
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

gitbuildsys/conf.py
tests/test_config.py
tools/gbs

index 7d219cb76f1eb5f1647436d908301184279cd7de..5d8a0608dacd3feb528d306ea3234163c81c9d2c 100644 (file)
@@ -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:
index f01ae8271b2a020c67b6b82617045e603439c3d2..6e3aedcb7011b6a8f0e85dc4a701335f765c362b 100644 (file)
@@ -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()
index 333b138ca5cfd3f612a9a5899d03d73ce75930f0..1e466e390ec5cc93da01395b6aab5e2d4bf448c1 100755 (executable)
--- 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])