forked from cogtool/cogtool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-comments
executable file
·67 lines (58 loc) · 2.25 KB
/
update-comments
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
#! /usr/bin/env python
# Updates the copyright notices in CogTool source files. Rewrites all matching source
# files so that their initial copyright notices are as in the file supplied as the
# "notice" command line argument, which defaults to copyright.txt
import os
import os.path
import re
from optparse import OptionParser
patterns = {
".java": (re.compile("/\\*.*[\r\n]+.*CogTool Copyright Notice", re.IGNORECASE), # hasCopyRight
re.compile("/\\*.*?\\*/", re.DOTALL), # wholeNotice
"/*******************************************************************************\n", # start
" ******************************************************************************/", # end
" * ") # prefix
}
def update(dir):
for f in os.listdir(dir):
if (f[:1] == "."):
continue
p = os.path.join(dir, f)
if (os.path.isdir(p)):
update(p)
else:
ext = os.path.splitext(f)[1]
if not ext in patterns:
continue
hasCopyRight, wholeNotice, start, end, prefix = patterns[ext]
strm = open(p, "r")
try:
contents = strm.read()
finally:
strm.close()
if not re.match(hasCopyRight, contents):
continue
if (not options.quiet):
print p
out = open(p, "w")
try:
out.write(start)
for line in noticeContents:
out.write(prefix)
out.write(line)
out.write(end)
out.write(re.sub(wholeNotice, "", contents, 1))
finally:
out.close()
parser = OptionParser()
parser.add_option("-n", "--notice", dest="notice", help="read copyright NOTICE from file", default="copyright.txt")
parser.add_option("-r", "--root", dest="root", help="update copyrights under this ROOT directory", default=".")
parser.add_option("-q", "--quiet", dest="quiet", help="be quiet and don't list files modified", default=False)
(options, args) = parser.parse_args()
strm = open(os.path.join(options.root, options.notice), "r")
try:
noticeContents = strm.readlines()
finally:
strm.close()
update(os.path.join(options.root, "java"))
update(os.path.join(options.root, "mac-support"))