-
Notifications
You must be signed in to change notification settings - Fork 19
test: add copyright header test #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jericht
merged 1 commit into
OpenJobDescription:mainline
from
jericht:jericht/copyright
May 13, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| import re | ||
| from pathlib import Path | ||
|
|
||
| # For distributed open source and proprietary code, we must include a copyright header in source every file: | ||
| _copyright_header_re = re.compile( | ||
| r"Copyright Amazon\.com, Inc\. or its affiliates\. All Rights Reserved\.", re.IGNORECASE | ||
| ) | ||
| _generated_by_scm = re.compile( | ||
| r"# file generated by (setuptools[_-]scm|vcs-versioning)", re.IGNORECASE | ||
| ) | ||
|
|
||
|
|
||
| class FileMissingCopyRight(Exception): | ||
| filepath: Path | ||
|
|
||
| def __init__(self, message, filepath): | ||
| super().__init__(message) | ||
| self.filepath = filepath | ||
|
|
||
|
|
||
| def _check_file(filename: Path) -> None: | ||
| with open(filename, encoding="utf-8", errors="ignore") as infile: | ||
| lines_read = 0 | ||
| for line in infile: | ||
| if _copyright_header_re.search(line): | ||
| return # success | ||
| lines_read += 1 | ||
| if lines_read > 10: | ||
| raise FileMissingCopyRight( | ||
| f"Could not find a valid Amazon.com copyright header in the top of {filename}." | ||
| " Please add one.", | ||
| Path(filename), | ||
| ) | ||
| # __init__.py files are usually empty, this is to catch that. | ||
| raise FileMissingCopyRight( | ||
| f"Could not find a valid Amazon.com copyright header in the top of {filename}." | ||
| " Please add one.", | ||
| Path(filename), | ||
| ) | ||
|
|
||
|
|
||
| def _is_version_file(filename: Path) -> bool: | ||
| if filename.name != "_version.py": | ||
| return False | ||
| with open(filename, encoding="utf-8", errors="ignore") as infile: | ||
| lines_read: list[str] = [] | ||
| for line in infile: | ||
| if _generated_by_scm.search(line): | ||
| return True | ||
| lines_read.append(line) | ||
| if len(lines_read) > 10: | ||
| break | ||
| print(f"_version.py file ({filename}) found that is not generated by scm:\n{lines_read}") | ||
| return False | ||
|
|
||
|
|
||
| def test_copyright_headers(): | ||
| """Verifies every .py file has an Amazon copyright header.""" | ||
| root_project_dir = Path(__file__) | ||
| # The root of the project is the directory that contains the test directory. | ||
| while not (root_project_dir / "test").exists(): | ||
| root_project_dir = root_project_dir.parent | ||
| # Choose only a few top level directories to test. | ||
| # That way we don't snag any virtual envs a developer might create, at the risk of missing | ||
| # some top level .py files. | ||
| top_level_dirs = ["src", "test", "scripts"] | ||
| file_count = 0 | ||
| failed_files = set() | ||
| for top_level_dir in top_level_dirs: | ||
| for glob_pattern in ("**/*.py", "**/*.sh"): | ||
| for path in Path(root_project_dir / top_level_dir).glob(glob_pattern): | ||
| print(path) | ||
| if not _is_version_file(path): | ||
| try: | ||
| _check_file(path) | ||
| except FileMissingCopyRight as e: | ||
| failed_files.add(e.filepath) | ||
| file_count += 1 | ||
|
|
||
| if failed_files: | ||
| formatted_failed_files = "\n\t".join(str(filepath) for filepath in failed_files) | ||
| raise Exception( | ||
| f"Found {len(failed_files)} files without copyright headers:\n\t{formatted_failed_files}" | ||
| ) | ||
|
|
||
| print(f"test_copyright_headers checked {file_count} files successfully.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_copyright_headers() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.