Debian Codegraph @

doc/example/svn-sync-repo.py

raw symbols SCIP
1 #!/usr/bin/env python
2 """
3
4 small utility for hot-syncing a svn repository through ssh.
5 uses execnet.
6
7 """
8
9 import os
10 import pathlib
11 import subprocess
12 import sys
13
14 import execnet
15
16
17 def usage():
18 arg0 = sys.argv[0]
19 print(arg0, "[user@]remote-host:/repo/location localrepo [ssh-config-file]")
20
21
22 def main(args):
23 remote = args[0]
24 localrepo = pathlib.Path(args[1])
25 if not localrepo.is_dir():
26 raise SystemExit(f"localrepo {localrepo} does not exist")
27 if len(args) == 3:
28 configfile = args[2]
29 else:
30 configfile = None
31 remote_host, path = remote.split(":", 1)
32 print("ssh-connecting to", remote_host)
33 gw = getgateway(remote_host, configfile)
34
35 local_rev = get_svn_youngest(localrepo)
36
37 # local protocol
38 # 1. client sends rev/repo -> server
39 # 2. server checks for newer revisions and sends dumps
40 # 3. client receives dumps, updates local repo
41 # 4. client goes back to step 1
42 c = gw.remote_exec(
43 """
44 import os
45 import subprocess
46 import time
47
48 remote_rev, repopath = channel.receive()
49 while True:
50 rev = subprocess.run(
51 ["svnlook", "youngest", repopath],
52 check=True,
53 capture_output=True,
54 text=True,
55 ).stdout
56 rev = int(rev)
57 if rev > remote_rev:
58 revrange = (remote_rev+1, rev)
59 dumpchannel = channel.gateway.newchannel()
60 channel.send(revrange)
61 channel.send(dumpchannel)
62
63 f = os.popen(
64 "svnadmin dump -q --incremental -r %s:%s %s"
65 % (revrange[0], revrange[1], repopath), 'r')
66 try:
67 maxcount = dumpchannel.receive()
68 count = maxcount
69 while 1:
70 s = f.read(8192)
71 if not s:
72 raise EOFError
73 dumpchannel.send(s)
74 count = count - 1
75 if count <= 0:
76 ack = dumpchannel.receive()
77 count = maxcount
78
79 except EOFError:
80 dumpchannel.close()
81 remote_rev = rev
82 else:
83 # using svn-hook instead would be nice here
84 time.sleep(30)
85 """
86 )
87
88 c.send((local_rev, path))
89 print("checking revisions from %d in %s" % (local_rev, remote))
90 while 1:
91 revstart, revend = c.receive()
92 dumpchannel = c.receive()
93 print("receiving revisions", revstart, "-", revend, "replaying...")
94 svn_load(localrepo, dumpchannel)
95 print("current revision", revend)
96
97
98 def svn_load(repo, dumpchannel, maxcount=100):
99 # every maxcount we will send an ACK to the other
100 # side in order to synchronise and avoid our side
101 # growing buffers (execnet does not control
102 # RAM usage or receive queue sizes)
103 dumpchannel.send(maxcount)
104 f = os.popen(f"svnadmin load -q {repo}", "w")
105 count = maxcount
106 for x in dumpchannel:
107 sys.stdout.write(".")
108 sys.stdout.flush()
109 f.write(x)
110 count = count - 1
111 if count <= 0:
112 dumpchannel.send(maxcount)
113 count = maxcount
114 print()
115 f.close()
116
117
118 def get_svn_youngest(repo):
119 rev = subprocess.run(
120 ["svnlook", "youngest", repo],
121 check=True,
122 capture_output=True,
123 text=True,
124 ).stdout
125 return int(rev)
126
127
128 def getgateway(host, configfile=None):
129 xspec = "ssh=%s" % host
130 if configfile is not None:
131 xspec += "//ssh_config=%s" % configfile
132 return execnet.makegateway(xspec)
133
134
135 if __name__ == "__main__":
136 if len(sys.argv) < 3:
137 usage()
138 raise SystemExit(1)
139
140 main(sys.argv[1:])