-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from pavoljuhas/update-pylint-copyright-checker
Update copyright checker for pylint-3
- Loading branch information
Showing
118 changed files
with
475 additions
and
261 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,117 @@ | ||
# Copyright 2024 The Unitary Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from astroid import nodes | ||
from pylint.checkers import BaseRawFileChecker | ||
|
||
if TYPE_CHECKING: | ||
from pylint.lint import PyLinter | ||
|
||
|
||
class CopyrightChecker(BaseRawFileChecker): | ||
"""Check for the copyright notices at the beginning of a Python source file. | ||
This checker can be disabled by putting `# pylint: disable=wrong-or-nonexistent-copyright-notice` | ||
at the beginning of a file. | ||
""" | ||
|
||
name = "copyright-notice" | ||
msgs = { | ||
"R0001": ( | ||
"Missing or wrong copyright notice", | ||
"wrong-or-nonexistent-copyright-notice", | ||
"Consider putting a correct copyright notice at the beginning of a file.", | ||
) | ||
} | ||
options = () | ||
|
||
def process_module(self, node: nodes.Module) -> None: | ||
"""Check whether the copyright notice is correctly placed in the source file of a module. | ||
Compare the first lines of a source file against the standard copyright notice (i.e., the | ||
`golden` variable below). Suffix whitespace (including newline symbols) is not considered | ||
during the comparison. Pylint will report a message if the copyright notice is not | ||
correctly placed. | ||
Args: | ||
node: the module to be checked. | ||
""" | ||
# Exit if the checker is disabled in the source file. | ||
if not self.linter.is_message_enabled("wrong-or-nonexistent-copyright-notice"): | ||
return | ||
golden = [ | ||
b'# Copyright 20XX The Unitary Authors', | ||
b'#', | ||
b'# Licensed under the Apache License, Version 2.0 (the "License");', | ||
b'# you may not use this file except in compliance with the License.', | ||
b'# You may obtain a copy of the License at', | ||
b'#', | ||
b'# https://www.apache.org/licenses/LICENSE-2.0', | ||
b'#', | ||
b'# Unless required by applicable law or agreed to in writing, software', | ||
b'# distributed under the License is distributed on an "AS IS" BASIS,', | ||
b'# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.', | ||
b'# See the License for the specific language governing permissions and', | ||
b'# limitations under the License.', | ||
] | ||
with node.stream() as stream: | ||
|
||
def skip_shebang(stream): | ||
"""Skip shebang line if present, and blank lines between shebang and content.""" | ||
lines = iter(enumerate(stream)) | ||
try: | ||
lineno, line = next(lines) | ||
except StopIteration: | ||
return | ||
if line.startswith(b'#!'): | ||
# skip shebang and blank lines | ||
for lineno, line in lines: | ||
if line.strip(): | ||
yield lineno, line | ||
break | ||
else: | ||
# no shebang | ||
yield lineno, line | ||
yield from lines | ||
|
||
for expected_line, (i, (lineno, line)) in zip(golden, enumerate(skip_shebang(stream))): | ||
for expected_char, (colno, char) in zip(expected_line, enumerate(line)): | ||
# The text needs to be same as the template except for the year. | ||
if expected_char != char and not (i == 0 and 14 <= colno <= 15): | ||
self.add_message( | ||
"wrong-or-nonexistent-copyright-notice", | ||
line=lineno + 1, | ||
col_offset=colno, | ||
) | ||
return | ||
# The line cannot be shorter than the template or contain extra text. | ||
if len(line) < len(expected_line) or line[len(expected_line) :].strip() != b'': | ||
self.add_message( | ||
"wrong-or-nonexistent-copyright-notice", | ||
line=lineno + 1, | ||
col_offset=min(len(line), len(expected_line)), | ||
) | ||
return | ||
|
||
|
||
def register(linter: PyLinter): | ||
"""Register this checker to pylint. | ||
The registration is done automatically if this file is in $PYTHONPATH. | ||
""" | ||
linter.register_checker(CopyrightChecker(linter)) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.