-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathpush_codeheader.py
More file actions
89 lines (71 loc) · 3.06 KB
/
push_codeheader.py
File metadata and controls
89 lines (71 loc) · 3.06 KB
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
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
import codecs
import json
import os
import re
import sys
from collections import namedtuple
AgsVersion = namedtuple('AgsVersion', ['version_year', 'license_link'])
def load_version(path):
with open(path, "r") as f:
j = json.load(f)
version_year = j['versionYear']
license_link = j['licenseLink']
return AgsVersion(version_year, license_link)
def read_file(path, encoding):
print(path, encoding)
with codecs.open(path, "r", encoding=encoding) as f:
try:
return f.read()
except UnicodeDecodeError:
return None
except:
raise
def write_file(path, encoding, data):
with codecs.open(path, "w", encoding=encoding) as f:
f.write(data)
def replace_group(match, group, data, replacement):
return data[:match.start(group)] + replacement + data[match.end(group):]
def replace_in_file(file_path, encoding, version):
data = read_file(file_path, encoding)
if data is None:
print('\t\t- READ FAILED')
return
m = re.search(r'// Copyright \(C\) 1999-2011 Chris Jones and \d+-(\w*) ([\S ]*)\s*//', data)
if m is None:
print('\t\t- NO PATTERN')
return # first pattern not found, no sense in searching for the rest
data = replace_group(m, 1, data, version.version_year)
data = replace_group(m, 2, data, "various contributors")
m = re.search(r'// A copy of this license can be found in the file License.txt and at\s*// (\S+://\S+.\S+)\s*//', data)
if m is not None:
data = replace_group(m, 1, data, version.license_link)
write_file(file_path, encoding, data)
print('\t\t- PROCESSED')
# -----------------------------------------------------------------------------
# For each C or C++ code file in the given dir, do the replacement
def replace_in_dir_rec(dir_path, encoding, version):
for dir, subdirs, files in os.walk(dir_path):
print('--\ndirectory = ' + dir)
for subdir in subdirs:
print('\t- subdirectory ' + subdir)
for filename in files:
_, file_ext = os.path.splitext(filename)
file_path = os.path.join(dir, filename)
# print('\t- file %s (ext: %s) (full path: %s)' % (filename, file_ext, file_path))
if file_ext != '.h' and file_ext != '.c' and file_ext != '.cpp' and file_ext != '.inl':
continue
replace_in_file(file_path, encoding, version)
def main():
# load key/value data, we will use it as a replacement reference
version = load_version("../version.json")
# -----------------------------------------------------------------------------
# Do the replacement in all the code dirs, where applicable
replace_in_dir_rec("../Common", "utf-8", version)
replace_in_dir_rec("../Compiler", "utf-8", version)
replace_in_dir_rec("../Editor/AGS.Native", "utf-8", version)
replace_in_dir_rec("../Engine", "utf-8", version)
replace_in_dir_rec("../Plugins", "utf-8", version)
replace_in_dir_rec("../Tools", "utf-8", version)
if __name__ == "__main__":
main()