Add a default parser in ConfigMgr
authorHuang Hao <hao.h.huang@intel.com>
Mon, 17 Sep 2012 07:23:12 +0000 (15:23 +0800)
committerHuang Hao <hao.h.huang@intel.com>
Tue, 18 Sep 2012 02:30:03 +0000 (10:30 +0800)
- Fix bug#333.
- Change config item like %(xxx)s to ${xxx} in DEFAULTS
    dict, otherwise InterpolationError will be raised
- Transform ${upstream_tag} back to %(upstream_tag)s before
    calling gbp, which does not change functionality of gbp

Change-Id: I3ba122ef4a633c23d59e136773ae98d1e280dab3

gitbuildsys/cmd_export.py
gitbuildsys/conf.py
tests/test_config.py
tests/testdata/ini/interpolation.ini [new file with mode: 0644]
tools/gbs

index ff7dfe1721956105a38bc6e967fd398fc831165a..4d445ca3c45c931a047bb1f8634d48ce735c08f1 100644 (file)
@@ -20,6 +20,7 @@
 """
 
 import os
+import re
 import shutil
 import errno
 
@@ -54,6 +55,10 @@ def is_native_pkg(repo, opts):
         upstream_branch = configmgr.get('upstream_branch', 'general')
     return not repo.has_branch(upstream_branch)
 
+def transform_var_format_from_shell_to_python(whole):
+    '''replace string like ${xxx} with %(xxx)s'''
+    return re.sub(r'\$\{([^}]+)\}', r'%(\1)s', whole)
+
 def create_gbp_export_args(repo, commit, export_dir, tmp_dir, spec, opts,
                            force_native=False):
     """
@@ -67,6 +72,7 @@ def create_gbp_export_args(repo, commit, export_dir, tmp_dir, spec, opts,
         upstream_tag = opts.upstream_tag
     else:
         upstream_tag = configmgr.get('upstream_tag', 'general')
+        upstream_tag = transform_var_format_from_shell_to_python(upstream_tag)
     msger.debug("Using upstream branch: %s" % upstream_branch)
     msger.debug("Using upstream tag format: '%s'" % upstream_tag)
 
index 1ed756ac7a3a7c5bdd4fe192901383b7fb3aae64..4ffa4ea5b61cd527ead96c5a74ffc87d8c9bfb2d 100644 (file)
@@ -24,7 +24,7 @@ import os
 import ast
 import base64
 import shutil
-from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError, \
+from ConfigParser import SafeConfigParser, NoSectionError, \
                          MissingSectionHeaderError, Error
 
 from gitbuildsys import msger, errors
@@ -185,7 +185,7 @@ class ConfigMgr(object):
                 'tmpdir': '/var/tmp',
                 'editor': '',
                 'upstream_branch': 'upstream',
-                'upstream_tag': 'upstream/%(upstreamversion)s',
+                'upstream_tag': 'upstream/${upstreamversion}',
                 'squash_patches_until': '',
             },
             'build': {
@@ -233,6 +233,15 @@ url = http://download.tizen.org/snapshots/trunk/common/latest/
         self._cfgparsers = []
         self.reset_from_conf(fpath)
 
+    def _create_default_parser(self):
+        'create a default parser that handle DEFAULTS values'
+        parser = BrainConfigParser()
+        for sec, options in self.DEFAULTS.iteritems():
+            parser.add_section(sec)
+            for key, val in options.iteritems():
+                parser.set(sec, key, val)
+        return parser
+
     def reset_from_conf(self, fpath):
         'reset all config values by files passed in'
         if fpath:
@@ -255,6 +264,8 @@ url = http://download.tizen.org/snapshots/trunk/common/latest/
             except Error, err:
                 raise errors.ConfigError('config file error:%s' % err)
             self._cfgparsers.append(cfgparser)
+        self._cfgparsers.append(self._create_default_parser())
+
         self._check_passwd()
 
     @staticmethod
@@ -316,23 +327,12 @@ url = http://download.tizen.org/snapshots/trunk/common/latest/
 
     def _get(self, opt, section='general'):
         'get value from multi-levels of config file'
-        sect_found = False
         for cfgparser in self._cfgparsers:
             try:
                 return cfgparser.get(section, opt)
-            except NoSectionError:
+            except Error, err:
                 pass
-            except NoOptionError:
-                sect_found = True
-
-        if section in self.DEFAULTS and opt in self.DEFAULTS[section]:
-            return self.DEFAULTS[section][opt]
-
-        if not sect_found:
-            raise errors.ConfigError('no section %s' % str(section))
-        else:
-            raise errors.ConfigError('no opt: %s in section %s' \
-                                     % (opt, str(section)))
+        raise errors.ConfigError(err)
 
     def options(self, section='general'):
         'merge and return options of certain section from multi-levels'
@@ -342,15 +342,11 @@ url = http://download.tizen.org/snapshots/trunk/common/latest/
             try:
                 options.update(cfgparser.options(section))
                 sect_found = True
-            except NoSectionError:
+            except Error, err:
                 pass
 
-        if section in self.DEFAULTS:
-            options.update(self.DEFAULTS[section].keys())
-            sect_found = True
-
         if not sect_found:
-            raise errors.ConfigError('invalid section %s' % (section))
+            raise errors.ConfigError(err)
 
         return options
 
index 14244a5640749dc8d0b9ee807a3c3289108b38f1..f01ae8271b2a020c67b6b82617045e603439c3d2 100644 (file)
@@ -134,6 +134,11 @@ class ConfigGettingTest(unittest.TestCase):
         'test invalid cointinuation line'
         self.assertRaises(ConfigError, reload, gitbuildsys.conf)
 
+    @Fixture(home='interpolation.ini')
+    def test_interpolation(self):
+        'test interpolation is supported'
+        self.assertEquals('abc/def', self.get('remote', 'target'))
+
 
 if __name__ == '__main__':
     unittest.main()
\ No newline at end of file
diff --git a/tests/testdata/ini/interpolation.ini b/tests/testdata/ini/interpolation.ini
new file mode 100644 (file)
index 0000000..256f991
--- /dev/null
@@ -0,0 +1,3 @@
+[remote]
+base = abc
+target = %(base)s/def
\ No newline at end of file
index c96cfa24405b2fbe737a814ab64554d3db35faf3..0bb3b05ef015863ea7cac0ae3dcc7e9990d39958 100755 (executable)
--- a/tools/gbs
+++ b/tools/gbs
@@ -159,7 +159,7 @@ class Gbs(cmdln.Cmdln):
     @cmdln.option('--upstream-tag',
                   default=None,
                   dest='upstream_tag',
-                  help="Upstream tag format, e.g. 'v%(upstreamversion)s'")
+                  help="Upstream tag format, e.g. 'v${upstreamversion}'")
     @cmdln.option('--squash-patches-until',
                   default=None,
                   dest='squash_patches_until',
@@ -404,7 +404,7 @@ class Gbs(cmdln.Cmdln):
     @cmdln.option('--upstream-tag',
                   default=None,
                   dest='upstream_tag',
-                  help="Upstream tag format, e.g. 'v%(upstreamversion)s'")
+                  help="Upstream tag format, e.g. 'v${upstreamversion}'")
     @cmdln.option('--squash-patches-until',
                   default=None,
                   dest='squash_patches_until',
@@ -512,7 +512,7 @@ class Gbs(cmdln.Cmdln):
     @cmdln.option('--upstream-tag',
                   default=None,
                   dest='upstream_tag',
-                  help="Upstream tag format, e.g. 'v%(upstreamversion)s'")
+                  help="Upstream tag format, e.g. 'v${upstreamversion}'")
     @cmdln.option('--squash-patches-until',
                   default=None,
                   dest='squash_patches_until',