hgbook

diff en/examples/run-example @ 122:3af28630fe8c

How to goodbye depression by reverting one thousand times daily.
author Bryan O'Sullivan <bos@serpentine.com>
date Wed Nov 15 16:57:29 2006 -0800 (2006-11-15)
parents b476081a9c04
children c9aad709bd3a
line diff
     1.1 --- a/en/examples/run-example	Tue Oct 03 13:03:42 2006 -0700
     1.2 +++ b/en/examples/run-example	Wed Nov 15 16:57:29 2006 -0800
     1.3 @@ -39,7 +39,8 @@
     1.4          
     1.5  class example:
     1.6      shell = '/usr/bin/env bash'
     1.7 -    prompt = '__run_example_prompt__ '
     1.8 +    ps1 = '__run_example_ps1__ '
     1.9 +    ps2 = '__run_example_ps2__ '
    1.10      pi_re = re.compile(r'#\$\s*(name):\s*(.*)$')
    1.11      
    1.12      timeout = 5
    1.13 @@ -99,21 +100,23 @@
    1.14                  s = self.read()
    1.15              except OSError, err:
    1.16                  if err.errno == errno.EIO:
    1.17 -                    return ''
    1.18 +                    return '', ''
    1.19                  raise
    1.20              if self.verbose:
    1.21                  print >> sys.stderr, self.debugrepr(s)
    1.22              out.write(s)
    1.23              s = out.getvalue()
    1.24 -            if s.endswith(self.prompt):
    1.25 -                return s.replace('\r\n', '\n')[:-len(self.prompt)]
    1.26 +            if s.endswith(self.ps1):
    1.27 +                return self.ps1, s.replace('\r\n', '\n')[:-len(self.ps1)]
    1.28 +            if s.endswith(self.ps2):
    1.29 +                return self.ps2, s.replace('\r\n', '\n')[:-len(self.ps2)]
    1.30          
    1.31      def sendreceive(self, s):
    1.32          self.send(s)
    1.33 -        r = self.receive()
    1.34 +        ps, r = self.receive()
    1.35          if r.startswith(s):
    1.36              r = r[len(s):]
    1.37 -        return r
    1.38 +        return ps, r
    1.39      
    1.40      def run(self):
    1.41          ofp = None
    1.42 @@ -128,8 +131,8 @@
    1.43          
    1.44          rcfile = os.path.join(tmpdir, '.bashrc')
    1.45          rcfp = open(rcfile, 'w')
    1.46 -        print >> rcfp, 'PS1="%s"' % self.prompt
    1.47 -        print >> rcfp, 'PS2="%s"' % self.prompt
    1.48 +        print >> rcfp, 'PS1="%s"' % self.ps1
    1.49 +        print >> rcfp, 'PS2="%s"' % self.ps2
    1.50          print >> rcfp, 'unset HISTFILE'
    1.51          print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
    1.52          print >> rcfp, 'export LANG=C'
    1.53 @@ -153,12 +156,19 @@
    1.54                  os._exit(0)
    1.55          self.poll.register(self.cfd, select.POLLIN | select.POLLERR |
    1.56                             select.POLLHUP)
    1.57 +
    1.58 +        prompts = {
    1.59 +            '': '',
    1.60 +            self.ps1: '$',
    1.61 +            self.ps2: '>',
    1.62 +            }
    1.63 +
    1.64          try:
    1.65              try:
    1.66                  # eat first prompt string from shell
    1.67                  self.read()
    1.68                  # setup env and prompt
    1.69 -                self.sendreceive('source %s\n' % rcfile)
    1.70 +                ps, output = self.sendreceive('source %s\n' % rcfile)
    1.71                  for hunk in self.parse():
    1.72                      # is this line a processing instruction?
    1.73                      m = self.pi_re.match(hunk)
    1.74 @@ -174,18 +184,20 @@
    1.75                                  ofp = None
    1.76                      elif hunk.strip():
    1.77                          # it's something we should execute
    1.78 -                        output = self.sendreceive(hunk)
    1.79 +                        newps, output = self.sendreceive(hunk)
    1.80                          if not ofp:
    1.81                              continue
    1.82                          # first, print the command we ran
    1.83                          if not hunk.startswith('#'):
    1.84                              nl = hunk.endswith('\n')
    1.85 -                            hunk = ('$ \\textbf{%s}' %
    1.86 -                                    tex_escape(hunk.rstrip('\n')))
    1.87 +                            hunk = ('%s \\textbf{%s}' %
    1.88 +                                    (prompts[ps],
    1.89 +                                     tex_escape(hunk.rstrip('\n'))))
    1.90                              if nl: hunk += '\n'
    1.91                          ofp.write(hunk)
    1.92                          # then its output
    1.93                          ofp.write(tex_escape(output))
    1.94 +                    ps = newps
    1.95                  self.status('\n')
    1.96                  open(self.name + '.run', 'w')
    1.97              except:
    1.98 @@ -195,7 +207,7 @@
    1.99                  raise
   1.100              else:
   1.101                  try:
   1.102 -                    output = self.sendreceive('exit\n')
   1.103 +                    ps, output = self.sendreceive('exit\n')
   1.104                      if ofp:
   1.105                          ofp.write(output)
   1.106                      os.close(self.cfd)