generated from ivoa-std/doc-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_generated.py
65 lines (51 loc) · 1.59 KB
/
update_generated.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
#!/usr/bin/env python
# Update all generated sections in RegTAP.tex
#
# Generarated sections are between % GENERATED: <command>
# and % /GENERATED. They are supposed to contain the output of
# <command>. <command> get shell-expanded, but since it gets executed
# anyway, it's not even worth doing shell injection.
#
# When this script finishes, it either has updated all sections or
# stopped with an error message of a failed command, in which case the
# original file is unchanged.
import re
import subprocess
import sys
S_NAME = "RegTAP.tex" # and don't you dare make that generic...
def ExecError(Exception):
def __init__(self, command, stderr):
Exception.__init__("Failed command %s"%repr(command))
self.command, self.stderr = command, stderr
def processOne(matchObj):
command = matchObj.group("command")
print "Executing %s"%command
f = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, bufsize=-1)
stdout, stderr = f.communicate()
if f.returncode!=0:
raise ExecError(command, stderr)
return ("%% GENERATED: %s\n"%(command.strip())
+stdout
+"\n% /GENERATED")
def processAll(content):
return re.sub(r"(?sm)^%\s+GENERATED:\s+(?P<command>.*?)$"
".*?"
r"%\s+/GENERATED",
processOne,
content)
def main():
with open(S_NAME) as f:
content = f.read()
try:
content = processAll(content)
except ExecError, ex:
sys.stderr.write("Command %s failed. Message below. Aborting.\n"%
ex.command)
sys.stderr.write(ex.stderr+"\n")
sys.exit(1)
with open(S_NAME, "w") as f:
f.write(content)
if __name__=="__main__":
main()