Skip to content

Commit

Permalink
bears/general: Add LicenseCheckBear
Browse files Browse the repository at this point in the history
Closes coala#823
  • Loading branch information
yash-nisar committed Mar 8, 2017
1 parent 6ed3380 commit 6f06fc4
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .ci/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ addons:
- chktex
- clang-3.4
- cppcheck
- devscripts
- flawfinder
- gfortran
- ghc
Expand Down
40 changes: 40 additions & 0 deletions bears/general/LicenseCheckBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.DistributionRequirement import (
DistributionRequirement)


@linter(executable='licensecheck',
output_format='regex',
output_regex=r'(^(.*/)*)([^/]*): .*UNKNOWN$',
result_message='No license found.')
class LicenseCheckBear:
"""
Attempts to check the given file for a license, by searching the start
of the file for text belonging to various licenses.
For Ubuntu/Debian users, the ``licensecheck_lines`` option has to be used
in accordance with the ``licensecheck_tail`` option.
"""
LANGUAGES = {'All'}
REQUIREMENTS = {DistributionRequirement(apt_get='devscripts',
dnf='licensecheck')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'License'}

@staticmethod
def create_arguments(filename, file, config_file,
licensecheck_lines: int=60,
licensecheck_tail: int=5000):
"""
:param licensecheck_lines:
Specify how many lines of the file header should be parsed for
license information. Set to 0 to parse the whole file (and ignore
``licensecheck_tail``).
:param licensecheck_tail:
Specify how many bytes to parse at end of file. Set to 0 to disable
parsing from end of file.
"""
return ('--lines', str(licensecheck_lines), '--tail',
str(licensecheck_tail), filename)
67 changes: 67 additions & 0 deletions tests/general/LicenseCheckBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
from queue import Queue

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):
return open(get_testfile_path(name)).readlines()


@generate_skip_decorator(LicenseCheckBear)
class LicenseCheckBearTest(LocalBearTestHelper):

def setUp(self):
self.uut = LicenseCheckBear(Section('name'), Queue())

def test_license(self):
file_contents = load_testfile('mit_license.py')
self.check_results(
self.uut,
file_contents,
[],
filename=get_testfile_path('mit_license.py'))

def test_license_without_copyright(self):
file_contents = load_testfile('apache_license_without_copyright.py')
self.check_results(
self.uut,
file_contents,
[],
filename=get_testfile_path('apache_license_without_copyright.py'),
settings={'licensecheck_lines': 70,
'licensecheck_tail': 0})

def copyright_without_license(self):
file_contents = load_testfile('copyright_without_license.py')
self.check_results(
self.uut,
file_contents,
[Result.from_values('LicenseCheckBear',
'No license found.',
file=get_testfile_path('copyright_without_'
'license.py'))],
filename=get_testfile_path('copyright_without_license.py'),
settings={'licensecheck_lines': 0,
'licensecheck_tail': 0}) # Parse entire file

def no_license(self):
file_contents = load_testfile('no_license.py')
self.check_results(
self.uut,
file_contents,
[Result.from_values('LicenseCheckBear',
'No license found.',
file=get_testfile_path('no_license.py'))],
filename=get_testfile_path('no_license.py'),
settings={'licensecheck_lines': 10})
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright (c) 2008 <name of author>
27 changes: 27 additions & 0 deletions tests/general/licensecheck_test_files/mit_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) 2008 <name of author>
#
# 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.


def add_two_numbers(a, b):
return a + b


print(add_two_numbers(4, 5))
2 changes: 2 additions & 0 deletions tests/general/licensecheck_test_files/no_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# No license in this file
print('Hello world')

0 comments on commit 6f06fc4

Please sign in to comment.