Skip to content

Commit 6afcdcb

Browse files
authored
test: enable copyright header checks in linting (#44)
1 parent c64ee28 commit 6afcdcb

7 files changed

+145
-22
lines changed

.pylintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ limit-inference-results=100
1111

1212
# List of plugins (as comma separated values of python module names) to load,
1313
# usually to register additional checkers.
14-
#load-plugins=
14+
load-plugins=copyright-checker,
15+
license-checker
1516

1617
# Pickle collected data for later comparisons.
1718
persistent=yes

ibmcloudant/common.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
# © Copyright IBM Corporation 2020.
3+
# © Copyright IBM Corporation 2020, 2021.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -23,25 +23,26 @@
2323
USER_AGENT_HEADER = 'User-Agent'
2424
SDK_NAME = 'cloudant-python-sdk'
2525

26-
# pylint: disable=missing-function-docstring
27-
def get_system_info():
26+
27+
def get_system_info(): # pylint: disable=missing-docstring
2828
return '{0} {1} {2}'.format(platform.system(), # OS
2929
platform.release(), # OS version
3030
platform.python_version()) # Python version
3131

32-
# pylint: disable=missing-function-docstring
33-
def get_user_agent():
32+
33+
def get_user_agent(): # pylint: disable=missing-docstring
3434
return user_agent
3535

36-
# pylint: disable=missing-function-docstring
37-
def get_sdk_analytics(service_name, service_version, operation_id):
36+
37+
def get_sdk_analytics(service_name, service_version, operation_id): # pylint: disable=missing-docstring
3838
return 'service_name={0};service_version={1};operation_id={2}'.format(
3939
service_name, service_version, operation_id)
4040

41+
4142
user_agent = '{0}-{1} {2}'.format(SDK_NAME, __version__, get_system_info())
4243

43-
# pylint: disable=missing-function-docstring
44-
def get_sdk_headers(service_name, service_version, operation_id):
44+
45+
def get_sdk_headers(service_name, service_version, operation_id): # pylint: disable=missing-docstring
4546
headers = {}
4647
headers[SDK_ANALYTICS_HEADER] = get_sdk_analytics(service_name, service_version, operation_id)
4748
headers[USER_AGENT_HEADER] = get_user_agent()

ibmcloudant/couchdb_session_base_service_patch.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
# © Copyright IBM Corporation 2020.
3+
# © Copyright IBM Corporation 2020, 2021.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -27,8 +27,8 @@
2727

2828
old_init = CloudantV1.__init__
2929

30-
# pylint: disable=missing-function-docstring
31-
def new_init(self, authenticator: Authenticator = None):
30+
31+
def new_init(self, authenticator: Authenticator = None): # pylint: disable=missing-docstring
3232
old_init(self, authenticator)
3333
if isinstance(authenticator, CouchDbSessionAuthenticator):
3434
# Replacing BaseService's http.cookiejar.CookieJar as RequestsCookieJar supports update(CookieJar)
@@ -38,8 +38,8 @@ def new_init(self, authenticator: Authenticator = None):
3838

3939
old_set_service_url = CloudantV1.set_service_url
4040

41-
# pylint: disable=missing-function-docstring
42-
def new_set_service_url(self, service_url: str):
41+
42+
def new_set_service_url(self, service_url: str): # pylint: disable=missing-docstring
4343
old_set_service_url(self, service_url)
4444
try:
4545
if isinstance(self.authenticator, CouchDbSessionAuthenticator):
@@ -50,8 +50,8 @@ def new_set_service_url(self, service_url: str):
5050

5151
old_set_default_headers = CloudantV1.set_default_headers
5252

53-
# pylint: disable=missing-function-docstring
54-
def new_set_default_headers(self, headers: Dict[str, str]):
53+
54+
def new_set_default_headers(self, headers: Dict[str, str]): # pylint: disable=missing-docstring
5555
old_set_default_headers(self, headers)
5656
if isinstance(self.authenticator, CouchDbSessionAuthenticator):
5757
combined_headers = {}
@@ -66,8 +66,8 @@ def new_set_default_headers(self, headers: Dict[str, str]):
6666

6767
old_set_disable_ssl_verification = CloudantV1.set_disable_ssl_verification
6868

69-
# pylint: disable=missing-function-docstring
70-
def new_set_disable_ssl_verification(self, status: bool = False) -> None:
69+
70+
def new_set_disable_ssl_verification(self, status: bool = False) -> None: # pylint: disable=missing-docstring
7171
old_set_disable_ssl_verification(self, status)
7272
if isinstance(self.authenticator, CouchDbSessionAuthenticator):
7373
self.authenticator.token_manager.set_disable_ssl_verification(status)

ibmcloudant/couchdb_session_get_authenticator_patch.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
# © Copyright IBM Corporation 2020.
3+
# © Copyright IBM Corporation 2020, 2021.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -21,8 +21,8 @@
2121

2222
old_construct_authenticator = get_authenticator.__construct_authenticator
2323

24-
# pylint: disable=missing-function-docstring
25-
def new_construct_authenticator(config):
24+
25+
def new_construct_authenticator(config): # pylint: disable=missing-docstring
2626
auth_type = config.get('AUTH_TYPE').upper() if config.get('AUTH_TYPE') else ''
2727
if auth_type == 'COUCHDB_SESSION':
2828
return CouchDbSessionAuthenticator(

pylint/checkers/copyright-checker.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# © Copyright IBM Corporation 2021.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
import re
19+
20+
from pylint.interfaces import IRawChecker, HIGH
21+
from pylint.checkers import BaseChecker
22+
23+
24+
class CopyrightChecker(BaseChecker):
25+
"""
26+
Check for copyright header
27+
"""
28+
29+
__implements__ = IRawChecker
30+
31+
name = 'copyright-checker'
32+
msgs = {'E9902': ('Include or fix copyright in file',
33+
'file-no-copyright',
34+
'Your file has no copyright or it needs to be fixed'),
35+
}
36+
options = ()
37+
38+
def process_module(self, node):
39+
ibm_copyright_regex = r'^# © Copyright IBM Corporation 20\d\d(?:, 20\d\d)?\.$'
40+
ibm_generated_copyright_regex = r'^# \(C\) Copyright IBM Corp\. 20\d\d\.$'
41+
with node.stream() as stream:
42+
for (lineno, raw_actual_line) in enumerate(stream):
43+
actual_line = raw_actual_line.decode("utf-8").strip()
44+
if actual_line == '':
45+
continue
46+
if actual_line[0] != '#':
47+
self.add_message('file-no-copyright', line=lineno, confidence=HIGH)
48+
break
49+
if re.match(ibm_copyright_regex, actual_line) or re.match(ibm_generated_copyright_regex, actual_line):
50+
break
51+
52+
53+
def register(linter):
54+
linter.register_checker(CopyrightChecker(linter))

pylint/checkers/license-checker.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# © Copyright IBM Corporation 2021.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
from pylint.interfaces import IRawChecker, HIGH
19+
from pylint.checkers import BaseChecker
20+
21+
LICENSE_HEADER = """
22+
# Licensed under the Apache License, Version 2.0 (the "License");
23+
# you may not use this file except in compliance with the License.
24+
# You may obtain a copy of the License at
25+
#
26+
# http://www.apache.org/licenses/LICENSE-2.0
27+
#
28+
# Unless required by applicable law or agreed to in writing, software
29+
# distributed under the License is distributed on an "AS IS" BASIS,
30+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31+
# See the License for the specific language governing permissions and
32+
# limitations under the License.
33+
""".strip().split("\n")
34+
35+
36+
class LicenseChecker(BaseChecker):
37+
"""
38+
Check for license header
39+
"""
40+
41+
__implements__ = IRawChecker
42+
43+
name = 'license-checker'
44+
msgs = {'E9903': ('Include or fix license header in file',
45+
'file-no-license',
46+
'Your file has no license or it needs to be fixed'),
47+
}
48+
options = ()
49+
50+
def process_module(self, node):
51+
i = 0
52+
with node.stream() as stream:
53+
for (lineno, raw_actual_line) in enumerate(stream):
54+
actual_line = raw_actual_line.decode("utf-8").strip()
55+
if actual_line == '':
56+
continue
57+
if actual_line[0] != '#' and i != len(LICENSE_HEADER):
58+
self.add_message('file-no-license', line=lineno, confidence=HIGH)
59+
break
60+
if i < len(LICENSE_HEADER) and actual_line == LICENSE_HEADER[i]:
61+
i += 1
62+
continue
63+
64+
65+
def register(linter):
66+
linter.register_checker(LicenseChecker(linter))

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ envlist = py37-lint, py35, py36, py37, py38
55
basepython = python3.7
66
deps = pylint
77
commands = pylint --rcfile=.pylintrc ibmcloudant test
8+
setenv = PYTHONPATH = pylint/checkers
89

910
[testenv]
1011
passenv = TOXENV CI TRAVIS*

0 commit comments

Comments
 (0)