From d15d520f390bbc6ddeb614cf81d4ccc73294dada Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Fri, 17 Dec 2021 18:17:24 -0800 Subject: [PATCH] Add extract_repos_subset.py script Add script to extract a subset of repository information from a .repos yaml file. Signed-off-by: Steve Peters --- jenkins-scripts/tools/extract_repos_subset.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 jenkins-scripts/tools/extract_repos_subset.py diff --git a/jenkins-scripts/tools/extract_repos_subset.py b/jenkins-scripts/tools/extract_repos_subset.py new file mode 100644 index 000000000..3c90b6750 --- /dev/null +++ b/jenkins-scripts/tools/extract_repos_subset.py @@ -0,0 +1,35 @@ +#/usr/bin/env/python3 +"""Extract a subset of repositories from a *.repos yaml file + +This script loads a .repos yaml file and prints a yaml file +containing a subset of the repositories specified in the file. +""" +import argparse +import sys +import yaml + +def main(): + + parser = argparse.ArgumentParser() + + parser.add_argument('file', type=str, + help = 'Yaml repos file to extract from') + parser.add_argument('repo_names_to_extract', type=str, nargs='+') + + args = parser.parse_args() + + f = open(args.file, 'r') + y = yaml.load(f.read()) + extracted_yaml = { 'repositories': {} } + extracted_repos = extracted_yaml['repositories'] + + for repo_name in args.repo_names_to_extract: + try: + extracted_repos[repo_name] = y['repositories'][repo_name] + except KeyError: + print(f"Error: could not find {repo_name} in {args.file}", file=sys.stderr) + print(yaml.dump(extracted_yaml)) + +# ------------------------------------------------ +if __name__ == '__main__': + main()