##// END OF EJS Templates
cmdserver: copy repo.ui before running commands
Idan Kamara -
r14750:f5f97a0f stable
parent child Browse files
Show More
@@ -1,223 +1,226 b''
1 # commandserver.py - communicate with Mercurial's API over a pipe
1 # commandserver.py - communicate with Mercurial's API over a pipe
2 #
2 #
3 # Copyright Matt Mackall <mpm@selenic.com>
3 # Copyright Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from i18n import _
8 from i18n import _
9 import struct
9 import struct
10 import sys
10 import sys
11 import dispatch, encoding, util
11 import dispatch, encoding, util
12
12
13 logfile = None
13 logfile = None
14
14
15 def log(*args):
15 def log(*args):
16 if not logfile:
16 if not logfile:
17 return
17 return
18
18
19 for a in args:
19 for a in args:
20 logfile.write(str(a))
20 logfile.write(str(a))
21
21
22 logfile.flush()
22 logfile.flush()
23
23
24 class channeledoutput(object):
24 class channeledoutput(object):
25 """
25 """
26 Write data from in_ to out in the following format:
26 Write data from in_ to out in the following format:
27
27
28 data length (unsigned int),
28 data length (unsigned int),
29 data
29 data
30 """
30 """
31 def __init__(self, in_, out, channel):
31 def __init__(self, in_, out, channel):
32 self.in_ = in_
32 self.in_ = in_
33 self.out = out
33 self.out = out
34 self.channel = channel
34 self.channel = channel
35
35
36 def write(self, data):
36 def write(self, data):
37 if not data:
37 if not data:
38 return
38 return
39 self.out.write(struct.pack('>cI', self.channel, len(data)))
39 self.out.write(struct.pack('>cI', self.channel, len(data)))
40 self.out.write(data)
40 self.out.write(data)
41 self.out.flush()
41 self.out.flush()
42
42
43 def __getattr__(self, attr):
43 def __getattr__(self, attr):
44 if attr in ('isatty', 'fileno'):
44 if attr in ('isatty', 'fileno'):
45 raise AttributeError, attr
45 raise AttributeError, attr
46 return getattr(self.in_, attr)
46 return getattr(self.in_, attr)
47
47
48 class channeledinput(object):
48 class channeledinput(object):
49 """
49 """
50 Read data from in_.
50 Read data from in_.
51
51
52 Requests for input are written to out in the following format:
52 Requests for input are written to out in the following format:
53 channel identifier - 'I' for plain input, 'L' line based (1 byte)
53 channel identifier - 'I' for plain input, 'L' line based (1 byte)
54 how many bytes to send at most (unsigned int),
54 how many bytes to send at most (unsigned int),
55
55
56 The client replies with:
56 The client replies with:
57 data length (unsigned int), 0 meaning EOF
57 data length (unsigned int), 0 meaning EOF
58 data
58 data
59 """
59 """
60
60
61 maxchunksize = 4 * 1024
61 maxchunksize = 4 * 1024
62
62
63 def __init__(self, in_, out, channel):
63 def __init__(self, in_, out, channel):
64 self.in_ = in_
64 self.in_ = in_
65 self.out = out
65 self.out = out
66 self.channel = channel
66 self.channel = channel
67
67
68 def read(self, size=-1):
68 def read(self, size=-1):
69 if size < 0:
69 if size < 0:
70 # if we need to consume all the clients input, ask for 4k chunks
70 # if we need to consume all the clients input, ask for 4k chunks
71 # so the pipe doesn't fill up risking a deadlock
71 # so the pipe doesn't fill up risking a deadlock
72 size = self.maxchunksize
72 size = self.maxchunksize
73 s = self._read(size, self.channel)
73 s = self._read(size, self.channel)
74 buf = s
74 buf = s
75 while s:
75 while s:
76 s = self._read(size, self.channel)
76 s = self._read(size, self.channel)
77 buf += s
77 buf += s
78
78
79 return buf
79 return buf
80 else:
80 else:
81 return self._read(size, self.channel)
81 return self._read(size, self.channel)
82
82
83 def _read(self, size, channel):
83 def _read(self, size, channel):
84 if not size:
84 if not size:
85 return ''
85 return ''
86 assert size > 0
86 assert size > 0
87
87
88 # tell the client we need at most size bytes
88 # tell the client we need at most size bytes
89 self.out.write(struct.pack('>cI', channel, size))
89 self.out.write(struct.pack('>cI', channel, size))
90 self.out.flush()
90 self.out.flush()
91
91
92 length = self.in_.read(4)
92 length = self.in_.read(4)
93 length = struct.unpack('>I', length)[0]
93 length = struct.unpack('>I', length)[0]
94 if not length:
94 if not length:
95 return ''
95 return ''
96 else:
96 else:
97 return self.in_.read(length)
97 return self.in_.read(length)
98
98
99 def readline(self, size=-1):
99 def readline(self, size=-1):
100 if size < 0:
100 if size < 0:
101 size = self.maxchunksize
101 size = self.maxchunksize
102 s = self._read(size, 'L')
102 s = self._read(size, 'L')
103 buf = s
103 buf = s
104 # keep asking for more until there's either no more or
104 # keep asking for more until there's either no more or
105 # we got a full line
105 # we got a full line
106 while s and s[-1] != '\n':
106 while s and s[-1] != '\n':
107 s = self._read(size, 'L')
107 s = self._read(size, 'L')
108 buf += s
108 buf += s
109
109
110 return buf
110 return buf
111 else:
111 else:
112 return self._read(size, 'L')
112 return self._read(size, 'L')
113
113
114 def __iter__(self):
114 def __iter__(self):
115 return self
115 return self
116
116
117 def next(self):
117 def next(self):
118 l = self.readline()
118 l = self.readline()
119 if not l:
119 if not l:
120 raise StopIteration
120 raise StopIteration
121 return l
121 return l
122
122
123 def __getattr__(self, attr):
123 def __getattr__(self, attr):
124 if attr in ('isatty', 'fileno'):
124 if attr in ('isatty', 'fileno'):
125 raise AttributeError, attr
125 raise AttributeError, attr
126 return getattr(self.in_, attr)
126 return getattr(self.in_, attr)
127
127
128 class server(object):
128 class server(object):
129 """
129 """
130 Listens for commands on stdin, runs them and writes the output on a channel
130 Listens for commands on stdin, runs them and writes the output on a channel
131 based stream to stdout.
131 based stream to stdout.
132 """
132 """
133 def __init__(self, ui, repo, mode):
133 def __init__(self, ui, repo, mode):
134 self.ui = ui
134 self.ui = ui
135
135
136 logpath = ui.config("cmdserver", "log", None)
136 logpath = ui.config("cmdserver", "log", None)
137 if logpath:
137 if logpath:
138 global logfile
138 global logfile
139 if logpath == '-':
139 if logpath == '-':
140 # write log on a special 'd'ebug channel
140 # write log on a special 'd'ebug channel
141 logfile = channeledoutput(sys.stdout, sys.stdout, 'd')
141 logfile = channeledoutput(sys.stdout, sys.stdout, 'd')
142 else:
142 else:
143 logfile = open(logpath, 'a')
143 logfile = open(logpath, 'a')
144
144
145 self.repo = repo
145 self.repo = repo
146 self.repoui = repo.ui
146
147
147 if mode == 'pipe':
148 if mode == 'pipe':
148 self.cerr = channeledoutput(sys.stderr, sys.stdout, 'e')
149 self.cerr = channeledoutput(sys.stderr, sys.stdout, 'e')
149 self.cout = channeledoutput(sys.stdout, sys.stdout, 'o')
150 self.cout = channeledoutput(sys.stdout, sys.stdout, 'o')
150 self.cin = channeledinput(sys.stdin, sys.stdout, 'I')
151 self.cin = channeledinput(sys.stdin, sys.stdout, 'I')
151 self.cresult = channeledoutput(sys.stdout, sys.stdout, 'r')
152 self.cresult = channeledoutput(sys.stdout, sys.stdout, 'r')
152
153
153 self.client = sys.stdin
154 self.client = sys.stdin
154 else:
155 else:
155 raise util.Abort(_('unknown mode %s') % mode)
156 raise util.Abort(_('unknown mode %s') % mode)
156
157
157 def _read(self, size):
158 def _read(self, size):
158 if not size:
159 if not size:
159 return ''
160 return ''
160
161
161 data = self.client.read(size)
162 data = self.client.read(size)
162
163
163 # is the other end closed?
164 # is the other end closed?
164 if not data:
165 if not data:
165 raise EOFError()
166 raise EOFError()
166
167
167 return data
168 return data
168
169
169 def runcommand(self):
170 def runcommand(self):
170 """ reads a list of \0 terminated arguments, executes
171 """ reads a list of \0 terminated arguments, executes
171 and writes the return code to the result channel """
172 and writes the return code to the result channel """
172
173
173 length = struct.unpack('>I', self._read(4))[0]
174 length = struct.unpack('>I', self._read(4))[0]
174 if not length:
175 if not length:
175 args = []
176 args = []
176 else:
177 else:
177 args = self._read(length).split('\0')
178 args = self._read(length).split('\0')
178
179
179 # copy the ui so changes to it don't persist between requests
180 # copy the uis so changes (e.g. --config or --verbose) don't
181 # persist between requests
182 self.repo.ui = self.repo.dirstate._ui = self.repoui.copy()
180 req = dispatch.request(args, self.ui.copy(), self.repo, self.cin,
183 req = dispatch.request(args, self.ui.copy(), self.repo, self.cin,
181 self.cout, self.cerr)
184 self.cout, self.cerr)
182
185
183 ret = dispatch.dispatch(req) or 0 # might return None
186 ret = dispatch.dispatch(req) or 0 # might return None
184
187
185 self.cresult.write(struct.pack('>i', int(ret)))
188 self.cresult.write(struct.pack('>i', int(ret)))
186
189
187 def getencoding(self):
190 def getencoding(self):
188 """ writes the current encoding to the result channel """
191 """ writes the current encoding to the result channel """
189 self.cresult.write(encoding.encoding)
192 self.cresult.write(encoding.encoding)
190
193
191 def serveone(self):
194 def serveone(self):
192 cmd = self.client.readline()[:-1]
195 cmd = self.client.readline()[:-1]
193 if cmd:
196 if cmd:
194 handler = self.capabilities.get(cmd)
197 handler = self.capabilities.get(cmd)
195 if handler:
198 if handler:
196 handler(self)
199 handler(self)
197 else:
200 else:
198 # clients are expected to check what commands are supported by
201 # clients are expected to check what commands are supported by
199 # looking at the servers capabilities
202 # looking at the servers capabilities
200 raise util.Abort(_('unknown command %s') % cmd)
203 raise util.Abort(_('unknown command %s') % cmd)
201
204
202 return cmd != ''
205 return cmd != ''
203
206
204 capabilities = {'runcommand' : runcommand,
207 capabilities = {'runcommand' : runcommand,
205 'getencoding' : getencoding}
208 'getencoding' : getencoding}
206
209
207 def serve(self):
210 def serve(self):
208 hellomsg = 'capabilities: ' + ' '.join(self.capabilities.keys())
211 hellomsg = 'capabilities: ' + ' '.join(self.capabilities.keys())
209 hellomsg += '\n'
212 hellomsg += '\n'
210 hellomsg += 'encoding: ' + encoding.encoding
213 hellomsg += 'encoding: ' + encoding.encoding
211
214
212 # write the hello msg in -one- chunk
215 # write the hello msg in -one- chunk
213 self.cout.write(hellomsg)
216 self.cout.write(hellomsg)
214
217
215 try:
218 try:
216 while self.serveone():
219 while self.serveone():
217 pass
220 pass
218 except EOFError:
221 except EOFError:
219 # we'll get here if the client disconnected while we were reading
222 # we'll get here if the client disconnected while we were reading
220 # its request
223 # its request
221 return 1
224 return 1
222
225
223 return 0
226 return 0
General Comments 0
You need to be logged in to leave comments. Login now