diff options
-rw-r--r-- | gbp/config.py | 54 | ||||
-rw-r--r-- | gbp/scripts/import_dscs.py | 3 | ||||
-rw-r--r-- | tests/test_Config.py | 14 |
3 files changed, 49 insertions, 22 deletions
diff --git a/gbp/config.py b/gbp/config.py index a1c279a6..333d74d5 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -25,6 +25,7 @@ try: except ImportError: gbp_version = "[Unknown version]" import gbp.tristate +from gbp.git import GitRepositoryError, GitRepository no_upstream_branch_msg = """ Repository does not have branch '%s' for upstream sources. If there is none see @@ -273,28 +274,32 @@ class GbpOptionParser(OptionParser): def_config_files = [ '/etc/git-buildpackage/gbp.conf', '~/.gbp.conf', - '.gbp.conf', - 'debian/gbp.conf', - '.git/gbp.conf' ] + '%(top_dir)s/.gbp.conf', + '%(top_dir)s/debian/gbp.conf', + '%(git_dir)s/gbp.conf' ] @classmethod - def get_config_files(klass): + def get_config_files(klass, no_local=False): """ Get list of config files from the I{GBP_CONF_FILES} environment variable. + @param no_local: don't return the per-repo configuration files + @type no_local: C{str} @return: list of config files we need to parse @rtype: C{list} >>> conf_backup = os.getenv('GBP_CONF_FILES') >>> if conf_backup is not None: del os.environ['GBP_CONF_FILES'] + >>> homedir = os.path.expanduser("~") >>> files = GbpOptionParser.get_config_files() - - # Remove the ~-expanded one - >>> del files[1] - >>> files - ['/etc/git-buildpackage/gbp.conf', '.gbp.conf', 'debian/gbp.conf', '.git/gbp.conf'] - + >>> files_mangled = [file.replace(homedir, 'HOME') for file in files] + >>> files_mangled + ['/etc/git-buildpackage/gbp.conf', 'HOME/.gbp.conf', '%(top_dir)s/.gbp.conf', '%(top_dir)s/debian/gbp.conf', '%(git_dir)s/gbp.conf'] + >>> files = GbpOptionParser.get_config_files(no_local=True) + >>> files_mangled = [file.replace(homedir, 'HOME') for file in files] + >>> files_mangled + ['/etc/git-buildpackage/gbp.conf', 'HOME/.gbp.conf'] >>> os.environ['GBP_CONF_FILES'] = 'test1:test2' >>> GbpOptionParser.get_config_files() ['test1', 'test2'] @@ -303,7 +308,24 @@ class GbpOptionParser(OptionParser): """ envvar = os.environ.get('GBP_CONF_FILES') files = envvar.split(':') if envvar else klass.def_config_files - return [ os.path.expanduser(f) for f in files ] + files = [os.path.expanduser(fname) for fname in files] + if no_local: + files = [fname for fname in files if fname.startswith('/')] + return files + + def _read_config_file(self, parser, repo, filename): + """Read config file""" + str_fields = {} + if repo: + str_fields['git_dir'] = repo.git_dir + if not repo.bare: + str_fields['top_dir'] = repo.path + try: + filename = filename % str_fields + except KeyError: + # Skip if filename wasn't expanded, i.e. we're not in git repo + return + parser.read(filename) def _parse_config_files(self): """ @@ -315,7 +337,14 @@ class GbpOptionParser(OptionParser): self.config = dict(self.__class__.defaults) # Update with the values from the defaults section. This is needed # in case the config file doesn't have a [<command>] section at all - parser.read(self.config_files) + config_files = self.get_config_files() + try: + repo = GitRepository(".") + except GitRepositoryError: + repo = None + # Read all config files + for filename in config_files: + self._read_config_file(parser, repo, filename) self.config.update(dict(parser.defaults())) # Make sure we read any legacy sections prior to the real subcommands @@ -369,7 +398,6 @@ class GbpOptionParser(OptionParser): self.sections = sections self.prefix = prefix self.config = {} - self.config_files = self.get_config_files() self._parse_config_files() OptionParser.__init__(self, option_class=GbpOption, usage=usage, version='%s %s' % (self.command, diff --git a/gbp/scripts/import_dscs.py b/gbp/scripts/import_dscs.py index 28413faf..f480fea4 100644 --- a/gbp/scripts/import_dscs.py +++ b/gbp/scripts/import_dscs.py @@ -73,8 +73,7 @@ def set_gbp_conf_files(): Filter out all gbp.conf files that are local to the git repository and set GBP_CONF_FILES accordingly so gbp import-dsc will only use these. """ - files = GbpOptionParser.get_config_files() - global_config = [ f for f in files if f.startswith('/') ] + global_config = GbpOptionParser.get_config_files(no_local=True) gbp_conf_files = ':'.join(global_config) os.environ['GBP_CONF_FILES'] = gbp_conf_files gbp.log.debug("Setting GBP_CONF_FILES to '%s'" % gbp_conf_files) diff --git a/tests/test_Config.py b/tests/test_Config.py index 9a68f8cc..c4666f4d 100644 --- a/tests/test_Config.py +++ b/tests/test_Config.py @@ -67,14 +67,14 @@ def test_parser_fallback(): >>> import os >>> from gbp.config import GbpOptionParser - >>> parser = GbpOptionParser('foo') >>> tmpdir = str(context.new_tmpdir('foo')) >>> confname = os.path.join(tmpdir, 'gbp.conf') - >>> parser.config_files = [confname] >>> f = open(confname, 'w') >>> f.write('[foo]\\nthere = is\\n[git-foo]\\nno = truth\\n') >>> f.close() - >>> parser._parse_config_files() + >>> os.environ['GBP_CONF_FILES'] = confname + >>> parser = GbpOptionParser('foo') + >>> del os.environ['GBP_CONF_FILES'] >>> parser.config['there'] 'is' >>> parser.config['no'] @@ -86,21 +86,21 @@ def test_filter(): The filter option should always parse as a list >>> import os >>> from gbp.config import GbpOptionParser - >>> parser = GbpOptionParser('bar') >>> tmpdir = str(context.new_tmpdir('bar')) >>> confname = os.path.join(tmpdir, 'gbp.conf') - >>> parser.config_files = [confname] >>> f = open(confname, 'w') >>> f.write('[bar]\\nfilter = asdf\\n') >>> f.close() - >>> parser._parse_config_files() + >>> os.environ['GBP_CONF_FILES'] = confname + >>> parser = GbpOptionParser('bar') >>> parser.config['filter'] ['asdf'] >>> f = open(confname, 'w') >>> f.write("[bar]\\nfilter = ['this', 'is', 'a', 'list']\\n") >>> f.close() - >>> parser._parse_config_files() + >>> parser = GbpOptionParser('bar') >>> parser.config['filter'] ['this', 'is', 'a', 'list'] + >>> del os.environ['GBP_CONF_FILES'] """ |