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 33653c1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
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 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,
)
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 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)
46 changes: 45 additions & 1 deletion 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, path
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_encoders


def get_host_token(host_name):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -136,5 +168,17 @@ def main():
raise exp

org_host = Host(token, org, verbose=args.verbose)

if args.export_repos:
export_data = encode_repo_list(org_host.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)

return

org_status = aggregate_org_status(org_host, threads=args.threads)
present_status(org_status, args.no_color)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
git+https://gitlab.com/gitmate/open-source/IGitt.git#egg=IGitt
requests
termcolor
giturlparse

0 comments on commit 33653c1

Please sign in to comment.