This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
process_m4.py
executable file
·314 lines (258 loc) · 9.72 KB
/
process_m4.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/python
import os, sys, re, shutil, string, getopt
M4PATH = "/usr/bin/m4"
CPPATH = "/bin/cp"
# These three can be overriden by commandline arguments
# Relative paths are taken from cwd of program caller, not program
DEFAULT_INPUTPATH = "../src"
DEFAULT_TEMPPATH = "../m4-tmp"
DEFAULT_OUTPUTPATH = "../html"
DEFAULT_STDLIBPATH = "../m4/worksp.lib.m4"
verbose_mode = False
changed_header_count = 0
inserted_header_count = 0
processed_count = 0
nonprocessed_count = 0
# with os.path.walk
def print_filenames(arg, dirname, names):
for name in names:
if name[-5:] == ".html":
print name
# with os.path.walk
def delete_CVS_dir(arg, dirname, names):
if dirname[-3:] == "CVS":
shutil.rmtree(dirname)
# with os.path.walk
def add_m4_include(arg, dirname, names):
global changed_header_count
global inserted_header_count
#print "dir: " + dirname
if dirname[-3:] == "CVS":
return
if not arg:
raise Exception("m4 lib path not configured, see example in main()")
regex = re.compile("m4_include")
for name in names:
if name[-5:] != ".html":
continue
path = os.path.join(dirname, name)
try:
f = open(path)
oldlines = f.readlines()
f.close()
if len(oldlines) < 1:
print "found empty file '%s', not processing it" % path
else:
mobj = regex.search(oldlines[0])
newlines = None
# if an m4 include statement is present, change it if necc.
if mobj:
shouldbe = "m4_include(%s)\n" % arg
newlines = oldlines
if newlines[0] != shouldbe:
newlines[0] = shouldbe
changed_header_count += 1
if verbose_mode:
print "Changing m4 header: %s" % path
else:
newlines = None
# if not present, insert it
else:
newlines = ["m4_include(%s)\n" % arg]
newlines.extend(oldlines)
if verbose_mode:
print "Inserting m4 header: %s" % path
inserted_header_count += 1
if newlines:
f = open(path, 'w')
for line in newlines:
f.write(line)
finally:
f.close()
# with os.path.walk
def process_m4(args, dirname, names):
"""So much nicer than a Makefile"""
global processed_count
global nonprocessed_count
if len(args) < 2:
raise Exception("in or out path not configured, see example in main()")
if not args[0] or not args[1]:
raise Exception("in or out path not configured, see example in main()")
inputdir = args[0]
outputdir = args[1]
#print "dir: " + dirname
if dirname[-3:] == "CVS":
return
regex = re.compile("(.*)(%s)(.*)" % inputdir)
mobj = regex.search(dirname)
if mobj:
outputdir = outputdir + mobj.group(3)
else:
raise Exception("no mobj?")
if not os.path.exists(outputdir):
os.mkdir(outputdir)
if verbose_mode:
print "Created directory %s" % outputdir
for name in names:
path = os.path.join(dirname, name)
outpath = os.path.join(outputdir, name)
if os.path.isdir(path):
continue
if name[-5:] != ".html":
cmd = "%s %s %s" % (CPPATH, path, outpath)
ret = os.system(cmd)
if ret:
print "cmd failed: %s" % cmd
else:
nonprocessed_count += 1
if verbose_mode:
print "Added %s" % outpath
else:
cmd = "%s -P <%s >%s" % (M4PATH, path, outpath)
ret = os.system(cmd)
if ret:
print "cmd failed: %s" % cmd
else:
processed_count += 1
if verbose_mode:
print "Processed %s" % outpath
class UsageError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
def absolute(path):
x = string.strip(path)
return os.path.realpath(x)
def help():
print "Usage: %s [options]\n" % sys.argv[0]
print " -i,--inputdir $path Overrides doc source directory"
print " Default: %s" % absolute(DEFAULT_INPUTPATH)
print "\n -o,--outputdir $path Overrides doc output directory"
print " Default: %s" % absolute(DEFAULT_OUTPUTPATH)
print "\n -t,--tempdir $path Overrides doc temp directory"
print " Default: %s" % absolute(DEFAULT_TEMPPATH)
print "\n -l,--stdlibpath $path Overrides m4 library path"
print " Default: %s" % absolute(DEFAULT_STDLIBPATH)
print "\n -v,--verbose Verbose mode"
def main(argv=None):
if argv is None:
argv = sys.argv
inputdir = None
outputdir = None
stdlibpath = None
tempdir = None
global verbose_mode
try:
## Collect ##
#if len(argv) < 2:
# raise UsageError("arguments?")
try:
opts, args = getopt.getopt(argv[1:],
"hi:o:l:t:v",
["help", "inputdir=", "outputdir=",
"stdlibpath=", "tempdir=", "verbose"])
except getopt.error, msg:
raise UsageError(msg)
## Assign ##
for o,a in opts:
if o in ("-h", "--help"):
help()
return 0
elif o in ("-i", "--inputdir"):
inputdir = a
elif o in ("-o", "--outputdir"):
outputdir = a
elif o in ("-l", "--stdlibpath"):
stdlibpath = a
elif o in ("-t", "--tempdir"):
tempdir = a
elif o in ("-v", "--verbose"):
verbose_mode = True
if inputdir:
inputdir = absolute(inputdir)
else:
inputdir = absolute(DEFAULT_INPUTPATH)
if outputdir:
outputdir = absolute(outputdir)
else:
outputdir = absolute(DEFAULT_OUTPUTPATH)
if stdlibpath:
stdlibpath = absolute(stdlibpath)
else:
stdlibpath = absolute(DEFAULT_STDLIBPATH)
for i in (inputdir, outputdir, stdlibpath):
if not i:
raise Exception("don't erase defaults")
if not os.path.exists(i):
raise Exception("'%s' does not exist" % i)
x = os.access(i, os.R_OK)
if not x:
raise Exception("'%s' exists on the filesystem but is not readable" % i)
x = os.access(inputdir, os.X_OK | os.R_OK)
if not x:
raise Exception("'%s' exists on the filesystem but is not rx-able" % inputdir)
x = os.access(outputdir, os.W_OK | os.X_OK | os.R_OK)
if not x:
raise Exception("'%s' exists on the filesystem but is not rwx-able" % outputdir)
if tempdir:
tempdir = absolute(tempdir)
else:
tempdir = absolute(DEFAULT_TEMPPATH)
if os.path.exists(tempdir):
raise Exception("'%s' exists already?" % tempdir)
print "Input directory: %s" % inputdir
print "Temp directory: %s" % tempdir
print "Output directory: %s" % outputdir
print "m4 macro library: %s" % stdlibpath
for i in (M4PATH, CPPATH):
if not os.path.exists(i):
raise Exception("'%s' does not exist" % i)
x = os.access(i, os.X_OK)
if not x:
raise Exception("'%s' exists on the filesystem but is not executable" % i)
print "\n*** Copying source to temp directory... ",
sys.stdout.flush()
shutil.copytree(inputdir, tempdir)
print "done."
print "*** Removing CVS dirs from temp directory... ",
sys.stdout.flush()
os.path.walk(tempdir, delete_CVS_dir, None)
print "done."
# must be run from scripts subdir for now
# cvs.globus.org has Python version < 2.3 which is when
# the "os.walk" function was introduced, so instead using
# os.path.walk
if verbose_mode:
print "\n\n"
print "*** Adding/changing m4 library references"
os.path.walk(tempdir, add_m4_include, stdlibpath)
if verbose_mode:
print "\n\n"
if verbose_mode:
print "\n\n"
print "*** Processing m4"
os.path.walk(tempdir, process_m4, (tempdir,outputdir))
if verbose_mode:
print "\n\n"
shutil.rmtree(tempdir)
print "*** Removed temp directory"
print "*** Summary:"
print " New m4 headers : %d" % inserted_header_count
print " Altered m4 headers : %d" % changed_header_count
print " m4 processed files : %d" % processed_count
print " Other files : %d" % nonprocessed_count
except UsageError,err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2
except:
exception_type = sys.exc_type
try:
exceptname = exception_type.__name__
except AttributeError:
exceptname = exception_type
print >>sys.stderr, "ERROR: %s: %s" % (str(exceptname), str(sys.exc_value))
return 1
if __name__ == '__main__':
sys.exit(main())