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