1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import sys
22 import os
23 import re
24 from stat import *
25
27 "get the mode of the file named fn, suitable for os.chmod() or open() calls"
28 mode = os.stat(fn)[ST_MODE]
29 cmode = S_IMODE(mode)
30 return cmode
31
33 "Replace file with newfile"
34
35 os.unlink(file)
36 os.rename(newfile,file)
37
39 "remove existing legal header, if any"
40 retval = []
41 skipping = False
42 start_pattern = re.compile(r"^(/[*]BEGIN_LEGAL)|(" + prefix + "BEGIN_LEGAL)")
43 stop_pattern = re.compile(r"^[ ]*(END_LEGAL[ ]?[*]/)|(" + prefix + "[ ]*END_LEGAL)")
44 for line in contents:
45 if start_pattern.match(line):
46 skipping = True
47 if skipping == False:
48 retval.append(line)
49 if stop_pattern.match(line):
50 skipping = False
51 return retval
52
59
61 "apply header to file using C++ comment style"
62 f = open(file,"r")
63 mode = _get_mode(file)
64 contents = f.readlines()
65 f.close()
66 trimmed_contents = _remove_existing_header(contents)
67 newfile = file + ".new"
68 o = open(newfile,"w")
69 o.write("/*BEGIN_LEGAL \n")
70 o.writelines(header)
71 o.write("END_LEGAL */\n")
72 o.writelines(trimmed_contents)
73 o.close()
74 os.chmod(newfile,mode)
75 _replace_original_with_new_file(file,newfile)
76
77
78
79
80
82 """return true if the lines are the start of shell script or
83 something that needs a mode comment at the top"""
84
85 first = ""
86 second = ""
87 if len(lines) > 0:
88 first = lines[0];
89 if len(lines) > 1:
90 second = lines[1];
91
92 if re.match("#!",first):
93
94 return True
95 if re.search("-\*-",first) or re.search("-\*-",second):
96
97 return True
98 return False
99
101 "Return a tuple of (header, body) for shell scripts, based on an input line list"
102 header = []
103 body = []
104
105 f = lines.pop(0)
106 while re.match("#",f) or re.search("-\*-",f):
107 header.append(f)
108 f = lines.pop(0)
109
110
111 body.append(f);
112 body.extend(lines);
113 return (header,body)
114
116 "Write the file header for a script"
117 o.write(prefix+"BEGIN_LEGAL\n")
118 o.writelines(lines)
119 o.write(prefix+"END_LEGAL\n")
120
122 "apply header to file using script comment style"
123 f = open(file,"r")
124 mode = _get_mode(file)
125
126 contents = f.readlines()
127 f.close()
128 trimmed_contents = _remove_existing_header(contents, prefix)
129 newfile = file + ".new"
130 o = open(newfile,"w")
131 augmented_header = _prepend_script_comment(header,prefix)
132 if _shell_script(trimmed_contents):
133 (script_header, script_body) = _split_script(trimmed_contents)
134 o.writelines(script_header)
135 _write_script_header(o, augmented_header, prefix)
136 o.writelines(script_body)
137 else:
138 _write_script_header(o,augmented_header,prefix)
139 o.writelines(trimmed_contents)
140 o.close()
141 os.chmod(newfile,mode)
142 _replace_original_with_new_file(file,newfile)
143
144
145
146
147 if __name__ == '__main__':
148 if len(sys.argv) < 4:
149 print "Usage " + sys.argv[0] + " [-s|-t] legal-header file-name [file-name...]\n"
150 sys.exit(1)
151
152 type = sys.argv[1]
153 header_file = sys.argv[2]
154 if not os.path.exists(header_file):
155 print "Could not find header file: [%s]\n" % (header_file)
156 sys.exit(1)
157
158 files_to_tag = sys.argv[3:]
159 f = open(header_file,"r")
160 header = f.readlines()
161 f.close()
162
163 sources = files_to_tag
164
165 if type == "-s":
166 for file in sources:
167 if re.search(".svn",file) == None and re.search(".new$",file) == None:
168 apply_header_to_source_file(header, file.strip())
169 elif type == "-t":
170 for file in sources:
171 if re.search(".svn",file) == None and re.search(".new$",file) == None:
172 apply_header_to_data_file(header, file.strip())
173 else:
174 print "2nd argument must be -s or -t\n"
175 sys.exit(1)
176