From c48fd4650183ddea3aca514447d86cbd936f52b0 Mon Sep 17 00:00:00 2001 From: hyokeun Date: Thu, 3 Aug 2017 16:46:31 +0900 Subject: [PATCH] Support multiple snapshot conf files Change-Id: Id98ff20ec73aab6ac92ece5041c3e0c5364b6327 --- common/backenddb.py | 23 ++++++++++++++++++++++- job_load_repos.yaml.py | 9 +++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/common/backenddb.py b/common/backenddb.py index 049e51e..35b193a 100644 --- a/common/backenddb.py +++ b/common/backenddb.py @@ -167,18 +167,39 @@ class BackendDB(): """Return repos entity object.""" return Entity(self._redis, "repo:", ["Targets"]) - def read_repos(self, yamlobj): + def read_repos_dryrun(self, yamlobj, extra_files=[]): + try: + repos = yaml.load(yamlobj)["Repositories"] + if extra_files is not None and isinstance(extra_files, list): + for df in extra_files: + print 'Appending %s' % os.path.basename(df) + repos.append(yaml.load(file(df, 'r').read())) + for repo in repos: + if repo.has_key('Target') or not repo.has_key('Targets'): + continue + repo.pop('Name') + repo['Project'] + except Exception as err: + return '\n\n%s' % repr(err) + return '' + + def read_repos(self, yamlobj, extra_files=[]): """ Read repos from repos.yaml. Args: yamlobj (str or file object): Content of repos.yaml or its file object + extra_files: Path list of extra files to append Raises: BackendDBError when can't load yaml """ try: repos = yaml.load(yamlobj)["Repositories"] + if extra_files is not None and isinstance(extra_files, list): + for df in extra_files: + print 'Appending %s' % os.path.basename(df) + repos.append(yaml.load(file(df, 'r').read())) except (yaml.YAMLError, TypeError), err: raise BackendDBError("Error loading yaml: %s" % err) diff --git a/job_load_repos.yaml.py b/job_load_repos.yaml.py index e02be08..910492c 100644 --- a/job_load_repos.yaml.py +++ b/job_load_repos.yaml.py @@ -38,10 +38,15 @@ def load_repos(config_file): Args: config_file (str): the repos.yaml file location """ - bdb = BackendDB(os.getenv('REDIS_HOST'), int(os.getenv('REDIS_PORT'))) - ret = bdb.read_repos(file(config_file, 'r').read()) + + extra_files = [] + for root, dirs, files in os.walk(os.path.dirname(config_file)): + extra_files.extend([ os.path.join(root, x) for x in files if x.endswith('.yaml') ]) + extra_files.remove(config_file) + + ret = bdb.read_repos(file(config_file, 'r').read(), extra_files=extra_files) # Print db contents repos = bdb.get_repos() -- 2.7.4