annotate es/examples/run-example @ 684:3b062018273a

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