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 Feb 18, 2017
1 parent a06d34e commit 2ddce93
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
49 changes: 49 additions & 0 deletions bears/general/LicenseCheckBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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='licensecheck')}
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 = ()
if licensecheck_lines != 60:
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,)
64 changes: 64 additions & 0 deletions tests/general/LicenseCheckBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from queue import Queue
import os.path

from bears.general.LicenseCheckBear import LicenseCheckBear
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper
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


class LicenseCheckBearTest(LocalBearTestHelper):

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

def test_license(self):
file_contents = [load_testfile(
get_testfile_path('apache_license_2.0.py'))]
test_file_path = get_testfile_path('apache_license_2.0.py')
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):
file_contents = [load_testfile(get_testfile_path('mit_license.py'))]
test_file_path = get_testfile_path('mit_license.py')
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'})

def test_no_license(self):
file_contents = [load_testfile(get_testfile_path('no_license.py'))]
test_file_path = get_testfile_path('no_license.py')
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)
13 changes: 13 additions & 0 deletions tests/general/licensecheck_test_files/apache_license_2.0.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 2ddce93

Please sign in to comment.