Skip to content

Instantly share code, notes, and snippets.

@jasonm23
Last active November 24, 2023 18:14
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 jasonm23/de0dca73e7c453249f391583c611dca4 to your computer and use it in GitHub Desktop.
Save jasonm23/de0dca73e7c453249f391583c611dca4 to your computer and use it in GitHub Desktop.
See README.md

Left truncate on long pathnames in an llvm-cov HTML report.

Usage: $0 report_root_path source_starts_at_dir

For example the absolute, root path to a file.

/Long/pathname/that/eventually/gets/to/Source/app/module/something.ext

Will left truncate with a leading elipsis:

.../Source/app/module/something.ext
#!/usr/bin/env python3
import os
import sys
def modify_html_report(file_path, source_starts_at_dir):
with open(file_path, 'r') as file:
content = file.read()
start_tag = "<div class='source-name-title'><pre>"
end_tag = '</pre></div>'
start_index = content.find(start_tag)
end_index = content.find(end_tag, start_index)
while start_index != -1 and end_index != -1:
original_path = content[start_index + len(start_tag):end_index].strip()
if original_path.find(source_starts_at_dir) != -1:
print(f"found: {original_path}")
modified_path = original_path.split(f'/{source_starts_at_dir}/')[-1]
replacement = f".../{source_starts_at_dir}/{modified_path}"
print(f"modified to: {replacement}")
content = content[:start_index + len(start_tag)] + replacement + content[end_index:]
start_index = content.find(start_tag, start_index + len(replacement))
end_index = content.find(end_tag, start_index)
with open(file_path, 'w') as file:
file.write(content)
else:
print(f"No replacement in: {file_path}")
def main():
if len(sys.argv) != 3:
print("Usage: {} <reports_root_path> <source_starts_at_dir>".format(sys.argv[0]))
sys.exit(1)
reports_root = sys.argv[1]
source_starts_at_dir = sys.argv[2]
for root, _, files in os.walk(reports_root):
for file in files:
if file.endswith('.html'):
print(f"Checking: {file}")
file_path = os.path.join(root, file)
modify_html_report(file_path, source_starts_at_dir)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment