Skip to content

Commit

Permalink
Adding support for displaying relative source filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
ALLARD Antoine committed May 23, 2023
1 parent cdf98f7 commit 97b9c9e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
27 changes: 27 additions & 0 deletions ValgrindCI/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def main():
help="specifies a substitution rule `from:to` for finding source files on disk. example: --substitute-path /foo:/bar",
nargs='?'
)
parser.add_argument(
"--relativize",
action="append",
help="specifies a prefix to remove from displayed source filenames. example: --relativize /foo/bar",
nargs='?'
)
parser.add_argument(
"--relativize-from-substitute-paths",
default=False,
action="store_true",
help="use the `from` values in the substitution rules as prefixes to remove from displayed source filenames",
)
parser.add_argument(
"--output-dir", help="directory where the HTML report will be generated"
)
Expand Down Expand Up @@ -67,6 +79,21 @@ def main():
substitute_paths.append({"from": s.split(":")[0], "to": s.split(":")[1] })
data.set_substitute_paths(substitute_paths)

if args.relativize:
prefixes = []
for p in args.relativize:
prefixes.append(p)
data.set_relative_prefixes(prefixes)

if args.relativize_from_substitute_paths:
if not args.substitute_path:
print("No substitution paths specified on the command line.")
else:
prefixes = data._relative_prefixes.copy()
for s in data._substitute_paths:
prefixes.append(s.get("from"))
data.set_relative_prefixes(prefixes)

errors_total = data.get_num_errors()
if args.abort_on_errors and errors_total != 0:
print("{} errors reported by Valgrind - Abort".format(errors_total))
Expand Down
13 changes: 13 additions & 0 deletions ValgrindCI/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def __init__(self) -> None:
self.errors: List[Error] = []
self._source_dir: Optional[str] = None
self._substitute_paths: Optional[List[dict]] = []
self._relative_prefixes: Optional[List[str]] = []

def parse(self, xml_file: str) -> None:
root = et.parse(xml_file).getroot()
Expand All @@ -135,11 +136,23 @@ def set_substitute_paths(self, substitute_paths: Optional[List[dict]]) -> None:
if substitute_paths is not None:
self._substitute_paths = substitute_paths

def set_relative_prefixes(self, relative_prefixes: Optional[str]) -> None:
if relative_prefixes is not None:
self._relative_prefixes = relative_prefixes

def substitute_path(self, path: str) -> str:
for s in self._substitute_paths:
path = path.replace(s.get("from"), s.get("to"))
return path

def relativize(self, path: str) -> str:
for p in self._relative_prefixes:
if path.startswith(p):
path = path.replace(p, "")
if path.startswith("/"):
path = path[1:]
return path

def get_num_errors(self) -> int:
if self._source_dir is None:
return len(self.errors)
Expand Down
10 changes: 5 additions & 5 deletions ValgrindCI/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def render(self, output_dir: str, lines_before: int, lines_after: int) -> None:
f.write(
self._source_tmpl.render(
num_errors=num_errors,
source_file_name=source_file,
source_file_name=self._data.relativize(source_file),
codelines=lines_of_code,
)
)

summary.append(
{
"filename": source_file,
"filename": self._data.relativize(source_file),
"errors": num_errors,
"link": html_filename,
}
Expand Down Expand Up @@ -121,9 +121,9 @@ def _extract_error_data(
assert error_line is not None
stack["line"] = error_line - lines_before - 1
stack["error_line"] = lines_before + 1
stack["fileref"] = "{}:{}".format(
frame.get_path(self._source_dir), error_line
)
frame_source = frame.get_path(self._source_dir)
frame_source = self._data.relativize(frame_source)
stack["fileref"] = "{}:{}".format(frame_source, error_line)
fullname = self._data.substitute_path(fullname)
try:
with open(fullname, "r", errors="replace") as f:
Expand Down

0 comments on commit 97b9c9e

Please sign in to comment.