From 72617f3ef5e311e3ae8e15bc265ed5d99952e58e Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Wed, 1 Apr 2015 14:20:49 -0700 Subject: [PATCH 1/4] add support for licenses --- ament_copyright/ament_copyright/__init__.py | 424 +-------------- .../ament_copyright/copyright_names.py | 18 + ament_copyright/ament_copyright/crawler.py | 77 +++ ament_copyright/ament_copyright/licenses.py | 260 ++++++++++ ament_copyright/ament_copyright/main.py | 489 ++++++++++++++++++ ament_copyright/ament_copyright/names.py | 4 - ament_copyright/ament_copyright/parser.py | 258 +++++++++ ament_copyright/doc/index.rst | 31 +- ament_copyright/package.xml | 5 +- ament_copyright/setup.py | 14 +- 10 files changed, 1165 insertions(+), 415 deletions(-) create mode 100644 ament_copyright/ament_copyright/copyright_names.py create mode 100644 ament_copyright/ament_copyright/crawler.py create mode 100644 ament_copyright/ament_copyright/licenses.py create mode 100644 ament_copyright/ament_copyright/main.py delete mode 100644 ament_copyright/ament_copyright/names.py create mode 100644 ament_copyright/ament_copyright/parser.py diff --git a/ament_copyright/ament_copyright/__init__.py b/ament_copyright/ament_copyright/__init__.py index e37b0d3a..bca450a0 100644 --- a/ament_copyright/ament_copyright/__init__.py +++ b/ament_copyright/ament_copyright/__init__.py @@ -1,5 +1,5 @@ -# Copyright 2014 Open Source Robotics Foundation, Inc. - +# Copyright 2015 Open Source Robotics Foundation, Inc. +# # 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 @@ -12,414 +12,44 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - -import argparse -import os import pkg_resources -import re -import sys -import time -from xml.sax.saxutils import escape -from xml.sax.saxutils import quoteattr - -AMENT_COPYRIGHT_GROUP = 'ament_copyright.names' - - -def main(argv=sys.argv[1:]): - extensions = [ - 'c', 'cc', 'cpp', 'cxx', 'h', 'hh', 'hpp', 'hxx', - 'cmake', - 'py', - ] - - parser = argparse.ArgumentParser( - description='Check code for existance of a copyright reference.', - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument( - 'paths', - nargs='*', - default=[os.curdir], - help="The files or directories to check. For directories files ending " - 'in %s will be considered.' % - ', '.join(["'.%s'" % e for e in extensions])) - group = parser.add_mutually_exclusive_group() - group.add_argument( - '--add-missing', - help='Add missing copyright notice with the passed copyright holder') - group.add_argument( - '--add-copyright-year', - action='store_true', - help='Add the current year to existing copyright notices') - group.add_argument( - '--list-names', - action='store_true', - help='List name of possible copyright holders') - # not using a file handle directly - # in order to prevent leaving an empty file when something fails early - group.add_argument( - '--xunit-file', - help='Generate a xunit compliant XML file') - args = parser.parse_args(argv) - - names = get_copyright_names() - if args.list_names: - for key in sorted(names.keys()): - print('%s: %s' % (key, names[key])) - return 0 - - if args.xunit_file: - start_time = time.time() - - filenames = get_files(args.paths, extensions) - if not filenames: - print('No files found', file=sys.stderr) - return 0 - if args.add_missing: - name = names.get(args.add_missing, args.add_missing) - add_missing(filenames, name) - return 0 - if args.add_copyright_year: - add_copyright_year(filenames) - return 0 +COPYRIGHT_GROUP = 'ament_copyright.copyright_name' +LICENSE_GROUP = 'ament_copyright.license' - report = [] +SOURCE_FILETYPE = 1 +CONTRIBUTING_FILENAME = 'CONTRIBUTING.md' +CONTRIBUTING_FILETYPE = 2 +LICENSE_FILENAME = 'LICENSE' +LICENSE_FILETYPE = 3 - # check each file for copyright - for filename in filenames: - print(filename) - has_copyright = check_file_for_copyright(filename) - if not has_copyright: - print('%s: could not find copyright reference' % filename, - file=sys.stderr) - report.append((filename, has_copyright)) - print('') +ALL_FILETYPES = { + SOURCE_FILETYPE: None, + CONTRIBUTING_FILETYPE: CONTRIBUTING_FILENAME, + LICENSE_FILETYPE: LICENSE_FILENAME, +} - # output summary - error_count = len([r for r in report if not r[1]]) - if not error_count: - print('No errors') - rc = 0 - else: - print('%d errors' % error_count, file=sys.stderr) - rc = 1 - - # generate xunit file - if args.xunit_file: - folder_name = os.path.basename(os.path.dirname(args.xunit_file)) - file_name = os.path.basename(args.xunit_file) - suffix = '.xml' - if file_name.endswith(suffix): - file_name = file_name[0:-len(suffix)] - testname = '%s.%s' % (folder_name, file_name) - - xml = get_xunit_content(report, testname, time.time() - start_time) - path = os.path.dirname(os.path.abspath(args.xunit_file)) - if not os.path.exists(path): - os.makedirs(path) - with open(args.xunit_file, 'w') as f: - f.write(xml) - - return rc +UNKNOWN_IDENTIFIER = '' def get_copyright_names(): names = {} for entry_point in pkg_resources.iter_entry_points( - group=AMENT_COPYRIGHT_GROUP): + group=COPYRIGHT_GROUP): + assert entry_point.name != UNKNOWN_IDENTIFIER, \ + "Invalid entry point name '%s'" % entry_point.name name = entry_point.load() names[entry_point.name] = name return names -def get_files(paths, extensions): - files = [] - for path in paths: - if os.path.isdir(path): - for dirpath, dirnames, filenames in os.walk(path): - # ignore folder starting with . or _ - dirnames[:] = [d for d in dirnames if d[0] not in ['.', '_']] - dirnames.sort() - - # select files by extension - for filename in sorted(filenames): - _, ext = os.path.splitext(filename) - if ext in ['.%s' % e for e in extensions]: - files.append(os.path.join(dirpath, filename)) - if os.path.isfile(path): - files.append(path) - return files - - -def add_missing(filenames, name): - copyright = 'Copyright %s %s' % (time.strftime('%Y'), name) - print('Adding the following copyright notice:') - print(' ', copyright) - print('To the following file:') - - for filename in filenames: - has_copyright = check_file_for_copyright(filename) - if has_copyright: - continue - - print('*', filename) - add_copyright(filename, copyright) - - -def add_copyright_year(filenames): - current_year = time.strftime('%Y') - print('Adding the current year to existing copyright notices:') - - # regex for matching years or year ranges (yyyy-yyyy) separated by colons - year = '\d{4}' - year_range = '%s-%s' % (year, year) - year_or_year_range = '(%s|%s)' % (year, year_range) - pattern = '(\s+%s(\s*,\s*%s)*)\s+.+' % \ - (year_or_year_range, year_or_year_range) - regex = re.compile(pattern) - - for filename in filenames: - has_copyright = check_file_for_copyright(filename) - if not has_copyright: - continue - - with open(filename, 'r') as h: - content = h.read() - - index = scan_past_coding_and_shebang_lines(content) - index = scan_past_empty_lines(content, index) - - assert is_copyright_line(content, index), \ - "Could not find copyright line in file '%s'" % filename - - # extract copyright years - end_index = get_index_of_next_line(content, index) - line = content[index:end_index] - - copyright = 'copyright' - copyright_index = line.lower().index(copyright) - rest_line = line[copyright_index + len(copyright):] - - match = regex.match(rest_line) - if not match: - print("Could not find copyright year in file '%s' in line [%s]" % - (filename, line), file=sys.stderr) - continue - copyright_year = match.group(1) - - # skip if copyright year is already up-to-date - if current_year in copyright_year: - continue - - end_index = index + copyright_index + len(copyright) + \ - len(copyright_year) - if copyright_year[-4:] != str(int(current_year) - 1): - # append current year - content = content[:end_index] + ', ' + current_year + \ - content[end_index:] - elif copyright_year[-5:-4] == '-': - # update end year of range - content = content[:end_index - 4] + current_year + \ - content[end_index:] - else: - # update year to be a range - content = content[:end_index] + '-' + current_year + \ - content[end_index:] - - # output beginning of file for debugging - # index = end_index - # for _ in range(3): - # index = get_index_of_next_line(content, index) - # print('<<<') - # print(content[:index - 1]) - # print('>>>') - - with open(filename, 'w') as h: - h.write(content) - - -def add_copyright(filename, copyright): - """ - Add the copyright message to a file. - - The copyright is placed below an optional shebang line as well as an - optional coding line. - It is preceded by an empty line if not at the beginning of the file and - always followed by an empty line. - """ - with open(filename, 'r') as h: - content = h.read() - - begin_index = scan_past_coding_and_shebang_lines(content) - end_index = scan_past_empty_lines(content, begin_index) - - # inject copyright message - comment = get_comment(filename, copyright) - inserted_block = '%s\n\n' % comment - if begin_index > 0: - inserted_block = '\n' + inserted_block - content = content[:begin_index] + inserted_block + content[end_index:] - - # output beginning of file for debugging - # index = end_index + len(inserted_block) - # for _ in range(3): - # index = get_index_of_next_line(content, index) - # print('<<<') - # print(content[:index - 1]) - # print('>>>') - - with open(filename, 'w') as h: - h.write(content) - - -def scan_past_coding_and_shebang_lines(content): - index = 0 - while ( - is_comment_line(content, index) and - (is_coding_line(content, index) or - is_shebang_line(content, index)) - ): - index = get_index_of_next_line(content, index) - return index - - -def scan_past_empty_lines(content, index): - while is_empty_line(content, index): - index = get_index_of_next_line(content, index) - return index - - -def get_index_of_next_line(content, index): - index_n = content.find('\n', index) - index_r = content.find('\r', index) - index_rn = content.find('\r\n', index) - indices = set([]) - if index_n != -1: - indices.add(index_n) - if index_r != -1: - indices.add(index_r) - if index_rn != -1: - indices.add(index_rn) - if not indices: - return len(content) - index = min(indices) - if index == index_rn: - return index + 2 - return index + 1 - - -def is_comment_line(content, index): - return content[index] == '#' or content[index:index + 1] == '//' - - -def is_coding_line(content, index): - end_index = get_index_of_next_line(content, index) - line = content[index:end_index] - return 'coding=' in line or 'coding:' in line - - -def is_shebang_line(content, index): - return content[index:index + 1] == '#!' - - -def is_empty_line(content, index): - return get_index_of_next_line(content, index) == index + 1 - - -def get_comment(filename, msg): - if filename.endswith('.py'): - line_prefix = '# ' - else: - line_prefix = '// ' - - comment = '' - index = 0 - while True: - new_index = get_index_of_next_line(msg, index) - if new_index == index: - break - comment += line_prefix + msg[index:new_index] - index = new_index - return comment - - -def check_file_for_copyright(filename): - with open(filename, 'r') as h: - content = h.read() - - index = scan_past_coding_and_shebang_lines(content) - index = scan_past_empty_lines(content, index) - - return is_copyright_line(content, index) - - -def is_copyright_line(content, index): - if not is_comment_line(content, index): - return False - - end_index = get_index_of_next_line(content, index) - line = content[index:end_index].lower() - - if line.startswith('#'): - line = line[1:] - elif line.startswith('//'): - line = line[2:] - else: - assert False, 'Unknown comment line [%s]' % line - - return line.lstrip().lower().startswith('copyright') - - -def get_xunit_content(report, testname, elapsed): - test_count = len(report) - error_count = len([r for r in report if not r[1]]) - data = { - 'testname': testname, - 'test_count': test_count, - 'error_count': error_count, - 'time': '%.3f' % round(elapsed, 3), - } - xml = ''' - -''' % data - - for (filename, has_copyright) in report: - - data = { - 'quoted_filename': quoteattr(filename), - 'testname': testname, - } - if not has_copyright: - # report missing copyright as a failing testcase - xml += ''' - - -''' % data - - else: - # if there is a copyright report a single successful test - xml += ''' -''' % data - - # output list of checked files - data = { - 'escaped_files': escape(''.join(['\n* %s' % r[0] for r in report])), - } - xml += ''' Checked files:%(escaped_files)s -''' % data - - xml += '\n' - return xml +def get_licenses(): + licenses = {} + for entry_point in pkg_resources.iter_entry_points( + group=LICENSE_GROUP): + assert entry_point.name != UNKNOWN_IDENTIFIER, \ + "Invalid entry point name '%s'" % entry_point.name + license = entry_point.load() + licenses[entry_point.name] = license + return licenses diff --git a/ament_copyright/ament_copyright/copyright_names.py b/ament_copyright/ament_copyright/copyright_names.py new file mode 100644 index 00000000..d7e2a0ad --- /dev/null +++ b/ament_copyright/ament_copyright/copyright_names.py @@ -0,0 +1,18 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# 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. + +# to add more copyright holder names do not extend this file +# instead create a separate package and register custom names as entry points + +osrf = 'Open Source Robotics Foundation, Inc.' diff --git a/ament_copyright/ament_copyright/crawler.py b/ament_copyright/ament_copyright/crawler.py new file mode 100644 index 00000000..591f627e --- /dev/null +++ b/ament_copyright/ament_copyright/crawler.py @@ -0,0 +1,77 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# 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. + +import os + +from ament_copyright import ALL_FILETYPES +from ament_copyright import SOURCE_FILETYPE + + +def get_files(paths, extensions, skip_package_level_setup_py=True): + files = {} + for path in paths: + if os.path.isdir(path): + if is_repository_root(path): + add_files_for_all_filetypes(path, files) + for dirpath, dirnames, filenames in os.walk(path): + if is_repository_root(dirpath): + add_files_for_all_filetypes(dirpath, files) + + # ignore folder starting with . or _ + dirnames[:] = [d for d in dirnames if d[0] not in ['.', '_']] + dirnames.sort() + + # select files by extension + for filename in sorted(filenames): + # skip package level setup.py file + if ( + skip_package_level_setup_py and + filename == 'setup.py' and + os.path.exists(os.path.join(dirpath, 'package.xml')) + ): + continue + if match_filename(filename, extensions): + files[os.path.join(dirpath, filename)] = SOURCE_FILETYPE + + if os.path.isfile(path) and match_filename(path, extensions): + files[path] = SOURCE_FILETYPE + + if is_repository_root(os.path.dirname(path)): + basename = os.path.basename(path) + for filetype, filename in ALL_FILETYPES.items(): + if filename == basename: + files[path] = filetype + break + return files + + +def is_repository_root(path): + """Check if the path is the root of a git or mercurial repository.""" + return ( + os.path.exists(os.path.join(path, '.git')) or + os.path.exists(os.path.join(path, '.hg')) + ) + + +def match_filename(filename, extensions): + """Check if the filename has one of the extensions.""" + _, ext = os.path.splitext(filename) + return ext in ['.%s' % e for e in extensions] + + +def add_files_for_all_filetypes(path, files): + for filetype, filename in ALL_FILETYPES.items(): + if filename is None: + continue + files[os.path.join(path, filename)] = filetype diff --git a/ament_copyright/ament_copyright/licenses.py b/ament_copyright/ament_copyright/licenses.py new file mode 100644 index 00000000..3ab09641 --- /dev/null +++ b/ament_copyright/ament_copyright/licenses.py @@ -0,0 +1,260 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# 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. + +# to add more licenses do not extend this file +# instead create a separate package and register custom licenses as entry points + +from collections import namedtuple + +LicenseEntryPoint = namedtuple( + 'LicenseEntryPoint', ['name', 'file_header', 'license_file', 'contributing_file']) + +apache2 = LicenseEntryPoint( + 'Apache License, Version 2.0', + + """\ +{copyright} + +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.""", + + """\ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + {copyright} + + 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. +""", + + """\ +Any contribution that you make to this repository will +be under the Apache 2 License, as dictated by that +[license](http://www.apache.org/licenses/LICENSE-2.0.html): + +~~~ +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. +~~~ +""") diff --git a/ament_copyright/ament_copyright/main.py b/ament_copyright/ament_copyright/main.py new file mode 100644 index 00000000..f36edfbd --- /dev/null +++ b/ament_copyright/ament_copyright/main.py @@ -0,0 +1,489 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# 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. + +from __future__ import print_function + +import argparse +from itertools import groupby +import os +import re +import sys +import time +from xml.sax.saxutils import escape +from xml.sax.saxutils import quoteattr + +from ament_copyright import CONTRIBUTING_FILETYPE +from ament_copyright import get_copyright_names +from ament_copyright import get_licenses +from ament_copyright import LICENSE_FILETYPE +from ament_copyright import SOURCE_FILETYPE +from ament_copyright import UNKNOWN_IDENTIFIER +from ament_copyright.crawler import get_files +from ament_copyright.parser import get_index_of_next_line +from ament_copyright.parser import get_comment_block +from ament_copyright.parser import parse_file +from ament_copyright.parser import scan_past_coding_and_shebang_lines +from ament_copyright.parser import scan_past_empty_lines +from ament_copyright.parser import _search_copyright_information + + +def main(argv=sys.argv[1:]): + extensions = [ + 'c', 'cc', 'cpp', 'cxx', 'h', 'hh', 'hpp', 'hxx', + 'cmake', + 'py', + ] + + parser = argparse.ArgumentParser( + description='Check code files for copyright and license information.', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument( + 'paths', + nargs='*', + default=[os.curdir], + help='The files or directories to check. For directories files ending ' + 'in %s will be considered (except setup.py files beside package.xml files).' % + ', '.join(["'.%s'" % e for e in extensions])) + group = parser.add_mutually_exclusive_group() + group.add_argument( + '--add-missing', + nargs=2, + metavar=('COPYRIGHT_NAME', 'LICENSE'), + help=( + 'Add missing copyright notice and license information using the ' + 'passed copyright holder and license')) + group.add_argument( + '--add-copyright-year', + nargs='*', + type=int, + help='Add the current year to existing copyright notices') + group.add_argument( + '--list-copyright-names', + action='store_true', + help='List names of known copyright holders') + group.add_argument( + '--list-licenses', + action='store_true', + help='List names of known licenses') + parser.add_argument( + '--verbose', + action='store_true', + help='Show all files instead of only the ones with errors / modifications') + # not using a file handle directly + # in order to prevent leaving an empty file when something fails early + group.add_argument( + '--xunit-file', + help='Generate a xunit compliant XML file') + args = parser.parse_args(argv) + + names = get_copyright_names() + if args.list_copyright_names: + for key in sorted(names.keys()): + print('%s: %s' % (key, names[key])) + return 0 + + licenses = get_licenses() + if args.list_licenses: + for key in sorted(licenses.keys()): + print('%s: %s' % (key, licenses[key].name)) + return 0 + + if args.xunit_file: + start_time = time.time() + + filenames = get_files(args.paths, extensions) + if not filenames: + print('No repository roots and files found', file=sys.stderr) + return 0 + + file_descriptors = {} + for filename in sorted(filenames): + file_descriptors[filename] = parse_file(filename) + + if args.add_missing: + name = names.get(args.add_missing[0], args.add_missing[0]) + if args.add_missing[1] not in licenses: + parser.error( + "'LICENSE' argument must be a known license name. " + "Use the '--list-licenses' options to see alist of valid license names.") + license = licenses[args.add_missing[1]] + add_missing_header(file_descriptors, name, license, args.verbose) + return 0 + + if args.add_copyright_year is not None: + if not args.add_copyright_year: + args.add_copyright_year.append(time.strftime('%Y')) + args.add_copyright_year = [int(year) for year in args.add_copyright_year] + add_copyright_year(file_descriptors, args.add_copyright_year, args.verbose) + return 0 + + report = [] + + # check each directory for CONTRIBUTING.md and LICENSE files + for path in sorted(file_descriptors.keys()): + file_descriptor = file_descriptors[path] + message = None + has_error = False + + if file_descriptor.filetype == SOURCE_FILETYPE: + if not file_descriptor.exists: + message = 'file not found' + has_error = True + + elif not file_descriptor.content: + message = 'file empty' + + elif not file_descriptor.copyright_identifier: + message = 'could not find copyright notice' + has_error = True + + else: + message = 'copyright=%s%s, license=%s' % \ + (file_descriptor.copyright_identifier, + ' (%s)' % file_descriptor.copyright_years + if file_descriptor.copyright_years else '', + file_descriptor.license_identifier) + has_error = file_descriptor.license_identifier == UNKNOWN_IDENTIFIER + + elif file_descriptor.filetype == CONTRIBUTING_FILETYPE: + if not file_descriptor.exists: + message = 'file not found' + has_error = True + + elif not file_descriptor.content: + message = 'file empty' + has_error = True + + elif file_descriptor.license_identifier: + message = file_descriptor.license_identifier + has_error = file_descriptor.license_identifier == UNKNOWN_IDENTIFIER + + else: + assert False, file_descriptor + + elif file_descriptor.filetype == LICENSE_FILETYPE: + if not file_descriptor.exists: + message = 'file not found' + has_error = True + + elif not file_descriptor.content: + message = 'could not find copyright notice' + print('%s: could not find copyright notice' % file_descriptor.path, + file=sys.stderr) + has_error = True + + elif file_descriptor.license_identifier == UNKNOWN_IDENTIFIER: + message = '%s%s%s' % \ + (file_descriptor.license_identifier, + ' (%s)' % file_descriptor.copyright_years + if file_descriptor.copyright_years else '', + ' (%s)' % file_descriptor.copyright_name + if file_descriptor.copyright_name else '') + has_error = True + + elif file_descriptor.license_identifier: + message = '%s%s' % \ + (file_descriptor.license_identifier, + ' (%s)' % file_descriptor.copyright_years + if file_descriptor.copyright_years else '') + + else: + assert False, file_descriptor + + else: + assert False, 'Unknown filetype: ' + file_descriptor.filetype + + if args.verbose or has_error: + print('%s: %s' % (file_descriptor.path, message), + file=sys.stderr if has_error else sys.stdout) + report.append((file_descriptor.path, not has_error, message)) + + # output summary + error_count = len([r for r in report if not r[1]]) + if not error_count: + print('No errors, checked %d files' % len(report)) + rc = 0 + else: + print('%d errors, checked %d files' % (error_count, len(report)), file=sys.stderr) + rc = 1 + + # generate xunit file + if args.xunit_file: + folder_name = os.path.basename(os.path.dirname(args.xunit_file)) + file_name = os.path.basename(args.xunit_file) + suffix = '.xml' + if file_name.endswith(suffix): + file_name = file_name[0:-len(suffix)] + testname = '%s.%s' % (folder_name, file_name) + + xml = get_xunit_content(report, testname, time.time() - start_time) + path = os.path.dirname(os.path.abspath(args.xunit_file)) + if not os.path.exists(path): + os.makedirs(path) + with open(args.xunit_file, 'w') as f: + f.write(xml) + + return rc + + +def add_missing_header(file_descriptors, name, license, verbose): + copyright = 'Copyright %s %s' % (time.strftime('%Y'), name) + header = license.file_header.format(**{'copyright': copyright}) + lines = header.splitlines() + + if verbose: + print('Adding the following copyright / license header or repository level files:') + for line in lines: + print('+', line) + print() + + for path in sorted(file_descriptors.keys()): + file_descriptor = file_descriptors[path] + skip = False + + if file_descriptor.filetype == SOURCE_FILETYPE: + # ignore empty files + if not file_descriptor.content: + skip = True + # ignore files which already have a header + if file_descriptor.copyright_identifier is not None: + skip = True + elif file_descriptor.exists: + # ignore non-source files if they already exist + skip = True + + if skip: + if verbose: + print(' ', file_descriptor.path) + continue + + if file_descriptor.filetype == SOURCE_FILETYPE: + print('*', file_descriptor.path) + add_header(file_descriptor, header) + + elif file_descriptor.filetype == CONTRIBUTING_FILETYPE: + print('+', file_descriptor.path) + with open(file_descriptor.path, 'w') as h: + h.write(license.contributing_file) + + elif file_descriptor.filetype == LICENSE_FILETYPE: + print('+', file_descriptor.path) + content = license.license_file.format(**{'copyright': copyright}) + with open(file_descriptor.path, 'w') as h: + h.write(content) + + else: + assert False, 'Unknown filetype: ' + file_descriptor.filetype + + +def add_copyright_year(file_descriptors, new_years, verbose): + if verbose: + print('Adding the current year to existing copyright notices:') + print() + + for path in sorted(file_descriptors.keys()): + file_descriptor = file_descriptors[path] + + # ignore files which do not have a header + if not getattr(file_descriptor, 'copyright_identifier', None): + continue + + index = scan_past_coding_and_shebang_lines(file_descriptor.content) + index = scan_past_empty_lines(file_descriptor.content, index) + + if file_descriptor.filetype == SOURCE_FILETYPE: + block, block_offset = get_comment_block(file_descriptor.content, index) + if not block: + assert False, "Could not find comment block in file '%s'" % file_descriptor.path + else: + block = file_descriptor.content[index:] + block_offset = 0 + copyright_span, years_span, name_span = _search_copyright_information(block) + if copyright_span is None: + assert False, "Could not find copyright information in file '%s'" % \ + file_descriptor.path + + # skip if all new years are already included + years = get_years_from_string(block[years_span[0]:years_span[1]]) + if all([(new_year in years) for new_year in new_years]): + if verbose: + print(' ', file_descriptor.path) + continue + print('*' if file_descriptor.exists else '+', file_descriptor.path) + + for new_year in new_years: + years.add(new_year) + years_string = get_string_from_years(years) + + # overwrite previous years with new years + offset = index + block_offset + global_years_span = [offset + years_span[0], offset + years_span[1]] + content = file_descriptor.content[:global_years_span[0]] + years_string + \ + file_descriptor.content[global_years_span[1]:] + + # output beginning of file for debugging + # index = global_years_span[0] + # for _ in range(3): + # index = get_index_of_next_line(content, index) + # print('<<<') + # print(content[:index - 1]) + # print('>>>') + + with open(file_descriptor.path, 'w') as h: + h.write(content) + + +def get_years_from_string(content): + # remove all whitespaces + content = re.sub('\s', '', content) + # split items by comma + items = content.split(',') + + years = set([]) + for item in items: + # each item can be a plain year or a range of years + parts = item.split('-', 1) + if len(parts) == 1: + # plain year + years.add(int(parts[0])) + else: + # year range + start_year = int(parts[0]) + end_year = int(parts[1]) + assert end_year >= start_year + for year in range(start_year, end_year + 1): + years.add(year) + return years + + +def get_string_from_years(years): + ranges = [] + for _, iterable in groupby(enumerate(sorted(years)), lambda x: x[1] - x[0]): + r = list(iterable) + if len(r) == 1: + ranges.append(str(r[0][1])) + else: + ranges.append('%d-%d' % (r[0][1], r[-1][1])) + return ', '.join(ranges) + + +def add_header(file_descriptor, header): + """ + Add the copyright / license message to a file. + + The copyright / license is placed below an optional shebang line as + well as an optional coding line. + It is preceded by an empty line if not at the beginning of the file + and always followed by an empty line. + """ + begin_index = scan_past_coding_and_shebang_lines(file_descriptor.content) + end_index = scan_past_empty_lines(file_descriptor.content, begin_index) + + # inject copyright message + comment = get_comment(file_descriptor.path, header) + inserted_block = '%s\n\n' % comment + if begin_index > 0: + inserted_block = '\n' + inserted_block + content = file_descriptor.content[:begin_index] + inserted_block + \ + file_descriptor.content[end_index:] + + # output beginning of file for debugging + # index = end_index + len(inserted_block) + # for _ in range(3): + # index = get_index_of_next_line(content, index) + # print('<<<') + # print(content[:index - 1]) + # print('>>>') + + with open(file_descriptor.path, 'w') as h: + h.write(content) + + +def get_comment(filename, msg): + if filename.endswith('.cmake') or filename.endswith('.py'): + line_prefix = '#' + else: + line_prefix = '//' + + comment = '' + index = 0 + while True: + new_index = get_index_of_next_line(msg, index) + if new_index == index: + break + comment += line_prefix + line = msg[index:new_index] + if line.splitlines()[0]: + comment += ' ' + comment += line + index = new_index + return comment + + +def get_xunit_content(report, testname, elapsed): + test_count = len(report) + error_count = len([r for r in report if not r[1]]) + data = { + 'testname': testname, + 'test_count': test_count, + 'error_count': error_count, + 'time': '%.3f' % round(elapsed, 3), + } + xml = ''' + +''' % data + + for (filename, has_error, message) in report: + + data = { + 'quoted_filename': quoteattr(filename), + 'testname': testname, + 'escaped_message': escape(message), + } + if not has_error: + # report missing / unknown copyright / license as a failing testcase + xml += ''' + + +''' % data + + else: + # if there is a knwon copyright / license report a single successful test + xml += ''' +''' % data + + # output list of checked files + data = { + 'escaped_files': escape(''.join(['\n* %s' % r[0] for r in report])), + } + xml += ''' Checked files:%(escaped_files)s +''' % data + + xml += '\n' + return xml + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/ament_copyright/ament_copyright/names.py b/ament_copyright/ament_copyright/names.py deleted file mode 100644 index 5a3d2369..00000000 --- a/ament_copyright/ament_copyright/names.py +++ /dev/null @@ -1,4 +0,0 @@ -# to add more copyright holder names do not extend this file -# instead create a separate package and register custom names as entry points - -osrf = 'Open Source Robotics Foundation, Inc.' diff --git a/ament_copyright/ament_copyright/parser.py b/ament_copyright/ament_copyright/parser.py new file mode 100644 index 00000000..fe5db0d2 --- /dev/null +++ b/ament_copyright/ament_copyright/parser.py @@ -0,0 +1,258 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# 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. + +import os +import re + +from ament_copyright import ALL_FILETYPES +from ament_copyright import CONTRIBUTING_FILETYPE +from ament_copyright import get_copyright_names +from ament_copyright import get_licenses +from ament_copyright import LICENSE_FILETYPE +from ament_copyright import SOURCE_FILETYPE +from ament_copyright import UNKNOWN_IDENTIFIER + + +class FileDescriptor(object): + + def __init__(self, filetype, path): + self.filetype = filetype + self.path = path + self.exists = os.path.exists(path) + self.content = None + + def read(self): + if not self.exists: + return + with open(self.path, 'r') as h: + self.content = h.read() + + def parse(self): + raise NotImplemented() + + def identify_copyright(self): + for identifier, name in get_copyright_names().items(): + if self.copyright_name is not None and self.copyright_name == name: + self.copyright_identifier = identifier + break + else: + self.copyright_identifier = UNKNOWN_IDENTIFIER + + def identify_license(self, content, license_part): + for name, license in get_licenses().items(): + if content is not None and getattr(license, license_part) == content: + self.license_identifier = name + break + else: + self.license_identifier = UNKNOWN_IDENTIFIER + + +class SourceDescriptor(FileDescriptor): + + def __init__(self, path): + super(SourceDescriptor, self).__init__(SOURCE_FILETYPE, path) + + self.copyright_name = None + self.copyright_years = None + + self.copyright_identifier = None + self.license_identifier = None + + def parse(self): + self.read() + if not self.content: + return + + # skip over coding and shebang lines + index = scan_past_coding_and_shebang_lines(self.content) + index = scan_past_empty_lines(self.content, index) + + # get first comment block without leading comment tokens + block, _ = get_comment_block(self.content, index) + if not block: + return + copyright_span, years_span, name_span = _search_copyright_information(block) + if copyright_span is None: + return None + + self.copyright_years = block[years_span[0]:years_span[1]] + self.copyright_name = block[name_span[0]:name_span[1]] + + self.identify_copyright() + + content = '{copyright}' + block[name_span[1]:] + self.identify_license(content, 'file_header') + + +class ContributingDescriptor(FileDescriptor): + + def __init__(self, path): + super(ContributingDescriptor, self).__init__(CONTRIBUTING_FILETYPE, path) + + self.license_identifier = None + + def parse(self): + self.read() + if not self.content: + return + + self.identify_license(self.content, 'contributing_file') + + +class LicenseDescriptor(FileDescriptor): + + def __init__(self, path): + super(LicenseDescriptor, self).__init__(LICENSE_FILETYPE, path) + + self.copyright_years = None + self.copyright_name = None + + self.copyright_identifier = None + self.license_identifier = None + + def parse(self): + self.read() + if not self.content: + return + + self.identify_copyright() + + content = _replace_copyright_with_placeholder(self.content, self) + self.identify_license(content, 'license_file') + + +def parse_file(path): + filetype = determine_filetype(path) + if filetype == SOURCE_FILETYPE: + d = SourceDescriptor(path) + elif filetype == CONTRIBUTING_FILETYPE: + d = ContributingDescriptor(path) + elif filetype == LICENSE_FILETYPE: + d = LicenseDescriptor(path) + else: + return None + d.parse() + return d + + +def determine_filetype(path): + basename = os.path.basename(path) + for filetype, filename in ALL_FILETYPES.items(): + if basename == filename: + return filetype + return SOURCE_FILETYPE + + +def _replace_copyright_with_placeholder(content, file_descriptor): + copyright_span, years_span, name_span = _search_copyright_information(content) + if copyright_span is None: + return None + + file_descriptor.copyright_years = content[years_span[0]:years_span[1]] + file_descriptor.copyright_name = content[name_span[0]:name_span[1]] + + return content[:copyright_span[0]] + '{copyright}' + content[name_span[1]:] + + +def _search_copyright_information(content): + # regex for matching years or year ranges (yyyy-yyyy) separated by colons + year = '\d{4}' + year_range = '%s-%s' % (year, year) + year_or_year_range = '(?:%s|%s)' % (year, year_range) + pattern = '^[^\n\r]?\s*(Copyright)\s+(%s(?:,\s*%s)*)\s+([^\n\r]+)$' % \ + (year_or_year_range, year_or_year_range) + regex = re.compile(pattern, re.DOTALL | re.MULTILINE) + + match = regex.search(content) + if not match: + return None, None, None + return match.span(1), match.span(2), match.span(3) + + +def scan_past_coding_and_shebang_lines(content): + index = 0 + while ( + is_comment_line(content, index) and + (is_coding_line(content, index) or + is_shebang_line(content, index)) + ): + index = get_index_of_next_line(content, index) + return index + + +def get_index_of_next_line(content, index): + index_n = content.find('\n', index) + index_r = content.find('\r', index) + index_rn = content.find('\r\n', index) + indices = set([]) + if index_n != -1: + indices.add(index_n) + if index_r != -1: + indices.add(index_r) + if index_rn != -1: + indices.add(index_rn) + if not indices: + return len(content) + index = min(indices) + if index == index_rn: + return index + 2 + return index + 1 + + +def is_comment_line(content, index): + return content[index] == '#' or content[index:index + 1] == '//' + + +def is_coding_line(content, index): + end_index = get_index_of_next_line(content, index) + line = content[index:end_index] + return 'coding=' in line or 'coding:' in line + + +def is_shebang_line(content, index): + return content[index:index + 2] == '#!' + + +def get_comment_block(content, index): + # regex for matching the beginning of the first comment + pattern = '^(#|//)' + regex = re.compile(pattern, re.MULTILINE) + + match = regex.search(content, index) + if not match: + return None, None + comment_token = match.group(1) + start_index = match.start(1) + + end_index = start_index + while True: + end_index = get_index_of_next_line(content, end_index) + if content[end_index:end_index + len(comment_token)] != comment_token: + break + + block = content[start_index:end_index] + lines = block.splitlines() + lines = [line[len(comment_token) + 1:] for line in lines] + + return '\n'.join(lines), start_index + len(comment_token) + 1 + + +def scan_past_empty_lines(content, index): + while is_empty_line(content, index): + index = get_index_of_next_line(content, index) + return index + + +def is_empty_line(content, index): + return get_index_of_next_line(content, index) == index + 1 diff --git a/ament_copyright/doc/index.rst b/ament_copyright/doc/index.rst index 4b7a445c..3bccf38e 100644 --- a/ament_copyright/doc/index.rst +++ b/ament_copyright/doc/index.rst @@ -2,11 +2,18 @@ ament_copyright =============== Checks C / C++ / CMake / Python source files for the existance of a copyright -notice. +and licence notice. Files with the following extensions are being considered: ``.c``, ``.cc``, ``.cpp``, ``.cxx``, ``.h``, ``.hh``, ``.hpp``, ``.hxx``, ``.cmake``, ``.py``. +When being searched for recursively the following files are being excluded: + +- ``setup.py`` when being a sibling of ``package.xml`` + +Additionally it checks if the root of a repository contains a ``LICENSE`` and +``CONTRIBUTING.md`` file. + How to run the check from the command line? ------------------------------------------- @@ -15,14 +22,16 @@ How to run the check from the command line? ament_copyright [ ...] -When using the option ``--list-names`` a list of known copyright holders is -shown. +When using the option ``--list-copyright-names`` a list of known copyright +holders is shown. The option ``--list-licenses`` outputs a list of known +licenses. When using the option -``--add-missing `` a copyright notice is -added to all files which lack one. -The argument can either be a name from the list returned by ``--list-names`` or -a custom string. +``--add-missing `` +a copyright notice and license is added to all files which lack one. +The first argument can either be a name from the list returned by +``--list-copyright-names`` or a custom string. The second argument must be a +name from the list returned by ``--list-licenses``. When using the option ``--add-copyright-year`` existing copyright notices are being updated to include the current year. @@ -33,3 +42,11 @@ How to run the check from within a CMake ament package as part of the tests? The CMake integration is provided by the package `ament_cmake_copyright `_. + + +Why are my existing copyright / license notices not detected? +------------------------------------------------------------- + +This script currently only checks line comments (lines starting with ``#`` / +``//`` depending on the language). Block comments / C-style comment (starting +with ``/*``) are not being detected to keep the complexity minimal. diff --git a/ament_copyright/package.xml b/ament_copyright/package.xml index 0b60d98f..862c1fa8 100644 --- a/ament_copyright/package.xml +++ b/ament_copyright/package.xml @@ -2,7 +2,10 @@ ament_copyright 0.0.0 - The ability to check every source file contains copyright reference in the ament buildsystem. + + The ability to check source files for copyright and license + information. + Dirk Thomas Apache License 2.0 diff --git a/ament_copyright/setup.py b/ament_copyright/setup.py index 50105683..5166cd14 100644 --- a/ament_copyright/setup.py +++ b/ament_copyright/setup.py @@ -21,16 +21,18 @@ ], description='Check source files for copyright reference.', long_description='''\ -The ability to check every source file contains copyright reference -in the ament buildsystem.''', +The ability to check sources file for copyright and license information.''', license='Apache License, Version 2.0', test_suite='test', entry_points={ - 'console_scripts': [ - 'ament_copyright = ament_copyright:main', + 'ament_copyright.copyright_name': [ + 'osrf = ament_copyright.copyright_names:osrf', + ], + 'ament_copyright.license': [ + 'apache2 = ament_copyright.licenses:apache2', ], - 'ament_copyright.names': [ - 'osrf = ament_copyright.names:osrf', + 'console_scripts': [ + 'ament_copyright = ament_copyright.main:main', ], }, ) From f64223eeebaaeaafbbb2fb7c2da3de182b3790c3 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Fri, 3 Apr 2015 12:34:02 -0700 Subject: [PATCH 2/4] move apache2 snippets into separate files --- ament_copyright/ament_copyright/licenses.py | 244 +----------------- ament_copyright/ament_copyright/main.py | 40 +-- ament_copyright/ament_copyright/parser.py | 24 +- .../template/apache2_contributing.txt | 13 + .../template/apache2_header.txt | 13 + .../template/apache2_license.txt | 202 +++++++++++++++ ament_copyright/setup.py | 3 + 7 files changed, 251 insertions(+), 288 deletions(-) create mode 100644 ament_copyright/ament_copyright/template/apache2_contributing.txt create mode 100644 ament_copyright/ament_copyright/template/apache2_header.txt create mode 100644 ament_copyright/ament_copyright/template/apache2_license.txt diff --git a/ament_copyright/ament_copyright/licenses.py b/ament_copyright/ament_copyright/licenses.py index 3ab09641..28b60b4f 100644 --- a/ament_copyright/ament_copyright/licenses.py +++ b/ament_copyright/ament_copyright/licenses.py @@ -16,245 +16,25 @@ # instead create a separate package and register custom licenses as entry points from collections import namedtuple +import os LicenseEntryPoint = namedtuple( 'LicenseEntryPoint', ['name', 'file_header', 'license_file', 'contributing_file']) -apache2 = LicenseEntryPoint( - 'Apache License, Version 2.0', +TEMPLATE_DIRECTORY = os.path.join(os.path.dirname(__file__), 'template') - """\ -{copyright} -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 +def read_license_data(path, name, prefix): + path_template = os.path.join(path, prefix + '_%s.txt') - http://www.apache.org/licenses/LICENSE-2.0 + with open(path_template % 'header', 'r') as h: + file_header = h.read() + with open(path_template % 'license', 'r') as h: + license_file = h.read() + with open(path_template % 'contributing', 'r') as h: + contributing_file = h.read() -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.""", + return LicenseEntryPoint(name, file_header, license_file, contributing_file) - """\ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - {copyright} - - 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. -""", - - """\ -Any contribution that you make to this repository will -be under the Apache 2 License, as dictated by that -[license](http://www.apache.org/licenses/LICENSE-2.0.html): - -~~~ -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. -~~~ -""") +apache2 = read_license_data(TEMPLATE_DIRECTORY, 'Apache License, Version 2.0', 'apache2') diff --git a/ament_copyright/ament_copyright/main.py b/ament_copyright/ament_copyright/main.py index f36edfbd..221862e5 100644 --- a/ament_copyright/ament_copyright/main.py +++ b/ament_copyright/ament_copyright/main.py @@ -35,7 +35,7 @@ from ament_copyright.parser import parse_file from ament_copyright.parser import scan_past_coding_and_shebang_lines from ament_copyright.parser import scan_past_empty_lines -from ament_copyright.parser import _search_copyright_information +from ament_copyright.parser import search_copyright_information def main(argv=sys.argv[1:]): @@ -156,7 +156,7 @@ def main(argv=sys.argv[1:]): file_descriptor.license_identifier) has_error = file_descriptor.license_identifier == UNKNOWN_IDENTIFIER - elif file_descriptor.filetype == CONTRIBUTING_FILETYPE: + elif file_descriptor.filetype in [CONTRIBUTING_FILETYPE, LICENSE_FILETYPE]: if not file_descriptor.exists: message = 'file not found' has_error = True @@ -172,35 +172,6 @@ def main(argv=sys.argv[1:]): else: assert False, file_descriptor - elif file_descriptor.filetype == LICENSE_FILETYPE: - if not file_descriptor.exists: - message = 'file not found' - has_error = True - - elif not file_descriptor.content: - message = 'could not find copyright notice' - print('%s: could not find copyright notice' % file_descriptor.path, - file=sys.stderr) - has_error = True - - elif file_descriptor.license_identifier == UNKNOWN_IDENTIFIER: - message = '%s%s%s' % \ - (file_descriptor.license_identifier, - ' (%s)' % file_descriptor.copyright_years - if file_descriptor.copyright_years else '', - ' (%s)' % file_descriptor.copyright_name - if file_descriptor.copyright_name else '') - has_error = True - - elif file_descriptor.license_identifier: - message = '%s%s' % \ - (file_descriptor.license_identifier, - ' (%s)' % file_descriptor.copyright_years - if file_descriptor.copyright_years else '') - - else: - assert False, file_descriptor - else: assert False, 'Unknown filetype: ' + file_descriptor.filetype @@ -238,7 +209,7 @@ def main(argv=sys.argv[1:]): def add_missing_header(file_descriptors, name, license, verbose): - copyright = 'Copyright %s %s' % (time.strftime('%Y'), name) + copyright = 'Copyright %d %s' % (int(time.strftime('%Y')) - 1 + 1, name) header = license.file_header.format(**{'copyright': copyright}) lines = header.splitlines() @@ -279,9 +250,8 @@ def add_missing_header(file_descriptors, name, license, verbose): elif file_descriptor.filetype == LICENSE_FILETYPE: print('+', file_descriptor.path) - content = license.license_file.format(**{'copyright': copyright}) with open(file_descriptor.path, 'w') as h: - h.write(content) + h.write(license.license_file) else: assert False, 'Unknown filetype: ' + file_descriptor.filetype @@ -309,7 +279,7 @@ def add_copyright_year(file_descriptors, new_years, verbose): else: block = file_descriptor.content[index:] block_offset = 0 - copyright_span, years_span, name_span = _search_copyright_information(block) + copyright_span, years_span, name_span = search_copyright_information(block) if copyright_span is None: assert False, "Could not find copyright information in file '%s'" % \ file_descriptor.path diff --git a/ament_copyright/ament_copyright/parser.py b/ament_copyright/ament_copyright/parser.py index fe5db0d2..6c776064 100644 --- a/ament_copyright/ament_copyright/parser.py +++ b/ament_copyright/ament_copyright/parser.py @@ -82,7 +82,7 @@ def parse(self): block, _ = get_comment_block(self.content, index) if not block: return - copyright_span, years_span, name_span = _search_copyright_information(block) + copyright_span, years_span, name_span = search_copyright_information(block) if copyright_span is None: return None @@ -115,10 +115,6 @@ class LicenseDescriptor(FileDescriptor): def __init__(self, path): super(LicenseDescriptor, self).__init__(LICENSE_FILETYPE, path) - self.copyright_years = None - self.copyright_name = None - - self.copyright_identifier = None self.license_identifier = None def parse(self): @@ -126,10 +122,7 @@ def parse(self): if not self.content: return - self.identify_copyright() - - content = _replace_copyright_with_placeholder(self.content, self) - self.identify_license(content, 'license_file') + self.identify_license(self.content, 'license_file') def parse_file(path): @@ -154,18 +147,7 @@ def determine_filetype(path): return SOURCE_FILETYPE -def _replace_copyright_with_placeholder(content, file_descriptor): - copyright_span, years_span, name_span = _search_copyright_information(content) - if copyright_span is None: - return None - - file_descriptor.copyright_years = content[years_span[0]:years_span[1]] - file_descriptor.copyright_name = content[name_span[0]:name_span[1]] - - return content[:copyright_span[0]] + '{copyright}' + content[name_span[1]:] - - -def _search_copyright_information(content): +def search_copyright_information(content): # regex for matching years or year ranges (yyyy-yyyy) separated by colons year = '\d{4}' year_range = '%s-%s' % (year, year) diff --git a/ament_copyright/ament_copyright/template/apache2_contributing.txt b/ament_copyright/ament_copyright/template/apache2_contributing.txt new file mode 100644 index 00000000..6f63de9e --- /dev/null +++ b/ament_copyright/ament_copyright/template/apache2_contributing.txt @@ -0,0 +1,13 @@ +Any contribution that you make to this repository will +be under the Apache 2 License, as dictated by that +[license](http://www.apache.org/licenses/LICENSE-2.0.html): + +~~~ +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. +~~~ diff --git a/ament_copyright/ament_copyright/template/apache2_header.txt b/ament_copyright/ament_copyright/template/apache2_header.txt new file mode 100644 index 00000000..d5a3ca26 --- /dev/null +++ b/ament_copyright/ament_copyright/template/apache2_header.txt @@ -0,0 +1,13 @@ +{copyright} + +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. \ No newline at end of file diff --git a/ament_copyright/ament_copyright/template/apache2_license.txt b/ament_copyright/ament_copyright/template/apache2_license.txt new file mode 100644 index 00000000..d6456956 --- /dev/null +++ b/ament_copyright/ament_copyright/template/apache2_license.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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/ament_copyright/setup.py b/ament_copyright/setup.py index 5166cd14..5fe857f8 100644 --- a/ament_copyright/setup.py +++ b/ament_copyright/setup.py @@ -6,6 +6,9 @@ version='0.0.0', packages=find_packages(exclude=['test']), install_requires=['setuptools'], + package_data={'': [ + 'template/*', + ]}, author='Dirk Thomas', author_email='dthomas@osrfoundation.org', maintainer='Dirk Thomas', From 90e2480db098095bd49ffa31bff68cf1e56cb5cb Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Fri, 3 Apr 2015 12:34:12 -0700 Subject: [PATCH 3/4] update license file to keep copyright template --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index fdd9c106..d6456956 100644 --- a/LICENSE +++ b/LICENSE @@ -187,7 +187,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2014 Open Source Robotics Foundation, Inc. + Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 7f05f31736bb0f2c7528bb2d0df010fe95ecd9df Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Fri, 3 Apr 2015 12:55:03 -0700 Subject: [PATCH 4/4] update doc --- ament_copyright/ament_copyright/main.py | 3 ++- ament_copyright/doc/index.rst | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ament_copyright/ament_copyright/main.py b/ament_copyright/ament_copyright/main.py index 221862e5..6863fa2a 100644 --- a/ament_copyright/ament_copyright/main.py +++ b/ament_copyright/ament_copyright/main.py @@ -53,7 +53,8 @@ def main(argv=sys.argv[1:]): nargs='*', default=[os.curdir], help='The files or directories to check. For directories files ending ' - 'in %s will be considered (except setup.py files beside package.xml files).' % + "in %s will be considered (except directories starting with '.' " + "or '_' and 'setup.py' files beside 'package.xml' files)." % ', '.join(["'.%s'" % e for e in extensions])) group = parser.add_mutually_exclusive_group() group.add_argument( diff --git a/ament_copyright/doc/index.rst b/ament_copyright/doc/index.rst index 3bccf38e..eb2c69c3 100644 --- a/ament_copyright/doc/index.rst +++ b/ament_copyright/doc/index.rst @@ -1,14 +1,17 @@ ament_copyright =============== -Checks C / C++ / CMake / Python source files for the existance of a copyright -and licence notice. +Checks C / C++ / CMake / Python source files for the existence of a copyright +and license notice. Files with the following extensions are being considered: ``.c``, ``.cc``, ``.cpp``, ``.cxx``, ``.h``, ``.hh``, ``.hpp``, ``.hxx``, ``.cmake``, ``.py``. -When being searched for recursively the following files are being excluded: +When being searched for recursively the following directories and files are +being excluded: +- directories starting with ``.`` (e.g. ``.git``) or ``_`` (e.g. + ``__pycache__``) - ``setup.py`` when being a sibling of ``package.xml`` Additionally it checks if the root of a repository contains a ``LICENSE`` and @@ -44,9 +47,19 @@ The CMake integration is provided by the package `ament_cmake_copyright `_. +How to add support for more licenses or copyright holders? +---------------------------------------------------------- + +The package uses Python entry points to get all list of known licenses and +copyright holder. +You can implement a custom package and contribute more implementations to these +entry points or extend this package with more licenses. + + Why are my existing copyright / license notices not detected? ------------------------------------------------------------- This script currently only checks line comments (lines starting with ``#`` / ``//`` depending on the language). Block comments / C-style comment (starting with ``/*``) are not being detected to keep the complexity minimal. +Also the content must match the templates exactly.