Created
May 8, 2019 01:30
-
-
Save pansila/79bc091bf4df6edec038444e9cd6921a to your computer and use it in GitHub Desktop.
cull the interested cpplint violations from the complete report
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, os | |
from sys import argv | |
from unidiff import PatchSet | |
import subprocess | |
def parse_log_line(line): | |
""" | |
parse a line from the cpplint results with --output=vs7 | |
""" | |
if ":" not in line: | |
return None | |
file_lineno = line.split(":")[0] | |
if "(" not in file_lineno: | |
return None | |
(file, lineno) = file_lineno.split("(") | |
file = file.replace("\\", "/") # for Windows | |
lineno = lineno[:-1] | |
try: | |
lineno = int(lineno) | |
except: | |
return None | |
return (file, lineno) | |
def cull_violations(inputFile, outputFile, diff): | |
cull_list = [] | |
with open(inputFile) as input: | |
try: | |
patch_files = PatchSet(diff, encoding='utf-8') | |
except UnicodeDecodeError as e: | |
print(e) | |
return 0 | |
for line in input: | |
parsedLine = parse_log_line(line) | |
if parsedLine is None: | |
continue | |
for patch in patch_files: | |
if patch.path != parsedLine[0]: | |
continue | |
for p in patch: | |
for l in p.target_lines(): | |
# if parsedLine[1] == 943: | |
# print(l.target_line_no, parsedLine[1], patch, l, l.is_context, l.is_removed, l.is_added) | |
if any((l.is_removed, l.is_context)): | |
continue | |
if l.target_line_no != parsedLine[1]: | |
continue | |
cull_list.append(line) | |
with open(outputFile, 'w') as out: | |
if len(cull_list) == 0: | |
out.write("\n") | |
return 0 | |
for line in cull_list: | |
out.write(line) | |
return 1 | |
if __name__ == "__main__": | |
if len(argv) < 3: | |
print("Usage: %s {input log file} {output log file} <diff file>" % argv[0]) | |
sys.exit(1) | |
if len(argv) != 4: | |
diff = subprocess.check_output("git format-patch -1 --stdout HEAD", shell=True) | |
else: | |
with open(argv[3]) as f: | |
diff = f.read() | |
ret = cull_violations(argv[1], argv[2], diff) | |
sys.exit(ret) |
Author
pansila
commented
May 8, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment