Mercurial > hg-stable
annotate mercurial/sshserver.py @ 20933:d3775db748a0
localrepo: move the addchangegroup method in changegroup module
This is a gratuitous code move aimed at reducing the localrepo bloatness.
The method had few callers, not enough to be kept in local repo.
author | Pierre-Yves David <pierre-yves.david@fb.com> |
---|---|
date | Tue, 01 Apr 2014 15:27:53 -0700 |
parents | 8d477543882b |
children | e2806b8613ca |
rev | line source |
---|---|
2399 | 1 # sshserver.py - ssh protocol server support for mercurial |
2396 | 2 # |
4635
63b9d2deed48
Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4258
diff
changeset
|
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
2859 | 4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
2396 | 5 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8109
diff
changeset
|
6 # This software may be used and distributed according to the terms of the |
10263 | 7 # GNU General Public License version 2 or any later version. |
2396 | 8 |
12337
6a6149487817
bundle: encapsulate all bundle streams in unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12062
diff
changeset
|
9 import util, hook, wireproto, changegroup |
11596
47ca289a3a08
ssh: drop some old imports
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
10 import os, sys |
2396 | 11 |
20903
8d477543882b
wireproto: introduce an abstractserverproto class
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
17563
diff
changeset
|
12 class sshserver(wireproto.abstractserverproto): |
2396 | 13 def __init__(self, ui, repo): |
14 self.ui = ui | |
15 self.repo = repo | |
16 self.lock = None | |
14614
afccc64eea73
ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents:
14233
diff
changeset
|
17 self.fin = ui.fin |
afccc64eea73
ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents:
14233
diff
changeset
|
18 self.fout = ui.fout |
2396 | 19 |
5833
323b9c55b328
hook: redirect stdout to stderr for ssh and http servers
Matt Mackall <mpm@selenic.com>
parents:
4635
diff
changeset
|
20 hook.redirect(True) |
14614
afccc64eea73
ui: use I/O descriptors internally
Idan Kamara <idankk86@gmail.com>
parents:
14233
diff
changeset
|
21 ui.fout = repo.ui.fout = ui.ferr |
2396 | 22 |
23 # Prevent insertion/deletion of CRs | |
14233
659f34b833b9
rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents:
13721
diff
changeset
|
24 util.setbinary(self.fin) |
659f34b833b9
rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents:
13721
diff
changeset
|
25 util.setbinary(self.fout) |
2396 | 26 |
11579 | 27 def getargs(self, args): |
28 data = {} | |
29 keys = args.split() | |
30 for n in xrange(len(keys)): | |
31 argline = self.fin.readline()[:-1] | |
32 arg, l = argline.split() | |
33 if arg not in keys: | |
34 raise util.Abort("unexpected parameter %r" % arg) | |
35 if arg == '*': | |
36 star = {} | |
13721
3458c15ab2f0
wireproto: fix handling of '*' args for HTTP and SSH
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
13719
diff
changeset
|
37 for k in xrange(int(l)): |
3458c15ab2f0
wireproto: fix handling of '*' args for HTTP and SSH
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
13719
diff
changeset
|
38 argline = self.fin.readline()[:-1] |
11579 | 39 arg, l = argline.split() |
40 val = self.fin.read(int(l)) | |
41 star[arg] = val | |
42 data['*'] = star | |
43 else: | |
13721
3458c15ab2f0
wireproto: fix handling of '*' args for HTTP and SSH
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
13719
diff
changeset
|
44 val = self.fin.read(int(l)) |
11579 | 45 data[arg] = val |
46 return [data[k] for k in keys] | |
47 | |
48 def getarg(self, name): | |
49 return self.getargs(name)[0] | |
2396 | 50 |
11621
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
51 def getfile(self, fpout): |
11622
1d48681b17a4
protocol: rename send methods to get grouping by prefix
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11621
diff
changeset
|
52 self.sendresponse('') |
11621
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
53 count = int(self.fin.readline()) |
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
54 while count: |
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
55 fpout.write(self.fin.read(count)) |
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
56 count = int(self.fin.readline()) |
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
57 |
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
58 def redirect(self): |
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
59 pass |
e46a8b2331a6
protocol: shuffle server methods to group send methods
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11618
diff
changeset
|
60 |
11623
31d0a6d50ee2
protocol: extract compression from streaming mechanics
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11622
diff
changeset
|
61 def groupchunks(self, changegroup): |
11584
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
62 while True: |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
63 d = changegroup.read(4096) |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
64 if not d: |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
65 break |
11623
31d0a6d50ee2
protocol: extract compression from streaming mechanics
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11622
diff
changeset
|
66 yield d |
11584
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
67 |
11623
31d0a6d50ee2
protocol: extract compression from streaming mechanics
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11622
diff
changeset
|
68 def sendresponse(self, v): |
31d0a6d50ee2
protocol: extract compression from streaming mechanics
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11622
diff
changeset
|
69 self.fout.write("%d\n" % len(v)) |
31d0a6d50ee2
protocol: extract compression from streaming mechanics
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11622
diff
changeset
|
70 self.fout.write(v) |
11584
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
71 self.fout.flush() |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
72 |
11585
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
73 def sendstream(self, source): |
17563
46db0ec640f3
sshserver: avoid a multi-dot attribute lookup in a hot loop
Bryan O'Sullivan <bryano@fb.com>
parents:
15585
diff
changeset
|
74 write = self.fout.write |
11625
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
75 for chunk in source.gen: |
17563
46db0ec640f3
sshserver: avoid a multi-dot attribute lookup in a hot loop
Bryan O'Sullivan <bryano@fb.com>
parents:
15585
diff
changeset
|
76 write(chunk) |
11585
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
77 self.fout.flush() |
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
78 |
11625
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
79 def sendpushresponse(self, rsp): |
11622
1d48681b17a4
protocol: rename send methods to get grouping by prefix
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11621
diff
changeset
|
80 self.sendresponse('') |
11625
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
81 self.sendresponse(str(rsp.res)) |
11593
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
82 |
12703
40bb5853fc4b
wireproto: introduce pusherr() to deal with "unsynced changes" error
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
12337
diff
changeset
|
83 def sendpusherror(self, rsp): |
40bb5853fc4b
wireproto: introduce pusherr() to deal with "unsynced changes" error
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
12337
diff
changeset
|
84 self.sendresponse(rsp.res) |
40bb5853fc4b
wireproto: introduce pusherr() to deal with "unsynced changes" error
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
12337
diff
changeset
|
85 |
15017
f4522df38c65
wireproto: add out-of-band error class to allow remote repo to report errors
Andrew Pritchard <andrewp@fogcreek.com>
parents:
14614
diff
changeset
|
86 def sendooberror(self, rsp): |
f4522df38c65
wireproto: add out-of-band error class to allow remote repo to report errors
Andrew Pritchard <andrewp@fogcreek.com>
parents:
14614
diff
changeset
|
87 self.ui.ferr.write('%s\n-\n' % rsp.message) |
f4522df38c65
wireproto: add out-of-band error class to allow remote repo to report errors
Andrew Pritchard <andrewp@fogcreek.com>
parents:
14614
diff
changeset
|
88 self.ui.ferr.flush() |
f4522df38c65
wireproto: add out-of-band error class to allow remote repo to report errors
Andrew Pritchard <andrewp@fogcreek.com>
parents:
14614
diff
changeset
|
89 self.fout.write('\n') |
f4522df38c65
wireproto: add out-of-band error class to allow remote repo to report errors
Andrew Pritchard <andrewp@fogcreek.com>
parents:
14614
diff
changeset
|
90 self.fout.flush() |
f4522df38c65
wireproto: add out-of-band error class to allow remote repo to report errors
Andrew Pritchard <andrewp@fogcreek.com>
parents:
14614
diff
changeset
|
91 |
2396 | 92 def serve_forever(self): |
8109
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
93 try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
94 while self.serve_one(): |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
95 pass |
8109
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
96 finally: |
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
97 if self.lock is not None: |
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
98 self.lock.release() |
2396 | 99 sys.exit(0) |
100 | |
11625
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
101 handlers = { |
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
102 str: sendresponse, |
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
103 wireproto.streamres: sendstream, |
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
104 wireproto.pushres: sendpushresponse, |
12703
40bb5853fc4b
wireproto: introduce pusherr() to deal with "unsynced changes" error
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
12337
diff
changeset
|
105 wireproto.pusherr: sendpusherror, |
15017
f4522df38c65
wireproto: add out-of-band error class to allow remote repo to report errors
Andrew Pritchard <andrewp@fogcreek.com>
parents:
14614
diff
changeset
|
106 wireproto.ooberror: sendooberror, |
11625
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
107 } |
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
108 |
2396 | 109 def serve_one(self): |
110 cmd = self.fin.readline()[:-1] | |
11618
83070a9cd526
protocol: command must be checked before passing in
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11596
diff
changeset
|
111 if cmd and cmd in wireproto.commands: |
11625
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
112 rsp = wireproto.dispatch(self.repo, self, cmd) |
cdeb861335d5
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11623
diff
changeset
|
113 self.handlers[rsp.__class__](self, rsp) |
11618
83070a9cd526
protocol: command must be checked before passing in
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11596
diff
changeset
|
114 elif cmd: |
2396 | 115 impl = getattr(self, 'do_' + cmd, None) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
116 if impl: |
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
117 r = impl() |
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
118 if r is not None: |
11622
1d48681b17a4
protocol: rename send methods to get grouping by prefix
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11621
diff
changeset
|
119 self.sendresponse(r) |
1d48681b17a4
protocol: rename send methods to get grouping by prefix
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11621
diff
changeset
|
120 else: self.sendresponse("") |
2396 | 121 return cmd != '' |
122 | |
123 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
|
124 '''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
|
125 |
2396 | 126 self.lock = self.repo.lock() |
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
127 return "" |
2396 | 128 |
129 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
|
130 '''DEPRECATED''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
131 |
2396 | 132 if self.lock: |
133 self.lock.release() | |
134 self.lock = None | |
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
135 return "" |
2396 | 136 |
137 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
|
138 '''DEPRECATED''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
139 |
2396 | 140 if not self.lock: |
11622
1d48681b17a4
protocol: rename send methods to get grouping by prefix
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11621
diff
changeset
|
141 self.sendresponse("not locked") |
2396 | 142 return |
143 | |
11622
1d48681b17a4
protocol: rename send methods to get grouping by prefix
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
11621
diff
changeset
|
144 self.sendresponse("") |
12337
6a6149487817
bundle: encapsulate all bundle streams in unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12062
diff
changeset
|
145 cg = changegroup.unbundle10(self.fin, "UN") |
20933
d3775db748a0
localrepo: move the addchangegroup method in changegroup module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
20903
diff
changeset
|
146 r = changegroup.addchangegroup(self.repo, cg, 'serve', self._client()) |
15585
a348739da8f0
addchangegroup: remove the lock argument on the addchangegroup methods
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
15017
diff
changeset
|
147 self.lock.release() |
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
148 return str(r) |
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
149 |
11593
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
150 def _client(self): |
2673
109a22f5434a
hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2622
diff
changeset
|
151 client = os.environ.get('SSH_CLIENT', '').split(' ', 1)[0] |
109a22f5434a
hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2622
diff
changeset
|
152 return 'remote:ssh:' + client |