##// END OF EJS Templates
convert: abstract darcs's commandline handling
Bryan O'Sullivan -
r5512:8cd26ccc default
parent child Browse files
Show More
@@ -188,6 +188,68 b' class converter_sink(object):'
188 """
188 """
189 pass
189 pass
190
190
191 def before(self):
192 pass
193
194 def after(self):
195 pass
196
197
198 class commandline(object):
199 def __init__(self, ui, command):
200 self.ui = ui
201 self.command = command
202
203 def prerun(self):
204 pass
205
206 def postrun(self):
207 pass
208
209 def _run(self, cmd, *args, **kwargs):
210 cmdline = [self.command, cmd] + list(args)
211 for k, v in kwargs.iteritems():
212 if len(k) == 1:
213 cmdline.append('-' + k)
214 else:
215 cmdline.append('--' + k.replace('_', '-'))
216 try:
217 if len(k) == 1:
218 cmdline.append('' + v)
219 else:
220 cmdline[-1] += '=' + v
221 except TypeError:
222 pass
223 cmdline = [util.shellquote(arg) for arg in cmdline]
224 cmdline += ['<', util.nulldev]
225 cmdline = util.quotecommand(' '.join(cmdline))
226 self.ui.debug(cmdline, '\n')
227
228 self.prerun()
229 try:
230 return util.popen(cmdline)
231 finally:
232 self.postrun()
233
234 def run(self, cmd, *args, **kwargs):
235 fp = self._run(cmd, *args, **kwargs)
236 output = fp.read()
237 self.ui.debug(output)
238 return output, fp.close()
239
240 def checkexit(self, status, output=''):
241 if status:
242 if output:
243 self.ui.warn(_('%s error:\n') % self.command)
244 self.ui.warn(output)
245 msg = util.explain_exit(status)[0]
246 raise util.Abort(_('%s %s') % (self.command, msg))
247
248 def run0(self, cmd, *args, **kwargs):
249 output, status = self.run(cmd, *args, **kwargs)
250 self.checkexit(status, output)
251 return output
252
191
253
192 class mapfile(dict):
254 class mapfile(dict):
193 def __init__(self, ui, path):
255 def __init__(self, ui, path):
@@ -224,4 +286,6 b' class mapfile(dict):'
224 super(mapfile, self).__setitem__(key, value)
286 super(mapfile, self).__setitem__(key, value)
225
287
226 def close(self):
288 def close(self):
227 self.fp.close()
289 if self.fp:
290 self.fp.close()
291 self.fp = None
@@ -1,6 +1,6 b''
1 # darcs support for the convert extension
1 # darcs support for the convert extension
2
2
3 from common import NoRepo, commit, converter_source, checktool
3 from common import NoRepo, checktool, commandline, commit, converter_source
4 from mercurial.i18n import _
4 from mercurial.i18n import _
5 from mercurial import util
5 from mercurial import util
6 import os, shutil, tempfile
6 import os, shutil, tempfile
@@ -17,9 +17,10 b' except ImportError:'
17 except ImportError: ElementTree = None
17 except ImportError: ElementTree = None
18
18
19
19
20 class darcs_source(converter_source):
20 class darcs_source(converter_source, commandline):
21 def __init__(self, ui, path, rev=None):
21 def __init__(self, ui, path, rev=None):
22 super(darcs_source, self).__init__(ui, path, rev=rev)
22 converter_source.__init__(self, ui, path, rev=rev)
23 commandline.__init__(self, ui, 'darcs')
23
24
24 if not os.path.exists(os.path.join(path, '_darcs', 'inventory')):
25 if not os.path.exists(os.path.join(path, '_darcs', 'inventory')):
25 raise NoRepo("couldn't open darcs repo %s" % path)
26 raise NoRepo("couldn't open darcs repo %s" % path)
@@ -42,7 +43,8 b' class darcs_source(converter_source):'
42 output, status = self.run('init', repodir=self.tmppath)
43 output, status = self.run('init', repodir=self.tmppath)
43 self.checkexit(status)
44 self.checkexit(status)
44
45
45 tree = self.xml('changes', '--xml-output', '--summary')
46 tree = self.xml('changes', xml_output=True, summary=True,
47 repodir=self.path)
46 tagname = None
48 tagname = None
47 child = None
49 child = None
48 for elt in tree.findall('patch'):
50 for elt in tree.findall('patch'):
@@ -62,31 +64,9 b' class darcs_source(converter_source):'
62 self.ui.debug('cleaning up %s\n' % self.tmppath)
64 self.ui.debug('cleaning up %s\n' % self.tmppath)
63 shutil.rmtree(self.tmppath, ignore_errors=True)
65 shutil.rmtree(self.tmppath, ignore_errors=True)
64
66
65 def _run(self, cmd, *args, **kwargs):
67 def xml(self, cmd, **kwargs):
66 cmdline = ['darcs', cmd, '--repodir', kwargs.get('repodir', self.path)]
67 cmdline += args
68 cmdline = [util.shellquote(arg) for arg in cmdline]
69 cmdline += ['<', util.nulldev]
70 cmdline = ' '.join(cmdline)
71 self.ui.debug(cmdline, '\n')
72 return util.popen(cmdline)
73
74 def run(self, cmd, *args, **kwargs):
75 fp = self._run(cmd, *args, **kwargs)
76 output = fp.read()
77 return output, fp.close()
78
79 def checkexit(self, status, output=''):
80 if status:
81 if output:
82 self.ui.warn(_('darcs error:\n'))
83 self.ui.warn(output)
84 msg = util.explain_exit(status)[0]
85 raise util.Abort(_('darcs %s') % msg)
86
87 def xml(self, cmd, *opts):
88 etree = ElementTree()
68 etree = ElementTree()
89 fp = self._run(cmd, *opts)
69 fp = self._run(cmd, **kwargs)
90 etree.parse(fp)
70 etree.parse(fp)
91 self.checkexit(fp.close())
71 self.checkexit(fp.close())
92 return etree.getroot()
72 return etree.getroot()
@@ -102,15 +82,15 b' class darcs_source(converter_source):'
102 desc=desc.strip(), parents=self.parents[rev])
82 desc=desc.strip(), parents=self.parents[rev])
103
83
104 def pull(self, rev):
84 def pull(self, rev):
105 output, status = self.run('pull', self.path, '--all',
85 output, status = self.run('pull', self.path, all=True,
106 '--match', 'hash %s' % rev,
86 match='hash %s' % rev,
107 '--no-test', '--no-posthook',
87 no_test=True, no_posthook=True,
108 '--external-merge', '/bin/false',
88 external_merge='/bin/false',
109 repodir=self.tmppath)
89 repodir=self.tmppath)
110 if status:
90 if status:
111 if output.find('We have conflicts in') == -1:
91 if output.find('We have conflicts in') == -1:
112 self.checkexit(status, output)
92 self.checkexit(status, output)
113 output, status = self.run('revert', '--all', repodir=self.tmppath)
93 output, status = self.run('revert', all=True, repodir=self.tmppath)
114 self.checkexit(status, output)
94 self.checkexit(status, output)
115
95
116 def getchanges(self, rev):
96 def getchanges(self, rev):
General Comments 0
You need to be logged in to leave comments. Login now