Skip to content

Commit

Permalink
fix add-copyright-headers.sh and improve errors from test
Browse files Browse the repository at this point in the history
  • Loading branch information
jericht committed Aug 31, 2023
1 parent 25e0523 commit eeab60c
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions scripts/add_copyright_headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ if [ $# -eq 0 ]; then
fi

for file in "$@"; do
if ! head -1 | grep 'Copyright ' "$file" >/dev/null; then
case $file in
if ! head -1 "$file" | grep 'Copyright ' >/dev/null; then
case "$file" in
*.java)
CONTENT=$(cat "$file")
cat > "$file" <<EOF
Expand Down
2 changes: 2 additions & 0 deletions src/deadline_test_scaffolding/cloudformation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

from .job_attachments_bootstrap_stack import JobAttachmentsBootstrapStack
from .worker_bootstrap_stack import WorkerBootstrapStack

Expand Down
2 changes: 2 additions & 0 deletions src/deadline_test_scaffolding/deadline/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

from .resources import (
Farm,
Fleet,
Expand Down
2 changes: 2 additions & 0 deletions src/deadline_test_scaffolding/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

from __future__ import annotations

import re
Expand Down
2 changes: 2 additions & 0 deletions src/deadline_test_scaffolding/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

from __future__ import annotations

import botocore.exceptions
Expand Down
29 changes: 20 additions & 9 deletions test/test_copyright_headers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

import json
import re
from pathlib import Path

Expand All @@ -10,6 +11,15 @@
_generated_by_scm = re.compile(r"# file generated by setuptools_scm", re.IGNORECASE)


class CopyrightHeaderNotFoundException(Exception):
def __init__(self, filepath: Path, *args, **kwargs) -> None:
super().__init__(
f"Could not find a valid Amazon.com copyright header in the top of {filepath}."
" Please add one."
)
self.filepath = filepath


def _check_file(filename: Path) -> None:
with open(filename) as infile:
lines_read = 0
Expand All @@ -18,16 +28,10 @@ def _check_file(filename: Path) -> None:
return # success
lines_read += 1
if lines_read > 10:
raise Exception(
f"Could not find a valid Amazon.com copyright header in the top of {filename}."
" Please add one."
)
raise CopyrightHeaderNotFoundException(filename)
else:
# __init__.py files are usually empty, this is to catch that.
raise Exception(
f"Could not find a valid Amazon.com copyright header in the top of {filename}."
" Please add one."
)
raise CopyrightHeaderNotFoundException(filename)


def _is_version_file(filename: Path) -> bool:
Expand Down Expand Up @@ -55,15 +59,22 @@ def test_copyright_headers():
# some top level .py files.
top_level_dirs = ["src", "test", "scripts"]
file_count = 0
error_files = []
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):
_check_file(path)
try:
_check_file(path)
except CopyrightHeaderNotFoundException as e:
error_files.append(e.filepath)
file_count += 1

print(f"test_copyright_headers checked {file_count} files successfully.")
assert (
len(error_files) == 0
), f"Copyright headers are missing from files: {json.dumps([str(ef) for ef in error_files])}"


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions test/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
1 change: 1 addition & 0 deletions test/unit/cloudformation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
1 change: 1 addition & 0 deletions test/unit/deadline/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 changes: 2 additions & 0 deletions test/unit/deadline/test_resources.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

import datetime
import json
from dataclasses import replace
Expand Down

0 comments on commit eeab60c

Please sign in to comment.