contrib / svn-fe / svnrdump_sim.pyon commit Merge branch 'fa/remote-svn' (530f237)
   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
  10
  11def getrevlimit():
  12        var = 'SVNRMAX'
  13        if os.environ.has_key(var):
  14                return os.environ[var]
  15        return None
  16
  17def writedump(url, lower, upper):
  18        if url.startswith('sim://'):
  19                filename = url[6:]
  20                if filename[-1] == '/': filename = filename[:-1] #remove terminating slash
  21        else:
  22                raise ValueError('sim:// url required')
  23        f = open(filename, 'r');
  24        state = 'header'
  25        wroterev = False
  26        while(True):
  27                l = f.readline()
  28                if l == '': break
  29                if state == 'header' and l.startswith('Revision-number: '):
  30                        state = 'prefix'
  31                if state == 'prefix' and l == 'Revision-number: %s\n' % lower:
  32                        state = 'selection'
  33                if not upper == 'HEAD' and state == 'selection' and l == 'Revision-number: %s\n' % upper:
  34                        break;
  35
  36                if state == 'header' or state == 'selection':
  37                        if state == 'selection': wroterev = True
  38                        sys.stdout.write(l)
  39        return wroterev
  40
  41if __name__ == "__main__":
  42        if not (len(sys.argv) in (3, 4, 5)):
  43                print "usage: %s dump URL -rLOWER:UPPER"
  44                sys.exit(1)
  45        if not sys.argv[1] == 'dump': raise NotImplementedError('only "dump" is suppported.')
  46        url = sys.argv[2]
  47        r = ('0', 'HEAD')
  48        if len(sys.argv) == 4 and sys.argv[3][0:2] == '-r':
  49                r = sys.argv[3][2:].lstrip().split(':')
  50        if not getrevlimit() is None: r[1] = getrevlimit()
  51        if writedump(url, r[0], r[1]): ret = 0
  52        else: ret = 1
  53        sys.exit(ret)