|
| 1 | +# Copyright 2026 Datastrato, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Check that all source files contain the required license header.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | + |
| 20 | +COPYRIGHT_MARKER = "Copyright 2026 Datastrato, Inc." |
| 21 | + |
| 22 | +SCAN_RULES = [ |
| 23 | + {"dirs": ["src", "tests"], "ext": ".py"}, |
| 24 | + {"dirs": [os.path.join(".github", "workflows")], "ext": ".yml"}, |
| 25 | +] |
| 26 | + |
| 27 | +SKIP_DIRS = {".venv", "dist", "__pycache__", ".mypy_cache", ".pytest_cache", ".ruff_cache"} |
| 28 | +SKIP_FILES = {os.path.join("src", "adp_sdk", "py.typed")} |
| 29 | + |
| 30 | + |
| 31 | +def should_skip_dir(dirname: str) -> bool: |
| 32 | + return dirname in SKIP_DIRS |
| 33 | + |
| 34 | + |
| 35 | +def collect_files() -> list[str]: |
| 36 | + files: list[str] = [] |
| 37 | + for rule in SCAN_RULES: |
| 38 | + for base_dir in rule["dirs"]: |
| 39 | + for root, dirs, filenames in os.walk(base_dir): |
| 40 | + dirs[:] = [d for d in dirs if not should_skip_dir(d)] |
| 41 | + for filename in filenames: |
| 42 | + if filename.endswith(rule["ext"]): |
| 43 | + filepath = os.path.join(root, filename) |
| 44 | + if filepath not in SKIP_FILES: |
| 45 | + files.append(filepath) |
| 46 | + return sorted(files) |
| 47 | + |
| 48 | + |
| 49 | +def check_header(filepath: str) -> bool: |
| 50 | + with open(filepath, encoding="utf-8") as f: |
| 51 | + content = f.read() |
| 52 | + return COPYRIGHT_MARKER in content |
| 53 | + |
| 54 | + |
| 55 | +def main() -> int: |
| 56 | + files = collect_files() |
| 57 | + missing: list[str] = [] |
| 58 | + |
| 59 | + for filepath in files: |
| 60 | + if not check_header(filepath): |
| 61 | + missing.append(filepath) |
| 62 | + |
| 63 | + if missing: |
| 64 | + print("Files missing license header:") |
| 65 | + for f in missing: |
| 66 | + print(f" {f}") |
| 67 | + print(f"\n{len(missing)} file(s) missing the required license header.") |
| 68 | + return 1 |
| 69 | + |
| 70 | + print(f"All {len(files)} file(s) have the required license header.") |
| 71 | + return 0 |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + sys.exit(main()) |
0 commit comments