author | Vadim Gelfer <vadim.gelfer@gmail.com> |
Sat, 15 Jul 2006 09:19:40 -0700 | |
changeset 2619 | fc0ec2b64150 |
parent 2612 | ffb895f16925 |
child 2621 | 5a5852a417b1 |
permissions | -rw-r--r-- |
2399 | 1 |
# sshserver.py - ssh protocol server support for mercurial |
2396 | 2 |
# |
3 |
# Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
4 |
# |
|
5 |
# This software may be used and distributed according to the terms |
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
|
7 |
||
8 |
from demandload import demandload |
|
9 |
from i18n import gettext as _ |
|
10 |
from node import * |
|
2612
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2439
diff
changeset
|
11 |
demandload(globals(), "os streamclone sys tempfile util") |
2396 | 12 |
|
13 |
class sshserver(object): |
|
14 |
def __init__(self, ui, repo): |
|
15 |
self.ui = ui |
|
16 |
self.repo = repo |
|
17 |
self.lock = None |
|
18 |
self.fin = sys.stdin |
|
19 |
self.fout = sys.stdout |
|
20 |
||
21 |
sys.stdout = sys.stderr |
|
22 |
||
23 |
# Prevent insertion/deletion of CRs |
|
24 |
util.set_binary(self.fin) |
|
25 |
util.set_binary(self.fout) |
|
26 |
||
27 |
def getarg(self): |
|
28 |
argline = self.fin.readline()[:-1] |
|
29 |
arg, l = argline.split() |
|
30 |
val = self.fin.read(int(l)) |
|
31 |
return arg, val |
|
32 |
||
33 |
def respond(self, v): |
|
34 |
self.fout.write("%d\n" % len(v)) |
|
35 |
self.fout.write(v) |
|
36 |
self.fout.flush() |
|
37 |
||
38 |
def serve_forever(self): |
|
39 |
while self.serve_one(): pass |
|
40 |
sys.exit(0) |
|
41 |
||
42 |
def serve_one(self): |
|
43 |
cmd = self.fin.readline()[:-1] |
|
44 |
if cmd: |
|
45 |
impl = getattr(self, 'do_' + cmd, None) |
|
46 |
if impl: impl() |
|
2397
e9d402506514
merge change to ssh protocol.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2396
diff
changeset
|
47 |
else: self.respond("") |
2396 | 48 |
return cmd != '' |
49 |
||
50 |
def do_heads(self): |
|
51 |
h = self.repo.heads() |
|
52 |
self.respond(" ".join(map(hex, h)) + "\n") |
|
53 |
||
2419
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
54 |
def do_hello(self): |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
55 |
'''the hello command returns a set of lines describing various |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
56 |
interesting things about the server, in an RFC822-like format. |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
57 |
Currently the only one defined is "capabilities", which |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
58 |
consists of a line in the form: |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
59 |
|
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
60 |
capabilities: space separated list of tokens |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
61 |
''' |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
62 |
|
2612
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2439
diff
changeset
|
63 |
r = "capabilities: unbundle stream=%d\n" % (self.repo.revlogversion,) |
2419
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
64 |
self.respond(r) |
b17eebc911ae
Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents:
2399
diff
changeset
|
65 |
|
2396 | 66 |
def do_lock(self): |
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
67 |
'''DEPRECATED - allowing remote client to lock repo is not safe''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
68 |
|
2396 | 69 |
self.lock = self.repo.lock() |
70 |
self.respond("") |
|
71 |
||
72 |
def do_unlock(self): |
|
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
73 |
'''DEPRECATED''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
74 |
|
2396 | 75 |
if self.lock: |
76 |
self.lock.release() |
|
77 |
self.lock = None |
|
78 |
self.respond("") |
|
79 |
||
80 |
def do_branches(self): |
|
81 |
arg, nodes = self.getarg() |
|
82 |
nodes = map(bin, nodes.split(" ")) |
|
83 |
r = [] |
|
84 |
for b in self.repo.branches(nodes): |
|
85 |
r.append(" ".join(map(hex, b)) + "\n") |
|
86 |
self.respond("".join(r)) |
|
87 |
||
88 |
def do_between(self): |
|
89 |
arg, pairs = self.getarg() |
|
90 |
pairs = [map(bin, p.split("-")) for p in pairs.split(" ")] |
|
91 |
r = [] |
|
92 |
for b in self.repo.between(pairs): |
|
93 |
r.append(" ".join(map(hex, b)) + "\n") |
|
94 |
self.respond("".join(r)) |
|
95 |
||
96 |
def do_changegroup(self): |
|
97 |
nodes = [] |
|
98 |
arg, roots = self.getarg() |
|
99 |
nodes = map(bin, roots.split(" ")) |
|
100 |
||
101 |
cg = self.repo.changegroup(nodes, 'serve') |
|
102 |
while True: |
|
103 |
d = cg.read(4096) |
|
104 |
if not d: |
|
105 |
break |
|
106 |
self.fout.write(d) |
|
107 |
||
108 |
self.fout.flush() |
|
109 |
||
110 |
def do_addchangegroup(self): |
|
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
111 |
'''DEPRECATED''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
112 |
|
2396 | 113 |
if not self.lock: |
114 |
self.respond("not locked") |
|
115 |
return |
|
116 |
||
117 |
self.respond("") |
|
118 |
r = self.repo.addchangegroup(self.fin, 'serve') |
|
119 |
self.respond(str(r)) |
|
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
120 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
121 |
def do_unbundle(self): |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
122 |
their_heads = self.getarg()[1].split() |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
123 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
124 |
def check_heads(): |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
125 |
heads = map(hex, self.repo.heads()) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
126 |
return their_heads == [hex('force')] or their_heads == heads |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
127 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
128 |
# fail early if possible |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
129 |
if not check_heads(): |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
130 |
self.respond(_('unsynced changes')) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
131 |
return |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
132 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
133 |
self.respond('') |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
134 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
135 |
# write bundle data to temporary file because it can be big |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
136 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
137 |
try: |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
138 |
fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-') |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
139 |
fp = os.fdopen(fd, 'wb+') |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
140 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
141 |
count = int(self.fin.readline()) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
142 |
while count: |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
143 |
fp.write(self.fin.read(count)) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
144 |
count = int(self.fin.readline()) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
145 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
146 |
was_locked = self.lock is not None |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
147 |
if not was_locked: |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
148 |
self.lock = self.repo.lock() |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
149 |
try: |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
150 |
if not check_heads(): |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
151 |
# someone else committed/pushed/unbundled while we |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
152 |
# were transferring data |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
153 |
self.respond(_('unsynced changes')) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
154 |
return |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
155 |
self.respond('') |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
156 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
157 |
# push can proceed |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
158 |
|
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
159 |
fp.seek(0) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
160 |
r = self.repo.addchangegroup(fp, 'serve') |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
161 |
self.respond(str(r)) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
162 |
finally: |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
163 |
if not was_locked: |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
164 |
self.lock.release() |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
165 |
self.lock = None |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
166 |
finally: |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
167 |
fp.close() |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
168 |
os.unlink(tempname) |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
169 |
|
2612
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2439
diff
changeset
|
170 |
def do_stream_out(self): |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2439
diff
changeset
|
171 |
streamclone.stream_out(self.repo, self.fout) |