Skip to content

Commit b03e260

Browse files
authored
Implement checker for copywrite header (aws-observability#6)
*Description of changes:* In this commit, we are adding and enabling a checker that ensures a copywrite header is added to every code file. Also we are fixing a warning message by updating the .pylintrc to reference "builtins.Exception" instead of "Exception". Also we apply header to files missing it. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
2 parents 9319463 + d5fdca9 commit b03e260

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

.pylintrc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ ignore-patterns=
1515
ignore-paths=
1616

1717
# Python code to execute, usually for sys.path manipulation such as
18-
# pygtk.require().
19-
#init-hook=
18+
# pygtk.require(). Required for custom checkers.
19+
init-hook='import sys; sys.path.append(".")'
2020

2121
# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the
2222
# number of processors available to use.
@@ -29,7 +29,7 @@ limit-inference-results=100
2929

3030
# List of plugins (as comma separated values of python modules names) to load,
3131
# usually to register additional checkers.
32-
load-plugins=pylint.extensions.no_self_use
32+
load-plugins=pylint.extensions.no_self_use,checkers.file_header_checker
3333

3434
# Pickle collected data for later comparisons.
3535
persistent=yes
@@ -491,4 +491,4 @@ min-public-methods=2
491491

492492
# Exceptions that will emit a warning when being caught. Defaults to
493493
# "Exception".
494-
overgeneral-exceptions=Exception
494+
overgeneral-exceptions=builtins.Exception

checkers/file_header_checker.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from pylint.checkers import BaseRawFileChecker
5+
6+
COPYWRITE_STRING = (
7+
"# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n"
8+
)
9+
COPYWRITE_BYTES = bytes(COPYWRITE_STRING, "utf-8")
10+
LICENSE_STRING = "# SPDX-License-Identifier: Apache-2.0"
11+
LICENSE_BYTES = bytes(LICENSE_STRING, "utf-8")
12+
13+
14+
class FileHeaderChecker(BaseRawFileChecker):
15+
name = "file_header_checker"
16+
msgs = {
17+
"E1234": (
18+
"File has missing or malformed header",
19+
"missing-header",
20+
"All files must have required header: \n"
21+
+ COPYWRITE_STRING
22+
+ LICENSE_STRING,
23+
),
24+
}
25+
options = ()
26+
27+
def process_module(self, node):
28+
"""
29+
Check if the file has the required header in first and second lines of
30+
the file. Some files may be scripts, which requires the first line to
31+
be the shebang line, so handle that by ignoring the first line.
32+
"""
33+
first_line = 0
34+
second_line = 1
35+
with node.stream() as stream:
36+
for line_num, line in enumerate(stream):
37+
if line_num == first_line and line.startswith(b"#!"):
38+
first_line += 1
39+
second_line += 1
40+
elif line_num == first_line and is_bad_copywrite_line(line):
41+
self.add_message("missing-header", line=line_num)
42+
break
43+
elif line_num == second_line and is_bad_license_line(line):
44+
self.add_message("missing-header", line=line_num)
45+
break
46+
elif line_num > second_line:
47+
break
48+
49+
50+
def is_bad_copywrite_line(line: bytes) -> bool:
51+
return not line.startswith(COPYWRITE_BYTES)
52+
53+
54+
def is_bad_license_line(line: bytes) -> bool:
55+
return not line.startswith(LICENSE_BYTES)
56+
57+
58+
def register(linter) -> None:
59+
linter.register_checker(FileHeaderChecker(linter))

scripts/check_for_valid_readme.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
13
"""Test script to check given paths for valid README.rst files."""
24
import argparse
35
import sys

scripts/eachdist.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python3
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
24

35
import argparse
46
import os

0 commit comments

Comments
 (0)