Skip to content

Commit

Permalink
Add support to export list of org repo
Browse files Browse the repository at this point in the history
Add option to export list of org repos into different formats.

Closes ksdme#6
  • Loading branch information
kx-chen committed Nov 9, 2018
1 parent d832331 commit 026035f
Showing 4 changed files with 83 additions and 7 deletions.
13 changes: 13 additions & 0 deletions org_status/encoders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class OrgListEncoder:
NAME = None

def convert_org_list_to_format(self, repos):
raise NotImplementedError()


def get_all_supported_exporters():
from org_status.encoders.gitman import GitManEncoder

return (
GitManEncoder,
)
19 changes: 19 additions & 0 deletions org_status/encoders/gitman.py
Original file line number Diff line number Diff line change
@@ -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)
56 changes: 49 additions & 7 deletions org_status/org_status.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from os import environ
from os import environ, getcwd
from multiprocessing.dummy import Pool
from argparse import ArgumentParser

from termcolor import colored

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_exporters


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_exporters()
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
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
git+https://gitlab.com/gitmate/open-source/IGitt.git#egg=IGitt
requests
termcolor
furl
git+https://github.com/nephila/giturlparse

0 comments on commit 026035f

Please sign in to comment.