contrib / svn-fe / svnrdump_sim.pyon commit Merge branch 'rs/leave-base-name-in-name-field-of-tar' (f12f3af)
   1#!/usr/bin/python
   2"""
   3Simulates svnrdump by replaying an existing dump from a file, taking care
   4of the specified revision range.
   5To simulate incremental imports the environment variable SVNRMAX can be set
   6to the highest revision that should be available.
   7"""
   8import sys, os
   9
  10if sys.hexversion < 0x02040000:
  11        # The limiter is the ValueError() calls. This may be too conservative
  12        sys.stderr.write("svnrdump-sim.py: requires Python 2.4 or later.\n")
  13        sys.exit(1)
  14
  15def getrevlimit():
  16        var = 'SVNRMAX'
  17        if os.environ.has_key(var):
  18                return os.environ[var]
  19        return None
  20
  21def writedump(url, lower, upper):
  22        if url.startswith('sim://'):
  23                filename = url[6:]
  24                if filename[-1] == '/': filename = filename[:-1] #remove terminating slash
  25        else:
  26                raise ValueError('sim:// url required')
  27        f = open(filename, 'r');
  28        state = 'header'
  29        wroterev = False
  30        while(True):
  31                l = f.readline()
  32                if l == '': break
  33                if state == 'header' and l.startswith('Revision-number: '):
  34                        state = 'prefix'
  35                if state == 'prefix' and l == 'Revision-number: %s\n' % lower:
  36                        state = 'selection'
  37                if not upper == 'HEAD' and state == 'selection' and l == 'Revision-number: %s\n' % upper:
  38                        break;
  39
  40                if state == 'header' or state == 'selection':
  41                        if state == 'selection': wroterev = True
  42                        sys.stdout.write(l)
  43        return wroterev
  44
  45if __name__ == "__main__":
  46        if not (len(sys.argv) in (3, 4, 5)):
  47                print "usage: %s dump URL -rLOWER:UPPER"
  48                sys.exit(1)
  49        if not sys.argv[1] == 'dump': raise NotImplementedError('only "dump" is suppported.')
  50        url = sys.argv[2]
  51        r = ('0', 'HEAD')
  52        if len(sys.argv) == 4 and sys.argv[3][0:2] == '-r':
  53                r = sys.argv[3][2:].lstrip().split(':')
  54        if not getrevlimit() is None: r[1] = getrevlimit()
  55        if writedump(url, r[0], r[1]): ret = 0
  56        else: ret = 1
  57        sys.exit(ret)