示例#1
0
def fixfile(ui, opts, fixers, fixctx, path, basectxs):
    """Run any configured fixers that should affect the file in this context

    Returns the file content that results from applying the fixers in some order
    starting with the file's content in the fixctx. Fixers that support line
    ranges will affect lines that have changed relative to any of the basectxs
    (i.e. they will only avoid lines that are common to all basectxs).

    A fixer tool's stdout will become the file's new content if and only if it
    exits with code zero.
    """
    newdata = fixctx[path].data()
    for fixername, fixer in fixers.iteritems():
        if fixer.affects(opts, fixctx, path):
            rangesfn = lambda: lineranges(opts, path, basectxs, fixctx, newdata)
            command = fixer.command(ui, path, rangesfn)
            if command is None:
                continue
            ui.debug('subprocess: %s\n' % (command,))
            proc = subprocess.Popen(
                procutil.tonativestr(command),
                shell=True,
                cwd=procutil.tonativestr(b'/'),
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
            newerdata, stderr = proc.communicate(newdata)
            if stderr:
                showstderr(ui, fixctx.rev(), fixername, stderr)
            if proc.returncode == 0:
                newdata = newerdata
            elif not stderr:
                showstderr(ui, fixctx.rev(), fixername,
                           _('exited with status %d\n') % (proc.returncode,))
    return newdata
示例#2
0
文件: fix.py 项目: CJX32/my_blog
def fixfile(ui, repo, opts, fixers, fixctx, path, basectxs):
    """Run any configured fixers that should affect the file in this context

    Returns the file content that results from applying the fixers in some order
    starting with the file's content in the fixctx. Fixers that support line
    ranges will affect lines that have changed relative to any of the basectxs
    (i.e. they will only avoid lines that are common to all basectxs).

    A fixer tool's stdout will become the file's new content if and only if it
    exits with code zero. The fixer tool's working directory is the repository's
    root.
    """
    metadata = {}
    newdata = fixctx[path].data()
    for fixername, fixer in pycompat.iteritems(fixers):
        if fixer.affects(opts, fixctx, path):
            ranges = lineranges(opts, path, basectxs, fixctx, newdata)
            command = fixer.command(ui, path, ranges)
            if command is None:
                continue
            ui.debug(b'subprocess: %s\n' % (command, ))
            proc = subprocess.Popen(
                procutil.tonativestr(command),
                shell=True,
                cwd=procutil.tonativestr(repo.root),
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            )
            stdout, stderr = proc.communicate(newdata)
            if stderr:
                showstderr(ui, fixctx.rev(), fixername, stderr)
            newerdata = stdout
            if fixer.shouldoutputmetadata():
                try:
                    metadatajson, newerdata = stdout.split(b'\0', 1)
                    metadata[fixername] = pycompat.json_loads(metadatajson)
                except ValueError:
                    ui.warn(
                        _(b'ignored invalid output from fixer tool: %s\n') %
                        (fixername, ))
                    continue
            else:
                metadata[fixername] = None
            if proc.returncode == 0:
                newdata = newerdata
            else:
                if not stderr:
                    message = _(b'exited with status %d\n') % (
                        proc.returncode, )
                    showstderr(ui, fixctx.rev(), fixername, message)
                checktoolfailureaction(
                    ui,
                    _(b'no fixes will be applied'),
                    hint=_(b'use --config fix.failure=continue to apply any '
                           b'successful fixes anyway'),
                )
    return metadata, newdata
示例#3
0
 def runshellcommand(script, env):
     # double-fork to completely detach from the parent process
     # based on http://code.activestate.com/recipes/278731
     pid = os.fork()
     if pid:
         # parent
         return
     # subprocess.Popen() forks again, all we need to add is
     # flag the new process as a new session.
     if sys.version_info < (3, 2):
         newsession = {'preexec_fn': os.setsid}
     else:
         newsession = {'start_new_session': True}
     try:
         # connect std* to devnull to make sure the subprocess can't
         # muck up these stream for mercurial.
         # Connect all the streams to be more close to Windows behavior
         # and pager will wait for scripts to end if we don't do that
         nullrfd = open(os.devnull, 'r')
         nullwfd = open(os.devnull, 'w')
         subprocess.Popen(procutil.tonativestr(script),
                          shell=True,
                          stdin=nullrfd,
                          stdout=nullwfd,
                          stderr=nullwfd,
                          env=procutil.tonativeenv(env),
                          close_fds=True,
                          **newsession)
     finally:
         # mission accomplished, this child needs to exit and not
         # continue the hg process here.
         os._exit(0)
示例#4
0
 def popen(cmdline):
     p = subprocess.Popen(procutil.tonativestr(cmdline),
                          shell=True,
                          bufsize=-1,
                          close_fds=procutil.closefds,
                          stdout=subprocess.PIPE)
     return p
示例#5
0
def auth_getkey(self, params):
    if not self.ui.interactive():
        raise error.Abort(_('factotum not interactive'))
    if 'user='******'%s user?' % params
    params = '%s !password?' % params
    os.system(procutil.tonativestr("%s -g '%s'" % (_executable, params)))
示例#6
0
 def runshellcommand(script, env):
     # we can't use close_fds *and* redirect stdin. I'm not sure that we
     # need to because the detached process has no console connection.
     subprocess.Popen(procutil.tonativestr(script),
                      shell=True,
                      env=procutil.tonativeenv(env),
                      close_fds=True,
                      creationflags=_creationflags)
示例#7
0
def _systembackground(cmd, environ=None, cwd=None):
    ''' like 'procutil.system', but returns the Popen object directly
        so we don't have to wait on it.
    '''
    cmd = procutil.quotecommand(cmd)
    env = procutil.shellenviron(environ)
    proc = subprocess.Popen(procutil.tonativestr(cmd),
                            shell=True,
                            close_fds=procutil.closefds,
                            env=procutil.tonativeenv(env),
                            cwd=pycompat.rapply(procutil.tonativestr, cwd))
    return proc
 def _command(self, *args):
     watchmanargs = (args[0], self._root) + args[1:]
     try:
         if self._watchmanclient is None:
             self._firsttime = False
             watchman_exe = self._ui.configpath(b'fsmonitor',
                                                b'watchman_exe')
             self._watchmanclient = pywatchman.client(
                 timeout=self._timeout,
                 useImmutableBser=True,
                 binpath=procutil.tonativestr(watchman_exe),
             )
         return self._watchmanclient.query(*watchmanargs)
     except pywatchman.CommandError as ex:
         if 'unable to resolve root' in ex.msg:
             raise WatchmanNoRoot(self._root,
                                  stringutil.forcebytestr(ex.msg))
         raise Unavailable(stringutil.forcebytestr(ex.msg))
     except pywatchman.WatchmanError as ex:
         raise Unavailable(stringutil.forcebytestr(ex))
示例#9
0
文件: _ssh.py 项目: danchr/hg-git
 def run_command(self, host, command, username=None, port=None):
     assert isinstance(command, str)
     command = command.encode(SSHGitClient.DEFAULT_ENCODING)
     sshcmd = ui.config(b"ui", b"ssh", b"ssh")
     args = procutil.sshargs(
         sshcmd, pycompat.bytesurl(host), username, port
     )
     cmd = b'%s %s %s' % (sshcmd, args, procutil.shellquote(command))
     # consistent with mercurial
     ui.debug(b'running %s\n' % cmd)
     # we cannot use Mercurial's procutil.popen4() since it
     # always redirects stderr into a pipe
     proc = subprocess.Popen(
         procutil.tonativestr(cmd),
         shell=True,
         bufsize=0,
         stdin=subprocess.PIPE,
         stdout=subprocess.PIPE,
     )
     return SubprocessWrapper(proc)