Ciro Santilli has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/35535 )

Change subject: util: add update-copyright utility
......................................................................

util: add update-copyright utility

Only ARM copyright headers are implemented now, but the script is designed
to be easily generalizable by other organizations.

Change-Id: I4e1803e53f4530f88fb344f56e08ea29fbfcd41d
---
A util/update-copyright
1 file changed, 177 insertions(+), 0 deletions(-)



diff --git a/util/update-copyright b/util/update-copyright
new file mode 100755
index 0000000..7851646
--- /dev/null
+++ b/util/update-copyright
@@ -0,0 +1,177 @@
+#!/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 re
+
+import git_filter_repo
+
+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.
+
+Example usage:
+
+```
+python3 -m pip install --user git-filter-repo
+./update-copyright HEAD~3 arm
+```
+
+The above would act on the 3 last commits (HEAD~2, HEAD~ and HEAD),
+leaving HEAD~3 unchanged.
+""",
+    formatter_class=argparse.RawTextHelpFormatter,
+)
+parser.add_argument('--test',
+        action='store_true',
+        default=False,
+        help="Run unit tests instead of running.")
+parser.add_argument('start',
+        default=None,
+        nargs='?',
+        help="The commit before the last commit to be modified")
+parser.add_argument('org', default='arm', nargs='?', choices=('arm',),
+        help="Organization to update the copyright for")
+args = parser.parse_args()
+
+update_arm_copyright_regexp = re.compile(
+    b' Copyright \\(c\\) ([0-9,\- ]+) ARM Limited',
+    re.IGNORECASE
+)
+
+update_arm_copyright_year_regexp = re.compile(b'(.*?)([0-9]+)$')
+
+def update_copyright_years(m, cur_year):
+    '''
+    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
+    :return: the new years part of the string
+    :rtype: bytes
+    '''
+    cur_year_bytes = str(cur_year).encode()
+    m = update_arm_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) ' + new_years_string + b' ARM Limited'
+
+def update_arm_copyright(data, cur_year):
+    return update_arm_copyright_regexp.sub(
+        lambda m: update_copyright_years(m, cur_year),
+        data,
+        count=1,
+    )
+
+def blob_callback(blob, callback_metadata):
+ blob.data = update_arm_copyright(blob.data, datetime.datetime.now().year)
+
+if args.test:
+    import unittest
+    class TestConversion(unittest.TestCase):
+        def test_cpp(self):
+            self.assertEqual(update_arm_copyright(
+                    b' * Copyright (c) 2019 ARM Limited', 2020),
+                    b' * Copyright (c) 2019-2020 ARM Limited')
+        def test_python(self):
+            self.assertEqual(update_arm_copyright(
+                    b'# Copyright (c) 2019 ARM Limited', 2020),
+                    b'# Copyright (c) 2019-2020 ARM Limited')
+        def test_multiline(self):
+            self.assertEqual(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(update_arm_copyright(
+                    b'# Copyright (c) 2018 ARM Limited', 2020),
+                    b'# Copyright (c) 2018, 2020 ARM Limited')
+        def test_extend_dash(self):
+            self.assertEqual(update_arm_copyright(
+                    b'# Copyright (c) 2018-2019 ARM Limited', 2020),
+                    b'# Copyright (c) 2018-2020 ARM Limited')
+        def test_comma_and_dash_extend(self):
+            self.assertEqual(update_arm_copyright(
+                    b'# Copyright (c) 2016, 2018-2019 ARM Limited', 2020),
+                    b'# Copyright (c) 2016, 2018-2020 ARM Limited')
+        def test_standardize_case(self):
+            self.assertEqual(update_arm_copyright(
+                    b'# Copyright (c) 2020 Arm Limited', 2020),
+                    b'# Copyright (c) 2020 ARM Limited')
+    unittest.main(argv=[
+        'test',
+        # Uncomment to run a single test.
+        #'TestConversion.test_multiline'
+    ])
+else:
+    if args.start is None:
+        raise Exception('start must be given')
+    # 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'
+    git_filter_repo.RepoFilter(
+        filter_repo_args,
+        blob_callback=blob_callback
+    ).run()

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/35535
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I4e1803e53f4530f88fb344f56e08ea29fbfcd41d
Gerrit-Change-Number: 35535
Gerrit-PatchSet: 1
Gerrit-Owner: Ciro Santilli <ciro.santi...@arm.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to