-
Notifications
You must be signed in to change notification settings - Fork 9
/
bump_copyright
executable file
·80 lines (60 loc) · 1.95 KB
/
bump_copyright
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
"""\
Update LICENSE (copyright year) and copy it to file headers.
"""
import sys
import os
import re
import argparse
from datetime import datetime
DIR = os.path.dirname(os.path.abspath(__file__))
LICENSE = os.path.join(DIR, "LICENSE")
HDR_START = "#%L"
HDR_END = "#L%"
CURRENT_YEAR = datetime.now().strftime("%Y")
def update_license(year):
with open(LICENSE) as f:
content = f.read()
new_content = re.sub(r"-(\d{4})", "-%s" % year, content)
with open(LICENSE, "w") as f:
f.write(new_content)
return new_content
def add_comments(txt):
new_lines = []
for line in txt.strip().splitlines():
line = line.strip()
new_lines.append("# {}".format(line) if line else "#")
return "\n".join(new_lines) + "\n"
def update_hdr(new_txt, path):
with open(path) as f:
content = f.read()
start = content.find(HDR_START)
end = content.find(HDR_END)
if start > 0 and end > 0:
new_txt = add_comments(new_txt)
new_content = "%s%s\n%s# %s" % (
content[:start], HDR_START, new_txt, content[end:]
)
with open(path, "w") as f:
f.write(new_content)
def update_headers(new_txt):
for d, subdirs, fnames in os.walk(DIR):
for fn in fnames:
if fn.endswith(".py"):
update_hdr(new_txt, os.path.join(d, fn))
def year(s):
if not re.match(r"\d{4}", s):
raise argparse.ArgumentTypeError("year must be in YYYY format")
return s
def make_parser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-y", "--year", metavar="YYYY", type=year,
default=CURRENT_YEAR, help="end copyright year")
return parser
def main(argv):
parser = make_parser()
args = parser.parse_args(argv[1:])
new_txt = update_license(args.year)
update_headers(new_txt)
if __name__ == "__main__":
main(sys.argv)