contrib / svn-fe / svnrdump_sim.pyon commit Merge branch 'ps/contains-id-error-message' (62c0fd4)
   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
   9import os
  10
  11if sys.hexversion < 0x02040000:
  12    # The limiter is the ValueError() calls. This may be too conservative
  13    sys.stderr.write("svnrdump-sim.py: requires Python 2.4 or later.\n")
  14    sys.exit(1)
  15
  16
  17def getrevlimit():
  18    var = 'SVNRMAX'
  19    if var in os.environ:
  20        return os.environ[var]
  21    return None
  22
  23
  24def writedump(url, lower, upper):
  25    if url.startswith('sim://'):
  26        filename = url[6:]
  27        if filename[-1] == '/':
  28            filename = filename[:-1]  # remove terminating slash
  29    else:
  30        raise ValueError('sim:// url required')
  31    f = open(filename, 'r')
  32    state = 'header'
  33    wroterev = False
  34    while(True):
  35        l = f.readline()
  36        if l == '':
  37            break
  38        if state == 'header' and l.startswith('Revision-number: '):
  39            state = 'prefix'
  40        if state == 'prefix' and l == 'Revision-number: %s\n' % lower:
  41            state = 'selection'
  42        if not upper == 'HEAD' and state == 'selection' and \
  43                l == 'Revision-number: %s\n' % upper:
  44            break
  45
  46        if state == 'header' or state == 'selection':
  47            if state == 'selection':
  48                wroterev = True
  49            sys.stdout.write(l)
  50    return wroterev
  51
  52if __name__ == "__main__":
  53    if not (len(sys.argv) in (3, 4, 5)):
  54        print("usage: %s dump URL -rLOWER:UPPER")
  55        sys.exit(1)
  56    if not sys.argv[1] == 'dump':
  57        raise NotImplementedError('only "dump" is suppported.')
  58    url = sys.argv[2]
  59    r = ('0', 'HEAD')
  60    if len(sys.argv) == 4 and sys.argv[3][0:2] == '-r':
  61        r = sys.argv[3][2:].lstrip().split(':')
  62    if not getrevlimit() is None:
  63        r[1] = getrevlimit()
  64    if writedump(url, r[0], r[1]):
  65        ret = 0
  66    else:
  67        ret = 1
  68    sys.exit(ret)