Merge branch 'mh/avoid-rewriting-packed-refs' into maint
[git.git] / contrib / svn-fe / svnrdump_sim.py
blob11ac6f692733fbfde96a44cc553002ad748065db
1 #!/usr/bin/python
2 """
3 Simulates svnrdump by replaying an existing dump from a file, taking care
4 of the specified revision range.
5 To simulate incremental imports the environment variable SVNRMAX can be set
6 to the highest revision that should be available.
7 """
8 import sys
9 import os
11 if 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)
17 def getrevlimit():
18 var = 'SVNRMAX'
19 if var in os.environ:
20 return os.environ[var]
21 return None
24 def 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
46 if state == 'header' or state == 'selection':
47 if state == 'selection':
48 wroterev = True
49 sys.stdout.write(l)
50 return wroterev
52 if __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)