-
Notifications
You must be signed in to change notification settings - Fork 90
/
do.py
152 lines (127 loc) · 5.17 KB
/
do.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""CLI for hall of fame.
Commands
add: Adds a repo to track.
remove: Deletes a tracked repo.
list: Lists all tracked repos.
update: Updates a repo.
print: Prints a tracked repo.
glorify: Generate the hall of fame.
code: Generate MD or RST integration code.
"""
__copyright__ = '2018 Sourcerer, Inc.'
__author__ = 'Sergey Surkov'
import argparse
import os.path
from tabulate import tabulate
import fame.storage
import fame.ssl_hack
from fame.code_gen import make_md_code, make_rst_code
from fame.github_tracker import RepoTracker
from fame.glory import Glory
class Command:
ADD = 'add'
REMOVE = 'remove'
UPDATE = 'update'
LIST = 'list'
PRINT = 'print'
GLORIFY = 'glorify'
CODE = 'code'
@staticmethod
def is_repo_command(command):
return command in [
Command.ADD, Command.REMOVE, Command.UPDATE,
Command.PRINT, Command.GLORIFY]
@staticmethod
def get_all():
return [
Command.ADD, Command.REMOVE, Command.UPDATE,
Command.LIST, Command.PRINT, Command.GLORIFY, Command.CODE]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('command', type=str,
choices=Command.get_all(),
help='Command to execute')
parser.add_argument('--user', type=str,
help='Github user that tracks a repo')
parser.add_argument('--owner', type=str,
help='Github user that owns a repo')
parser.add_argument('--repo', type=str,
help='Github repo name, excluding owner')
parser.add_argument('--work_dir', type=str,
help='Working directory to store data')
parser.add_argument('--gcloud_bucket', type=str,
help='Google cloud bucket to store data')
parser.add_argument('--format', type=str, choices=['md', 'rst'],
default='md', help='Code format to generate')
parser.add_argument('--token', type=str, help='Github API token')
parser.add_argument('--sourcerer_origin', type=str,
default='https://sourcerer.io',
help='Sourcerer origin')
parser.add_argument('--sourcerer_api_origin', type=str,
help='Sourcerer API origin')
parser.add_argument('--sourcerer_api_secret', type=str,
help='Sourcerer API secret')
parser.add_argument('--no_ssl_host_check', action='store_true',
default=False,
help='Disable SSL host checks, useful for debugging')
args = parser.parse_args()
if Command.is_repo_command(args.command) or args.command == Command.CODE:
if not args.user:
parser.error('Must provide repo user')
if not args.owner:
parser.error('Must provide repo owner')
if not args.repo:
parser.error('Must provide repo name')
if Command.is_repo_command(args.command) and not args.sourcerer_origin:
parser.error('Must provide Sourcerer origin')
if args.work_dir and not os.path.isdir(args.work_dir):
parser.error('--work_dir must be an existing directory')
if not args.work_dir and not args.gcloud_bucket:
parser.error('Either --work_dir or --gcloud_bucket must be provided')
return args
def main():
args = parse_args()
if args.no_ssl_host_check:
fame.ssl_hack.disable_ssl_host_check()
if args.work_dir:
fame.storage.configure_for_local(args.work_dir)
elif args.gcloud_bucket:
fame.storage.configure_for_google_cloud(args.gcloud_bucket)
try:
tracker = RepoTracker()
if Command.is_repo_command(args.command):
tracker.configure(args.user, args.owner, args.repo,
args.sourcerer_api_origin,
args.sourcerer_api_secret,
args.token)
if args.command == Command.ADD:
tracker.add()
elif args.command == Command.REMOVE:
tracker.remove()
elif args.command == Command.UPDATE:
tracker.update()
elif args.command == Command.LIST:
table = []
for result in RepoTracker.list(args.user):
user, owner, repo, status, modified, error = result
modified = modified.strftime('%Y-%m-%d %H:%M:%S')
table.append([
'%s:%s/%s' % (user, owner, repo),
status, modified, error])
print(tabulate(table))
elif args.command == Command.PRINT:
repo = tracker.load()
print(repo)
elif args.command == Command.GLORIFY:
repo = tracker.load()
glory = Glory(args.sourcerer_origin, args.sourcerer_api_origin)
glory.make(repo)
elif args.command == Command.CODE:
if args.format == 'md':
print(make_md_code(args.user, args.owner, args.repo))
elif args.format == 'rst':
print(make_rst_code(args.user, args.owner, args.repo))
except Exception as e:
print('e %s' % str(e))
if __name__ == '__main__':
main()