Skip to content

Commit

Permalink
Add gitshelf as a format and fetch certain repos
Browse files Browse the repository at this point in the history
This adds support for gitshelf as a format for a list
of repositories. This also adds support for fetching
only repos that are listed in a list of repositories,
as specified by --import-repos and --format.

Closes ksdme#6 and ksdme#29
  • Loading branch information
CLiu13 committed Nov 13, 2018
1 parent a805a81 commit 87c7e54
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
15 changes: 15 additions & 0 deletions org_status/decoders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class RepoListDecoder:
NAME = None

def convert_format_to_repo_list(self, file_name):
raise NotImplementedError()


def get_all_supported_decoders():
from org_status.decoders.gitman import GitManDecoder
from org_status.decoders.gitshelf import GitShelfDecoder

return (
GitManDecoder,
GitShelfDecoder,
)
18 changes: 18 additions & 0 deletions org_status/decoders/gitman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import yaml

from org_status.decoders import RepoListDecoder


class GitManDecoder(RepoListDecoder):
NAME = 'gitman'

def convert_format_to_repo_list(self, file_name):
repos = []

with open(file_name, 'r') as file:
yml_data = yaml.load(file)

for repo in yml_data['sources']:
repos.append(repo['name'])

return repos
18 changes: 18 additions & 0 deletions org_status/decoders/gitshelf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import yaml

from org_status.decoders import RepoListDecoder


class GitShelfDecoder(RepoListDecoder):
NAME = 'gitshelf'

def convert_format_to_repo_list(self, file_name):
repos = []

with open(file_name, 'r') as file:
yml_data = yaml.load(file)

for repo in yml_data['books']:
repos.append(repo['book'])

return repos
2 changes: 2 additions & 0 deletions org_status/encoders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def convert_repo_list_to_format(self, repos):

def get_all_supported_encoders():
from org_status.encoders.gitman import GitManEncoder
from org_status.encoders.gitshelf import GitShelfEncoder

return (
GitManEncoder,
GitShelfEncoder,
)
19 changes: 19 additions & 0 deletions org_status/encoders/gitshelf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import yaml
from giturlparse import parse

from encoders import RepoListEncoder


class GitShelfEncoder(RepoListEncoder):
NAME = 'gitshelf'

def convert_repo_list_to_format(self, repos):
yml_data = {'books': []}

for repo in repos:
name = parse(repo.web_url).repo
yml_data['books'].append({'book': name,
'git': repo.web_url,
'branch': 'master'})

return yaml.dump(yml_data, default_flow_style=False)
37 changes: 34 additions & 3 deletions org_status/org_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from org_status.status_providers import Status, get_supported_status_providers
from org_status.org_hosts import get_all_supported_hosts
from org_status.encoders import get_all_supported_encoders
from org_status.decoders import get_all_supported_decoders


def get_host_token(host_name):
Expand Down Expand Up @@ -84,6 +85,7 @@ 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('--import-repos', type=str)
parser.add_argument('--export-repos', type=str)
parser.add_argument('--format', type=str, default='gitman')
parser.add_argument('--check-providers-only', action='store_true')
Expand All @@ -100,14 +102,31 @@ def encode_repo_list(repo_data, encoder_name, styled):
try:
encoded_repo_list = encoder().convert_repo_list_to_format(
repo_data)
return encoded_repo_list
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
print(styled(f'unknown export format {encoder_name}', 'red'))


def decode_repo_list(file_name, decoder_name, styled):
decoders = get_all_supported_decoders()
decoded_repo_list = None

for decoder in decoders:
if decoder.NAME == decoder_name:
try:
decoded_repo_list = decoder().convert_format_to_repo_list(
file_name)
return decoded_repo_list
except NotImplementedError:
print(styled(
f'{decoder_name} does not support exporting results',
'red'))

print(styled(f'unknown export format {decoder_name}', 'red'))


def write_data_to_file(encoded_data, filename, styled, verbose):
Expand Down Expand Up @@ -195,6 +214,18 @@ def main():
all_repositories += org_host.repositories
continue

if args.import_repos:
repos = decode_repo_list(args.import_repos, args.format, styled)
repos_to_remove = []

for repo in org_host.repositories:
repo_name = repo.full_name.split('/')[-1]
if repo_name not in repos:
repos_to_remove.append(repo)

for repo in repos_to_remove:
org_host.repositories.remove(repo)

org_status = aggregate_org_status(org_host, threads=args.threads)
present_status(org_status, args.no_color)

Expand Down

0 comments on commit 87c7e54

Please sign in to comment.