rev |
line source |
bos@67
|
1 #!/usr/bin/env python
|
bos@4
|
2 #
|
bos@4
|
3 # This program takes something that resembles a shell script and runs
|
bos@4
|
4 # it, spitting input (commands from the script) and output into text
|
bos@4
|
5 # files, for use in examples.
|
bos@3
|
6
|
bos@3
|
7 import cStringIO
|
bos@73
|
8 import errno
|
bos@78
|
9 import getopt
|
bos@3
|
10 import os
|
bos@3
|
11 import pty
|
bos@3
|
12 import re
|
bos@4
|
13 import shutil
|
bos@6
|
14 import signal
|
bos@36
|
15 import stat
|
bos@3
|
16 import sys
|
bos@4
|
17 import tempfile
|
bos@4
|
18 import time
|
bos@3
|
19
|
bos@77
|
20 tex_subs = {
|
bos@77
|
21 '\\': '\\textbackslash{}',
|
bos@77
|
22 '{': '\\{',
|
bos@77
|
23 '}': '\\}',
|
bos@77
|
24 }
|
bos@77
|
25
|
bos@77
|
26 def gensubs(s):
|
bos@77
|
27 start = 0
|
bos@77
|
28 for i, c in enumerate(s):
|
bos@77
|
29 sub = tex_subs.get(c)
|
bos@77
|
30 if sub:
|
bos@77
|
31 yield s[start:i]
|
bos@77
|
32 start = i + 1
|
bos@77
|
33 yield sub
|
bos@77
|
34 yield s[start:]
|
bos@77
|
35
|
bos@4
|
36 def tex_escape(s):
|
bos@77
|
37 return ''.join(gensubs(s))
|
bos@4
|
38
|
bos@3
|
39 class example:
|
bos@70
|
40 shell = '/usr/bin/env bash'
|
bos@73
|
41 prompt = '__run_example_prompt__ '
|
bos@71
|
42 pi_re = re.compile(r'#\$\s*(name):\s*(.*)$')
|
bos@4
|
43
|
bos@78
|
44 def __init__(self, name, verbose):
|
bos@3
|
45 self.name = name
|
bos@78
|
46 self.verbose = verbose
|
bos@3
|
47
|
bos@3
|
48 def parse(self):
|
bos@4
|
49 '''yield each hunk of input from the file.'''
|
bos@3
|
50 fp = open(self.name)
|
bos@3
|
51 cfp = cStringIO.StringIO()
|
bos@3
|
52 for line in fp:
|
bos@3
|
53 cfp.write(line)
|
bos@3
|
54 if not line.rstrip().endswith('\\'):
|
bos@3
|
55 yield cfp.getvalue()
|
bos@3
|
56 cfp.seek(0)
|
bos@3
|
57 cfp.truncate()
|
bos@3
|
58
|
bos@3
|
59 def status(self, s):
|
bos@3
|
60 sys.stdout.write(s)
|
bos@3
|
61 if not s.endswith('\n'):
|
bos@3
|
62 sys.stdout.flush()
|
bos@3
|
63
|
bos@6
|
64 def send(self, s):
|
bos@78
|
65 if self.verbose:
|
bos@78
|
66 print >> sys.stderr, '>', self.debugrepr(s)
|
bos@73
|
67 while s:
|
bos@73
|
68 count = os.write(self.cfd, s)
|
bos@73
|
69 s = s[count:]
|
bos@6
|
70
|
bos@78
|
71 def debugrepr(self, s):
|
bos@78
|
72 rs = repr(s)
|
bos@78
|
73 limit = 60
|
bos@78
|
74 if len(rs) > limit:
|
bos@78
|
75 return ('%s%s ... [%d bytes]' % (rs[:limit], rs[0], len(s)))
|
bos@78
|
76 else:
|
bos@78
|
77 return rs
|
bos@78
|
78
|
bos@6
|
79 def receive(self):
|
bos@6
|
80 out = cStringIO.StringIO()
|
bos@4
|
81 while True:
|
bos@73
|
82 try:
|
bos@78
|
83 if self.verbose:
|
bos@78
|
84 sys.stderr.write('< ')
|
bos@73
|
85 s = os.read(self.cfd, 1024)
|
bos@73
|
86 except OSError, err:
|
bos@73
|
87 if err.errno == errno.EIO:
|
bos@73
|
88 return ''
|
bos@73
|
89 raise
|
bos@78
|
90 if self.verbose:
|
bos@78
|
91 print >> sys.stderr, self.debugrepr(s)
|
bos@6
|
92 out.write(s)
|
bos@73
|
93 s = out.getvalue()
|
bos@73
|
94 if s.endswith(self.prompt):
|
bos@73
|
95 return s.replace('\r\n', '\n')[:-len(self.prompt)]
|
bos@4
|
96
|
bos@6
|
97 def sendreceive(self, s):
|
bos@6
|
98 self.send(s)
|
bos@6
|
99 r = self.receive()
|
bos@6
|
100 if r.startswith(s):
|
bos@6
|
101 r = r[len(s):]
|
bos@6
|
102 return r
|
bos@6
|
103
|
bos@3
|
104 def run(self):
|
bos@3
|
105 ofp = None
|
bos@4
|
106 basename = os.path.basename(self.name)
|
bos@4
|
107 self.status('running %s ' % basename)
|
bos@4
|
108 tmpdir = tempfile.mkdtemp(prefix=basename)
|
bos@78
|
109
|
bos@78
|
110 rcfile = os.path.join(tmpdir, '.hgrc')
|
bos@78
|
111 rcfp = open(rcfile, 'w')
|
bos@78
|
112 print >> rcfp, '[ui]'
|
bos@78
|
113 print >> rcfp, "username = Bryan O'Sullivan <bos@serpentine.com>"
|
bos@78
|
114
|
bos@6
|
115 rcfile = os.path.join(tmpdir, '.bashrc')
|
bos@6
|
116 rcfp = open(rcfile, 'w')
|
bos@6
|
117 print >> rcfp, 'PS1="%s"' % self.prompt
|
bos@6
|
118 print >> rcfp, 'unset HISTFILE'
|
bos@19
|
119 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
|
bos@6
|
120 print >> rcfp, 'export LANG=C'
|
bos@6
|
121 print >> rcfp, 'export LC_ALL=C'
|
bos@6
|
122 print >> rcfp, 'export TZ=GMT'
|
bos@6
|
123 print >> rcfp, 'export HGRC="%s/.hgrc"' % tmpdir
|
bos@6
|
124 print >> rcfp, 'export HGRCPATH=$HGRC'
|
bos@6
|
125 print >> rcfp, 'cd %s' % tmpdir
|
bos@6
|
126 rcfp.close()
|
bos@68
|
127 sys.stdout.flush()
|
bos@68
|
128 sys.stderr.flush()
|
bos@73
|
129 pid, self.cfd = pty.fork()
|
bos@6
|
130 if pid == 0:
|
bos@70
|
131 cmdline = ['/usr/bin/env', 'bash', '--noediting', '--noprofile',
|
bos@70
|
132 '--norc']
|
bos@68
|
133 try:
|
bos@68
|
134 os.execv(cmdline[0], cmdline)
|
bos@68
|
135 except OSError, err:
|
bos@68
|
136 print >> sys.stderr, '%s: %s' % (cmdline[0], err.strerror)
|
bos@68
|
137 sys.stderr.flush()
|
bos@68
|
138 os._exit(0)
|
bos@4
|
139 try:
|
bos@71
|
140 try:
|
bos@73
|
141 # eat first prompt string from shell
|
bos@73
|
142 os.read(self.cfd, 1024)
|
bos@71
|
143 # setup env and prompt
|
bos@73
|
144 self.sendreceive('source %s\n' % rcfile)
|
bos@71
|
145 for hunk in self.parse():
|
bos@71
|
146 # is this line a processing instruction?
|
bos@71
|
147 m = self.pi_re.match(hunk)
|
bos@71
|
148 if m:
|
bos@71
|
149 pi, rest = m.groups()
|
bos@71
|
150 if pi == 'name':
|
bos@71
|
151 self.status('.')
|
bos@71
|
152 out = rest
|
bos@71
|
153 assert os.sep not in out
|
bos@71
|
154 if out:
|
bos@71
|
155 ofp = open('%s.%s.out' % (self.name, out), 'w')
|
bos@71
|
156 else:
|
bos@71
|
157 ofp = None
|
bos@71
|
158 elif hunk.strip():
|
bos@71
|
159 # it's something we should execute
|
bos@71
|
160 output = self.sendreceive(hunk)
|
bos@71
|
161 if not ofp:
|
bos@71
|
162 continue
|
bos@71
|
163 # first, print the command we ran
|
bos@71
|
164 if not hunk.startswith('#'):
|
bos@71
|
165 nl = hunk.endswith('\n')
|
bos@71
|
166 hunk = ('$ \\textbf{%s}' %
|
bos@71
|
167 tex_escape(hunk.rstrip('\n')))
|
bos@71
|
168 if nl: hunk += '\n'
|
bos@71
|
169 ofp.write(hunk)
|
bos@71
|
170 # then its output
|
bos@71
|
171 ofp.write(tex_escape(output))
|
bos@71
|
172 self.status('\n')
|
bos@71
|
173 open(self.name + '.run', 'w')
|
bos@71
|
174 except:
|
bos@72
|
175 print >> sys.stderr, '(killed)'
|
bos@72
|
176 os.kill(pid, signal.SIGKILL)
|
bos@72
|
177 pid, rc = os.wait()
|
bos@71
|
178 raise
|
bos@72
|
179 else:
|
bos@71
|
180 try:
|
bos@71
|
181 output = self.sendreceive('exit\n')
|
bos@71
|
182 if ofp:
|
bos@71
|
183 ofp.write(output)
|
bos@73
|
184 os.close(self.cfd)
|
bos@71
|
185 except IOError:
|
bos@71
|
186 pass
|
bos@72
|
187 os.kill(pid, signal.SIGTERM)
|
bos@72
|
188 pid, rc = os.wait()
|
bos@72
|
189 if rc:
|
bos@72
|
190 if os.WIFEXITED(rc):
|
bos@72
|
191 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc)
|
bos@72
|
192 elif os.WIFSIGNALED(rc):
|
bos@72
|
193 print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc)
|
bos@72
|
194 return rc
|
bos@72
|
195 finally:
|
bos@4
|
196 shutil.rmtree(tmpdir)
|
bos@3
|
197
|
bos@3
|
198 def main(path='.'):
|
bos@78
|
199 opts, args = getopt.getopt(sys.argv[1:], 'v', ['verbose'])
|
bos@78
|
200 verbose = False
|
bos@78
|
201 for o, a in opts:
|
bos@78
|
202 if o in ('-v', '--verbose'):
|
bos@78
|
203 verbose = True
|
bos@71
|
204 errs = 0
|
bos@3
|
205 if args:
|
bos@3
|
206 for a in args:
|
bos@75
|
207 try:
|
bos@75
|
208 st = os.lstat(a)
|
bos@75
|
209 except OSError, err:
|
bos@75
|
210 print >> sys.stderr, '%s: %s' % (a, err.strerror)
|
bos@75
|
211 errs += 1
|
bos@75
|
212 continue
|
bos@75
|
213 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
|
bos@78
|
214 if example(a, verbose).run():
|
bos@75
|
215 errs += 1
|
bos@75
|
216 else:
|
bos@75
|
217 print >> sys.stderr, '%s: not a file, or not executable' % a
|
bos@71
|
218 errs += 1
|
bos@71
|
219 return errs
|
bos@3
|
220 for name in os.listdir(path):
|
bos@3
|
221 if name == 'run-example' or name.startswith('.'): continue
|
bos@3
|
222 if name.endswith('.out') or name.endswith('~'): continue
|
bos@45
|
223 if name.endswith('.run'): continue
|
bos@19
|
224 pathname = os.path.join(path, name)
|
bos@36
|
225 st = os.lstat(pathname)
|
bos@36
|
226 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
|
bos@78
|
227 if example(pathname, verbose).run():
|
bos@71
|
228 errs += 1
|
bos@4
|
229 print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
|
bos@71
|
230 return errs
|
bos@3
|
231
|
bos@3
|
232 if __name__ == '__main__':
|
bos@71
|
233 sys.exit(main())
|