util: add update-copyright utility to update copyright on commits
The utility can automatically update copyright for the chosen organization on all files touched in the selected range of git commits. Change-Id: I4e1803e53f4530f88fb344f56e08ea29fbfcd41d Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/35535 Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br> Maintainer: Gabe Black <gabe.black@gmail.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -203,7 +203,10 @@ both tags and in the author field of the changeset.
|
||||
For significant changes, authors are encouraged to add copyright information
|
||||
and their names at the beginning of the file. The main purpose of the author
|
||||
names on the file is to track who is most knowledgeable about the file (e.g.,
|
||||
who has contributed a significant amount of code to the file).
|
||||
who has contributed a significant amount of code to the file). The
|
||||
`util/update-copyright.py` helper script can help to keep your copyright dates
|
||||
up-to-date when you make further changes to files which already have your
|
||||
copyright but with older dates.
|
||||
|
||||
Note: If you do not follow these guidelines, the gerrit review site will
|
||||
automatically reject your patch.
|
||||
|
||||
141
util/update-copyright.py
Executable file
141
util/update-copyright.py
Executable file
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright (c) 2020 ARM Limited
|
||||
# All rights reserved
|
||||
#
|
||||
# The license below extends only to copyright in the software and shall
|
||||
# not be construed as granting a license to any other intellectual
|
||||
# property including but not limited to intellectual property relating
|
||||
# to a hardware implementation of the functionality of the software
|
||||
# licensed hereunder. You may use the software subject to the license
|
||||
# terms below provided that you ensure that this notice is replicated
|
||||
# unmodified and in its entirety in all distributions of the software,
|
||||
# modified or unmodified, in source code or in binary form.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met: redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer;
|
||||
# redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution;
|
||||
# neither the name of the copyright holders nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import git_filter_repo
|
||||
|
||||
import update_copyright
|
||||
|
||||
parser = argparse.ArgumentParser(description=
|
||||
"""Update copyright headers on files of a range of commits.
|
||||
|
||||
This can be used to easily update copyright headers at once on an entire
|
||||
patchset before submitting.
|
||||
|
||||
Only files touched by the selected commits are updated.
|
||||
|
||||
Only existing copyrights for the selected holder are updated, new
|
||||
notices are never automatically added if not already present.
|
||||
|
||||
The size of the changes is not taken into account, every touched file gets
|
||||
updated. If you want to undo that for a certain file because the change to
|
||||
it is trivial, you need to manually rebase and undo the copyright change
|
||||
for that file.
|
||||
|
||||
Example usage with an organization alias such as `arm`:
|
||||
|
||||
```
|
||||
python3 -m pip install --user --requirement \
|
||||
gem5/util/update_copyright/requirements.txt
|
||||
./update-copyright.py -o arm HEAD~3
|
||||
```
|
||||
|
||||
The above would act on the 3 last commits (HEAD~2, HEAD~ and HEAD),
|
||||
leaving HEAD~3 unchanged, and doing updates such as:
|
||||
|
||||
```
|
||||
- * Copyright (c) 2010, 2012-2013, 2015,2017-2019 ARM Limited
|
||||
+ * Copyright (c) 2010, 2012-2013, 2015,2017-2020 ARM Limited
|
||||
```
|
||||
|
||||
If the organization is not in the alias list, you can also explicitly give
|
||||
the organization string as in:
|
||||
|
||||
```
|
||||
./update-copyright.py HEAD~3 'ARM Limited'
|
||||
```
|
||||
|
||||
which is equivalent to the previous invocation.
|
||||
""",
|
||||
formatter_class=argparse.RawTextHelpFormatter,
|
||||
)
|
||||
parser.add_argument('start',
|
||||
nargs='?',
|
||||
help="The commit before the last commit to be modified")
|
||||
parser.add_argument('org-string',
|
||||
nargs='?',
|
||||
help="Copyright holder name")
|
||||
parser.add_argument('-o', '--org', choices=('arm',),
|
||||
help="Alias for known organizations")
|
||||
args = parser.parse_args()
|
||||
|
||||
def error(msg):
|
||||
print('error: ' + msg, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# The existing safety checks are too strict, so we just disable them
|
||||
# with force, and do our own checks to not overwrite uncommited changes
|
||||
# checks.
|
||||
# https://github.com/newren/git-filter-repo/issues/159
|
||||
if subprocess.call(['git', 'diff', '--staged', '--quiet']):
|
||||
error("uncommitted changes")
|
||||
if subprocess.call(['git', 'diff', '--quiet']):
|
||||
error("unstaged changes")
|
||||
|
||||
# Handle CLI arguments.
|
||||
if args.start is None:
|
||||
error("the start argument must be given")
|
||||
if args.org is None and getattr(args, 'org-string') is None:
|
||||
error("either --org or org-string must be given")
|
||||
if args.org is not None and getattr(args, 'org-string') is not None:
|
||||
error("both --org and org-string given")
|
||||
if args.org is not None:
|
||||
org_bytes = update_copyright.org_alias_map[args.org]
|
||||
else:
|
||||
org_bytes = getattr(args, 'org-string').encode()
|
||||
|
||||
# Call git_filter_repo.
|
||||
# Args deduced from:
|
||||
# print(git_filter_repo.FilteringOptions.parse_args(['--refs', 'HEAD',
|
||||
# '--force'], error_on_empty=False))
|
||||
filter_repo_args = git_filter_repo.FilteringOptions.default_options()
|
||||
filter_repo_args.force = True
|
||||
filter_repo_args.partial = True
|
||||
filter_repo_args.refs = ['{}..HEAD'.format(args.start)]
|
||||
filter_repo_args.repack=False
|
||||
filter_repo_args.replace_refs='update-no-add'
|
||||
def blob_callback(blob, callback_metadata, org_bytes):
|
||||
blob.data = update_copyright.update_copyright(blob.data,
|
||||
datetime.datetime.now().year, org_bytes)
|
||||
git_filter_repo.RepoFilter(
|
||||
filter_repo_args,
|
||||
blob_callback=lambda x, y: blob_callback( x, y, org_bytes)
|
||||
).run()
|
||||
87
util/update_copyright/__init__.py
Normal file
87
util/update_copyright/__init__.py
Normal file
@@ -0,0 +1,87 @@
|
||||
# Copyright (c) 2020 ARM Limited
|
||||
# All rights reserved
|
||||
#
|
||||
# The license below extends only to copyright in the software and shall
|
||||
# not be construed as granting a license to any other intellectual
|
||||
# property including but not limited to intellectual property relating
|
||||
# to a hardware implementation of the functionality of the software
|
||||
# licensed hereunder. You may use the software subject to the license
|
||||
# terms below provided that you ensure that this notice is replicated
|
||||
# unmodified and in its entirety in all distributions of the software,
|
||||
# modified or unmodified, in source code or in binary form.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met: redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer;
|
||||
# redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution;
|
||||
# neither the name of the copyright holders nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
"""
|
||||
Utilities to parse and modify copyright headers in gem5 source.
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
org_alias_map = {
|
||||
'arm': b'ARM Limited',
|
||||
'uc': b'The Regents of the University of California',
|
||||
}
|
||||
|
||||
_update_copyright_year_regexp = re.compile(b'(.*?)([0-9]+)$')
|
||||
|
||||
def _update_copyright_years(m, cur_year, org_bytes):
|
||||
'''
|
||||
Does e.g.: b'2016, 2018-2019' -> b'2016, 2018-2020'.
|
||||
|
||||
:param m: match containing only the years part of the string
|
||||
:type m: re.Match
|
||||
:param cur_year: the current year to update the copyright to
|
||||
:type cur_year: int
|
||||
:return: the new years part of the string
|
||||
:rtype: bytes
|
||||
'''
|
||||
global _update_copyright_year_regexp
|
||||
cur_year_bytes = str(cur_year).encode()
|
||||
m = _update_copyright_year_regexp.match(m.group(1))
|
||||
years_prefix = m.group(1)
|
||||
old_year_bytes = m.group(2)
|
||||
old_year = int(old_year_bytes.decode())
|
||||
if old_year == cur_year:
|
||||
new_years_string = old_year_bytes
|
||||
elif old_year == cur_year - 1:
|
||||
if len(years_prefix) > 0 and years_prefix[-1:] == b'-':
|
||||
new_years_string = cur_year_bytes
|
||||
else:
|
||||
new_years_string = old_year_bytes + b'-' + cur_year_bytes
|
||||
else:
|
||||
new_years_string = old_year_bytes + b', ' + cur_year_bytes
|
||||
new_years_string = years_prefix + new_years_string
|
||||
return b' Copyright (c) %b %b\n' % (new_years_string, org_bytes)
|
||||
|
||||
def update_copyright(data, cur_year, org_bytes):
|
||||
update_copyright_regexp = re.compile(
|
||||
b' Copyright \\(c\\) ([0-9,\- ]+) ' + org_bytes + b'\n',
|
||||
re.IGNORECASE
|
||||
)
|
||||
return update_copyright_regexp.sub(
|
||||
lambda m: _update_copyright_years(m, cur_year, org_bytes),
|
||||
data,
|
||||
count=1,
|
||||
)
|
||||
1
util/update_copyright/requirements.txt
Normal file
1
util/update_copyright/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
git-filter-repo==2.28.0
|
||||
0
util/update_copyright/test/__init__.py
Normal file
0
util/update_copyright/test/__init__.py
Normal file
86
util/update_copyright/test/test_copyright.py
Normal file
86
util/update_copyright/test/test_copyright.py
Normal file
@@ -0,0 +1,86 @@
|
||||
# Copyright (c) 2020 ARM Limited
|
||||
# All rights reserved
|
||||
#
|
||||
# The license below extends only to copyright in the software and shall
|
||||
# not be construed as granting a license to any other intellectual
|
||||
# property including but not limited to intellectual property relating
|
||||
# to a hardware implementation of the functionality of the software
|
||||
# licensed hereunder. You may use the software subject to the license
|
||||
# terms below provided that you ensure that this notice is replicated
|
||||
# unmodified and in its entirety in all distributions of the software,
|
||||
# modified or unmodified, in source code or in binary form.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met: redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer;
|
||||
# redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution;
|
||||
# neither the name of the copyright holders nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import unittest
|
||||
|
||||
import update_copyright
|
||||
|
||||
class TestUpdateCopyright(unittest.TestCase):
|
||||
def update_arm_copyright(self, data, cur_year):
|
||||
return update_copyright.update_copyright(
|
||||
data, cur_year,
|
||||
update_copyright.org_alias_map['arm'])
|
||||
def update_uc_copyright(self, data, cur_year):
|
||||
return update_copyright.update_copyright(
|
||||
data, cur_year,
|
||||
update_copyright.org_alias_map['uc'])
|
||||
def test_cpp(self):
|
||||
self.assertEqual(self.update_arm_copyright(
|
||||
b' * Copyright (c) 2019 ARM Limited\n', 2020),
|
||||
b' * Copyright (c) 2019-2020 ARM Limited\n')
|
||||
self.assertEqual(self.update_uc_copyright(
|
||||
b' * Copyright (c) 2019 The Regents of the University of California\n', 2020),
|
||||
b' * Copyright (c) 2019-2020 The Regents of the University of California\n')
|
||||
def test_python(self):
|
||||
self.assertEqual(self.update_arm_copyright(
|
||||
b'# Copyright (c) 2019 ARM Limited\n', 2020),
|
||||
b'# Copyright (c) 2019-2020 ARM Limited\n')
|
||||
def test_multiline(self):
|
||||
self.assertEqual(self.update_arm_copyright(
|
||||
b'''/*
|
||||
* Copyright (c) 2019 ARM Limited
|
||||
* All rights reserved.
|
||||
''', 2020),
|
||||
b'''/*
|
||||
* Copyright (c) 2019-2020 ARM Limited
|
||||
* All rights reserved.
|
||||
''',
|
||||
)
|
||||
def test_comma(self):
|
||||
self.assertEqual(self.update_arm_copyright(
|
||||
b'# Copyright (c) 2018 ARM Limited\n', 2020),
|
||||
b'# Copyright (c) 2018, 2020 ARM Limited\n')
|
||||
def test_extend_dash(self):
|
||||
self.assertEqual(self.update_arm_copyright(
|
||||
b'# Copyright (c) 2018-2019 ARM Limited\n', 2020),
|
||||
b'# Copyright (c) 2018-2020 ARM Limited\n')
|
||||
def test_comma_and_dash_extend(self):
|
||||
self.assertEqual(self.update_arm_copyright(
|
||||
b'# Copyright (c) 2016, 2018-2019 ARM Limited\n', 2020),
|
||||
b'# Copyright (c) 2016, 2018-2020 ARM Limited\n')
|
||||
def test_standardize_case(self):
|
||||
self.assertEqual(self.update_arm_copyright(
|
||||
b'# Copyright (c) 2020 Arm Limited\n', 2020),
|
||||
b'# Copyright (c) 2020 ARM Limited\n')
|
||||
Reference in New Issue
Block a user