From f416b79ac7331b99b40fbb058c1ba56b064fa9b7 Mon Sep 17 00:00:00 2001 From: Wonyoung Choi Date: Mon, 24 Dec 2018 10:54:21 +0900 Subject: [PATCH] [Build] Report build error by CodeChecker --- tools/scripts/CodeChecker/BuildLog.py | 47 ++++++++++++++-------------------- tools/scripts/CodeChecker/PRManager.py | 8 ++---- tools/scripts/CodeChecker/main.py | 45 ++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 51 deletions(-) diff --git a/tools/scripts/CodeChecker/BuildLog.py b/tools/scripts/CodeChecker/BuildLog.py index 4896b2c..ac6af0e 100644 --- a/tools/scripts/CodeChecker/BuildLog.py +++ b/tools/scripts/CodeChecker/BuildLog.py @@ -1,31 +1,22 @@ -class BuildLog: - FILE = 0 - LINE_NUMBER = 1 - WARNING_CODE = 2 - MESSAGE = 3 - - def __init__(self, filePath): - self._file = open(filePath, 'r') - self._parseLog() - - def _parseLog(self): - self._lines = self._file.readlines() - self._lines[:] = [line for line in self._lines if ": warning" in line] - self.warnings = [[0 for i in range(4)] for y in range(len(self._lines))] +import re - for index, line in enumerate(self._lines): - self._lines[index] = line.split(":") - self._lines[index][0] = self._lines[index][0][:-1].split("(") - - self.warnings[index][self.FILE] = self._lines[index][0][0] - self.warnings[index][self.LINE_NUMBER] = int(self._lines[index][0][1].split(",")[0]) - self.warnings[index][self.WARNING_CODE] = self._lines[index][1] - self.warnings[index][self.MESSAGE] = self._lines[index][2].split("[/")[0] +class BuildLog: + def __init__(self, filePath): + self.warnings = [] + self.errors = [] + self._file = open(filePath, 'r') + self._parseLog() - def __del__(self): - self._file.close() + def _parseLog(self): + errorPattern = re.compile('(.+)\(([0-9]+),[0-9]+\): (error|warning) ([A-Z0-9]+): (.+) \[/') + for line in self._file.readlines(): + m = errorPattern.match(line) + if m is not None: + item = {'file': m.group(1), 'line': int(m.group(2)), 'type': m.group(3), 'code': m.group(4), 'message': m.group(5)} + if item['type'] == 'warning': + self.warnings.append(item) + elif item['type'] == 'error': + self.errors.append(item) -if __name__ == "__main__": - logPath = "../../build.log" - logs = BuildLog(logPath) - print(logs.warnings) + def __del__(self): + self._file.close() diff --git a/tools/scripts/CodeChecker/PRManager.py b/tools/scripts/CodeChecker/PRManager.py index 6d87634..6b3ef2a 100644 --- a/tools/scripts/CodeChecker/PRManager.py +++ b/tools/scripts/CodeChecker/PRManager.py @@ -58,9 +58,5 @@ class PRManager: if self._CheckComment(file.filename, _position, body): self.pr.create_review_comment(body, self.latestCommit, file.filename, _position) -if __name__ == "__main__": - pr = 9 - gh = PRManager(pr) - for file in gh.changedFiles: - print("Chaged File: " + file.filename) - + def CreateIssueComment(self, body): + self.pr.create_issue_comment(body) diff --git a/tools/scripts/CodeChecker/main.py b/tools/scripts/CodeChecker/main.py index 0a66d65..297b06d 100644 --- a/tools/scripts/CodeChecker/main.py +++ b/tools/scripts/CodeChecker/main.py @@ -5,22 +5,33 @@ from PRManager import PRManager logPath = "./build.log" if __name__ == "__main__": - if len(sys.argv) < 3: - print("Execute with token and the PR number") - print(" ~$ python main.py [Token] [PR Number]") - exit(1) - logs = BuildLog(logPath) - pr = PRManager(sys.argv[1], int(sys.argv[2])) - warningsInFile = [] - for file in pr.changedFiles: - if file.patch is None: - continue - warningsInFile = [warning for warning in logs.warnings if file.filename.endswith(warning[logs.FILE])] - for diffLine in pr.fileDiffHunkPairs[file]: - for warning in warningsInFile: - if (diffLine[0]) <= warning[BuildLog.LINE_NUMBER] and warning[BuildLog.LINE_NUMBER] < (diffLine[0] + diffLine[1]): - print("{}, Warning on line {}! -> ".format(file.filename, warning[BuildLog.LINE_NUMBER]) + warning[BuildLog.MESSAGE]) - _warningMessage = warning[BuildLog.WARNING_CODE] + ":" +warning[BuildLog.MESSAGE] - pr.CreateReviewComment(file, warning[BuildLog.LINE_NUMBER], _warningMessage) + if len(sys.argv) < 3: + print("Execute with token and the PR number") + print(" ~$ python main.py [Token] [PR Number]") + exit(1) + # Parse build.log + logs = BuildLog(logPath) + + # Parse PullRequest + pr = PRManager(sys.argv[1], int(sys.argv[2])) + + # Report Warnings + for file in pr.changedFiles: + if file.patch is None: + continue + warningsInFile = [warning for warning in logs.warnings if file.filename.endswith(warning['file'])] + for diffLine in pr.fileDiffHunkPairs[file]: + for warning in warningsInFile: + if (diffLine[0]) <= warning['line'] and warning['line'] < (diffLine[0] + diffLine[1]): + _warningMessage = 'warning ' + warning['code'] + ": " + warning['message'] + print("{}({}): {}".format(file.filename, warning['line'], _warningMessage)) + pr.CreateReviewComment(file, warning['line'], _warningMessage) + + # Report Errors + if len(logs.erros) > 0: + errBody = '### Build Error:\n' + for err in logs.errors: + errBody += '> {}({}): {}: {}\n'.format(err['file'], err['line'], err['code'], err['message']) + pr.CreateIssueComment(errBody) -- 2.7.4