-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3017 from jay24rajput/sphinx_parser
[report-converter] Sphinx parser
- Loading branch information
Showing
11 changed files
with
295 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
7 changes: 7 additions & 0 deletions
7
tools/report-converter/codechecker_report_converter/sphinx/__init__.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- |
36 changes: 36 additions & 0 deletions
36
tools/report-converter/codechecker_report_converter/sphinx/analyzer_result.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- | ||
|
||
from codechecker_report_converter.analyzer_result import AnalyzerResult | ||
|
||
from .output_parser import SphinxParser | ||
from ..plist_converter import PlistConverter | ||
|
||
|
||
class SphinxAnalyzerResult(AnalyzerResult): | ||
""" Transform analyzer result of Sphinx. """ | ||
|
||
TOOL_NAME = 'sphinx' | ||
NAME = 'Sphinx' | ||
URL = 'https://github.com/sphinx-doc/sphinx' | ||
|
||
def parse(self, analyzer_result): | ||
""" Creates plist files from the given analyzer result to the given | ||
output directory. | ||
""" | ||
parser = SphinxParser(analyzer_result) | ||
|
||
content = self._get_analyzer_result_file_content(analyzer_result) | ||
if not content: | ||
return | ||
|
||
messages = parser.parse_messages(content) | ||
|
||
plist_converter = PlistConverter(self.TOOL_NAME) | ||
plist_converter.add_messages(messages) | ||
return plist_converter.get_plist_results() |
61 changes: 61 additions & 0 deletions
61
tools/report-converter/codechecker_report_converter/sphinx/output_parser.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- | ||
|
||
import logging | ||
import os | ||
import re | ||
|
||
from ..output_parser import BaseParser, Message | ||
LOG = logging.getLogger('ReportConverter') | ||
|
||
|
||
class SphinxParser(BaseParser): | ||
""" | ||
Parser for Sphinx Output | ||
""" | ||
|
||
def __init__(self, analyzer_result): | ||
super(SphinxParser, self).__init__() | ||
|
||
self.analyzer_result = analyzer_result | ||
|
||
self.message_line_re = re.compile( | ||
# File path starting with '/' and followed by a ':'. | ||
r'^(?P<path>\/[\S ]+?):' | ||
# Line number followed by a ':'. | ||
r'(?P<line>\d+?):\s' | ||
# Message. | ||
r'(?P<message>[\S \t]+)\s*') | ||
|
||
def parse_message(self, it, line): | ||
""" | ||
Actual Parsing function for the given line | ||
It is expected that each line contains a seperate report | ||
""" | ||
match = self.message_line_re.match(line) | ||
|
||
if match is None: | ||
return None, next(it) | ||
|
||
file_path = os.path.normpath( | ||
os.path.join(os.path.dirname(self.analyzer_result), | ||
match.group('path'))) | ||
|
||
checker_name = None | ||
|
||
message = Message( | ||
file_path, | ||
int(match.group('line')), | ||
0, | ||
match.group('message').strip(), | ||
checker_name) | ||
|
||
try: | ||
return message, next(it) | ||
except StopIteration: | ||
return message, '' |
4 changes: 4 additions & 0 deletions
4
tools/report-converter/tests/unit/sphinx_output_test_files/files/sample.rst
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Sample Test File | ||
---------------------------------- | ||
|
||
This is a sample test file for testing the output of sphinx parser |
69 changes: 69 additions & 0 deletions
69
tools/report-converter/tests/unit/sphinx_output_test_files/sample.expected.plist
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>diagnostics</key> | ||
<array> | ||
<dict> | ||
<key>category</key> | ||
<string>unknown</string> | ||
<key>check_name</key> | ||
<string>sphinx</string> | ||
<key>description</key> | ||
<string>WARNING: Title underline too short.</string> | ||
<key>issue_hash_content_of_line_in_context</key> | ||
<string>cb9e277095a47d920ef5a846b3af91fa</string> | ||
<key>location</key> | ||
<dict> | ||
<key>col</key> | ||
<integer>0</integer> | ||
<key>file</key> | ||
<integer>0</integer> | ||
<key>line</key> | ||
<integer>2</integer> | ||
</dict> | ||
<key>path</key> | ||
<array> | ||
<dict> | ||
<key>depth</key> | ||
<integer>0</integer> | ||
<key>kind</key> | ||
<string>event</string> | ||
<key>location</key> | ||
<dict> | ||
<key>col</key> | ||
<integer>0</integer> | ||
<key>file</key> | ||
<integer>0</integer> | ||
<key>line</key> | ||
<integer>2</integer> | ||
</dict> | ||
<key>message</key> | ||
<string>WARNING: Title underline too short.</string> | ||
</dict> | ||
</array> | ||
<key>type</key> | ||
<string>sphinx</string> | ||
</dict> | ||
</array> | ||
<key>files</key> | ||
<array> | ||
<string>files/sample.rst</string> | ||
</array> | ||
<key>metadata</key> | ||
<dict> | ||
<key>analyzer</key> | ||
<dict> | ||
<key>name</key> | ||
<string>sphinx</string> | ||
</dict> | ||
<key>generated_by</key> | ||
<dict> | ||
<key>name</key> | ||
<string>report-converter</string> | ||
<key>version</key> | ||
<string>x.y.z</string> | ||
</dict> | ||
</dict> | ||
</dict> | ||
</plist> |
1 change: 1 addition & 0 deletions
1
tools/report-converter/tests/unit/sphinx_output_test_files/sample.out
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/files/sample.rst:2: WARNING: Title underline too short. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# ------------------------------------------------------------------------- | ||
# | ||
# Part of the CodeChecker project, under the Apache License v2.0 with | ||
# LLVM Exceptions. See LICENSE for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ------------------------------------------------------------------------- | ||
|
||
""" | ||
This module tests the correctness of the SphinxAnalyzerResult, | ||
which used in sequence transform Sphinx output to a plist file. | ||
""" | ||
|
||
|
||
import os | ||
import plistlib | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
||
|
||
from codechecker_report_converter.sphinx.analyzer_result import \ | ||
SphinxAnalyzerResult | ||
|
||
|
||
class SphinxResultTestCase(unittest.TestCase): | ||
""" Test the output of the SphinxAnalyzerResult. """ | ||
|
||
def setUp(self): | ||
""" Setup the test. """ | ||
self.analyzer_result = SphinxAnalyzerResult() | ||
self.cc_result_dir = tempfile.mkdtemp() | ||
self.test_files = os.path.join(os.path.dirname(__file__), | ||
'sphinx_output_test_files') | ||
|
||
def tearDown(self): | ||
""" Clean temporary directory. """ | ||
shutil.rmtree(self.cc_result_dir) | ||
|
||
def test_no_sphinx_output_file(self): | ||
""" Test transforming single rst file. """ | ||
analyzer_result = os.path.join(self.test_files, 'files', | ||
'sample.rst') | ||
|
||
ret = self.analyzer_result.transform(analyzer_result, | ||
self.cc_result_dir) | ||
self.assertFalse(ret) | ||
|
||
def test_transform_dir(self): | ||
""" Test transforming a directory. """ | ||
analyzer_result = os.path.join(self.test_files) | ||
|
||
ret = self.analyzer_result.transform(analyzer_result, | ||
self.cc_result_dir) | ||
self.assertFalse(ret) | ||
|
||
def test_transform_single_file(self): | ||
""" Test transforming single output file. """ | ||
analyzer_result = os.path.join(self.test_files, 'sample.out') | ||
self.analyzer_result.transform(analyzer_result, self.cc_result_dir) | ||
|
||
plist_file = os.path.join(self.cc_result_dir, | ||
'sample.rst_sphinx.plist') | ||
|
||
with open(plist_file, mode='rb') as pfile: | ||
res = plistlib.load(pfile) | ||
|
||
# Use relative path for this test. | ||
res['files'][0] = os.path.join('files', 'sample.rst') | ||
|
||
self.assertTrue(res['metadata']['generated_by']['version']) | ||
res['metadata']['generated_by']['version'] = "x.y.z" | ||
|
||
plist_file = os.path.join(self.test_files, | ||
'sample.expected.plist') | ||
with open(plist_file, mode='rb') as pfile: | ||
exp = plistlib.load(pfile) | ||
|
||
self.assertEqual(res, exp) |