annotate ja/examples/run-example-tex @ 838:d1f676a6a4b3 default tip

update mq chapter. propagate ef53d025f410.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 03 Dec 2009 01:26:08 +0900
parents 4cf1bbfe4d2c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
789
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
1 #!/usr/bin/env python
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
2 #
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
3 # This program takes something that resembles a shell script and runs
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
4 # it, spitting input (commands from the script) and output into text
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
5 # files, for use in examples.
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
6
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
7 import cStringIO
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
8 import errno
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
9 import getopt
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
10 import os
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
11 import pty
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
12 import re
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
13 import select
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
14 import shutil
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
15 import signal
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
16 import stat
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
17 import sys
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
18 import tempfile
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
19 import time
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
20
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
21 tex_subs = {
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
22 '\\': '\\textbackslash{}',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
23 '{': '\\{',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
24 '}': '\\}',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
25 }
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
26
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
27 def gensubs(s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
28 start = 0
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
29 for i, c in enumerate(s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
30 sub = tex_subs.get(c)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
31 if sub:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
32 yield s[start:i]
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
33 start = i + 1
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
34 yield sub
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
35 yield s[start:]
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
36
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
37 def tex_escape(s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
38 return ''.join(gensubs(s))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
39
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
40 def maybe_unlink(name):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
41 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
42 os.unlink(name)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
43 return True
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
44 except OSError, err:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
45 if err.errno != errno.ENOENT:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
46 raise
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
47 return False
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
48
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
49 def find_path_to(program):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
50 for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
51 name = os.path.join(p, program)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
52 if os.access(name, os.X_OK):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
53 return p
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
54 return None
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
55
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
56 class example:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
57 shell = '/usr/bin/env bash'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
58 ps1 = '__run_example_ps1__ '
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
59 ps2 = '__run_example_ps2__ '
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
60 pi_re = re.compile(r'#\$\s*(name|ignore):\s*(.*)$')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
61
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
62 timeout = 10
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
63
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
64 def __init__(self, name, verbose):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
65 self.name = name
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
66 self.verbose = verbose
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
67 self.poll = select.poll()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
68
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
69 def parse(self):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
70 '''yield each hunk of input from the file.'''
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
71 fp = open(self.name)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
72 cfp = cStringIO.StringIO()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
73 for line in fp:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
74 cfp.write(line)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
75 if not line.rstrip().endswith('\\'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
76 yield cfp.getvalue()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
77 cfp.seek(0)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
78 cfp.truncate()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
79
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
80 def status(self, s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
81 sys.stdout.write(s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
82 if not s.endswith('\n'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
83 sys.stdout.flush()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
84
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
85 def send(self, s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
86 if self.verbose:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
87 print >> sys.stderr, '>', self.debugrepr(s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
88 while s:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
89 count = os.write(self.cfd, s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
90 s = s[count:]
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
91
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
92 def debugrepr(self, s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
93 rs = repr(s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
94 limit = 60
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
95 if len(rs) > limit:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
96 return ('%s%s ... [%d bytes]' % (rs[:limit], rs[0], len(s)))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
97 else:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
98 return rs
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
99
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
100 timeout = 5
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
101
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
102 def read(self, hint):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
103 events = self.poll.poll(self.timeout * 1000)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
104 if not events:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
105 print >> sys.stderr, ('[%stimed out after %d seconds]' %
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
106 (hint, self.timeout))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
107 os.kill(self.pid, signal.SIGHUP)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
108 return ''
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
109 return os.read(self.cfd, 1024)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
110
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
111 def receive(self, hint):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
112 out = cStringIO.StringIO()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
113 while True:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
114 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
115 if self.verbose:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
116 sys.stderr.write('< ')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
117 s = self.read(hint)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
118 except OSError, err:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
119 if err.errno == errno.EIO:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
120 return '', ''
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
121 raise
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
122 if self.verbose:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
123 print >> sys.stderr, self.debugrepr(s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
124 out.write(s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
125 s = out.getvalue()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
126 if s.endswith(self.ps1):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
127 return self.ps1, s.replace('\r\n', '\n')[:-len(self.ps1)]
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
128 if s.endswith(self.ps2):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
129 return self.ps2, s.replace('\r\n', '\n')[:-len(self.ps2)]
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
130
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
131 def sendreceive(self, s, hint):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
132 self.send(s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
133 ps, r = self.receive(hint)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
134 if r.startswith(s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
135 r = r[len(s):]
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
136 return ps, r
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
137
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
138 def run(self):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
139 ofp = None
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
140 basename = os.path.basename(self.name)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
141 self.status('running %s ' % basename)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
142 tmpdir = tempfile.mkdtemp(prefix=basename)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
143
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
144 # remove the marker file that we tell make to use to see if
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
145 # this run succeeded
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
146 maybe_unlink(self.name + '.run')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
147
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
148 rcfile = os.path.join(tmpdir, '.hgrc')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
149 rcfp = open(rcfile, 'w')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
150 print >> rcfp, '[ui]'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
151 print >> rcfp, "username = Bryan O'Sullivan <bos@serpentine.com>"
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
152
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
153 rcfile = os.path.join(tmpdir, '.bashrc')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
154 rcfp = open(rcfile, 'w')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
155 print >> rcfp, 'PS1="%s"' % self.ps1
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
156 print >> rcfp, 'PS2="%s"' % self.ps2
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
157 print >> rcfp, 'unset HISTFILE'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
158 path = ['/usr/bin', '/bin']
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
159 hg = find_path_to('hg')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
160 if hg and hg not in path:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
161 path.append(hg)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
162 def re_export(envar):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
163 v = os.getenv(envar)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
164 if v is not None:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
165 print >> rcfp, 'export ' + envar + '=' + v
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
166 print >> rcfp, 'export PATH=' + ':'.join(path)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
167 re_export('PYTHONPATH')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
168 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
169 print >> rcfp, 'export HGMERGE=merge'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
170 print >> rcfp, 'export LANG=C'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
171 print >> rcfp, 'export LC_ALL=C'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
172 print >> rcfp, 'export TZ=GMT'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
173 print >> rcfp, 'export HGRC="%s/.hgrc"' % tmpdir
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
174 print >> rcfp, 'export HGRCPATH=$HGRC'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
175 print >> rcfp, 'cd %s' % tmpdir
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
176 rcfp.close()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
177 sys.stdout.flush()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
178 sys.stderr.flush()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
179 self.pid, self.cfd = pty.fork()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
180 if self.pid == 0:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
181 cmdline = ['/usr/bin/env', '-i', 'bash', '--noediting',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
182 '--noprofile', '--norc']
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
183 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
184 os.execv(cmdline[0], cmdline)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
185 except OSError, err:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
186 print >> sys.stderr, '%s: %s' % (cmdline[0], err.strerror)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
187 sys.stderr.flush()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
188 os._exit(0)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
189 self.poll.register(self.cfd, select.POLLIN | select.POLLERR |
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
190 select.POLLHUP)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
191
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
192 prompts = {
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
193 '': '',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
194 self.ps1: '$',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
195 self.ps2: '>',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
196 }
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
197
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
198 ignore = [
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
199 r'\d+:[0-9a-f]{12}', # changeset number:hash
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
200 r'[0-9a-f]{40}', # long changeset hash
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
201 r'[0-9a-f]{12}', # short changeset hash
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
202 r'^(?:---|\+\+\+) .*', # diff header with dates
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
203 r'^date:.*', # date
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
204 #r'^diff -r.*', # "diff -r" is followed by hash
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
205 r'^# Date \d+ \d+', # hg patch header
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
206 ]
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
207
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
208 err = False
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
209 read_hint = ''
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
210
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
211 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
212 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
213 # eat first prompt string from shell
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
214 self.read(read_hint)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
215 # setup env and prompt
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
216 ps, output = self.sendreceive('source %s\n' % rcfile,
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
217 read_hint)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
218 for hunk in self.parse():
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
219 # is this line a processing instruction?
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
220 m = self.pi_re.match(hunk)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
221 if m:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
222 pi, rest = m.groups()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
223 if pi == 'name':
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
224 self.status('.')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
225 out = rest
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
226 if out in ('err', 'lxo', 'out', 'run', 'tmp'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
227 print >> sys.stderr, ('%s: illegal section '
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
228 'name %r' %
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
229 (self.name, out))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
230 return 1
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
231 assert os.sep not in out
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
232 if ofp is not None:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
233 ofp.close()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
234 err |= self.rename_output(ofp_basename, ignore)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
235 if out:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
236 ofp_basename = '%s.%s' % (self.name, out)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
237 read_hint = ofp_basename + ' '
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
238 ofp = open(ofp_basename + '.tmp', 'w')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
239 else:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
240 ofp = None
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
241 elif pi == 'ignore':
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
242 ignore.append(rest)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
243 elif hunk.strip():
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
244 # it's something we should execute
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
245 newps, output = self.sendreceive(hunk, read_hint)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
246 if not ofp:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
247 continue
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
248 # first, print the command we ran
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
249 if not hunk.startswith('#'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
250 nl = hunk.endswith('\n')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
251 hunk = ('%s \\textbf{%s}' %
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
252 (prompts[ps],
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
253 tex_escape(hunk.rstrip('\n'))))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
254 if nl: hunk += '\n'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
255 ofp.write(hunk)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
256 # then its output
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
257 ofp.write(tex_escape(output))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
258 ps = newps
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
259 self.status('\n')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
260 except:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
261 print >> sys.stderr, '(killed)'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
262 os.kill(self.pid, signal.SIGKILL)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
263 pid, rc = os.wait()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
264 raise
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
265 else:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
266 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
267 ps, output = self.sendreceive('exit\n', read_hint)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
268 if ofp is not None:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
269 ofp.write(output)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
270 ofp.close()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
271 err |= self.rename_output(ofp_basename, ignore)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
272 os.close(self.cfd)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
273 except IOError:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
274 pass
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
275 os.kill(self.pid, signal.SIGTERM)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
276 pid, rc = os.wait()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
277 err = err or rc
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
278 if err:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
279 if os.WIFEXITED(rc):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
280 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
281 elif os.WIFSIGNALED(rc):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
282 print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
283 else:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
284 open(self.name + '.run', 'w')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
285 # return err
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
286 return 0
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
287 finally:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
288 shutil.rmtree(tmpdir)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
289
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
290 def rename_output(self, base, ignore):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
291 mangle_re = re.compile('(?:' + '|'.join(ignore) + ')')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
292 def mangle(s):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
293 return mangle_re.sub('', s)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
294 def matchfp(fp1, fp2):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
295 while True:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
296 s1 = mangle(fp1.readline())
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
297 s2 = mangle(fp2.readline())
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
298 if cmp(s1, s2):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
299 break
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
300 if not s1:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
301 return True
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
302 return False
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
303
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
304 oldname = base + '.out'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
305 tmpname = base + '.tmp'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
306 errname = base + '.err'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
307 errfp = open(errname, 'w+')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
308 for line in open(tmpname):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
309 errfp.write(mangle_re.sub('', line))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
310 os.rename(tmpname, base + '.lxo')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
311 errfp.seek(0)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
312 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
313 oldfp = open(oldname)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
314 except IOError, err:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
315 if err.errno != errno.ENOENT:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
316 raise
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
317 os.rename(errname, oldname)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
318 return False
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
319 if matchfp(oldfp, errfp):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
320 os.unlink(errname)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
321 return False
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
322 else:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
323 print >> sys.stderr, '\nOutput of %s has changed!' % base
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
324 os.system('diff -u %s %s 1>&2' % (oldname, errname))
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
325 return True
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
326
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
327 def print_help(exit, msg=None):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
328 if msg:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
329 print >> sys.stderr, 'Error:', msg
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
330 print >> sys.stderr, 'Usage: run-example [options] [test...]'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
331 print >> sys.stderr, 'Options:'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
332 print >> sys.stderr, ' -a --all run all tests in this directory'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
333 print >> sys.stderr, ' -h --help print this help message'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
334 print >> sys.stderr, ' -v --verbose display extra debug output'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
335 sys.exit(exit)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
336
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
337 def main(path='.'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
338 opts, args = getopt.getopt(sys.argv[1:], '?ahv',
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
339 ['all', 'help', 'verbose'])
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
340 verbose = False
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
341 run_all = False
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
342 for o, a in opts:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
343 if o in ('-h', '-?', '--help'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
344 print_help(0)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
345 if o in ('-a', '--all'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
346 run_all = True
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
347 if o in ('-v', '--verbose'):
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
348 verbose = True
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
349 errs = 0
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
350 if args:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
351 for a in args:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
352 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
353 st = os.lstat(a)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
354 except OSError, err:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
355 print >> sys.stderr, '%s: %s' % (a, err.strerror)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
356 errs += 1
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
357 continue
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
358 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
359 if example(a, verbose).run():
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
360 errs += 1
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
361 else:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
362 print >> sys.stderr, '%s: not a file, or not executable' % a
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
363 errs += 1
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
364 elif run_all:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
365 names = os.listdir(path)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
366 names.sort()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
367 for name in names:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
368 if name == 'run-example' or name.startswith('.'): continue
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
369 if name.endswith('.out') or name.endswith('~'): continue
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
370 if name.endswith('.run'): continue
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
371 pathname = os.path.join(path, name)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
372 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
373 st = os.lstat(pathname)
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
374 except OSError, err:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
375 # could be an output file that was removed while we ran
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
376 if err.errno != errno.ENOENT:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
377 raise
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
378 continue
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
379 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
380 if example(pathname, verbose).run():
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
381 errs += 1
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
382 print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
383 else:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
384 print_help(1, msg='no test names given, and --all not provided')
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
385 return errs
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
386
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
387 if __name__ == '__main__':
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
388 try:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
389 sys.exit(main())
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
390 except KeyboardInterrupt:
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
391 print >> sys.stderr, 'interrupted!'
4cf1bbfe4d2c restore the script to generate examples for tex.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
392 sys.exit(1)