t9700: do not close STDERR
[git/mingw/j6t.git] / contrib / svn-fe / svnrdump_sim.py
blob4e78a1c3cda15659256c7aa64eb98cab7737c61f
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, os
10 if 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)
15 def getrevlimit():
16 var = 'SVNRMAX'
17 if var in os.environ:
18 return os.environ[var]
19 return None
21 def 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;
40 if state == 'header' or state == 'selection':
41 if state == 'selection': wroterev = True
42 sys.stdout.write(l)
43 return wroterev
45 if __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)