hgext/convert/common.py
changeset 5512 8cd26ccc68f8
parent 5510 11d7908a3ea8
child 5513 f0c58fd4b798
equal deleted inserted replaced
5511:03bb1296a1c3 5512:8cd26ccc68f8
   185         was changed in a revision, even if there was no change.  This method
   185         was changed in a revision, even if there was no change.  This method
   186         tells the destination that we're using a filemap and that it should
   186         tells the destination that we're using a filemap and that it should
   187         filter empty revisions.
   187         filter empty revisions.
   188         """
   188         """
   189         pass
   189         pass
       
   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
   190 
   252 
   191 
   253 
   192 class mapfile(dict):
   254 class mapfile(dict):
   193     def __init__(self, ui, path):
   255     def __init__(self, ui, path):
   194         super(mapfile, self).__init__()
   256         super(mapfile, self).__init__()
   222         self.fp.write('%s %s\n' % (key, value))
   284         self.fp.write('%s %s\n' % (key, value))
   223         self.fp.flush()
   285         self.fp.flush()
   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