-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchnotice.py
58 lines (52 loc) · 1.84 KB
/
switchnotice.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
53
54
55
56
57
58
#!/usr/bin/env python
# (c) Jan Dlabal, 2011
# Switches notice at the beginning of a file in all *.[ch] and *.[ch]pp files
# Assumes the notice is a block of lines starting with /*
# Assumes the notice is ended when the first line not starting with /* is encountered
#
# The program first reads the file, then drops all lines which start with /* until a line not satisfying this is encountered
# Then it writes the new notice into the file, and all original lines starting with the first not beginning with /*
#
# Make sure you understand the above to avoid surprises.
import os
from sys import argv
class PyWalk():
def __init__(self,d,action,daction=None):
self.d = d
self.action = action
self.daction = daction
def walk(self,path=None):
if not path:
path = self.d
if os.path.isdir(path):
l = os.listdir(path)
if path != self.d and self.daction:
self.daction(path)
for el in l:
self.walk(path+"/"+el)
else:
self.action(path)
def replacenotice(file):
global newnotice
if file.endswith(".c") or file.endswith(".h") or file.endswith(".cpp") or file.endswith(".hpp"):
with open(file,"r") as f:
lines = f.readlines()
with open(file,"w") as f:
f.write(newnotice)
if newnotice[-1]!='\n':
f.write("\n")
pastnotice = False
for x in lines:
if x.startswith("/*") and not pastnotice:
pass
else:
pastnotice = True
f.write(x)
newnotice = ""
if len(argv)!=3:
print "Usage : "+argv[0]+" NOTICEFILE PATH"
else:
with open(argv[1],"r") as f:
newnotice = "".join(f.readlines())
replacer = PyWalk(argv[2],replacenotice)
replacer.walk()