Skip to content

Commit 14b2e6f

Browse files
authored
Merge pull request #4672 from StackStorm/fix-license-check
Replace hacking with flake8 plugin to check license header
2 parents 59d2a75 + d113794 commit 14b2e6f

File tree

9 files changed

+258
-13
lines changed

9 files changed

+258
-13
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ flake8: requirements .flake8
251251
@echo
252252
@echo "==================== flake ===================="
253253
@echo
254+
. $(VIRTUALENV_DIR)/bin/activate; cd ./lint-configs/python/flake8_plugins; python setup.py develop
254255
. $(VIRTUALENV_DIR)/bin/activate; flake8 --config ./lint-configs/python/.flake8 $(COMPONENTS)
255256
. $(VIRTUALENV_DIR)/bin/activate; flake8 --config ./lint-configs/python/.flake8 $(COMPONENTS_RUNNERS)
256257
. $(VIRTUALENV_DIR)/bin/activate; flake8 --config ./lint-configs/python/.flake8 contrib/packs/actions/

lint-configs/.gitignore

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
*.egg-info/
22+
.installed.cfg
23+
*.egg
24+
25+
# PyInstaller
26+
# Usually these files are written by a python script from a template
27+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
28+
*.manifest
29+
*.spec
30+
31+
# Installer logs
32+
pip-log.txt
33+
pip-delete-this-directory.txt
34+
35+
# Unit test / coverage reports
36+
htmlcov/
37+
.tox/
38+
.coverage
39+
.cache
40+
nosetests.xml
41+
coverage.xml
42+
43+
# Translations
44+
*.mo
45+
*.pot
46+
47+
# Django stuff:
48+
*.log
49+
50+
# Sphinx documentation
51+
docs/_build/
52+
53+
# PyBuilder
54+
target/
55+
56+
# Virtual Environments
57+
.venv
58+
59+
# Temporary Files
60+
*.swp
61+
62+
# Visual Studio Code
63+
.vscode/

lint-configs/python/.flake8

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
[flake8]
22
max-line-length = 100
3-
ignore =
4-
E128,E402,E722,W504,
5-
H101,H104,H105,H106,H107,H108,H109,
6-
H201,H202,H203,H204,H205,H206,H207,H208,H209,H234,H238,
7-
H301,H302,H303,H304,H305,H306,H307,H308,H309,
8-
H401,H402,H403,H404,H405,H406,H407,H408,H409,
9-
H501,H502,H503,H504,H505,H506,H507,H508,H509,
10-
H601,H602,H603,H604,H605,H606,H607,H608,H609,
11-
H701,H702,H703,H704,H705,H706,H707,H708,H709,
12-
H801,H802,H803,H804,H805,H806,H807,H808,H809,
13-
H901,H902,H903,H904,H905,H906,H907,H908,H909
3+
# L102 - apache license header
4+
enable-extensions = L101,L102
5+
ignore = E128,E402,E722,W504
146
exclude=*.egg/*,build,dist
157

168
# Configuration for flake8-copyright extension
179
copyright-check = True
1810
copyright-min-file-size = 1
19-
select = E,F,W,C
11+
12+
# Settings for flake8-license
13+
license-type = apache
14+
15+
# NOTE: This requires flake8 >= 3.0.0 to work correctly.
16+
# If old version is used (< 3.0.0), it will select all the errors and it wont ignore ones
17+
# listed above as part of ignore list
18+
select = E,F,W,C,L

lint-configs/python/.flake8-oss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.flake8
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[flake8]
2+
max-line-length = 100
3+
# L101 - proprietary license header
4+
enable-extensions = L101,L102
5+
ignore = E128,E402,E722,W504
6+
exclude=*.egg/*,build,dist
7+
8+
# Configuration for flake8-copyright extension
9+
copyright-check = True
10+
copyright-min-file-size = 1
11+
12+
license-type = proprietary
13+
14+
# NOTE: This requires flake8 >= 3.0.0 to work correctly.
15+
# If old version is used (< 3.0.0), it will select all the errors and it wont ignore ones
16+
# listed above as part of ignore list
17+
select = E,F,W,C,L

lint-configs/python/flake8_plugins/flake8_plugins/__init__.py

Whitespace-only changes.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2019 Extreme Networks, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import absolute_import
16+
17+
import optparse
18+
19+
20+
APACHE_20_LICENSE = """
21+
# Licensed under the Apache License, Version 2.0 (the "License");
22+
# you may not use this file except in compliance with the License.
23+
# You may obtain a copy of the License at
24+
#
25+
# http://www.apache.org/licenses/LICENSE-2.0
26+
#
27+
# Unless required by applicable law or agreed to in writing, software
28+
# distributed under the License is distributed on an "AS IS" BASIS,
29+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30+
# See the License for the specific language governing permissions and
31+
# limitations under the License.
32+
"""
33+
34+
PROPRIETARY_LICENSE = """
35+
# Unauthorized copying of this file, via any medium is strictly
36+
# prohibited. Proprietary and confidential. See the LICENSE file
37+
# included with this work for details.
38+
"""
39+
40+
APACHE_20_LICENSE = APACHE_20_LICENSE.strip('\n')
41+
PROPRIETARY_LICENSE = PROPRIETARY_LICENSE.strip('\n')
42+
43+
LICENSE_TYPES = {
44+
'proprietary': PROPRIETARY_LICENSE,
45+
'apache': APACHE_20_LICENSE
46+
}
47+
48+
ERROR_MESSAGES = {
49+
'proprietary': 'L101 Proprietary license header not found',
50+
'apache': 'L102 Apache 2.0 license header not found'
51+
}
52+
53+
54+
# Temporary shim for flake8 2.x --> 3.x transition
55+
# http://flake8.pycqa.org/en/latest/plugin-development/cross-compatibility.html#option-handling-on-flake8-2-and-3
56+
def register_opt(parser, *args, **kwargs):
57+
try:
58+
# Flake8 3.x registration
59+
parser.add_option(*args, **kwargs)
60+
except (optparse.OptionError, TypeError):
61+
# Flake8 2.x registration
62+
parse_from_config = kwargs.pop('parse_from_config', False)
63+
kwargs.pop('comma_separated_list', False)
64+
kwargs.pop('normalize_paths', False)
65+
parser.add_option(*args, **kwargs)
66+
if parse_from_config:
67+
parser.config_options.append(args[-1].lstrip('-'))
68+
69+
70+
class LicenseChecker(object):
71+
name = 'flake8_license'
72+
version = '0.1.0'
73+
off_by_default = False
74+
75+
def __init__(self, tree, filename):
76+
self.tree = tree
77+
self.filename = filename
78+
79+
@classmethod
80+
def add_options(cls, parser):
81+
register_opt(
82+
parser,
83+
'--license-type',
84+
type='choice',
85+
choices=['proprietary', 'apache'],
86+
default='apache',
87+
action='store',
88+
parse_from_config=True,
89+
help='Checks for specific type of license header.'
90+
)
91+
92+
register_opt(
93+
parser,
94+
'--license-min-file-size',
95+
type='int',
96+
default=1,
97+
action='store',
98+
parse_from_config=True,
99+
help='Minimum number of characters in a file before requiring a license header.'
100+
)
101+
102+
@classmethod
103+
def parse_options(cls, options):
104+
cls.license_type = options.license_type
105+
cls.min_file_size = options.license_min_file_size
106+
107+
def run(self):
108+
"""Check for license header given license type.
109+
L101 Proprietary license header not found
110+
L102 Apache 2.0 license header not found
111+
"""
112+
with open(self.filename, 'r') as f:
113+
content = f.read()
114+
115+
if len(content) >= self.min_file_size and LICENSE_TYPES[self.license_type] not in content:
116+
yield 1, 1, ERROR_MESSAGES[self.license_type], type(self)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2019 Extreme Networks, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from setuptools import setup
16+
17+
REQUIRES = [
18+
'flake8'
19+
]
20+
21+
setup(
22+
name='st2_flake8_plugins',
23+
version='0.1.0',
24+
author='StackStorm',
25+
author_email='[email protected]',
26+
url='https://www.stackstorm.com',
27+
packages=[
28+
'flake8_plugins'
29+
],
30+
install_requires=REQUIRES,
31+
license='Apache License (2.0)',
32+
classifiers=[
33+
'Development Status :: 4 - Beta',
34+
'Intended Audience :: Information Technology',
35+
'Intended Audience :: System Administrators',
36+
'License :: OSI Approved :: Apache Software License',
37+
'Operating System :: POSIX :: Linux',
38+
'Programming Language :: Python',
39+
'Programming Language :: Python :: 2',
40+
'Programming Language :: Python :: 2.7',
41+
'Programming Language :: Python :: 3',
42+
'Programming Language :: Python :: 3.6'
43+
],
44+
entry_points={
45+
'flake8.extension': [
46+
'L = flake8_plugins.license_rules:LicenseChecker',
47+
]
48+
}
49+
)

test-requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ coverage==4.5.2
33
pep8==1.7.1
44
flake8==3.7.7
55
flake8-copyright==0.2.2
6-
hacking==1.1.0
76
astroid==1.6.5
87
pylint==1.9.4
98
pylint-plugin-utils>=0.4

0 commit comments

Comments
 (0)