diff --git a/org_status/encoders/__init__.py b/org_status/encoders/__init__.py new file mode 100644 index 0000000..94709ac --- /dev/null +++ b/org_status/encoders/__init__.py @@ -0,0 +1,13 @@ +class OrgListEncoder: + NAME = None + + def convert_org_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..17a5666 --- /dev/null +++ b/org_status/encoders/gitman.py @@ -0,0 +1,19 @@ +import yaml +from giturlparse import parse + +from org_status.encoders import OrgListEncoder + + +class GitManEncoder(OrgListEncoder): + NAME = 'gitman' + + def convert_org_list_to_format(self, repos): + yml_data = {'sources': []} + + for repo in repos: + name = parse(repo.repo_url).repo + yml_data['sources'].append({'name': name, + 'repo': repo.repo_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..5888bdd 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, getcwd 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,19 +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-orgs', type=str) + parser.add_argument('--format', type=str, default='yml') return parser -def main(): - parser = get_argument_parser() - args = parser.parse_args() +parser = get_argument_parser() +args = parser.parse_args() + +styled = (lambda l, *_: l) if args.no_color else colored +verbose = print if args.verbose else (lambda *_: None) + +hosts_only_print = print if args.hosts_only else verbose + + +def encode_repo_list(repo_data, exporter_name): + exporters = get_all_supported_encoders() + encoded_repo_list = None + + for exporter in exporters: + if exporter.NAME == exporter_name: + try: + encoded_repo_list = exporter().convert_org_list_to_format( + repo_data) + + except NotImplementedError: + print(styled('exporter does not support exporting results', + 'red')) + else: + print(styled(f'unknown export format {exporter_name}', 'red')) - styled = (lambda l, *_: l) if args.no_color else colored - verbose = print if args.verbose else (lambda *_: None) + return encoded_repo_list - hosts_only_print = print if args.hosts_only else verbose +def main(): if len(args.orgs) == 0 and args.hosts_only: args.orgs = ['coala'] elif len(args.orgs) == 0: @@ -137,4 +160,23 @@ def main(): org_host = Host(token, org, verbose=args.verbose) org_status = aggregate_org_status(org_host, threads=args.threads) + + if args.export_orgs: + export_data = encode_repo_list(org_status, args.format) + export_file = f'{getcwd()}/{args.export_orgs}' + + write_format_to_file(export_data, export_file) + present_status(org_status, args.no_color) + + +def write_format_to_file(export_data, export_file): + if export_data is not None: + try: + with open(export_file, 'w') as file: + file.write(export_data) + verbose(f"exported repo list as {args.format}") + + except FileNotFoundError: + print(styled(f'unable to find file {args.export_orgs}', 'red')) + return diff --git a/requirements.txt b/requirements.txt index 78da91f..8b9d6ce 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ git+https://gitlab.com/gitmate/open-source/IGitt.git#egg=IGitt requests termcolor +furl +git+https://github.com/nephila/giturlparse