Move the long option name enum from cl.h into main.c, because it is
[svn.git] / tools / examples / get-location-segments.py
blob65ac8d0521ae149ad6c0d80a4bf445c7728eeb5d
1 #!/usr/bin/env python
2 import sys
3 import os
4 from svn import client, ra, core
6 def printer(segment, pool):
7 path = segment.path is not None and segment.path or "(null)"
8 print "r%d-r%d: %s" % (segment.range_start, segment.range_end, path)
11 def parse_args(args):
12 argc = len(sys.argv)
14 # parse the target URL and optional peg revision
15 path_pieces = args[0].split('@')
16 if len(path_pieces) > 1:
17 peg_revision = int(path_pieces[-1])
18 assert peg_revision >= 0
19 url = '@'.join(path_pieces[:-1])
20 else:
21 peg_revision = core.SVN_INVALID_REVNUM
22 url = path_pieces[0]
23 url = core.svn_path_canonicalize(url)
25 # parse the revision range, if any
26 if argc > 2:
27 rev_pieces = args[1].split(':')
28 num_revs = len(rev_pieces)
29 assert num_revs < 3
30 if num_revs == 2:
31 start_revision = int(rev_pieces[0])
32 end_revision = int(rev_pieces[1])
33 else:
34 start_revision = end_revision = int(rev_pieces[0])
35 assert(start_revision >= 0)
36 assert(end_revision >= 0)
37 else:
38 start_revision = peg_revision
39 end_revision = 0
41 # validate
42 if start_revision >= 0 \
43 and end_revision >= 0 \
44 and end_revision > start_revision:
45 raise Exception, "End revision must not be younger than start revision"
46 if peg_revision >= 0 \
47 and start_revision >= 0 \
48 and start_revision > peg_revision:
49 raise Exception, "Start revision must not be younger than peg revision"
51 return url, peg_revision, start_revision, end_revision
54 def main():
55 try:
56 url, peg_revision, start_revision, end_revision = parse_args(sys.argv[1:])
57 except Exception, e:
58 sys.stderr.write("""Usage: %s URL[@PEG-REV] [START-REV[:END-REV]]
60 Trace the history of URL@PEG-REV, printing the location(s) of its
61 existence between START-REV and END-REV. If START-REV is not
62 provided, the entire history of URL@PEG-REV back to its origin will be
63 displayed. If provided, START-REV must not be younger than PEG-REV.
64 If END-REV is provided, it must not be younger than START-REV.
66 (This is a wrapper around Subversion's svn_ra_get_location_segments() API.)
68 ERROR: %s
69 """ % (os.path.basename(sys.argv[0]), str(e)))
70 sys.exit(1)
72 core.svn_config_ensure(None)
73 ctx = client.ctx_t()
74 providers = [
75 client.get_simple_provider(),
76 client.get_username_provider(),
77 client.get_ssl_server_trust_file_provider(),
78 client.get_ssl_client_cert_file_provider(),
79 client.get_ssl_client_cert_pw_file_provider(),
81 ctx.auth_baton = core.svn_auth_open(providers)
82 ctx.config = core.svn_config_get_config(None)
84 ra_callbacks = ra.callbacks_t()
85 ra_callbacks.auth_baton = ctx.auth_baton
86 ra_session = ra.open(url, ra_callbacks, None, ctx.config)
87 ra.get_location_segments(ra_session, "", peg_revision,
88 start_revision, end_revision, printer)
90 if __name__ == "__main__":
91 main()