-
Notifications
You must be signed in to change notification settings - Fork 0
/
upstream_crawler.py
741 lines (539 loc) · 18.9 KB
/
upstream_crawler.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#!/usr/bin/python3
import configparser
import csv
import git
import depot_tools
import os
import requests
import shutil
import time
from depot_tools.gerrit_util import CreateHttpConn
from depot_tools.gerrit_util import GerritError
from depot_tools.gerrit_util import ReadHttpJsonResponse
from openpyxl import Workbook
class BaseCrawler:
def __init__(self, cfg_path):
self.__users = []
self.__initialized = False
self.__config = configparser.ConfigParser()
try:
self.__config.read(cfg_path)
except configparser.Error:
print('fail to parse config file %s' % (cfg_path))
return
for key in self.__config:
if key.split(' ')[0] not in ['user', 'gerrit', 'git', 'github', 'patchwork', 'DEFAULT']:
print('invalid section name "%s"' % (key))
return
for key in self.__config:
if key.split(' ')[0] == 'user':
if self.__config[key]['disable'].lower() != 'false':
continue
self.__users.append({'name': self.__config[key]['name'],
'emails': [self.__config[key]['email1'], self.__config[key]['email2']],
'function': self.__config[key]['function'],
'github username': self.__config[key]['github username'],
})
self.__initialized = True
return
def get_initialized(self):
return self.__initialized
def get_config(self):
return self.__config
def get_users(self):
return self.__users
def get_user(self, github_username = '', email = ''):
if self.__initialized == False:
return None
for user in self.__users:
if github_username != '' and user['github username'] == github_username:
return user
if email != '' and email in user['emails']:
return user
return None
def export_csv_file(self, report_directory, report_name, csv_fields, rows):
if self.__initialized == False:
return False
csv_path = '%s/%s.csv' % (report_directory, report_name)
print('export data to %s' % (csv_path))
with open(csv_path, 'w', newline = '') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames = csv_fields)
csv_writer.writeheader()
for row in rows:
csv_writer.writerow(row)
print('- %d row(s) saved' % (len(rows)))
return True
def export_excel_file(self, report_directory, report_name, csv_fields, date_field, rows):
if self.__initialized == False:
return False
excel_path = '%s/%s.xlsx' % (report_directory, report_name)
print('export data to %s' % (excel_path))
book = Workbook()
sheet = book.active
# add one sheet for counts of each user
sheet.title = 'summary'
counts = {}
for user in self.__users:
counts[user['name']] = []
years = []
for row in rows:
year = row[date_field].split('-')[0]
if year not in years:
for user in self.__users:
counts[user['name']].append(0)
years.append('%s' % (year))
idx = years.index(year)
counts[row['user_name']][idx] += 1
now = time.localtime()
timestamp = time.strftime('%Y-%m%d', now)
sheet.append(['summary of %s (%s)' % (report_name, timestamp)])
data = ['']
for year in years:
data.append(year)
sheet.append(data)
for user in self.__users:
data = [user['name']]
for count in counts[user['name']]:
data.append('%s' % (count))
sheet.append(data)
# add one sheet for all data
sheet = book.create_sheet('all')
sheet.append(csv_fields)
for row in rows:
data = []
for field in csv_fields:
data.append(row[field])
sheet.append(data)
print('- sheet "%s" added' % (sheet.title))
# add one sheet for each year
year = ''
for row in rows:
this_year = row[date_field].split('-')[0]
if year != this_year:
if year != '':
print('- sheet "%s" added' % (sheet.title))
year = this_year
# create new sheet
sheet = book.create_sheet(year)
sheet.append(csv_fields)
data = []
for field in csv_fields:
data.append(row[field])
sheet.append(data)
if year != '':
print('- sheet "%s" added' % (sheet.title))
# add one sheet for each user
for user in self.__users:
# create new sheet
sheet = book.create_sheet(user['name'])
sheet.append(csv_fields)
for row in rows:
if row['user_name'] != user['name']:
continue
data = []
for field in csv_fields:
data.append(row[field])
sheet.append(data)
print('- sheet "%s" added' % (sheet.title))
book.save(excel_path)
return True
class GerritCrawler(BaseCrawler):
__csv_fields = ['user_name', 'user_function', 'repo_name', 'repo_url', 'project', 'branch', 'change_id', 'subject', 'status', 'created', 'updated', 'submitted', 'insertions', 'deletions', 'owner']
__report_name = 'gerrit-changes'
def __init__(self, cfg_path):
self.__servers = []
self.__initialized = False
# call parent's init
super().__init__(cfg_path)
if super().get_initialized() == False:
return
config = super().get_config()
for key in config:
if key.split(' ')[0] == 'gerrit':
if config[key]['disable'].lower() != 'false':
continue
self.__servers.append({'name': config[key]['name'],
'url': config[key]['url'],
})
self.__initialized = True
return
def get_changes(self):
# gerrit REST API doc:
# https://gerrit-review.googlesource.com/Documentation/rest-api.html
def useDateTime(element):
# 'created': '2021-11-02 07:16:18.000000000'
return element['created']
self.__changes = []
if self.__initialized == False:
return self.__changes
for server in self.__servers:
print('query changes from gerrit server "%s"' % (server['name']))
for user in self.get_users():
for email in user['emails']:
print('query changes for "%s <%s>"' % (user['name'], email))
start = 0
while True:
more_changes = False
try:
changes = ReadHttpJsonResponse(CreateHttpConn(server['url'], 'changes/?q=owner:' + email + '&start=' + str(start)))
except GerritError as error:
print('- gerrit error: %s' % (error.message))
break
print('- %d change(s) found' % (len(changes)))
for change in changes:
# optional field, and not every merged change has this field set
if 'submitted' not in change.keys():
change['submitted'] = ''
# ChangeInfo
# https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-info
self.__changes.append({'user_name': user['name'],
'user_function': user['function'],
'repo_name': server['name'],
'repo_url': server['url'],
'project': change['project'],
'branch': change['branch'],
'change_id': change['change_id'],
'subject': change['subject'],
'status': change['status'],
'created': change['created'],
'updated': change['updated'],
'submitted': change['submitted'],
'insertions': change['insertions'],
'deletions': change['deletions'],
'owner': email,
})
if '_more_changes' in change.keys() and change['_more_changes'] == True:
start += len(changes)
more_changes = True
if more_changes == False:
break
# sort the changes by date
self.__changes.sort(key = useDateTime)
return self.__changes
def export_csv_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_csv_file(report_directory, self.__report_name, self.__csv_fields, self.__changes)
return ret
def export_excel_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_excel_file(report_directory, self.__report_name, self.__csv_fields, 'created', self.__changes)
return ret
class GitCrawler(BaseCrawler):
__csv_fields = ['user_name', 'user_function', 'commit_hash', 'author_email', 'author_date', 'committer_email', 'committer_date', 'subject', 'status']
__report_name = 'git-commits'
def __init__(self, cfg_path):
self.__repos = []
self.__initialized = False
# call parent's init
super().__init__(cfg_path)
if super().get_initialized() == False:
return
config = super().get_config()
for key in config:
if key.split(' ')[0] == 'git':
if config[key]['disable'].lower() != 'false':
continue
self.__repos.append({'name': config[key]['name'],
'url': config[key]['url'],
'branch': config[key]['branch'],
})
# prepare the parameter for git log command
self.__log_param = []
for user in self.get_users():
for email in user['emails']:
self.__log_param.append('--author=%s' % (email))
# %H: commit hash
# %ae: author email
# %aI: author date, strict ISO 8601 format
# %ce: committer email
# %cI: committer date, strict ISO 8601 format
# %s: subject
self.__log_param.append('--pretty=format:%H%x09%ae%x09%aI%x09%ce%x09%cI%x09%s')
self.__log_param.append('--reverse')
# create the root directory for repos
self.__repo_root = os.path.abspath('./repo')
if os.path.isdir(self.__repo_root) == False:
os.mkdir(self.__repo_root)
self.__initialized = True
return
def __open_repo(self, repo):
repo_path = os.path.abspath(self.__repo_root + '/' + repo['name'])
if os.path.isdir(repo_path) == False:
# repo directory not exist
print('- clone git repo from %s' % (repo['url']))
repository = git.Repo.clone_from(repo['url'], repo_path)
else:
print('- open git repo at %s' % (repo_path))
repository = git.Repo(repo_path)
if repository.__class__ is git.Repo:
# check if repo is healthy
if repository.is_dirty(untracked_files = True):
print('- warning, repo is dirty')
if repository.remotes.origin.exists() == False:
print('- warning, remote origin does not exist')
return repository
# repo may be corrupted...
print('- repo corrupted, delete entire repo')
try:
shutil.rmtree(repo_path)
except OSError as error:
pass
return None
def get_commits(self):
def useDateTime(element):
# 'created': '2021-08-10T11:47:55+02:00'
return element['committer_date']
self.__commits = []
if self.__initialized == False:
return self.__commits
hash_cache = []
for repo in self.__repos:
print('query commits from git repo "%s"' % (repo['name']))
repository = self.__open_repo(repo)
if repository == None:
# a second shot
repository = self.__open_repo(repo)
if repository == None:
continue
# git fetch origin
#repository.remotes.origin.fetch('+refs/heads/*:refs/remotes/origin/*')
repository.remotes.origin.fetch()
# git checkout
repository.git.checkout(repo['branch'])
# git log
log = repository.git.log(self.__log_param)
# split the log into lines
commits = log.splitlines()
print('- %d commit(s) found' % (len(commits)))
for commit in commits:
item = commit.split('\t')
if len(item) != 6:
continue
# already found in other repo
commit_hash = item[0]
if commit_hash in hash_cache:
continue
hash_cache.append(commit_hash)
author_email = item[1]
author_date = item[2]
committer_email = item[3]
committer_date = item[4]
subject = item[5]
if repo['name'] == 'linux':
status = 'upstreamed'
else:
status = 'accepted' # waiting next merge window
user = self.get_user(email = author_email)
if user == None:
# should not happen
user['name'] = 'John Doe'
user['function'] = 'Dead man'
self.__commits.append({'user_name': user['name'],
'user_function': user['function'],
'commit_hash': commit_hash,
'author_email': author_email,
'author_date': author_date,
'committer_email': committer_email,
'committer_date': committer_date,
'subject': subject,
'status': status,
})
# sort the commits by date
self.__commits.sort(key = useDateTime)
return self.__commits
def export_csv_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_csv_file(report_directory, self.__report_name, self.__csv_fields, self.__commits)
return ret
def export_excel_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_excel_file(report_directory, self.__report_name, self.__csv_fields, 'committer_date', self.__commits)
return ret
class GithubCrawler(BaseCrawler):
__csv_fields = ['user_name', 'user_function', 'repo_name', 'repo_url', 'number', 'state', 'title', 'user', 'created_at', 'updated_at', 'closed_at', 'merged_at', 'head', 'base', 'commits', 'additions', 'deletions', 'changed_files']
__report_name = 'github-pulls'
def __init__(self, cfg_path, auth):
self.__repos = []
self.__initialized = False
# call parent's init
super().__init__(cfg_path)
if super().get_initialized() == False:
return
config = super().get_config()
for key in config:
if key.split(' ')[0] == 'github':
if config[key]['disable'].lower() != 'false':
continue
self.__repos.append({'name': config[key]['name'],
'owner/repo': config[key]['owner/repo'],
})
# save the github (username, token) pair
self.__auth = auth
self.__initialized = True
return
def get_pulls(self):
# github REST API doc:
# https://docs.github.com/en/rest
def useDateTime(element):
# 'created_at': '2019-06-11T09:10:12Z'
return element['created_at']
self.__pulls = []
if self.__initialized == False:
return self.__pulls
# there is no filter for submitter
usernames = []
for user in self.get_users():
usernames.append(user['github username'])
for repo in self.__repos:
print('query pulls from github repo "%s"' % (repo['name']))
found = checked = 0
# get the page one result
url = 'https://api.github.com/repos/%s/pulls?state=all&per_page=100&direction=asc' % (repo['owner/repo'])
while True:
try:
r = requests.get(url = url, auth = self.__auth)
except requests.exceptions.RequestException as error:
print('- github error: %s' % (error.message))
break
pulls = r.json()
invalid_pulls = False
for pull in pulls:
if type(pull) is not dict:
print('- fail to get pulls from repo')
invalid_pulls = True
break
if pull['user']['login'] not in usernames:
continue
# one valid pull is found but don't know who's the submitter
found += 1
user = self.get_user(github_username = pull['user']['login'])
if user == None:
# should not happen
user['name'] = 'John Doe'
user['function'] = 'Dead man'
detail = requests.get(url = pull['url'], auth = self.__auth).json()
# check the response of 'GET /repos/{owner}/{repo}/pulls'
# https://docs.github.com/en/rest/reference/pulls
self.__pulls.append({'user_name': user['name'],
'user_function': user['function'],
'repo_name': repo['name'],
'repo_url': 'github.com/%s' % (repo['owner/repo']),
'number': pull['number'],
'state': pull['state'],
'title': pull['title'],
'user': pull['user']['login'],
'created_at': pull['created_at'],
'updated_at': pull['updated_at'],
'closed_at': pull['closed_at'],
'merged_at': pull['merged_at'],
'head': pull['head']['label'],
'base': pull['base']['label'],
'commits': detail['commits'],
'additions': detail['additions'],
'deletions': detail['deletions'],
'changed_files': detail['changed_files'],
})
if invalid_pulls != False:
# try next repo
break
checked += len(pulls)
print('- %d pull(s) found / total %d pull(s) checked' % (found, checked))
# read url to next page
links = r.links
if 'next' in links.keys():
url = links['next']['url']
else:
break
# sort the pulls by date
self.__pulls.sort(key = useDateTime)
return self.__pulls
def export_csv_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_csv_file(report_directory, self.__report_name, self.__csv_fields, self.__pulls)
return ret
def export_excel_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_excel_file(report_directory, self.__report_name, self.__csv_fields, 'created_at', self.__pulls)
return ret
class PatchworkCrawler(BaseCrawler):
__csv_fields = ['user_name', 'user_function', 'repo_name', 'repo_url', 'project', 'date', 'name', 'state', 'submitter']
__report_name = 'patchwork-patches'
def __init__(self, cfg_path):
self.__servers = []
self.__initialized = False
# call parent's init
super().__init__(cfg_path)
if super().get_initialized() == False:
return
config = super().get_config()
for key in config:
if key.split(' ')[0] == 'patchwork':
if config[key]['disable'].lower() != 'false':
continue
self.__servers.append({'name': config[key]['name'],
'url': config[key]['url'],
})
self.__initialized = True
return
def get_patches(self):
# patchwork REST API doc:
# https://patchwork.readthedocs.io/en/latest/api/rest/
def useDateTime(element):
# 'date': '2018-04-24T11:15:52'
return element['date']
self.__patches = []
if self.__initialized == False:
return self.__patches
for server in self.__servers:
print('query patches from patchwork server "%s"' % (server['name']))
for user in self.get_users():
for email in user['emails']:
print('query patches for "%s <%s>"' % (user['name'], email))
# get the page one result
url = 'https://%s/api/1.2/patches?submitter=%s' % (server['url'], email)
while True:
try:
r = requests.get(url = url)
except requests.exceptions.RequestException as error:
print('- patchwork error: %s' % (error.message))
break
patches = r.json()
print('- %d patche(s) found' % (len(patches)))
for patch in patches:
# check the response of 'GET /api/1.2/patches/'
# https://patchwork.readthedocs.io/en/latest/api/rest/schemas/v1.2/
self.__patches.append({'user_name': user['name'],
'user_function': user['function'],
'repo_name': server['name'],
'repo_url': server['url'],
'project': patch['project']['name'],
'date': patch['date'],
'name': patch['name'],
'state': patch['state'],
'submitter': email,
})
# read url to next page
links = r.links
if 'next' in links.keys():
url = links['next']['url']
else:
break
# sort the patches by date
self.__patches.sort(key = useDateTime)
return self.__patches
def export_csv_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_csv_file(report_directory, self.__report_name, self.__csv_fields, self.__patches)
return ret
def export_excel_file(self, report_directory):
if self.__initialized == False:
return False
ret = super().export_excel_file(report_directory, self.__report_name, self.__csv_fields, 'date', self.__patches)
return ret