-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate.py
52 lines (45 loc) · 1.53 KB
/
update.py
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
#!/usr/bin/env python3
import os
import re
import sys
import fileinput
import argparse
from datetime import datetime
parser = argparse.ArgumentParser(
usage='%(prog)s [OPTION]',
description='Updates license file(s) to the current year.'
)
parser.add_argument('-f', '--files', help='Multiline string of file paths', required=True)
parser.add_argument('-e', '--exclude', help='Multiline string of words in author to exclude')
parser.add_argument('-r', '--no-range', help='Only keep last year instead of range of years')
args = parser.parse_args()
paths = []
for line in iter(args.files.replace('\\n', '\n').splitlines()):
if line != '':
if not os.path.isfile(line):
print(f'file not found at path "{line}"')
sys.exit(1)
elif line in paths:
print(f'duplicate path "{line}"')
sys.exit(1)
else:
paths.append(line)
excludes = []
for line in iter((args.exclude or '').replace('\\n', '\n').splitlines()):
if line != '':
if line in excludes:
print(f'duplicate exclude "{line}"')
sys.exit(1)
else:
excludes.append(line.lower())
PATTERN = re.compile(r'^([Cc]opyright.+) ([0-9-]{4,9}) (.+)$')
NEW_YEAR = datetime.now().year
for line in fileinput.input(files=paths, inplace=True):
match = PATTERN.match(line)
if match and match.group(2) != str(NEW_YEAR) and not any(w in match.group(3).lower() for w in excludes):
if args.no_range:
sys.stdout.write(f'{match.group(1)} {NEW_YEAR} {match.group(3)}\n')
else:
sys.stdout.write(f'{match.group(1)} {match.group(2)[:4]}-{NEW_YEAR} {match.group(3)}\n')
else:
sys.stdout.write(line)