From 5f2db68d03e36f6037d1a427edb9285e99272a26 Mon Sep 17 00:00:00 2001 From: yash-nisar Date: Sat, 18 Feb 2017 12:22:15 +0530 Subject: [PATCH] bears/general: Add LicenseCheckBear Closes https://github.com/coala/coala-bears/issues/823 --- .ci/deps.sh | 2 +- .travis.yml | 1 + bears/general/LicenseCheckBear.py | 37 +++++++++++ tests/general/LicenseCheckBearTest.py | 66 +++++++++++++++++++ .../licensecheck_test_files/__init__.py | 0 .../apache_license_2.py | 13 ++++ .../licensecheck_test_files/mit_license.py | 20 ++++++ .../licensecheck_test_files/no_license.py | 1 + 8 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 bears/general/LicenseCheckBear.py create mode 100644 tests/general/LicenseCheckBearTest.py create mode 100644 tests/general/licensecheck_test_files/__init__.py create mode 100644 tests/general/licensecheck_test_files/apache_license_2.py create mode 100644 tests/general/licensecheck_test_files/mit_license.py create mode 100644 tests/general/licensecheck_test_files/no_license.py diff --git a/.ci/deps.sh b/.ci/deps.sh index 377628377d..09de0faf15 100644 --- a/.ci/deps.sh +++ b/.ci/deps.sh @@ -13,7 +13,7 @@ esac # apt-get commands export DEBIAN_FRONTEND=noninteractive -deps="libclang1-3.4 indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder" +deps="libclang1-3.4 indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder devscripts" deps_infer="m4 opam" case $CIRCLE_BUILD_IMAGE in diff --git a/.travis.yml b/.travis.yml index 7b0e1cd1f9..caa18770fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ addons: - chktex - clang-3.4 - cppcheck + - devscripts - flawfinder - gfortran - ghc diff --git a/bears/general/LicenseCheckBear.py b/bears/general/LicenseCheckBear.py new file mode 100644 index 0000000000..01e4997bbe --- /dev/null +++ b/bears/general/LicenseCheckBear.py @@ -0,0 +1,37 @@ +from coalib.bearlib.abstractions.Linter import linter +from dependency_management.requirements.DistributionRequirement import ( + DistributionRequirement) + + +@linter(executable='licensecheck', + output_format='regex', + output_regex=r'(?P\S+.*)') +class LicenseCheckBear: + """ + Attempts to determine the license that applies to each file passed to + it, by searching the start of the file for text belonging to various + licenses. + """ + LANGUAGES = {'ALL'} + REQUIREMENTS = {DistributionRequirement(apt_get='devscripts')} + AUTHORS = {'The coala developers'} + AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} + LICENSE = 'AGPL-3.0' + CAN_DETECT = {'License', 'Copyright Notice'} + + @staticmethod + def create_arguments(filename, file, config_file, + licensecheck_lines: int=60, + licensecheck_copyright: bool=False): + """ + :param licensecheck_lines: + Specify how many lines of the file header should be parsed + for license information. + :param licensecheck_copyright: + Display the file's copyright. + """ + args = () + args += ('--lines', str(licensecheck_lines)) + if licensecheck_copyright: + args += ('--copyright',) + return args + (filename,) \ No newline at end of file diff --git a/tests/general/LicenseCheckBearTest.py b/tests/general/LicenseCheckBearTest.py new file mode 100644 index 0000000000..f5783882ca --- /dev/null +++ b/tests/general/LicenseCheckBearTest.py @@ -0,0 +1,66 @@ +from queue import Queue +import os + +from bears.general.LicenseCheckBear import LicenseCheckBear +from coalib.testing.LocalBearTestHelper import LocalBearTestHelper +from coalib.testing.BearTestHelper import generate_skip_decorator +from coalib.results.Result import Result +from coalib.settings.Section import Section + + +def get_testfile_path(name): + return os.path.join(os.path.dirname(__file__), + 'licensecheck_test_files', + name) + + +def load_testfile(name): + with open(get_testfile_path(name)) as fl: + contents = fl.read() + + return contents + + +@generate_skip_decorator(LicenseCheckBear) +class LicenseCheckBearTest(LocalBearTestHelper): + + def setUp(self): + self.uut = LicenseCheckBear(Section('name'), Queue()) + + def test_license(self): + test_file_path = get_testfile_path('apache_license_2.py') + file_contents = [load_testfile(test_file_path)] + self.check_results( + self.uut, + file_contents, + [Result.from_values('LicenseCheckBear', + test_file_path + ': Apache (v2.0)', + file=test_file_path)], + filename=test_file_path) + + def test_copyright(self): + test_file_path = get_testfile_path('mit_license.py') + file_contents = [load_testfile(test_file_path)] + self.check_results( + self.uut, + file_contents, + [Result.from_values('LicenseCheckBear', + test_file_path + ': MIT/X11 (BSD like)', + file=test_file_path), + Result.from_values('LicenseCheckBear', + '[Copyright: 2008 ]', + file=test_file_path)], + filename=test_file_path, + settings={'licensecheck_copyright': 'True', + 'licensecheck_lines': '70'}) + + def test_no_license(self): + test_file_path = get_testfile_path('no_license.py') + file_contents = [load_testfile(test_file_path)] + self.check_results( + self.uut, + file_contents, + [Result.from_values('LicenseCheckBear', + test_file_path + ': *No copyright* UNKNOWN', + file=test_file_path)], + filename=test_file_path) \ No newline at end of file diff --git a/tests/general/licensecheck_test_files/__init__.py b/tests/general/licensecheck_test_files/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/general/licensecheck_test_files/apache_license_2.py b/tests/general/licensecheck_test_files/apache_license_2.py new file mode 100644 index 0000000000..c9a687e6bf --- /dev/null +++ b/tests/general/licensecheck_test_files/apache_license_2.py @@ -0,0 +1,13 @@ +# Copyright (c) 2010-2012 +# +# 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 +# +# http://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. diff --git a/tests/general/licensecheck_test_files/mit_license.py b/tests/general/licensecheck_test_files/mit_license.py new file mode 100644 index 0000000000..485b9732b6 --- /dev/null +++ b/tests/general/licensecheck_test_files/mit_license.py @@ -0,0 +1,20 @@ +# Copyright (c) 2008 +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/tests/general/licensecheck_test_files/no_license.py b/tests/general/licensecheck_test_files/no_license.py new file mode 100644 index 0000000000..1bec7a8df8 --- /dev/null +++ b/tests/general/licensecheck_test_files/no_license.py @@ -0,0 +1 @@ +# This file doesn't have a license