diff options
author | Markus Lehtonen <markus.lehtonen@linux.intel.com> | 2012-07-06 09:58:06 +0300 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2014-12-28 13:35:36 +0100 |
commit | ff4cc8bb09574bc28491b11fb90e356eee691961 (patch) | |
tree | d7c2eaf6b7bd54e385b9ee956a068399995196a2 /gbp/config.py | |
parent | e098857ed3511da3c0de917518c6b8972b3f771c (diff) |
config: read the right config if run from subdir
A step towards being able to run GBP tools from subdirectories.
Now expands '%(top_dir)s' and '%(git_dir)s' in config file path to root
of the working directory and git metadata directory, respectively.
Also, adds a new method _read_config_file() in preparation for
supporting per-tree config files.
Fixes tests.test_Config: currently the only correct way to define the
config file(s) to be parsed is by using the GBP_CONF_FILES environment
variable.
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
Diffstat (limited to 'gbp/config.py')
-rw-r--r-- | gbp/config.py | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/gbp/config.py b/gbp/config.py index 63c11cb..8c0bc22 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 @@ -307,28 +308,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'] @@ -337,7 +342,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): """ @@ -349,7 +371,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 @@ -405,7 +434,6 @@ class GbpOptionParser(OptionParser): self.sections = sections self.prefix = prefix self.config = {} - self.config_files = self.get_config_files() self.parse_config_files() self.valid_options = [] |