3 # This uses the output from "support/git-set-file-times --list" to discern
4 # the last-modified year of each *.c & *.h file and updates the copyright
5 # year if it isn't set right.
7 import sys, os, re, argparse, subprocess
8 from datetime import datetime
10 MAINTAINER_NAME = 'Wayne Davison'
11 MAINTAINER_SUF = ' ' + MAINTAINER_NAME + "\n"
16 proc = subprocess.Popen('support/git-set-file-times --list'.split(), stdout=subprocess.PIPE, encoding='utf-8')
17 for line in proc.stdout:
18 m = re.match(r'^\S\s+(?P<year>\d\d\d\d)\S+\s+\S+\s+(?P<fn>.+)', line)
20 print("Failed to parse line from git-set-file-times:", line)
22 m = argparse.Namespace(**m.groupdict())
23 if m.year > latest_year:
25 if m.fn.startswith('zlib/') or m.fn.startswith('popt/'):
27 if re.search(r'\.(c|h|sh|test)$', m.fn):
28 maybe_edit_copyright_year(m.fn, m.year)
32 with open(fn, 'r', encoding='utf-8') as fh:
35 txt = f'#define LATEST_YEAR "{latest_year}"\n'
37 print(f"Updating {fn} with year {latest_year}")
38 with open(fn, 'w', encoding='utf-8') as fh:
42 def maybe_edit_copyright_year(fn, year):
46 with open(fn, 'r', encoding='utf-8') as fh:
47 for lineno, line in enumerate(fh):
48 opening_lines.append(line)
49 if lineno > 3 and not re.search(r'\S', line):
51 m = re.match(r'^(?P<pre>.*Copyright\s+\S+\s+)(?P<year>\d\d\d\d(?:-\d\d\d\d)?(,\s+\d\d\d\d)*)(?P<suf>.+)', line)
54 copyright_line = argparse.Namespace(**m.groupdict())
55 copyright_line.lineno = len(opening_lines)
56 copyright_line.is_maintainer_line = MAINTAINER_NAME in copyright_line.suf
57 copyright_line.txt = line
58 if copyright_line.is_maintainer_line:
61 if not copyright_line:
64 if copyright_line.is_maintainer_line:
65 cyears = copyright_line.year.split('-')
69 cyears = [ cyears[0], year ]
70 txt = copyright_line.pre + '-'.join(cyears) + MAINTAINER_SUF
71 if txt == copyright_line.txt:
73 opening_lines[copyright_line.lineno - 1] = txt
75 if fn.startswith('lib/') or fn.startswith('testsuite/'):
77 txt = copyright_line.pre + year + MAINTAINER_SUF
78 opening_lines[copyright_line.lineno - 1] += txt
80 remaining_txt = fh.read()
82 print(f"Updating {fn} with year {year}")
84 with open(fn, 'w', encoding='utf-8') as fh:
85 fh.write(''.join(opening_lines))
86 fh.write(remaining_txt)
89 if __name__ == '__main__':
90 parser = argparse.ArgumentParser(description="Grab the year of last mod for our c & h files and make sure the Copyright comment is up-to-date.")
91 args = parser.parse_args()