-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeCritic.py
More file actions
826 lines (695 loc) · 28 KB
/
Copy pathCodeCritic.py
File metadata and controls
826 lines (695 loc) · 28 KB
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#!/usr/bin/env python3
import argparse
import gerra.endpoints.changes as ch
import os
import sys
import utilpy.cfg.yamlmisc as yamlm
from collections import defaultdict
from IPython import embed
from plumbum import local, ProcessExecutionError
from pygerrit2 import GerritRestAPI, HTTPBasicAuth
from urllib.parse import urlparse
from requests.packages import urllib3
GERRIT_SERVER_URL = ""
SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
CUR_DIR = os.getcwd()
GERRIT_URL_BASE = None
# Make shell utils ready
git = local["git"]
bash = local["bash"]
def get_relation_chain(ch_api, change_info):
"""
Determine commits in URL. Usually, URL is a one commit or a tip of
branch ("Relation Chain") from review. Function counts only them
(not taking into account all patches in topic)
change_info: Dict of ChangeInfo
output: List of Dicts, listing all commits, concerning URL passed
(i.e single one or several ones from branch).
"""
# To find commit in particular repo and particular branch, we need to
# construct repo:<remote>/<branch> query for gerrit.
out = git("remote", "-v").split('\n')[0].split('\t')
# <remote> <gerrit ssh url with port>/<repo> (fetch)
# <remote> <gerrit ssh url with port>/<repo> (push)
remote_url = out[1].split(' ')[0]
parsed_url = urlparse(remote_url)
remote_path = parsed_url.path
if remote_path[0] != '/':
print("Path (as part of URL) on remote server does not start with '/'")
print("Analyze printout of 'git remote -v':\n{}". format(git("remote", "-v")))
raise
repo = remote_path[1:]
topic = change_info['topic']
branch = change_info['branch']
if repo != change_info['project']:
# they must match.
print("Local git info does not match info from Gerrit")
print(f"Local git info:\n{repo}")
print(f"Info from Gerrit:\n{change_info['project']}")
raise
commits_reply = ch_api.query((f"topic:{topic} status:open repo:{repo} "
f"branch:{branch}"))
# Place all needed info about commits into special List
commits = []
for commit in commits_reply:
commits.append(
{
"url": GERRIT_URL_BASE + "+/" + str(commit['_number']),
'change_id': commit['change_id'],
'triplet_id': commit['triplet_id'],
'subject': commit['subject'],
'_number': commit['_number'],
'project': commit['project'],
'branch': commit['branch'],
'topic': commit['topic']
}
)
return commits
def handle_url(args, ch_api):
change_info = ch_api.get_change_info(args.url)
numeric_id = change_info["_number"]
current_revision = change_info["current_revision"]
fetch_info = change_info["revisions"][current_revision]["fetch"]["ssh"]
fetch = (
"git fetch {} {} && "
"git checkout -b change-{} FETCH_HEAD".
format(fetch_info["url"], fetch_info["ref"], numeric_id)
)
try:
bash("-c", fetch)
except ProcessExecutionError as ex:
print(ex)
sys.exit(1)
chain = get_relation_chain(ch_api, change_info)
chain_nr = len(chain)
commits = []
git_commits = git("log", "--format=%H %d %s", "-n",
str(chain_nr)).strip().split('\n')
for git_commit in reversed(git_commits):
for commit in chain:
if commit['subject'] not in git_commit:
continue
git_hash = git_commit.split(' ')[0]
files = git('show', '--pretty=', '--name-only',
git_hash).strip().split('\n')
# To submit draft reviw we need revision_id. Extract it.
change_info = ch_api.get_change_info(commit['url'])
commits.append(
{
'git_hash': git_hash,
'files': files,
'revision_id': change_info['current_revision'],
**commit
}
)
# [{'git_hash': 'a8ff865c6c',
# 'files': ['drivers/mtd/nand/spi/core.c',
# 'include/linux/mtd/spinand.h'],
# 'url': 'http://some/gerrit/url/linux-next/+/27791',
# 'change_id': 'I487a015d8f4a7bf37e98945d7fad56d4ce440888',
# 'subject': '1st (oldest) commit in branch',
# '_number': 27791,
# 'project': 'linux/linux-next',
# 'branch': 'feature_branch',
# 'topic': 'upstream-feature'},
# {'git_hash': '7196704c34',
# 'files': ['drivers/mtd/nand/spi/Makefile',
# 'drivers/mtd/nand/spi/core.c',
# 'include/linux/mtd/spinand.h'],
# 'url': 'http://some/gerrit/url/linux-next/+/24836',
# 'change_id': 'I8917bdb3eae7e528f414589da12b629371a29444',
# 'subject': '2nd commit in branch',
# '_number': 24836,
# 'project': 'linux/linux-next',
# 'branch': 'feature_branch',
# 'topic': 'upstream-feature'},
# {'git_hash': '6cd78207e5',
# 'files': ['arch/arm/configs/versatile_defconfig'],
# 'url': 'http://some/gerrit/url/linux-next/+/25489',
# 'change_id': 'I7b72e09edc63960f6b8e2239d68d0d24a927491e',
# 'subject': '3rd (newest) commit in branch',
# '_number': 25489,
# 'project': 'linux/linux-next',
# 'branch': 'feature_branch',
# 'topic': 'upstream-feature'}]
# The oldest commit is the first item in the list
return commits
def filter_files(files, extensions):
"""
Filter files by extensions
files: list of files
extensions: tuple of possible extensions or tuple([]), denoting all files
"""
result = []
if not extensions:
# extensions == [] means all files, hence hack endswith() by empty ext
extensions = ""
# Do specific actions for some special fake extensions.
#
# Checkpatch utilize simple invocation form without any files:
# ./scripts/checkpatch.pl --strict -g HEAD~${nr_commits}..HEAD
if ".checkpatch" in extensions:
# so return empty str to avoid additional cmdline arg for checkpatch
result.append("")
return result
for afile in files:
if afile.endswith(extensions) and os.path.exists(afile):
result.append(afile)
return result
def handle_commit(args):
pass
def handle_file(args):
pass
def handle_flist(args):
pass
def is_report_in_commit(afile, lineno, git_hash):
"""
Returns True if paticular line (with lineno) in specified file 'afile'
is changed by current 'commit'
"""
if lineno == 0:
return False
# we need usual pager
aline = git("--no-pager", "blame", "-l", f"-L{lineno},{lineno}", afile)
return aline.split(" ")[0] == git_hash
def parse_cppcheck_output(report, commit, out, err):
for line in err.strip().split("\n"):
if len(line.strip()) == 0:
continue
# Filter out such rare case:
# line:include/linux/compiler-gcc.h###0###information###This file is \
# not analyzed. Cppcheck failed to extract a valid \
# configuration. Use -v for more details.
if "Cppcheck failed to extract a valid configuration" in line:
continue
afile, lineno, severity, err_msg = line.split("###")
# Such cases is possible:
# ######information###Cppcheck cannot find all the include files
# 'nofile###0###information###Cppcheck cannot find all the include files (use --check-config for details)'
if afile == "" or afile == "nofile" or lineno == "":
continue
# Check git blame and insert only valid reports' lines for lines,
# touched by commit on review
if not is_report_in_commit(afile, lineno, commit['git_hash']):
continue
# Form proper data structure:
# https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.0/rest-api-changes.html#comment-input
report['comments'][afile].append({
'path': afile,
'line': lineno,
'message': f"[{severity}] {err_msg}"
})
def parse_flake8_output(report, commit, out, err):
for line in out.strip().split("\n"):
if len(line.strip()) == 0:
continue
afile, lineno, severity, err_msg = line.split("###")
# Such cases is possible:
# ######information###Cppcheck cannot find all the include files
if afile == "" or lineno == "":
continue
# Check git blame and insert only valid reports' lines for lines,
# touched by commit on review
if not is_report_in_commit(afile, lineno, commit['git_hash']):
continue
# Form proper data structure:
# https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.0/rest-api-changes.html#comment-input
report['comments'][afile].append({
'path': afile,
'line': lineno,
'message': f"[{severity}] {err_msg}"
})
def parse_kernel_doc_output(report, commit, out, err):
# convert the list of lines into an iterator (lines) for further usage
# of next()
lines = iter(err.strip().split("\n"))
for line in lines:
if len(line.strip()) == 0:
continue
# Filter-out lines concerning deleted files:
# 'Error: Cannot open file deleted/file/displayed/in/git/name.h'
if not ("warning:" in line or "error:" in line):
continue
afile, lineno, severity, err_msg = line.split(":", maxsplit=3)
# filter-out such non-informative lines in report:
# common/log.c:198: info: Scanning doc for log_dispatch
if "info" in severity:
continue
# Multi-line Handling:
# after parsing the initial line, we peek at subsequent lines.
# * if the next line starts with whitespace (indicating
# continuation), it's appended to the current message.
# * If not, it's put back into the iterator for the next iteration.
additional_lines = []
while True:
try:
next_line = next(lines)
if next_line.startswith(" "):
additional_lines.append(next_line)
else:
# Next line is a new warning - reconstruct the iterator
lines = (aline for aline in [next_line] + list(lines))
break
except StopIteration:
break # End of lines
# Combine additional lines if any
if additional_lines:
err_msg += "\n" + "\n".join(additional_lines)
# Check git blame and insert only valid reports' lines for lines,
# touched by commit on review
if not is_report_in_commit(afile, lineno, commit['git_hash']):
continue
# Form proper data structure:
# https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.0/rest-api-changes.html#comment-input
report['comments'][afile].append({
'path': afile,
'line': lineno,
'message': f"```\n[{severity}]: {err_msg}\n```"
})
def parse_codespell_output(report, commit, out, err):
for line in out.strip().split("\n"):
if len(line.strip()) == 0:
continue
afile, lineno, err_msg = line.split(":")
# Commented code is from cppcheck:
#if afile == "" or lineno == "":
# continue
# Check git blame and insert only valid reports' lines for lines,
# touched by commit on review
if not is_report_in_commit(afile, lineno, commit['git_hash']):
continue
# Form proper data structure:
# https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.0/rest-api-changes.html#comment-input
report['comments'][afile].append({
'path': afile,
'line': lineno,
'message': f"misspell: {err_msg}"
})
def parse_shellcheck_output(report, commit, out, err):
# Filter-out the section 'For more information:' at the end of the report
out = out.strip().split("For more information:")[0]
# The lines 'In script.sh line 1:' are delimited by "\n\n\n"
for line in out.strip().split("\n\n\n"):
if len(line.strip()) == 0:
continue
# "In script.sh line 1:\n"
# "blabla \n -- SC2148 (error): Add a shebang."
afile_lineno, err_msg = line.split(":\n", maxsplit=1)
# ['In', 'script.sh', 'line', '1']
afile = afile_lineno.split()[1]
lineno = afile_lineno.split()[3]
# Such cases is possible:
# ######information###Cppcheck cannot find all the include files
if afile == "" or lineno == "":
continue
# Check git blame and insert only valid reports' lines for lines,
# touched by commit on review
if not is_report_in_commit(afile, lineno, commit['git_hash']):
continue
# Form proper data structure:
# https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.0/rest-api-changes.html#comment-input
report['comments'][afile].append({
'path': afile,
'line': lineno,
'message': f"```\n{err_msg}\n```"
})
def parse_checkpatch_output(report, commit, out, err):
import pprint
paragraphs_dirty = out.strip().split('\n\n')
paragraphs = []
# Skip unneeded info from checkpatch: delimiter lines, commit headers, and
# summary lines
for paragraph in paragraphs_dirty:
skip_paragraph = False
lines = paragraph.split('\n')
lines_new = []
for line in lines:
# Skip delimiter lines, commit headers, and summary lines
if (line.startswith('------') or
line.startswith('Commit ') or
line.startswith('total:') or
'has style problems' in line):
continue
if line.startswith('NOTE:'):
# Current paragraph contains only multi-line 'NOTE' hence mark this
# whole paragraph for skipping
skip_paragraph = True
break
#print(f"Preserve line:{line}")
lines_new.append(line)
if skip_paragraph:
continue
# If paragraph is empty (no lines) - skip
if not len(lines_new):
continue
paragraphs.append('\n'.join(lines_new))
# Typical checkpatch's errors reports look like:
# 1) can be just simple:
# 'WARNING: DT binding docs and includes should be a separate patch.<...>'
# 2) In-file comment
# ```
# WARNING|CHECK|ERROR: Alignment should match open parenthesis
# #266: FILE: drivers/some/path/some.c:226:
# + arm_smccc_smc(SOME, ANOTHER, id,
# + EXTRA_PARAM, 0, 0, 0, 0, &res);
# ```
# 3) In-body comment:
# ```
# ERROR: Remove Gerrit Change-Id's before submitting upstream
# #61:
# Change-Id: I8d28d6953006cacb0d7e9e8467bf0eaa82615c71
# ```
for paragraph in paragraphs:
print('===========')
pprint.pprint(paragraph)
print('===========\n')
msg = []
comment_type = 'simple'
afile = None
lineno = None
err_msg = None
for line in paragraph.split('\n'):
if not line.startswith('#'):
msg.append(line)
continue
words = line.split(':')
# Here words' items can have spaces, like:
# ['#266', ' FILE', ' drivers/some/path/some.c', '226', '']
words = [ s.strip() for s in words ]
if 'FILE' in words:
comment_type = 'file'
afile = words[2].strip()
lineno = words[3].strip()
else:
comment_type = 'body'
lineno = words[0][1:] # get rid of shebang sign in '#<lineno>'
err_msg = f"{msg[0]}\n"
# For 'in-file' comment, usually, the 1st line is actual checkpatch error,
# other lines are code snippet.
if comment_type in ['file', 'body']:
err = '\n'.join(msg[1:])
err_msg += f"```\n{err}\n```"
print(f"File:{afile}, lineno:{lineno}, err_msg:")
print(err_msg)
if comment_type in ['simple', 'body']:
report["message"] += err_msg
continue
# Check git blame and insert only valid reports' lines for lines,
# touched by commit on review
if not is_report_in_commit(afile, lineno, commit['git_hash']):
continue
# Form proper data structure:
# https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.0/rest-api-changes.html#comment-input
report['comments'][afile].append({
'path': afile,
'line': lineno,
'message': f"{err_msg}"
})
def tool_is_stop_execution(analyzer_config, commit, ret, out, err):
"""
Function returns conclusion 'whether to stop command execution (due to
command failure, no errors found) or not'?
"""
def tool_failure_log(progname, commit, ret, out, err):
print(f"Review: {commit['url']}")
print(f"\tSubject: {commit['subject']}")
print(f"\t\t[{progname}] failed with err:{ret}")
print(f"stderr:\n\t{err}")
print(f"stdout:\n\t{out}")
def tool_has_no_problems_log(progname, commit):
print(f"Review: {commit['url']}")
print(f"\tSubject: {commit['subject']}")
print(f"\t\t[{progname}] has NO problems.")
progname = analyzer_config['progname']
# Handle 'ret'
if ret != 0:
if progname in ['cppcheck', 'kernel-doc', 'flake8']:
tool_failure_log(progname, commit, ret, out, err)
return True
elif progname in ['codespell']:
# In case of error 65 is returned:
# https://github.com/codespell-project/codespell/blob/246fbf9b172c14256c68f22a98e35fde7d9c8692/codespell_lib/_codespell.py#L1349
# In case of success 0 is returned.
if ret != 65:
tool_failure_log(progname, commit, ret, out, err)
return True
elif progname in ['shellcheck']:
# 0 - no bugs found
# 1 - bugs in script found
# Catch possible rare caces
if ret != 1:
tool_failure_log(progname, commit, ret, out, err)
return True
# Handle out/err
if progname in ['cppcheck', 'kernel-doc']:
if len(err) == 0:
tool_has_no_problems_log(progname, commit)
# From the analyzer's point of view the absence of errors in code
# is the reason for stop further command processing
return True
return False
elif progname in ['flake8', 'codespell', 'shellcheck']:
if len(out) == 0:
tool_has_no_problems_log(progname, commit)
return True
return False
# fullpath: app (that is in PATH) or app's fullpath - see analyzers.yaml
# progname: analyzer name to be disbplayed in logs, e.g. cppcheck_c &
# cppcheck_cxx will be displayed as cppcheck in logs
#
# TODO: migrate this json to analyzers.yaml
analyzer_configs = {
'cppcheck_c': {
'progname': 'cppcheck',
'command': 'cppcheck',
'command_args': [
'--quiet', '--enable=all', '--inconclusive', '-f', '--language=c',
'--template={file}###{line}###{severity}###{message}'
],
'file_extensions': ['.h', '.c'],
'fullpath': '/home/19262532/Work/sources/analyzers/cppcheck/build/bin/cppcheck',
'output_parser': parse_cppcheck_output
},
'cppcheck_cxx': {
'progname': 'cppcheck',
'command': 'cppcheck',
'command_args': [
'--quiet', '--enable=all', '--inconclusive', '-f', '--language=c++',
'--template={file}###{line}###{severity}###{message}'
],
'file_extensions': ['.hpp', '.cc', '.cpp'],
'fullpath': '/home/19262532/Work/sources/analyzers/cppcheck/build/bin/cppcheck',
'output_parser': parse_cppcheck_output
},
'flake8': {
'progname': 'flake8',
'command': 'flake8',
'command_args': [
'--exit-zero',
'--format=%(path)s###%(row)d###%(code)s###%(text)s'
],
'file_extensions': ['.py'],
'fullpath': 'FLAKE8',
'output_parser': parse_flake8_output
},
'kernel_doc': {
'progname': 'kernel-doc',
'command': './scripts/kernel-doc',
'command_args': [
'-v', '-none'
],
'file_extensions': ['.c', '.h'],
'fullpath': 'KERNEL_DOC',
'output_parser': parse_kernel_doc_output
},
'codespell': {
'progname': 'codespell',
'command': 'codespell',
'command_args': ['--skip "obj,.git,tags"'],
'file_extensions': [], # All files
'fullpath': 'CODESPELL',
'output_parser': parse_codespell_output
},
'shellcheck': {
'progname': 'shellcheck',
'command': 'shellcheck',
'command_args': [
'--color=never',
'--exclude=SC1091'
],
'file_extensions': ['.sh'],
'fullpath': 'SHELLCHECK',
'output_parser': parse_shellcheck_output
},
'checkpatch': {
'progname': 'checkpatch',
'command': './scripts/checkpatch.pl',
# Due to nature of CodeCritic (it does git checkout on every commit in
# branch), it's enough to call checkpatch for particular commit via
# 'HEAD~1..HEAD'. So no files are needed, hence use fake ext.
'command_args': [
'--strict',
'-g',
'HEAD~1..HEAD'
],
'file_extensions': ['.checkpatch'], # fake ext
'fullpath': 'CHECKPATCH',
'output_parser': parse_checkpatch_output
},
}
def run_analyzer(commits, analyzer_config):
"""
Generic function to run any code analyzer on 'commits' with help of
'analyzer_config'
Args:
commits: List of commit dictionaries to analyze
analyzer_config: Dictionary containing analyzer-specific configuration
Returns:
Updated commits with analysis
"""
progname = analyzer_config['progname']
files_filter = tuple(analyzer_config['file_extensions'])
parse_tool_output = analyzer_config['output_parser']
print(f"Run {progname} analysis...")
tool_path = analyzer_config['command']
if analyzer_config['fullpath'] and os.path.exists(analyzer_config['fullpath']):
# use pre-installed tool in specific your system location
tool_path = analyzer_config['fullpath']
tool = local[tool_path][analyzer_config['command_args']]
for commit in commits:
commit['report'] = {}
print(f"\n=== Analyze: {commit['subject']}")
git("checkout", commit['git_hash'])
files = filter_files(commit['files'], files_filter)
if not files:
# Avoid calling the tool with empty files' list, because different
# tools behave differently with empty files list
print(f"No files to process for commit:{commit['git_hash']}. "
"Go to next...")
continue
ret, out, err = tool.run(
files,
retcode=None
)
if tool_is_stop_execution(analyzer_config, commit, ret, out, err):
continue
report = {}
# "message" is printed not in-file (i.e. as Reply to whole patch)
# Uncomment line below, when you will be ready to reveal yourself
# as person, using Gerra & CodeCritic
#report["message"] = f"[{progname}] Some issues need to be fixed."
report["message"] = "I've found some issues"
report["comments"] = defaultdict(list)
parse_tool_output(report, commit, out, err)
commit['report'].update(report)
def analyze(args, ch_api, commits):
analyzers = []
if "all" in args.analyzer and len(args.analyzer) > 1:
args.analyzer.remove("all")
analyzers.extend(args.analyzer)
elif "all" in args.analyzer and len(args.analyzer) == 1:
analyzers.extend(ANALYZERS_MAP.keys())
for a in analyzers:
run_analyzer(commits, analyzer_configs[a])
if args.local:
for commit in commits:
if not commit['report']:
continue
print("Review: {}".format(commit['url']))
print("\tSubject: {}".format(commit['subject']))
print(commit['report'])
print("\n")
continue
return
# Send report to Gerrit: commit-by-commit & file-by-file
print("\n")
for commit in commits:
if not commit['report']:
# Some comits can have no report at all
continue
for afile in commit['report']['comments']:
# https://gerrit-documentation.storage.googleapis.com/Documentation/3.3.0/rest-api-changes.html#comment-input
print(f"Send comments to Gerrit for {afile}:")
print("\t\t{}".format(commit['subject']))
print("\t\t{}".format(commit['url']))
CommentInput_list = commit['report']['comments'][afile]
ch_api.put_draft(commit['triplet_id'], commit['revision_id'],
CommentInput_list)
def parse_args():
global GERRIT_SERVER_URL
conf = yamlm.load_config(f"{SCRIPT_DIR}/gerrit_settings.yaml")
GERRIT_USER = conf["GERRIT_USER"]
GERRIT_SERVER_URL = conf["GERRIT_URL"]
GERRIT_HTTP_CRED_PWD = conf["GERRIT_HTTP_CRED_PWD"]
ap = argparse.ArgumentParser(
description=("Analyze gerrit-URL, git commit, ?files? and report "
"criticism on gerrit or locally.")
)
# Mutually exclusive group
xgroup = ap.add_mutually_exclusive_group(required=True)
xgroup.add_argument("--url", type=str,
help=("Gerrit review URL. URL is applied to repo, from which the "
"script is launched. URL can point to single commit or tip of "
"branch from gerrit. Applied review looks like if you click "
"'Download patch'-> 'Branch' -> and paste this command into "
"your terminal.")
)
xgroup.add_argument("-c", "--commit", type=str,
help="Git commit hash to be checked. Default is current 'HEAD'",
default="HEAD"
)
xgroup.add_argument("-f", "--file", type=str,
help="Specific file to be checked. (Report is local)."
)
xgroup.add_argument("-F", "--files-list", type=str,
help=("Check files, listed in file, specified by option. "
"(Report is local).")
)
ap.add_argument("-u", "--user", type=str,
help=("Gerrit user name. Default is name from {}/gerrit_settings.yaml".
format(SCRIPT_DIR)),
default=GERRIT_USER
)
ap.add_argument("-p", "--password", type=str,
help='Gerrit HTTP password (from "HTTP Credentials" tab)',
default=GERRIT_HTTP_CRED_PWD,
)
ap.add_argument("-a", "--analyzer",
help="Analyzer to be launched",
choices=list(analyzer_configs.keys()) + ["all"],
default=["all"],
action="append"
)
ap.add_argument("-l", "--local",
help=("Output report locally instead of submitting it to Gerrit"),
default=False,
action="store_true"
)
ap.add_argument("--force_no_verify",
help=("Allow requests with SSL verification disabled"),
default=False,
action="store_true"
)
return ap.parse_args()
def main():
global GERRIT_URL_BASE
args = parse_args();
if args.force_no_verify:
# Suppress only the InsecureRequestWarning from urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
auth = HTTPBasicAuth(args.user, args.password)
restapi = GerritRestAPI(url=GERRIT_SERVER_URL, auth=auth,
verify=not args.force_no_verify)
ch_api = ch.EndPoint(restapi)
if args.url:
GERRIT_URL_BASE = args.url.split('+')[0]
commits = handle_url(args, ch_api)
elif args.commit:
handle_commit(args)
elif args.file:
handle_file(args)
elif args.files_list:
handle_flist(args)
analyze(args, ch_api, commits)
if __name__ == "__main__":
sys.exit(main())