39 parser = ArgumentParser(
40 "Simple script to update the copyright year in the files passed on the command line"
41 )
42 parser.add_argument(
43 "--check-only",
44 action="store_true",
45 default=os.environ.get("CHECK_ONLY"),
46 help="Check for presence of copyright (do not update years)",
47 )
48 parser.add_argument(
49 "--no-check-only",
50 dest="check_only",
51 action="store_false",
52 help="Check for presence of copyright (do not update years)",
53 )
54 parser.add_argument(
55 "-Y",
56 "--year",
57 type=int,
58 default=datetime.datetime.now().year,
59 help="Year to use [default: current year]",
60 )
61 parser.add_argument("files", nargs="+", help="Files to update")
62
63 args = parser.parse_args()
64
65 exp = re.compile(r"[Cc]opyright.*(\d{4}-)?\d{4} CERN")
66
67 missing_copyright = []
68 for file in args.files:
69 with open(file, "r") as f:
70 content = f.readlines()
71
72 if not any(COPYRIGHT_SIGNATURE.search(line) for line in content):
73 missing_copyright.append(file)
74
75 elif not args.check_only:
76
77 new_content = [
78 update_year(line, args.year) if exp.search(line) else line
79 for line in content
80 ]
81
82 if new_content != content:
83 with open(file, "w") as f:
84 f.writelines(new_content)
85
86 if missing_copyright:
87 print(
88 "The following files are missing a copyright line:\n - "
89 + "\n - ".join(missing_copyright)
90 )
91 exit(1)
92
93