forked from sourcerer-io/hall-of-fame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo.py
124 lines (103 loc) · 4.22 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
"""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.
"""
__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.github_tracker import RepoTracker
from fame.glory import Glory
def is_repo_command(command):
return command in ['add', 'remove', 'update', 'print', 'glorify']
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('command', type=str,
choices=['add', 'remove', 'list',
'update', 'print', 'glorify'],
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('--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 is_repo_command(args.command):
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 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 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 == 'add':
tracker.add()
elif args.command == 'remove':
tracker.remove()
elif args.command == 'update':
tracker.update()
elif args.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 == 'print':
repo = tracker.load()
print(repo)
elif args.command == 'glorify':
repo = tracker.load()
glory = Glory(args.sourcerer_origin, args.sourcerer_api_origin)
glory.make(repo)
except Exception as e:
print('e %s' % str(e))
if __name__ == '__main__':
main()