|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate minified versions of source library files. |
| 4 | +This script processes source files from cp-algo/ and creates |
| 5 | +minified versions in two places: |
| 6 | +1. .competitive-verifier/minified/ (for CI/documentation) |
| 7 | +2. cp-algo/min/ (committed to repo for direct access) |
| 8 | +
|
| 9 | +Unlike bundled versions, minified versions preserve the original file structure |
| 10 | +without inlining dependencies. |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import re |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | + |
| 19 | +def minify_cpp(code): |
| 20 | + """Minify C++ code while preserving header guards and removing unnecessary whitespace.""" |
| 21 | + lines = code.split('\n') |
| 22 | + result = [] |
| 23 | + in_multiline_comment = False |
| 24 | + header_guard = None |
| 25 | + endif_lines = [] |
| 26 | + |
| 27 | + for i, line in enumerate(lines): |
| 28 | + stripped = line.strip() |
| 29 | + |
| 30 | + # Handle multiline comments |
| 31 | + if '/*' in stripped: |
| 32 | + in_multiline_comment = True |
| 33 | + if '*/' in stripped: |
| 34 | + in_multiline_comment = False |
| 35 | + continue |
| 36 | + if in_multiline_comment: |
| 37 | + continue |
| 38 | + |
| 39 | + # Remove single-line comments |
| 40 | + if '//' in stripped: |
| 41 | + stripped = stripped.split('//')[0].strip() |
| 42 | + |
| 43 | + # Skip empty lines |
| 44 | + if not stripped: |
| 45 | + continue |
| 46 | + |
| 47 | + # Detect and preserve header guards |
| 48 | + if stripped.startswith('#ifndef '): |
| 49 | + header_guard = stripped |
| 50 | + result.append(stripped) |
| 51 | + continue |
| 52 | + elif stripped.startswith('#define ') and header_guard and len(result) == 1: |
| 53 | + result.append(stripped) |
| 54 | + continue |
| 55 | + elif stripped == '#endif' and header_guard: |
| 56 | + endif_lines.append(stripped) |
| 57 | + continue |
| 58 | + |
| 59 | + # Keep preprocessor directives as-is (they need to be on own lines) |
| 60 | + if stripped.startswith('#'): |
| 61 | + result.append(stripped) |
| 62 | + continue |
| 63 | + |
| 64 | + # Compress spaces in code lines (but preserve strings) |
| 65 | + def compress_line(line): |
| 66 | + # Split by strings to preserve their content |
| 67 | + parts = re.split(r'("(?:[^"\\]|\\.)*")', line) |
| 68 | + for i in range(0, len(parts), 2): |
| 69 | + # Compress multiple spaces to one |
| 70 | + parts[i] = re.sub(r'\s+', ' ', parts[i]) |
| 71 | + # Remove spaces around operators (but keep space for clarity in some cases) |
| 72 | + parts[i] = re.sub(r'\s*([+\-*/%=<>!&|^~,;:?(){}[\]])\s*', r'\1', parts[i]) |
| 73 | + # Remove leading/trailing spaces |
| 74 | + parts[i] = parts[i].strip() |
| 75 | + return ''.join(parts) |
| 76 | + |
| 77 | + compressed = compress_line(stripped) |
| 78 | + if compressed: |
| 79 | + result.append(compressed) |
| 80 | + |
| 81 | + # Join lines - try to put on same line when possible |
| 82 | + code = '\n'.join(result) |
| 83 | + |
| 84 | + # Remove newlines after opening braces and before closing braces |
| 85 | + code = re.sub(r'\{\n', '{', code) |
| 86 | + code = re.sub(r'\n\}', '}', code) |
| 87 | + |
| 88 | + # Remove newlines around colons in class/struct definitions |
| 89 | + code = re.sub(r'\n:', ':', code) |
| 90 | + code = re.sub(r':\n', ':', code) |
| 91 | + |
| 92 | + # Remove multiple consecutive newlines (keep max 1) |
| 93 | + code = re.sub(r'\n\n+', '\n', code) |
| 94 | + |
| 95 | + # Add back endif if we had header guards |
| 96 | + if endif_lines: |
| 97 | + code = code + '\n' + '\n'.join(endif_lines) |
| 98 | + |
| 99 | + return code |
| 100 | + |
| 101 | + |
| 102 | +def process_file(bundled_path, minified_path, committed_path=None): |
| 103 | + """Process a single file and create minified version(s).""" |
| 104 | + try: |
| 105 | + with open(bundled_path, 'r', encoding='utf-8') as f: |
| 106 | + code = f.read() |
| 107 | + |
| 108 | + minified_code = minify_cpp(code) |
| 109 | + |
| 110 | + # Create minified version in .competitive-verifier/minified/ |
| 111 | + minified_path.parent.mkdir(parents=True, exist_ok=True) |
| 112 | + with open(minified_path, 'w', encoding='utf-8') as f: |
| 113 | + f.write(minified_code) |
| 114 | + |
| 115 | + # Also create in committed minified/ directory if path provided |
| 116 | + if committed_path: |
| 117 | + committed_path.parent.mkdir(parents=True, exist_ok=True) |
| 118 | + with open(committed_path, 'w', encoding='utf-8') as f: |
| 119 | + f.write(minified_code) |
| 120 | + |
| 121 | + original_size = len(code) |
| 122 | + minified_size = len(minified_code) |
| 123 | + reduction = original_size - minified_size |
| 124 | + reduction_pct = 100 * (1 - minified_size / original_size) if original_size > 0 else 0 |
| 125 | + |
| 126 | + print(f" {bundled_path.name}: {original_size:,} → {minified_size:,} bytes (-{reduction_pct:.1f}%)") |
| 127 | + return True |
| 128 | + except Exception as e: |
| 129 | + print(f" ERROR processing {bundled_path}: {e}", file=sys.stderr) |
| 130 | + return False |
| 131 | + |
| 132 | + |
| 133 | +def main(): |
| 134 | + # Source directory to minify |
| 135 | + source_dir = Path('cp-algo') |
| 136 | + |
| 137 | + # Output directories |
| 138 | + minified_ci_dir = Path('.competitive-verifier/minified') |
| 139 | + minified_committed_dir = Path('cp-algo/min') |
| 140 | + |
| 141 | + # Verify source directory exists |
| 142 | + if not source_dir.exists(): |
| 143 | + print(f"Error: {source_dir} does not exist", file=sys.stderr) |
| 144 | + sys.exit(1) |
| 145 | + |
| 146 | + # Clear output directories |
| 147 | + if minified_ci_dir.exists(): |
| 148 | + import shutil |
| 149 | + shutil.rmtree(minified_ci_dir) |
| 150 | + |
| 151 | + minified_ci_dir.mkdir(parents=True, exist_ok=True) |
| 152 | + minified_committed_dir.mkdir(parents=True, exist_ok=True) |
| 153 | + |
| 154 | + print("Generating minified versions from source files...") |
| 155 | + |
| 156 | + total_files = 0 |
| 157 | + processed_files = 0 |
| 158 | + |
| 159 | + # Process all source files in cp-algo (but not in cp-algo/min itself) |
| 160 | + for src_file in source_dir.rglob('*'): |
| 161 | + # Skip files in min directory |
| 162 | + if 'min' in src_file.parts: |
| 163 | + continue |
| 164 | + |
| 165 | + if src_file.is_file() and src_file.suffix in ['.hpp', '.cpp', '.h']: |
| 166 | + total_files += 1 |
| 167 | + |
| 168 | + # Calculate relative path within cp-algo |
| 169 | + rel_path = src_file.relative_to(source_dir) |
| 170 | + |
| 171 | + # Output paths |
| 172 | + minified_ci_file = minified_ci_dir / 'cp-algo' / rel_path |
| 173 | + minified_committed_file = minified_committed_dir / rel_path |
| 174 | + |
| 175 | + if process_file(src_file, minified_ci_file, minified_committed_file): |
| 176 | + processed_files += 1 |
| 177 | + |
| 178 | + print(f"\nProcessed {processed_files}/{total_files} files") |
| 179 | + print(f"Generated in:") |
| 180 | + print(f" - .competitive-verifier/minified/cp-algo/") |
| 181 | + print(f" - cp-algo/min/") |
| 182 | + |
| 183 | + if processed_files < total_files and total_files > 0: |
| 184 | + sys.exit(1) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == '__main__': |
| 188 | + main() |
0 commit comments