#!/usr/bin/env python3 """Create HTML files with line numbers for inline viewing.""" import os import sys import hashlib import html as html_module from pathlib import Path def create_html_files_for_logs(logs_dir, build_id): """Create .html files with line numbers for log files under 1MB.""" MAX_INLINE_SIZE = 1 * 1024 * 1024 # 1MB links_dir = os.path.join(logs_dir, '_links') # Create _links directory if it doesn't exist os.makedirs(links_dir, exist_ok=True) html_count = 0 file_mapping = {} # Map from original path to HTML path # Walk through all log files for root, dirs, filenames in os.walk(logs_dir): # Skip the _links directory itself if '_links' in root: continue for filename in filenames: file_path = os.path.join(root, filename) try: # Get file size size = os.path.getsize(file_path) if size < MAX_INLINE_SIZE: # Get relative path from logs_dir rel_path = os.path.relpath(file_path, logs_dir) # Generate unique HTML name by hashing the full path path_hash = hashlib.md5(rel_path.encode()).hexdigest()[:8] html_name = f"{filename}.{path_hash}.html" html_path = os.path.join(links_dir, html_name) # Read original file content with open(file_path, 'r', encoding='utf-8', errors='replace') as f: content = f.read() # Split into lines and add line numbers lines = content.split('\n') line_count = len(lines) line_number_width = len(str(line_count)) # Build content with line numbers numbered_lines = [] for i, line in enumerate(lines, 1): escaped_line = html_module.escape(line) line_num = str(i).rjust(line_number_width) numbered_lines.append(f'{line_num} {escaped_line}') numbered_content = '\n'.join(numbered_lines) # Wrap in HTML html_content = f''' {html_module.escape(filename)}
Invalid regex pattern
{numbered_content}
''' # Write HTML file with open(html_path, 'w', encoding='utf-8') as f: f.write(html_content) # Store mapping rel_html_path = f"logs/_links/{html_name}" file_mapping[rel_path] = rel_html_path html_count += 1 except Exception as e: print(f"WARNING: Could not create HTML for {file_path}: {e}", file=sys.stderr) print(f"Created {html_count} .html files for inline viewing", file=sys.stderr) return file_mapping def main(): if len(sys.argv) < 3: print("Usage: create_inline_html_files.py ") sys.exit(1) logs_dir = sys.argv[1] build_id = sys.argv[2] if not os.path.exists(logs_dir): print(f"ERROR: Logs directory not found: {logs_dir}", file=sys.stderr) sys.exit(1) file_mapping = create_html_files_for_logs(logs_dir, build_id) # Output mapping as JSON for use by other scripts import json print(json.dumps(file_mapping)) if __name__ == '__main__': main()