Skip to content

Commit

Permalink
bears/general: Add LicenseCheckBear
Browse files Browse the repository at this point in the history
Closes #823
  • Loading branch information
yash-nisar committed Feb 19, 2017
1 parent a06d34e commit 0dfe3e9
Show file tree
Hide file tree
Showing 8 changed files with 161 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
48 changes: 48 additions & 0 deletions bears/general/LicenseCheckBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.DistributionRequirement import (
DistributionRequirement)


@linter(executable='licensecheck',
output_format='regex',
output_regex=r'(?P<message>\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 = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'License', 'Copyright Notice'}

@staticmethod
def create_arguments(filename, file, config_file,
licensecheck_lines: int=60,
licensecheck_ignore: str='',
licensecheck_check: str='',
licensecheck_copyright: bool=False):
"""
:param licensecheck_lines:
Specify how many lines of the file header should be parsed
for license information.
:param licensecheck_ignore:
Specify that files/directories matching the regular expression
should be ignored when checking files.
:param licensecheck_check:
Specify a pattern indicating which files should be checked.
:param licensecheck_copyright:
Display the file's copyright
"""
args = ()
args += ('--lines', str(licensecheck_lines))
if licensecheck_ignore:
args += ('--ignore', str(licensecheck_ignore))
if licensecheck_check:
args += ('--check', str(licensecheck_check))
if licensecheck_copyright:
args += ('--copyright',)
return args + (filename,)
77 changes: 77 additions & 0 deletions tests/general/LicenseCheckBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 <name of author>]',
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)

def test_settings_check_ignore(self):
test_file_path = get_testfile_path('mit_license.py')
file_contents = [load_testfile(test_file_path)]
self.check_results(
self.uut,
file_contents,
[],
filename=test_file_path,
settings={'licensecheck_check': r'.+\.cpp',
'licensecheck_ignore': r'.+\.py'})
Empty file.
13 changes: 13 additions & 0 deletions tests/general/licensecheck_test_files/apache_license_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2010-2012 <name of author>
#
# 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.
20 changes: 20 additions & 0 deletions tests/general/licensecheck_test_files/mit_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.
1 change: 1 addition & 0 deletions tests/general/licensecheck_test_files/no_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file doesn't have a license

0 comments on commit 0dfe3e9

Please sign in to comment.