-
Notifications
You must be signed in to change notification settings - Fork 0
/
upstream_report.py
executable file
·142 lines (101 loc) · 3.14 KB
/
upstream_report.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
#!/usr/bin/python3
import argparse
import os
import time
from upstream_crawler import GerritCrawler
from upstream_crawler import GitCrawler
from upstream_crawler import GithubCrawler
from upstream_crawler import PatchworkCrawler
support_actions = ['gerrit', 'git', 'github', 'patchwork']
def find_report_directory(config_file):
# remove the directory part
_, tmp = os.path.split(config_file)
# remove the ext part
config_name, _ = os.path.splitext(tmp)
while True:
now = time.localtime()
timestamp = time.strftime('%Y-%m%d-%H%M', now)
report_name = '%s-%s' % (config_name, timestamp)
report_directory = os.path.abspath('./' + report_name)
if os.path.exists(report_directory) == False:
break
time.sleep(1)
return report_directory
def validate_args(args):
actions = []
if args.action == '' or args.action == 'all':
actions = support_actions
else:
actions = args.action.split(' ')
for action in actions:
if action not in support_actions:
print('invalid action \'%s\'' % (action))
return []
if 'github' in actions:
if args.user_name == None:
print('missing github username')
return []
if args.token == None:
print('missing github token')
return []
if args.config_file == None:
print('missing config file')
return []
if os.path.isfile(args.config_file) == False:
print('invalid config file')
return []
return actions
def main():
# parse argument
parser = argparse.ArgumentParser()
parser.add_argument('action', nargs = '?', default = '', help = 'action to do')
parser.add_argument('-c', '--config_file', help = 'config file')
parser.add_argument('-u', '--user_name', help = 'github username')
parser.add_argument('-t', '--token', help = 'github token')
args = parser.parse_args()
actions = validate_args(args)
if len(actions) == 0:
# no action to perform...
return
report_directory = find_report_directory(args.config_file)
os.mkdir(report_directory)
if 'gerrit' in actions:
# gerrit
crawler = GerritCrawler(args.config_file)
changes = crawler.get_changes()
if len(changes) != 0:
crawler.export_csv_file(report_directory)
crawler.export_excel_file(report_directory)
else:
print('fail to get changes from gerrit server')
if 'git' in actions:
# git
crawler = GitCrawler(args.config_file)
commits = crawler.get_commits()
if len(commits) != 0:
crawler.export_csv_file(report_directory)
crawler.export_excel_file(report_directory)
else:
print('fail to get commits from git repo')
if 'github' in actions:
# github
github_auth = (args.user_name, args.token)
crawler = GithubCrawler(args.config_file, github_auth)
pulls = crawler.get_pulls()
if len(pulls) != 0:
crawler.export_csv_file(report_directory)
crawler.export_excel_file(report_directory)
else:
print('fail to get pulls from github repo')
if 'patchwork' in actions:
# patchwork
crawler = PatchworkCrawler(args.config_file)
patches = crawler.get_patches()
if len(patches) != 0:
crawler.export_csv_file(report_directory)
crawler.export_excel_file(report_directory)
else:
print('fail to get changes from patchwork server')
return
if __name__ == '__main__':
main()