Mercurial > public > mercurial-scm > python-hglib
comparison hglib/client.py @ 4:a3a9cf58801f
client: use the cmdbuilder
author | Idan Kamara <idankk86@gmail.com> |
---|---|
date | Tue, 09 Aug 2011 00:39:26 +0300 |
parents | 5fa34c3ac9a0 |
children | 3182303f388d |
comparison
equal
deleted
inserted
replaced
3:d7903b923217 | 4:a3a9cf58801f |
---|---|
1 import subprocess, os, struct, cStringIO, collections | 1 import subprocess, os, struct, cStringIO, collections |
2 import hglib, error, util | 2 import hglib, error, util |
3 | |
4 from util import cmdbuilder | |
3 | 5 |
4 class hgclient(object): | 6 class hgclient(object): |
5 inputfmt = '>I' | 7 inputfmt = '>I' |
6 outputfmt = '>cI' | 8 outputfmt = '>cI' |
7 outputfmtsize = struct.calcsize(outputfmt) | 9 outputfmtsize = struct.calcsize(outputfmt) |
8 retfmt = '>i' | 10 retfmt = '>i' |
9 | 11 |
10 # XXX fix this hack | 12 # XXX fix this hack |
11 _stylesdir = os.path.join(os.path.dirname(__file__), 'styles') | 13 _stylesdir = os.path.join(os.path.dirname(__file__), 'styles') |
12 revstyle = ['--style', os.path.join(_stylesdir, 'rev.style')] | 14 revstyle = os.path.join(_stylesdir, 'rev.style') |
13 | 15 |
14 revision = collections.namedtuple('revision', 'rev, node, tags, ' | 16 revision = collections.namedtuple('revision', 'rev, node, tags, ' |
15 'branch, author, desc') | 17 'branch, author, desc') |
16 | 18 |
17 def __init__(self, path, encoding, configs): | 19 def __init__(self, path, encoding, configs): |
160 d[t].append(f) | 162 d[t].append(f) |
161 | 163 |
162 return d | 164 return d |
163 | 165 |
164 def log(self, revrange=None): | 166 def log(self, revrange=None): |
165 args = ['log'] + self.revstyle | 167 args = cmdbuilder('log', style=hgclient.revstyle, rev=revrange) |
166 if revrange: | |
167 args.append('-r') | |
168 args += revrange | |
169 | 168 |
170 out = self.outputruncommand(args)[1] | 169 out = self.outputruncommand(args)[1] |
171 out = out.split('\0')[:-1] | 170 out = out.split('\0')[:-1] |
172 | 171 |
173 return self._parserevs(out) | 172 return self._parserevs(out) |
174 | 173 |
175 def incoming(self, revrange=None, path=None): | 174 def incoming(self, revrange=None, path=None): |
176 args = ['incoming'] + self.revstyle | 175 args = cmdbuilder('incoming', |
177 if revrange: | 176 path, |
178 args.append('-r') | 177 style=hgclient.revstyle, rev=revrange) |
179 args += revrange | |
180 | |
181 if path: | |
182 args += [path] | |
183 | 178 |
184 ret, out, err = self.outputruncommand(args, raiseonerror=False) | 179 ret, out, err = self.outputruncommand(args, raiseonerror=False) |
185 if not ret: | 180 if not ret: |
186 out = self._eatlines(out, 2).split('\0')[:-1] | 181 out = self._eatlines(out, 2).split('\0')[:-1] |
187 return self._parserevs(out) | 182 return self._parserevs(out) |
189 return [] | 184 return [] |
190 else: | 185 else: |
191 raise error.CommandError(args, ret, out, err) | 186 raise error.CommandError(args, ret, out, err) |
192 | 187 |
193 def outgoing(self, revrange=None, path=None): | 188 def outgoing(self, revrange=None, path=None): |
194 args = ['outgoing'] + self.revstyle | 189 args = cmdbuilder('outgoing', |
195 if revrange: | 190 path, style=hgclient.revstyle, rev=revrange) |
196 args.append('-r') | |
197 args += revrange | |
198 | |
199 if path: | |
200 args += [path] | |
201 | 191 |
202 ret, out, err = self.outputruncommand(args, raiseonerror=False) | 192 ret, out, err = self.outputruncommand(args, raiseonerror=False) |
203 if not ret: | 193 if not ret: |
204 out = self._eatlines(out, 2).split('\0')[:-1] | 194 out = self._eatlines(out, 2).split('\0')[:-1] |
205 return self._parserevs(out) | 195 return self._parserevs(out) |
207 return [] | 197 return [] |
208 else: | 198 else: |
209 raise error.CommandError(args, ret, out, err) | 199 raise error.CommandError(args, ret, out, err) |
210 | 200 |
211 def commit(self, message, addremove=False): | 201 def commit(self, message, addremove=False): |
212 args = ['commit', '-m', message] | 202 args = cmdbuilder('commit', m=message, A=addremove) |
213 | |
214 if addremove: | |
215 args += ['-A'] | |
216 | 203 |
217 self.outputruncommand(args) | 204 self.outputruncommand(args) |
218 | 205 |
219 # hope the tip hasn't changed since we committed | 206 # hope the tip hasn't changed since we committed |
220 return self.tip() | 207 return self.tip() |
228 | 215 |
229 fp = patch | 216 fp = patch |
230 | 217 |
231 try: | 218 try: |
232 inchannels = {'I' : fp.read, 'L' : fp.readline} | 219 inchannels = {'I' : fp.read, 'L' : fp.readline} |
233 self.outputruncommand(['import', '-'], inchannels) | 220 self.outputruncommand(cmdbuilder('import', _=True), inchannels) |
234 finally: | 221 finally: |
235 if fp != patch: | 222 if fp != patch: |
236 fp.close() | 223 fp.close() |
237 | 224 |
238 def root(self): | 225 def root(self): |
239 return self.outputruncommand(['root'])[1].rstrip() | 226 return self.outputruncommand(['root'])[1].rstrip() |
240 | 227 |
241 def clone(self, source='.', dest=None, branch=None, updaterev=None, | 228 def clone(self, source='.', dest=None, branch=None, updaterev=None, |
242 revrange=None): | 229 revrange=None): |
243 args = ['clone'] | 230 args = cmdbuilder('clone', source, dest, b=branch, u=updaterev, r=revrange) |
244 | |
245 if branch: | |
246 args += ['-b', branch] | |
247 if updaterev: | |
248 args += ['-u', updaterev] | |
249 if revrange: | |
250 args.append('-r') | |
251 args += revrange | |
252 args.append(source) | |
253 | |
254 if dest: | |
255 args.append(dest) | |
256 | |
257 self.outputruncommand(args) | 231 self.outputruncommand(args) |
258 | 232 |
259 def tip(self): | 233 def tip(self): |
260 out = self.outputruncommand(['tip'] + self.revstyle)[1] | 234 args = cmdbuilder('tip', style=hgclient.revstyle) |
235 out = self.outputruncommand(args)[1] | |
261 out = out.split('\0') | 236 out = out.split('\0') |
262 | 237 |
263 return self._parserevs(out)[0] | 238 return self._parserevs(out)[0] |
264 | 239 |
265 def branch(self, name=None): | 240 def branch(self, name=None): |
281 if not out: | 256 if not out: |
282 return {} | 257 return {} |
283 | 258 |
284 return dict([s.split(' = ') for s in out.rstrip().split('\n')]) | 259 return dict([s.split(' = ') for s in out.rstrip().split('\n')]) |
285 else: | 260 else: |
286 args = ['paths', name] | 261 args = cmdbuilder('paths', name) |
287 ret, out, err = self.outputruncommand(args, raiseonerror=False) | 262 ret, out, err = self.outputruncommand(args, raiseonerror=False) |
288 if ret: | 263 if ret: |
289 raise error.CommandError(args, ret, out, err) | 264 raise error.CommandError(args, ret, out, err) |
290 return out.rstrip() | 265 return out.rstrip() |
291 | 266 |
292 def cat(self, files, rev=None, output=None): | 267 def cat(self, files, rev=None, output=None): |
293 args = ['cat'] | 268 args = cmdbuilder('cat', *files, r=rev, o=output) |
294 if rev: | |
295 args += ['-r', rev] | |
296 if output: | |
297 args += ['-o', output] | |
298 | |
299 args += files | |
300 ret, out, err = self.outputruncommand(args) | 269 ret, out, err = self.outputruncommand(args) |
301 | 270 |
302 if not output: | 271 if not output: |
303 return out | 272 return out |