|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Finds every checkstyle-header.txt in the repository and replaces its content |
| 4 | +with the Broadcom license header pattern for Checkstyle. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +CHECKSTYLE_HEADER = ( |
| 11 | + "^\\Q/*\\E$\n" |
| 12 | + "^\\Q * Copyright \u00a9 \\E20\\d\\d\\Q Broadcom Inc. and/or its subsidiaries. All Rights Reserved.\\E$\n" |
| 13 | + "^\\Q * Copyright \\E20\\d\\d\\-present\\Q the original author or authors.\\E$\n" |
| 14 | + "^\\Q */\\E$\n" |
| 15 | + "^$\n" |
| 16 | + "^.*$\n" |
| 17 | +) |
| 18 | + |
| 19 | +SKIP_DIRS = frozenset([ |
| 20 | + ".git", "node_modules", "target", "build", |
| 21 | + ".gradle", "__pycache__", ".mvn", ".idea", |
| 22 | +]) |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + root = sys.argv[1] if len(sys.argv) > 1 else "." |
| 27 | + changed = 0 |
| 28 | + for dirpath, dirnames, filenames in os.walk(root, topdown=True): |
| 29 | + dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS] |
| 30 | + for fname in filenames: |
| 31 | + if fname != "checkstyle-header.txt": |
| 32 | + continue |
| 33 | + path = os.path.join(dirpath, fname) |
| 34 | + try: |
| 35 | + with open(path, "r", encoding="utf-8") as f: |
| 36 | + current = f.read() |
| 37 | + if current == CHECKSTYLE_HEADER: |
| 38 | + print(f" Already up to date: {path}") |
| 39 | + continue |
| 40 | + with open(path, "w", encoding="utf-8") as f: |
| 41 | + f.write(CHECKSTYLE_HEADER) |
| 42 | + print(f" Updated: {path}") |
| 43 | + changed += 1 |
| 44 | + except Exception as e: |
| 45 | + print(f" WARNING: cannot process {path}: {e}") |
| 46 | + print(f"\nUpdated checkstyle headers in {changed} file(s).") |
| 47 | + return 0 |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + sys.exit(main()) |
0 commit comments