Skip to content

Instantly share code, notes, and snippets.

@pansila
Created May 8, 2019 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pansila/79bc091bf4df6edec038444e9cd6921a to your computer and use it in GitHub Desktop.
Save pansila/79bc091bf4df6edec038444e9cd6921a to your computer and use it in GitHub Desktop.
cull the interested cpplint violations from the complete report
#!/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)
@pansila
Copy link
Author

pansila commented May 8, 2019

@echo off
SETLOCAL EnableDelayedExpansion
set X=
for /f "tokens=*" %%i IN ('git diff --name-only HEAD~') DO SET X=%%i !X!
REM echo %X%

py -2 -m cpplint --counting=detailed --output=vs7 --filter=-whitespace/tab,-whitespace/line_length,-readability/casting,-legal/copyright,-build/header_guard,-build/include,-build/include_subdir,-runtime/references,-runtime/int %X% 2>cpplint.log
cull_violation.py cpplint.log cpplint_diff.log
type cpplint_diff.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment