|
| 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) |
0 commit comments