Skip to content

Commit

Permalink
Merge from main branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhlogin committed Jan 8, 2024
2 parents b911361 + b03e260 commit 1d1ff14
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 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))
1 change: 0 additions & 1 deletion scripts/check_for_valid_readme.py
Original file line number Diff line number Diff line change
@@ -1,6 +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

0 comments on commit 1d1ff14

Please sign in to comment.