comparison hglib/client.py @ 91:0383fc37102b

client: refactor opening of the cmd server to an open method so we can create instances of hgclient without automatically opening a command server (needed for clone/init).
author Idan Kamara <idankk86@gmail.com>
date Thu, 22 Dec 2011 19:12:47 +0200
parents b894c2222dff
children bd23bc72e662
comparison
equal deleted inserted replaced
90:b894c2222dff 91:0383fc37102b
35 inputfmt = '>I' 35 inputfmt = '>I'
36 outputfmt = '>cI' 36 outputfmt = '>cI'
37 outputfmtsize = struct.calcsize(outputfmt) 37 outputfmtsize = struct.calcsize(outputfmt)
38 retfmt = '>i' 38 retfmt = '>i'
39 39
40 def __init__(self, path, encoding, configs): 40 def __init__(self, path, encoding, configs, connect=True):
41 args = [hglib.HGPATH, 'serve', '--cmdserver', 'pipe', 41 self._args = [hglib.HGPATH, 'serve', '--cmdserver', 'pipe',
42 '--config', 'ui.interactive=True'] 42 '--config', 'ui.interactive=True']
43 if path: 43 if path:
44 args += ['-R', path] 44 self._args += ['-R', path]
45 if configs: 45 if configs:
46 args += ['--config'] + configs 46 self._args += ['--config'] + configs
47 env = {} 47 self._env = {}
48 if encoding: 48 if encoding:
49 env['HGENCODING'] = encoding 49 self._env['HGENCODING'] = encoding
50 50
51 self.server = util.popen(args, env) 51 self.server = None
52 self._readhello()
53 self._version = None 52 self._version = None
53
54 if connect:
55 self.open()
54 56
55 def __enter__(self): 57 def __enter__(self):
56 return self 58 return self
57 59
58 def __exit__(self, exc_type, exc_val, exc_tb): 60 def __exit__(self, exc_type, exc_val, exc_tb):
161 if eh is None: 163 if eh is None:
162 raise error.CommandError(args, ret, out, err) 164 raise error.CommandError(args, ret, out, err)
163 else: 165 else:
164 return eh(ret, out, err) 166 return eh(ret, out, err)
165 return out 167 return out
168
169 def open(self):
170 if self.server is not None:
171 raise ValueError('server already open')
172
173 self.server = util.popen(self._args, self._env)
174 self._readhello()
175 return self
166 176
167 def close(self): 177 def close(self):
168 """ 178 """
169 Closes the command server instance and waits for it to exit, returns the 179 Closes the command server instance and waits for it to exit, returns the
170 exit code. 180 exit code.