From 225b246889114baec5d65419e3de2ebd52a3c384 Mon Sep 17 00:00:00 2001 From: Kai Chen Date: Thu, 8 Nov 2018 02:04:12 -0800 Subject: [PATCH] Add support to export list of org repo Add option to export list of org repos into different formats. Closes https://github.com/ksdme/org-status/issues/6 --- org_status/encoders/__init__.py | 13 +++++++++ org_status/encoders/gitman.py | 19 +++++++++++++ org_status/org_status.py | 49 ++++++++++++++++++++++++++++++++- requirements.txt | 1 + 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 org_status/encoders/__init__.py create mode 100644 org_status/encoders/gitman.py diff --git a/org_status/encoders/__init__.py b/org_status/encoders/__init__.py new file mode 100644 index 0000000..1879851 --- /dev/null +++ b/org_status/encoders/__init__.py @@ -0,0 +1,13 @@ +class RepoListEncoder: + NAME = None + + def convert_repo_list_to_format(self, repos): + raise NotImplementedError() + + +def get_all_supported_encoders(): + from org_status.encoders.gitman import GitManEncoder + + return ( + GitManEncoder, + ) diff --git a/org_status/encoders/gitman.py b/org_status/encoders/gitman.py new file mode 100644 index 0000000..12eea2f --- /dev/null +++ b/org_status/encoders/gitman.py @@ -0,0 +1,19 @@ +import yaml +from giturlparse import parse + +from org_status.encoders import RepoListEncoder + + +class GitManEncoder(RepoListEncoder): + NAME = 'gitman' + + def convert_repo_list_to_format(self, repos): + yml_data = {'sources': []} + + for repo in repos: + name = parse(repo.web_url).repo + yml_data['sources'].append({'name': name, + 'repo': repo.web_url, + 'rev': 'master'}) + + return yaml.dump(yml_data, default_flow_style=False) diff --git a/org_status/org_status.py b/org_status/org_status.py index 8b42510..88f37f0 100644 --- a/org_status/org_status.py +++ b/org_status/org_status.py @@ -1,4 +1,4 @@ -from os import environ +from os import environ, path from multiprocessing.dummy import Pool from argparse import ArgumentParser @@ -6,6 +6,7 @@ from org_status.status_providers import Status from org_status.org_hosts import get_all_supported_hosts +from org_status.encoders import get_all_supported_encoders def get_host_token(host_name): @@ -75,10 +76,41 @@ def get_argument_parser(): parser.add_argument('--verbose', '-v', action='store_true') parser.add_argument('--hosts-only', '-o', action='store_true') parser.add_argument('--skip-host-checks', action='store_true') + parser.add_argument('--export-repos', type=str) + parser.add_argument('--format', type=str, default='gitman') return parser +def encode_repo_list(repo_data, encoder_name, styled): + encoders = get_all_supported_encoders() + encoded_repo_list = None + + for encoder in encoders: + if encoder.NAME == encoder_name: + try: + encoded_repo_list = encoder().convert_repo_list_to_format( + repo_data) + except NotImplementedError: + print(styled( + f'{encoder_name} does not support exporting results', + 'red')) + else: + print(styled(f'unknown export format {encoder_name}', 'red')) + + return encoded_repo_list + + +def write_data_to_file(encoded_data, filename, styled, verbose): + if encoded_data is not None: + try: + with open(filename, 'w') as file: + file.write(encoded_data) + verbose(f'exported repo list as {filename}') + except FileNotFoundError: + print(styled(f'unable to find file {filename}', 'red')) + + def main(): parser = get_argument_parser() args = parser.parse_args() @@ -94,6 +126,8 @@ def main(): print(styled('no organizations to check', 'red')) return + all_repositories = [] + for Host, org in generate_fetch_jobs(args.orgs): token = None @@ -136,5 +170,18 @@ def main(): raise exp org_host = Host(token, org, verbose=args.verbose) + + if args.export_repos: + all_repositories += org_host.repositories + continue + org_status = aggregate_org_status(org_host, threads=args.threads) present_status(org_status, args.no_color) + + export_data = encode_repo_list(all_repositories, + args.format, + styled) + export_file = path.abspath(args.export_repos) + + if export_data is not None: + write_data_to_file(export_data, export_file, styled, verbose) diff --git a/requirements.txt b/requirements.txt index 78da91f..5ee0a6b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ git+https://gitlab.com/gitmate/open-source/IGitt.git#egg=IGitt requests termcolor +giturlparse