hgext/convert/common.py
branchstable
changeset 17413 97f1f22c2dba
parent 17412 e169b11fa1e0
child 17974 337d728e644f
equal deleted inserted replaced
17412:e169b11fa1e0 17413:97f1f22c2dba
     3 #  Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
     3 #  Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
     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 import base64, errno
     8 import base64, errno, subprocess, os
     9 import os
       
    10 import cPickle as pickle
     9 import cPickle as pickle
    11 from mercurial import util
    10 from mercurial import util
    12 from mercurial.i18n import _
    11 from mercurial.i18n import _
    13 
    12 
    14 propertycache = util.propertycache
    13 propertycache = util.propertycache
   258         pass
   257         pass
   259 
   258 
   260     def postrun(self):
   259     def postrun(self):
   261         pass
   260         pass
   262 
   261 
   263     def _cmdline(self, cmd, closestdin, *args, **kwargs):
   262     def _cmdline(self, cmd, *args, **kwargs):
   264         cmdline = [self.command, cmd] + list(args)
   263         cmdline = [self.command, cmd] + list(args)
   265         for k, v in kwargs.iteritems():
   264         for k, v in kwargs.iteritems():
   266             if len(k) == 1:
   265             if len(k) == 1:
   267                 cmdline.append('-' + k)
   266                 cmdline.append('-' + k)
   268             else:
   267             else:
   275             except TypeError:
   274             except TypeError:
   276                 pass
   275                 pass
   277         cmdline = [util.shellquote(arg) for arg in cmdline]
   276         cmdline = [util.shellquote(arg) for arg in cmdline]
   278         if not self.ui.debugflag:
   277         if not self.ui.debugflag:
   279             cmdline += ['2>', os.devnull]
   278             cmdline += ['2>', os.devnull]
   280         if closestdin:
       
   281             cmdline += ['<', os.devnull]
       
   282         cmdline = ' '.join(cmdline)
   279         cmdline = ' '.join(cmdline)
   283         return cmdline
   280         return cmdline
   284 
   281 
   285     def _run(self, cmd, *args, **kwargs):
   282     def _run(self, cmd, *args, **kwargs):
   286         return self._dorun(util.popen, cmd, True, *args, **kwargs)
   283         def popen(cmdline):
       
   284             p = subprocess.Popen(cmdline, shell=True, bufsize=-1,
       
   285                     close_fds=util.closefds,
       
   286                     stdout=subprocess.PIPE)
       
   287             return p
       
   288         return self._dorun(popen, cmd, *args, **kwargs)
   287 
   289 
   288     def _run2(self, cmd, *args, **kwargs):
   290     def _run2(self, cmd, *args, **kwargs):
   289         return self._dorun(util.popen2, cmd, False, *args, **kwargs)
   291         return self._dorun(util.popen2, cmd, *args, **kwargs)
   290 
   292 
   291     def _dorun(self, openfunc, cmd, closestdin, *args, **kwargs):
   293     def _dorun(self, openfunc, cmd,  *args, **kwargs):
   292         cmdline = self._cmdline(cmd, closestdin, *args, **kwargs)
   294         cmdline = self._cmdline(cmd, *args, **kwargs)
   293         self.ui.debug('running: %s\n' % (cmdline,))
   295         self.ui.debug('running: %s\n' % (cmdline,))
   294         self.prerun()
   296         self.prerun()
   295         try:
   297         try:
   296             return openfunc(cmdline)
   298             return openfunc(cmdline)
   297         finally:
   299         finally:
   298             self.postrun()
   300             self.postrun()
   299 
   301 
   300     def run(self, cmd, *args, **kwargs):
   302     def run(self, cmd, *args, **kwargs):
   301         fp = self._run(cmd, *args, **kwargs)
   303         p = self._run(cmd, *args, **kwargs)
   302         output = fp.read()
   304         output = p.communicate()[0]
   303         self.ui.debug(output)
   305         self.ui.debug(output)
   304         return output, fp.close()
   306         return output, p.returncode
   305 
   307 
   306     def runlines(self, cmd, *args, **kwargs):
   308     def runlines(self, cmd, *args, **kwargs):
   307         fp = self._run(cmd, *args, **kwargs)
   309         p = self._run(cmd, *args, **kwargs)
   308         output = fp.readlines()
   310         output = p.stdout.readlines()
       
   311         p.wait()
   309         self.ui.debug(''.join(output))
   312         self.ui.debug(''.join(output))
   310         return output, fp.close()
   313         return output, p.returncode
   311 
   314 
   312     def checkexit(self, status, output=''):
   315     def checkexit(self, status, output=''):
   313         if status:
   316         if status:
   314             if output:
   317             if output:
   315                 self.ui.warn(_('%s error:\n') % self.command)
   318                 self.ui.warn(_('%s error:\n') % self.command)