hgbook

annotate en/examples/run-example @ 172:5f305adeb584

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