mklog.py: add --update-copyright option
authorMartin Liska <mliska@suse.cz>
Mon, 4 Jan 2021 08:58:28 +0000 (09:58 +0100)
committerMartin Liska <mliska@suse.cz>
Mon, 4 Jan 2021 09:09:07 +0000 (10:09 +0100)
contrib/ChangeLog:

* mklog.py: Add --update-copyright option which adds:
"Update copyright years." to ChangeLog files belonging
to a modified file.

contrib/mklog.py

index 57be299507036eb97f4dd03a65dfa044ee61008e..e696f5d038863c81ef9ac6acf90e7f7a82b82690 100755 (executable)
 # Author: Martin Liska <mliska@suse.cz>
 
 import argparse
+import datetime
 import os
 import re
+import subprocess
 import sys
 from itertools import takewhile
 
@@ -227,6 +229,28 @@ def generate_changelog(data, no_functions=False, fill_pr_titles=False):
     return out
 
 
+def update_copyright(data):
+    current_timestamp = datetime.datetime.now().strftime('%Y-%m-%d')
+    username = subprocess.check_output('git config user.name', shell=True,
+                                       encoding='utf8').strip()
+    email = subprocess.check_output('git config user.email', shell=True,
+                                    encoding='utf8').strip()
+
+    changelogs = set()
+    diff = PatchSet(data)
+
+    for file in diff:
+        changelog = os.path.join(find_changelog(file.path), 'ChangeLog')
+        if changelog not in changelogs:
+            changelogs.add(changelog)
+            with open(changelog) as f:
+                content = f.read()
+            with open(changelog, 'w+') as f:
+                f.write(f'{current_timestamp}  {username}  <{email}>\n\n')
+                f.write('\tUpdate copyright years.\n\n')
+                f.write(content)
+
+
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(description=help_message)
     parser.add_argument('input', nargs='?',
@@ -238,28 +262,33 @@ if __name__ == '__main__':
     parser.add_argument('-c', '--changelog',
                         help='Append the ChangeLog to a git commit message '
                              'file')
+    parser.add_argument('--update-copyright', action='store_true',
+                        help='Update copyright in ChangeLog files')
     args = parser.parse_args()
     if args.input == '-':
         args.input = None
 
     data = open(args.input) if args.input else sys.stdin
-    output = generate_changelog(data, args.no_functions,
-                                args.fill_up_bug_titles)
-    if args.changelog:
-        lines = open(args.changelog).read().split('\n')
-        start = list(takewhile(lambda l: not l.startswith('#'), lines))
-        end = lines[len(start):]
-        with open(args.changelog, 'w') as f:
-            if start:
-                # appent empty line
-                if start[-1] != '':
-                    start.append('')
-            else:
-                # append 2 empty lines
-                start = 2 * ['']
-            f.write('\n'.join(start))
-            f.write('\n')
-            f.write(output)
-            f.write('\n'.join(end))
+    if args.update_copyright:
+        update_copyright(data)
     else:
-        print(output, end='')
+        output = generate_changelog(data, args.no_functions,
+                                    args.fill_up_bug_titles)
+        if args.changelog:
+            lines = open(args.changelog).read().split('\n')
+            start = list(takewhile(lambda l: not l.startswith('#'), lines))
+            end = lines[len(start):]
+            with open(args.changelog, 'w') as f:
+                if start:
+                    # appent empty line
+                    if start[-1] != '':
+                        start.append('')
+                else:
+                    # append 2 empty lines
+                    start = 2 * ['']
+                f.write('\n'.join(start))
+                f.write('\n')
+                f.write(output)
+                f.write('\n'.join(end))
+        else:
+            print(output, end='')