Skip to content

Commit

Permalink
Implement checker for copywrite header (#6)
Browse files Browse the repository at this point in the history
*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.
  • Loading branch information
thpierce authored Jan 8, 2024
2 parents 9319463 + d5fdca9 commit b03e260
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ ignore-patterns=
ignore-paths=

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

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

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

# Pickle collected data for later comparisons.
persistent=yes
Expand Down Expand Up @@ -491,4 +491,4 @@ min-public-methods=2

# Exceptions that will emit a warning when being caught. Defaults to
# "Exception".
overgeneral-exceptions=Exception
overgeneral-exceptions=builtins.Exception
59 changes: 59 additions & 0 deletions checkers/file_header_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

from pylint.checkers import BaseRawFileChecker

COPYWRITE_STRING = (
"# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n"
)
COPYWRITE_BYTES = bytes(COPYWRITE_STRING, "utf-8")
LICENSE_STRING = "# SPDX-License-Identifier: Apache-2.0"
LICENSE_BYTES = bytes(LICENSE_STRING, "utf-8")


class FileHeaderChecker(BaseRawFileChecker):
name = "file_header_checker"
msgs = {
"E1234": (
"File has missing or malformed header",
"missing-header",
"All files must have required header: \n"
+ COPYWRITE_STRING
+ LICENSE_STRING,
),
}
options = ()

def process_module(self, node):
"""
Check if the file has the required header in first and second lines of
the file. Some files may be scripts, which requires the first line to
be the shebang line, so handle that by ignoring the first line.
"""
first_line = 0
second_line = 1
with node.stream() as stream:
for line_num, line in enumerate(stream):
if line_num == first_line and line.startswith(b"#!"):
first_line += 1
second_line += 1
elif line_num == first_line and is_bad_copywrite_line(line):
self.add_message("missing-header", line=line_num)
break
elif line_num == second_line and is_bad_license_line(line):
self.add_message("missing-header", line=line_num)
break
elif line_num > second_line:
break


def is_bad_copywrite_line(line: bytes) -> bool:
return not line.startswith(COPYWRITE_BYTES)


def is_bad_license_line(line: bytes) -> bool:
return not line.startswith(LICENSE_BYTES)


def register(linter) -> None:
linter.register_checker(FileHeaderChecker(linter))
2 changes: 2 additions & 0 deletions scripts/check_for_valid_readme.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.
# SPDX-License-Identifier: Apache-2.0
"""Test script to check given paths for valid README.rst files."""
import argparse
import sys
Expand Down
2 changes: 2 additions & 0 deletions scripts/eachdist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python3
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import argparse
import os
Expand Down

0 comments on commit b03e260

Please sign in to comment.