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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#!/usr/bin/env python
from optparse import OptionParser
import sys
import os
import os.path
import re
import subprocess
parser = OptionParser(usage="%prog [-v] DIR/FILE [...]", version="%prog $Id: $")
parser.add_option("-n", "--dry-run",
action="store_false", dest="act", default=True,
help="do not run any program")
parser.add_option("-f", "--force",
action="store_true", dest="force", default=False,
help="regenerate up-to-date files")
parser.add_option("-s", "--summary", dest="summary",
help="summarise failures and processed files at the end of the run (one of failures (default), files, both, no)",
choices=("no","files","failures","both"), default="failures")
parser.add_option("-v", "--verbose",
action="count", dest="verbosity", default=1,
help="give reasons for actions, may be repeated")
parser.add_option("-q", "--quiet",
action="store_false", dest="progress", default=True,
help="suppress progress reports")
parser.add_option("-i", "--ignore",
action="store", dest="ignorepattern", default=".*vorlage.*|\.\#.*",
help="regular expression of filenames to ignore")
parser.add_option("-p", "--required-pattern",
action="store", dest="requiredpattern", default='^\\\\documentclass|%%% TeX-master: t',
help="regular expression that must match the file content")
(options, args) = parser.parse_args()
interactionmode = "batchmode"
if options.verbosity>2:
interactionmode = "nonstopmode"
re_ignore = None
if options.ignorepattern != "":
re_ignore = re.compile(options.ignorepattern)
re_required = re.compile(options.requiredpattern)
re_texfile = re.compile(".*\.tex$", re.I)
errors = []
procfiles = []
errstr = {False: "ERROR", True: "WARNING"}
def error(file, desc, warning=False):
msg = "%s: %s: %s"%(errstr[warning], os.path.normpath(file), desc)
if options.progress:
print msg
errors.append(msg)
# iterate over all .tex files that are not ignored by -i or -p
def alltexfiles(args):
for a in args:
if os.path.isfile(a):
if re_texfile.match(a):
yield os.path.split(a)
else:
error(a, "is no tex file; skipped")
elif os.path.isdir(a):
for dir, subdirs, files in os.walk(a):
for name in files:
if re_texfile.match(name):
if re_ignore.match(name):
if options.verbosity > 1:
print "%s: skipped because of ignored name"%os.path.normpath(os.path.join(dir,name))
continue
for m in texgrep(re_required, dir, name):
yield (dir, name)
break
else:
if options.verbosity > 1:
print "%s: skipped because no main latex file"%os.path.normpath(os.path.join(dir,name))
def texgrep(matcher, dirname, filename, recurse=False):
try:
f = open(os.path.join(dirname,filename))
content = f.read()
for m in matcher.finditer(content):
yield m
if recurse:
for dep in dependencies(dirname, filename, texonly=True):
for m in texgrep(matcher, "", dep, recurse=False):
yield m
except IOError, (errno, strerror):
error(os.path.join(dirname, filename), "could not be read: %s"%strerror)
except StopIteration:
f.close()
else:
f.close()
RECURSIONDEPTH=10
re_inputinclude = re.compile('\\\\(input|include|@input)\\{([^}]*)\\}')
re_usepackage = re.compile('\\\\usepackage(\\[.*?\\])?\\{([^}]*)\\}')
re_graphics = re.compile('\\\\includegraphics(\\[.*?\\])?\\{([^}]*)\\}')
graphics_ext = ["", ".pdf", ".eps", ".png", ".jpg"]
re_bibliography = re.compile('\\\\bibliography\\{([^}]*)\\}')
re_commawhitespace = re.compile('\\s*,\\s*')
def dependencies(dirname, filename, texonly=False, recurse=RECURSIONDEPTH):
if not recurse:
return
try:
f = open(os.path.join(dirname,filename))
content = f.read()
for m in re_inputinclude.finditer(content):
texs = m.group(2)
for tex in re_commawhitespace.split(texs):
if not os.path.isfile(os.path.join(dirname,tex)):
tex+=".tex"
if os.path.isfile(os.path.join(dirname,tex)):
yield os.path.normpath(os.path.join(dirname,tex))
for m in dependencies(dirname, tex, texonly, recurse-1):
yield m
for m in re_usepackage.finditer(content):
stys = m.group(2)
for sty in re_commawhitespace.split(stys):
if not os.path.isfile(os.path.join(dirname,sty)):
sty+=".sty"
if os.path.isfile(os.path.join(dirname,sty)):
yield os.path.normpath(os.path.join(dirname,sty))
for m in dependencies(dirname, sty, texonly, recurse-1):
yield m
if not texonly:
for m in re_graphics.finditer(content):
names = m.group(2)
for name in re_commawhitespace.split(names):
for ext in graphics_ext:
if os.path.isfile(os.path.join(dirname,name+ext)):
yield os.path.normpath(os.path.join(dirname,name+ext))
for m in re_bibliography.finditer(content):
bibs = m.group(1)
for bib in re_commawhitespace.split(bibs):
if bib[-4:] != ".bib":
bib += ".bib"
if bib[-8:] != "-blx.bib":
yield os.path.normpath(os.path.join(dirname,os.path.expanduser(bib)))
except IOError, (errno, strerror):
error(os.path.join(dirname, filename), "could not be read: %s"%strerror)
except StopIteration:
f.close()
else:
f.close()
def bibfiles(dirname, texname):
for m in texgrep(re_bibliography, dirname, texname, recurse=True):
bibs = m.group(1)
for bib in re_commawhitespace.split(bibs):
if bib[-4:] != ".bib":
bib += ".bib"
if bib[-8:] != "-blx.bib":
yield os.path.normpath(os.path.join(dirname,os.path.expanduser(bib)))
def outdatedtexfiles(args):
for dirname, texname in alltexfiles(args):
#strip .tex extension
jobname=texname[:-4]
pdfname=jobname+".pdf"
reason = ""
# check if an update is needed:
if options.force and not options.verbosity:
yield (dirname, texname, reason)
elif not os.path.isfile(os.path.join(dirname,pdfname)):
if options.verbosity:
reason = " (because .pdf does not exist)"
yield (dirname, texname, reason)
else:
pdftime = os.path.getmtime(os.path.join(dirname, pdfname))
if pdftime < os.path.getmtime(os.path.join(dirname, texname)):
if options.verbosity:
reason = " (because .tex is newer than .pdf)"
yield (dirname, texname, reason)
else:
for f in dependencies(dirname, texname):
if pdftime < os.path.getmtime(f):
if options.verbosity:
reason = " (because %s is newer than .pdf)"%f
yield (dirname, texname, reason)
break
else:
r = haderrors(dirname, jobname)
if r:
if options.verbosity:
reason = r
yield (dirname, texname, reason)
elif options.force:
reason = " (because of --force)"
yield (dirname, texname, reason)
elif options.verbosity > 1:
print "%s: skipped because up-to-date"%os.path.normpath(os.path.join(dirname, texname))
re_nopdftex = re.compile('\\\\usepackage(\\[.*?\\])?\\{[^}]*pstricks[^}]*\\}')
def detecttextype(dirname, texname):
for m in texgrep(re_nopdftex, dirname, texname, recurse=True):
return "latex"
return "pdflatex"
MAXRUNS=5
def processtexfiles(args):
for dirname, texname, reason in outdatedtexfiles(args):
procfiles.append(os.path.normpath(os.path.join(dirname,texname)))
if options.progress:
print "processing %s%s..."%(os.path.normpath(os.path.join(dirname,texname)),reason)
# TODO: add support for pstricks/plain latex
tex = detecttextype(dirname, texname)
run([tex, "-interaction", interactionmode, texname], dirname)
#strip .tex extension
jobname=texname[:-4]
# run bibtex if any bibfile changed:
for bib in bibfiles(dirname, texname):
bbl = os.path.normpath(os.path.join(dirname, jobname+".bbl"))
if options.force or not os.path.isfile(bbl) or os.path.getmtime(bbl) < os.path.getmtime(bib):
reason = ""
if options.verbosity:
if not os.path.isfile(bbl):
reason = " (because .bbl does not exist yet)"
elif os.path.getmtime(bbl) < os.path.getmtime(bib):
reason = " (because %s is newer than .bbl)"%bib
else:
reason = " (because of --force)"
run(["bibtex8", "--wolfgang", jobname], dirname, reason=reason)
run([tex, "-interaction", interactionmode, texname], dirname, reason=" (because of updated .bbl)")
break
# check for undefined references and run requests
numrun=0
reqs=options.act
while reqs:
reqs = requests(os.path.join(dirname, jobname+".log"))
pri = reqs.values()
pri.sort(reverse=True)
for p in pri:
for req in reqs.keys():
rp = reqs[req]
if rp == p:
reason = ""
if options.verbosity:
reason = " (because of request in .aux file, priority %d)"%rp
if req == "latex":
run([tex, "-interaction", interactionmode, texname], dirname, reason=reason)
elif req == "bibtex":
run(["bibtex8", "--wolfgang", jobname], dirname, reason=reason)
else:
error(os.path.join(dirname,jobname+".aux"), "unsupported request: %s"%req)
numrun+=1
if numrun==MAXRUNS:
error(os.path.join(dirname, texname), "does not stabilise after %i runs"%MAXRUNS)
break
# update index if it exists - no way of knowing if it was updated
idx = jobname + ".idx"
if os.path.isfile(os.path.join(dirname, idx)):
run(["makeindex", jobname], dirname, reason=" (because .idx file might have changed)")
run([tex, "-interaction", interactionmode, texname], dirname, reason=" (because .ind file might have changed)")
if tex == "latex":
run(["dvips", jobname+".dvi"], dirname)
run(["ps2pdf", jobname+".ps"], dirname)
re_logmatcher = re.compile("^LaTeX Warning: There were undefined references\.$|^LaTeX Warning: Label\(s\) may have changed\. Rerun to get cross-references right\.$|^REQ:(\d+):(\w+):")
def requests(logpath):
reqs = {}
def addrequest(name, priority):
if reqs.has_key(name):
reqs[name] = max(reqs[name], priority)
else:
reqs[name] = priority
try:
log = open(logpath)
for line in log:
m = re_logmatcher.match(line)
if m:
if m.group() == 'LaTeX Warning: There were undefined references.':
addrequest("latex",0)
elif m.group() == 'LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.':
addrequest("latex",0)
elif m.group(1)!=None:
addrequest(m.group(2), int(m.group(1)))
# TODO: add support for multiple bibliographies, see biblatex.pdf Sec. 2.4.4
except IOError, (errno, strerror):
error(logpath, "could not be read: %s"%strerror)
else:
log.close()
return reqs
re_error = re.compile('^! ')
def haderrors(dirname, jobname):
logpath = os.path.join(dirname, jobname+".log")
if not os.path.isfile(logpath):
return None
try:
f = open(logpath)
for line in f:
if re_error.match(line):
return " (because of error in .log file)"
if re_logmatcher.match(line):
return " (because of request in .log file)"
except IOError, (errno, strerror):
error(logpath, "could not be read: %s"%strerror)
else:
f.close()
return False
def run(arglist, dirname, reason=""):
if options.verbosity>2:
# use default
output=None
else:
output=file("/dev/null")
if options.progress:
print " running %s%s..."%(" ".join(arglist),reason)
if options.act:
ret = subprocess.call(arglist, stdout=output, stderr=output, cwd=dirname)
if ret:
error(dirname, "failed command: %s"%(" ".join(arglist)))
# main program:
try:
if args:
processtexfiles(args)
else:
processtexfiles(["."])
if options.summary in [ "files", "both" ] :
if procfiles:
print "Processed the following files:"
for f in procfiles:
print " %s"%f
else:
print "Processed no files."
if options.summary in [ "failures", "both" ] :
if errors:
print "The following problems occured:"
for f in errors:
print " %s"%f
if errors:
sys.exit(1)
except KeyboardInterrupt:
sys.exit(2)
|