|  | 
|  | 1 | +#!/usr/bin/env python3 | 
|  | 2 | +""" | 
|  | 3 | +Fix whitespace issues in Cython files. | 
|  | 4 | +Removes trailing whitespace from blank lines (W293 errors). | 
|  | 5 | +""" | 
|  | 6 | +from pathlib import Path | 
|  | 7 | + | 
|  | 8 | + | 
|  | 9 | +def fix_whitespace_in_file(filepath: Path) -> bool: | 
|  | 10 | +    """ | 
|  | 11 | +    Fix whitespace issues in a single file. | 
|  | 12 | +     | 
|  | 13 | +    Returns: | 
|  | 14 | +        bool: True if file was modified, False otherwise | 
|  | 15 | +    """ | 
|  | 16 | +    try: | 
|  | 17 | +        with open(filepath, 'r', encoding='utf-8') as f: | 
|  | 18 | +            original_content = f.read() | 
|  | 19 | +         | 
|  | 20 | +        # Remove trailing whitespace from all lines | 
|  | 21 | +        # This fixes W293 (blank line contains whitespace) and similar issues | 
|  | 22 | +        lines = original_content.splitlines(keepends=True) | 
|  | 23 | +        fixed_lines = [line.rstrip() + ('\n' if line.endswith('\n') else '') for line in lines] | 
|  | 24 | +        fixed_content = ''.join(fixed_lines) | 
|  | 25 | +         | 
|  | 26 | +        # Remove trailing newline at end of file if present | 
|  | 27 | +        fixed_content = fixed_content.rstrip() + '\n' | 
|  | 28 | +         | 
|  | 29 | +        if fixed_content != original_content: | 
|  | 30 | +            with open(filepath, 'w', encoding='utf-8') as f: | 
|  | 31 | +                f.write(fixed_content) | 
|  | 32 | +            return True | 
|  | 33 | +        return False | 
|  | 34 | +    except (OSError, UnicodeDecodeError) as e: | 
|  | 35 | +        print(f"Error processing {filepath}: {e}") | 
|  | 36 | +        return False | 
|  | 37 | + | 
|  | 38 | + | 
|  | 39 | +def main(): | 
|  | 40 | +    """Fix whitespace in all Cython files.""" | 
|  | 41 | +    root = Path(__file__).parent.parent | 
|  | 42 | +    print(f"Scanning for .pyx files in {root}") | 
|  | 43 | +    pyx_files = list(root.rglob('opteryx/**/*.pyx')) | 
|  | 44 | +     | 
|  | 45 | +    if not pyx_files: | 
|  | 46 | +        print("No .pyx files found") | 
|  | 47 | +        return | 
|  | 48 | +     | 
|  | 49 | +    print(f"Found {len(pyx_files)} .pyx files") | 
|  | 50 | +    modified_count = 0 | 
|  | 51 | +     | 
|  | 52 | +    for filepath in sorted(pyx_files): | 
|  | 53 | +        if fix_whitespace_in_file(filepath): | 
|  | 54 | +            print(f"Fixed: {filepath.relative_to(root)}") | 
|  | 55 | +            modified_count += 1 | 
|  | 56 | +     | 
|  | 57 | +    print(f"\nFixed {modified_count} file(s)") | 
|  | 58 | + | 
|  | 59 | + | 
|  | 60 | +if __name__ == '__main__': | 
|  | 61 | +    main() | 
0 commit comments