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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#!/usr/bin/env python
# -----------------------------------------------------------------------------
# add_version_headers
#
# Add version header
#
# Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
# See file LICENSE for more details
# -----------------------------------------------------------------------------
""" @file Adds the version headers to every file.
## Add Version Headers
This file adds the version headers to every file in the directory
### Improvements
Add the ability to input command line arguments.
"""
import os
import re
import sys
header = """/* ---------------------------------------------------------------\
-------------
* {0}
*
* Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
* MIT License, see LICENSE file for more details.
* ----------------------------------------------------------------------------
*/
"""
lic_regex = re.compile(
"\/\* -+\n \* ([a-z]+\.(?:cpp|h)|\{0\})\n(?: \*[ a-zA-Z0-9_.,\-()<>@]*\n)+ \* -+\n \*/", re.MULTILINE)
class HeaderUpdate(object):
"""Updates the header in all the source and header files in the code"""
def __init__(self, **kwargs):
self.match_re = ".*[.]cpp$|.*[.]h$"
self.exclude_re = "^$"
self.exclude_build = True
self.exclude_dir = ".*build.*"
self.match_dir = ""
self.starting_dir = os.getcwd()
for key, value in kwargs.items():
setattr(self, key, value)
def writeHeader(self):
def writeFileOperation(filePath):
with open(filePath, "r+") as f:
src = f.read()
f.seek(0)
if not lic_regex.match(src):
print("Adding {}: ".format(
os.path.basename(filePath)), end="")
src = header.format(os.path.basename(filePath)) + src
print("done")
f.write(src)
f.truncate()
self._traverseDir(self.starting_dir, writeFileOperation)
def updateHeader(self):
def updateFileOperation(filePath):
with open(filePath, "r+") as f:
src = f.read()
f.seek(0)
if lic_regex.match(src):
print("Updating {}: ".format(
os.path.basename(filePath)), end="")
src = lic_regex.sub(header.format(
os.path.basename(filePath)), src)
print("done")
else:
print("Adding {}: ".format(
os.path.basename(filePath)), end="")
src = header.format(os.path.basename(filePath)) + src
print("done")
f.write(src)
f.truncate()
self._traverseDir(self.starting_dir, updateFileOperation)
def removeHeader(self):
...
def _traverseDir(self, path, fileOperation):
for subdir, dirs, files in os.walk(os.getcwd()):
if (re.match(self.match_dir, subdir)) and (
not re.match(self.exclude_dir, subdir)):
for fileName in files:
if (re.match(self.match_re, fileName)) and (
not re.match(self.exclude_re, fileName)):
fileOperation(os.path.join(subdir, fileName))
def main(argv):
update = HeaderUpdate(exclude_re="^picopng\.cpp",
match_dir=".*yage|.*tests",
exclude_dir=".*lib.*|.*build.*")
update.updateHeader()
if __name__ == "__main__":
main(sys.argv)
|