Exemple #1
0
    def update(self, inc=1):
        self._done += inc

        if _NOT_TTY or IsTrace():
            return

        if not self._show:
            if 0.5 <= time() - self._start:
                self._show = True
            else:
                return

        if self._total <= 0:
            sys.stderr.write('\r%s: %d, ' % (self._title, self._done))
            sys.stderr.flush()
        else:
            p = (100 * self._done) / self._total

            if self._lastp != p or self._always_print_percentage:
                self._lastp = p
                sys.stderr.write(
                    '\r%s: %3d%% (%d%s/%d%s)%s' %
                    (self._title, p, self._done, self._units, self._total,
                     self._units, "\n" if self._print_newline else ""))
                sys.stderr.flush()
Exemple #2
0
    def update(self, inc=1, msg=''):
        self._done += inc

        if _NOT_TTY or IsTrace():
            return

        if not self._show:
            if 0.5 <= time() - self._start:
                self._show = True
            else:
                return

        if self._total <= 0:
            sys.stderr.write('%s\r\033[1;33m%s: %d,\033[0;0m' %
                             (CSI_ERASE_LINE, self._title, self._done))
            sys.stderr.flush()
        else:
            p = (100 * self._done) / self._total

            if self._lastp != p or self._always_print_percentage:
                self._lastp = p
                sys.stderr.write(
                    '%s\r\033[1;33m%s: %3d%% (%d%s/%d%s)\033[0;0m %s%s%s' %
                    (CSI_ERASE_LINE, self._title, p, self._done, self._units,
                     self._total, self._units, ' ' if msg else '', msg,
                     "\n" if self._print_newline else ""))
                sys.stderr.flush()
Exemple #3
0
    def update(self, inc=1):
        self._done += inc

        if _NOT_TTY or IsTrace():
            return

        if not self._show:
            if 0.5 <= time() - self._start:
                self._show = True
            else:
                return

        if self._total <= 0:
            sys.stderr.write('\r%s: %d, ' % (
                self._title,
                self._done))
            sys.stderr.flush()
        else:
            p = (100 * self._done) / self._total

            if self._lastp != p:
                self._lastp = p
                sys.stderr.write('\r%s: %3d%% (%d%s/%d%s)  ' % (
                    self._title,
                    p,
                    self._done, self._units,
                    self._total, self._units))
                sys.stderr.flush()
Exemple #4
0
def _WindowsPager(repo):
    (name, cmd, gopts, _gargs, copts, cargs, argv) = repo.config
    if _UsePager(name, cmd, gopts, copts):
        python = sys.executable
        thisScript = os.path.abspath(__file__)

        args = sys.argv[1:]
        argsSplit = args.index('--')
        args1 = args[:argsSplit]
        args2 = args[argsSplit + 1:]
        pager = _SelectPager(cmd.manifest.globalConfig)
        shellCommand = [python, thisScript] + args1 + [
            '--', '--piped-into-pager', '--no-pager'
        ] + args2 + ['|', pager]
        if IsTrace():
            Trace(' '.join(shellCommand))
        subprocess.call(shellCommand, shell=True)
        return True
    else:
        # set global variable if output is piped into pager; means that pager is simulated, this
        # leads to correct coloring in windows
        import pager

        pager.active = gopts.pipedIntoPager

        return False
Exemple #5
0
    def update(self, inc=1, msg=''):
        self._done += inc

        if _NOT_TTY or IsTrace():
            return

        if not self._show:
            if 0.5 <= time() - self._start:
                self._show = True
            else:
                return

        if self._total <= 0:
            sys.stderr.write('%s\r%s: %d,' %
                             (CSI_ERASE_LINE, self._title, self._done))
            sys.stderr.flush()
        else:
            p = (100 * self._done) / self._total
            if self._show_jobs:
                jobs = '[%d job%s] ' % (self._active,
                                        's' if self._active > 1 else '')
            else:
                jobs = ''
            sys.stderr.write(
                '%s\r%s: %2d%% %s(%d%s/%d%s)%s%s%s' %
                (CSI_ERASE_LINE, self._title, p, jobs, self._done, self._units,
                 self._total, self._units, ' ' if msg else '', msg,
                 '\n' if self._print_newline else ''))
            sys.stderr.flush()
Exemple #6
0
    def end(self):
        if _NOT_TTY or IsTrace() or not self._show:
            return

        if self._total <= 0:
            sys.stderr.write('\r%s: %d, done.  \n' % (self._title, self._done))
            sys.stderr.flush()
        else:
            p = (100 * self._done) / self._total
            sys.stderr.write('\r%s: %3d%% (%d%s/%d%s), done.  \n' %
                             (self._title, p, self._done, self._units,
                              self._total, self._units))
            sys.stderr.flush()
Exemple #7
0
def os_link(src, dst):
    if isUnix():
        # requires src in relation to dst
        src = os.path.relpath(src, os.path.dirname(dst))
        os.symlink(src, dst)
    else:
        isDir = True if os.path.isdir(src) else False
        src = os.path.relpath(src, os.path.dirname(dst))
        src = toWindowsPath(src)
        dst = toWindowsPath(dst)
        # ln in MinGW does not create hard links? - it copies
        # call windows cmd tool 'mklink' from git bash (mingw)
        if isDir:
            cmd = 'cmd /c mklink /D "%s" "%s"' % (dst, src)
            if IsTrace():
                Trace(cmd)
            subprocess.Popen(cmd, stdout=subprocess.PIPE).wait()
        else:
            cmd = 'cmd /c mklink "%s" "%s"' % (dst, src)
            if IsTrace():
                Trace(cmd)
            subprocess.Popen(cmd, stdout=subprocess.PIPE).wait()
Exemple #8
0
    def end(self):
        if _NOT_TTY or IsTrace() or not self._show:
            return

        if self._total <= 0:
            sys.stderr.write('%s\r\033[1;33m%s: %d, done.\033[0;0m\n' %
                             (CSI_ERASE_LINE, self._title, self._done))
            sys.stderr.flush()
        else:
            p = (100 * self._done) / self._total
            sys.stderr.write(
                '%s\r\033[1;33m%s: %3d%% (%d%s/%d%s), done.\033[0;0m\n' %
                (CSI_ERASE_LINE, self._title, p, self._done, self._units,
                 self._total, self._units))
            sys.stderr.flush()
Exemple #9
0
    def end(self):
        if _NOT_TTY or IsTrace() or not self._show:
            return

        duration = duration_str(time() - self._start)
        if self._total <= 0:
            sys.stderr.write(
                '%s\r%s: %d, done in %s\n' %
                (CSI_ERASE_LINE, self._title, self._done, duration))
            sys.stderr.flush()
        else:
            p = (100 * self._done) / self._total
            sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done in %s\n' %
                             (CSI_ERASE_LINE, self._title, p, self._done,
                              self._units, self._total, self._units, duration))
            sys.stderr.flush()
Exemple #10
0
    def __init__(self,
                 project,
                 cmdv,
                 bare=False,
                 provide_stdin=False,
                 capture_stdout=False,
                 capture_stderr=False,
                 disable_editor=False,
                 ssh_proxy=False,
                 cwd=None,
                 gitdir=None):
        env = os.environ.copy()

        for key in [
                REPO_TRACE, GIT_DIR, 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
                'GIT_OBJECT_DIRECTORY', 'GIT_WORK_TREE', 'GIT_GRAFT_FILE',
                'GIT_INDEX_FILE'
        ]:
            if key in env:
                del env[key]

        # If we are not capturing std* then need to print it.
        self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}

        if disable_editor:
            _setenv(env, 'GIT_EDITOR', ':')
        if ssh_proxy:
            _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
            _setenv(env, 'GIT_SSH', _ssh_proxy())
            _setenv(env, 'GIT_SSH_VARIANT', 'ssh')
        if 'http_proxy' in env and 'darwin' == sys.platform:
            s = "'http.proxy=%s'" % (env['http_proxy'], )
            p = env.get('GIT_CONFIG_PARAMETERS')
            if p is not None:
                s = p + ' ' + s
            _setenv(env, 'GIT_CONFIG_PARAMETERS', s)
        if 'GIT_ALLOW_PROTOCOL' not in env:
            _setenv(
                env, 'GIT_ALLOW_PROTOCOL',
                'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc'
            )

        if project:
            if not cwd:
                cwd = project.worktree
            if not gitdir:
                gitdir = project.gitdir

        command = [GIT]
        if bare:
            if gitdir:
                _setenv(env, GIT_DIR, gitdir)
            cwd = None
        command.append(cmdv[0])
        # Need to use the --progress flag for fetch/clone so output will be
        # displayed as by default git only does progress output if stderr is a TTY.
        if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
            if '--progress' not in cmdv and '--quiet' not in cmdv:
                command.append('--progress')
        command.extend(cmdv[1:])

        if provide_stdin:
            stdin = subprocess.PIPE
        else:
            stdin = None

        stdout = subprocess.PIPE
        stderr = subprocess.PIPE

        if IsTrace():
            global LAST_CWD
            global LAST_GITDIR

            dbg = ''

            if cwd and LAST_CWD != cwd:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': cd %s\n' % cwd
                LAST_CWD = cwd

            if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
                LAST_GITDIR = env[GIT_DIR]

            dbg += ': '
            dbg += ' '.join(command)
            if stdin == subprocess.PIPE:
                dbg += ' 0<|'
            if stdout == subprocess.PIPE:
                dbg += ' 1>|'
            if stderr == subprocess.PIPE:
                dbg += ' 2>|'
            Trace('%s', dbg)

        try:
            p = subprocess.Popen(command,
                                 cwd=cwd,
                                 env=env,
                                 stdin=stdin,
                                 stdout=stdout,
                                 stderr=stderr)
        except Exception as e:
            raise GitError('%s: %s' % (command[1], e))

        if ssh_proxy:
            _add_ssh_client(p)

        self.process = p
        self.stdin = p.stdin
Exemple #11
0
    def __init__(self,
                 project,
                 cmdv,
                 bare=False,
                 provide_stdin=False,
                 capture_stdout=False,
                 capture_stderr=False,
                 disable_editor=False,
                 ssh_proxy=False,
                 cwd=None,
                 gitdir=None):
        env = os.environ.copy()

        for key in [
                REPO_TRACE, GIT_DIR, 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
                'GIT_OBJECT_DIRECTORY', 'GIT_WORK_TREE', 'GIT_GRAFT_FILE',
                'GIT_INDEX_FILE'
        ]:
            if key in env:
                del env[key]

        if disable_editor:
            _setenv(env, 'GIT_EDITOR', ':')
        if ssh_proxy:
            _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
            _setenv(env, 'GIT_SSH', _ssh_proxy())
        if 'http_proxy' in env and 'darwin' == sys.platform:
            s = "'http.proxy=%s'" % (env['http_proxy'], )
            p = env.get('GIT_CONFIG_PARAMETERS')
            if p is not None:
                s = p + ' ' + s
            _setenv(env, 'GIT_CONFIG_PARAMETERS', s)

        if project:
            if not cwd:
                cwd = project.worktree
            if not gitdir:
                gitdir = project.gitdir

        command = [GIT]
        if bare:
            if gitdir:
                env[GIT_DIR] = gitdir
            cwd = None
        command.extend(cmdv)

        if provide_stdin:
            stdin = subprocess.PIPE
        else:
            stdin = None

        if capture_stdout:
            stdout = subprocess.PIPE
        else:
            stdout = None

        if capture_stderr:
            stderr = subprocess.PIPE
        else:
            stderr = None

        if IsTrace():
            global LAST_CWD
            global LAST_GITDIR

            dbg = ''

            if cwd and LAST_CWD != cwd:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': cd %s\n' % cwd
                LAST_CWD = cwd

            if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
                LAST_GITDIR = env[GIT_DIR]

            dbg += ': '
            dbg += ' '.join(command)
            if stdin == subprocess.PIPE:
                dbg += ' 0<|'
            if stdout == subprocess.PIPE:
                dbg += ' 1>|'
            if stderr == subprocess.PIPE:
                dbg += ' 2>|'
            Trace('%s', dbg)

        try:
            p = subprocess.Popen(command,
                                 cwd=cwd,
                                 env=env,
                                 stdin=stdin,
                                 stdout=stdout,
                                 stderr=stderr)
        except Exception as e:
            raise GitError('%s: %s' % (command[1], e))

        if ssh_proxy:
            _add_ssh_client(p)

        portable.SUBPROCESSES.append(p)
        self.process = p
        self.stdin = p.stdin
Exemple #12
0
    def __init__(self,
                 project,
                 cmdv,
                 bare=False,
                 input=None,
                 capture_stdout=False,
                 capture_stderr=False,
                 merge_output=False,
                 disable_editor=False,
                 ssh_proxy=None,
                 cwd=None,
                 gitdir=None):
        env = self._GetBasicEnv()

        if disable_editor:
            env['GIT_EDITOR'] = ':'
        if ssh_proxy:
            env['REPO_SSH_SOCK'] = ssh_proxy.sock()
            env['GIT_SSH'] = ssh_proxy.proxy
            env['GIT_SSH_VARIANT'] = 'ssh'
        if 'http_proxy' in env and 'darwin' == sys.platform:
            s = "'http.proxy=%s'" % (env['http_proxy'], )
            p = env.get('GIT_CONFIG_PARAMETERS')
            if p is not None:
                s = p + ' ' + s
            env['GIT_CONFIG_PARAMETERS'] = s
        if 'GIT_ALLOW_PROTOCOL' not in env:
            env['GIT_ALLOW_PROTOCOL'] = (
                'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc'
            )
        env['GIT_HTTP_USER_AGENT'] = user_agent.git

        if project:
            if not cwd:
                cwd = project.worktree
            if not gitdir:
                gitdir = project.gitdir

        command = [GIT]
        if bare:
            if gitdir:
                # Git on Windows wants its paths only using / for reliability.
                if platform_utils.isWindows():
                    gitdir = gitdir.replace('\\', '/')
                env[GIT_DIR] = gitdir
            cwd = None
        command.append(cmdv[0])
        # Need to use the --progress flag for fetch/clone so output will be
        # displayed as by default git only does progress output if stderr is a TTY.
        if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
            if '--progress' not in cmdv and '--quiet' not in cmdv:
                command.append('--progress')
        command.extend(cmdv[1:])

        stdin = subprocess.PIPE if input else None
        stdout = subprocess.PIPE if capture_stdout else None
        stderr = (subprocess.STDOUT if merge_output else
                  (subprocess.PIPE if capture_stderr else None))

        if IsTrace():
            global LAST_CWD
            global LAST_GITDIR

            dbg = ''

            if cwd and LAST_CWD != cwd:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': cd %s\n' % cwd
                LAST_CWD = cwd

            if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
                LAST_GITDIR = env[GIT_DIR]

            dbg += ': '
            dbg += ' '.join(command)
            if stdin == subprocess.PIPE:
                dbg += ' 0<|'
            if stdout == subprocess.PIPE:
                dbg += ' 1>|'
            if stderr == subprocess.PIPE:
                dbg += ' 2>|'
            elif stderr == subprocess.STDOUT:
                dbg += ' 2>&1'
            Trace('%s', dbg)

        try:
            p = subprocess.Popen(command,
                                 cwd=cwd,
                                 env=env,
                                 encoding='utf-8',
                                 errors='backslashreplace',
                                 stdin=stdin,
                                 stdout=stdout,
                                 stderr=stderr)
        except Exception as e:
            raise GitError('%s: %s' % (command[1], e))

        if ssh_proxy:
            ssh_proxy.add_client(p)

        self.process = p
        if input:
            if isinstance(input, str):
                input = input.encode('utf-8')
            p.stdin.write(input)
            p.stdin.close()

        try:
            self.stdout, self.stderr = p.communicate()
        finally:
            if ssh_proxy:
                ssh_proxy.remove_client(p)
        self.rc = p.wait()
Exemple #13
0
    def __init__(
        self,
        project,
        cmdv,
        bare=False,
        provide_stdin=False,
        capture_stdout=False,
        capture_stderr=False,
        merge_output=False,
        disable_editor=False,
        ssh_proxy=False,
        cwd=None,
        gitdir=None,
    ):
        env = self._GetBasicEnv()

        # If we are not capturing std* then need to print it.
        self.tee = {"stdout": not capture_stdout, "stderr": not capture_stderr}

        if disable_editor:
            env["GIT_EDITOR"] = ":"
        if ssh_proxy:
            env["REPO_SSH_SOCK"] = ssh_sock()
            env["GIT_SSH"] = _ssh_proxy()
            env["GIT_SSH_VARIANT"] = "ssh"
        if "http_proxy" in env and "darwin" == sys.platform:
            s = "'http.proxy=%s'" % (env["http_proxy"], )
            p = env.get("GIT_CONFIG_PARAMETERS")
            if p is not None:
                s = p + " " + s
            env["GIT_CONFIG_PARAMETERS"] = s
        if "GIT_ALLOW_PROTOCOL" not in env:
            env["GIT_ALLOW_PROTOCOL"] = "file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc"
        env["GIT_HTTP_USER_AGENT"] = user_agent.git

        if project:
            if not cwd:
                cwd = project.worktree
            if not gitdir:
                gitdir = project.gitdir

        command = [GIT]
        if bare:
            if gitdir:
                env[GIT_DIR] = gitdir
            cwd = None
        command.append(cmdv[0])
        # Need to use the --progress flag for fetch/clone so output will be
        # displayed as by default git only does progress output if stderr is a TTY.
        if sys.stderr.isatty() and cmdv[0] in ("fetch", "clone"):
            if "--progress" not in cmdv and "--quiet" not in cmdv:
                command.append("--progress")
        command.extend(cmdv[1:])

        if provide_stdin:
            stdin = subprocess.PIPE
        else:
            stdin = None

        stdout = subprocess.PIPE
        stderr = subprocess.STDOUT if merge_output else subprocess.PIPE

        if IsTrace():
            global LAST_CWD
            global LAST_GITDIR

            dbg = ""

            if cwd and LAST_CWD != cwd:
                if LAST_GITDIR or LAST_CWD:
                    dbg += "\n"
                dbg += ": cd %s\n" % cwd
                LAST_CWD = cwd

            if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
                if LAST_GITDIR or LAST_CWD:
                    dbg += "\n"
                dbg += ": export GIT_DIR=%s\n" % env[GIT_DIR]
                LAST_GITDIR = env[GIT_DIR]

            dbg += ": "
            dbg += " ".join(command)
            if stdin == subprocess.PIPE:
                dbg += " 0<|"
            if stdout == subprocess.PIPE:
                dbg += " 1>|"
            if stderr == subprocess.PIPE:
                dbg += " 2>|"
            elif stderr == subprocess.STDOUT:
                dbg += " 2>&1"
            Trace("%s", dbg)

        try:
            p = subprocess.Popen(command,
                                 cwd=cwd,
                                 env=env,
                                 stdin=stdin,
                                 stdout=stdout,
                                 stderr=stderr)
        except Exception as e:
            raise GitError("%s: %s" % (command[1], e))

        if ssh_proxy:
            _add_ssh_client(p)

        self.process = p
        self.stdin = p.stdin
Exemple #14
0
    def Execute(self, opt, args):
        if not opt.command:
            self.Usage()

        cmd = [opt.command[0]]

        shell = True
        if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
            shell = False

        if shell and portable.isPosix():
            cmd.append(cmd[0])
        cmd.extend(opt.command[1:])

        if opt.project_header \
            and not shell \
            and cmd[0] == 'git':
            # If this is a direct git command that can enable colorized
            # output and the user prefers coloring, add --color into the
            # command line because we are going to wrap the command into
            # a pipe and git won't know coloring should activate.
            #
            for cn in cmd[1:]:
                if not cn.startswith('-'):
                    break
            else:
                cn = None
                # pylint: disable=W0631
            if cn and cn in _CAN_COLOR:
                class ColorCmd(Coloring):
                    def __init__(self, config, cmd):
                        Coloring.__init__(self, config, cmd)

                if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
                    cmd.insert(cmd.index(cn) + 1, '--color')
                    # pylint: enable=W0631

        mirror = self.manifest.IsMirror
        out = ForallColoring(self.manifest.manifestProject.config)
        out.redirect(sys.stdout)

        rc = 0
        first = True

        for project in self.GetProjects(args):
            env = os.environ.copy()

            def setenv(name, val):
                if val is None:
                    val = ''
                env[name] = val

            setenv('REPO_PROJECT', project.name)
            setenv('REPO_PATH', project.relpath)
            setenv('REPO_REMOTE', project.remote.name)
            setenv('REPO_LREV', project.GetRevisionId())
            setenv('REPO_RREV', project.revisionExpr)
            for a in project.annotations:
                setenv("REPO__%s" % (a.name), a.value)

            if mirror:
                setenv('GIT_DIR', project.gitdir)
                cwd = project.gitdir
            else:
                cwd = project.worktree

            if not os.path.exists(cwd):
                if (opt.project_header and opt.verbose) \
                    or not opt.project_header:
                    print('skipping %s/' % project.relpath, file=sys.stderr)
                continue

            if opt.project_header:
                stdin = subprocess.PIPE
                stdout = subprocess.PIPE
                stderr = subprocess.PIPE
            else:
                stdin = None
                stdout = None
                stderr = None

            if not portable.isUnix():
                if type(cmd) is list:
                    cmd = " ".join(cmd)

            if IsTrace():
                Trace("execute command: %s" % str(cmd).replace("%", "%%"))
                Trace("environment is: %s" % str(env))

            p = subprocess.Popen(cmd,
                                 cwd=cwd,
                                 shell=shell,
                                 env=env,
                                 stdin=stdin,
                                 stdout=stdout,
                                 stderr=stderr)

            if opt.project_header:
                class sfd(object):
                    def __init__(self, fd, dest):
                        self.fd = fd
                        self.dest = dest

                    def fileno(self):
                        return self.fd.fileno()

                    #        empty = True
                    #        errbuf = ''
                    #
                    #        p.stdin.close()
                    #        s_in = [sfd(p.stdout, sys.stdout),
                    #                sfd(p.stderr, sys.stderr)]
                    #
                    #        for s in s_in:
                    #          flags = fcntl.fcntl(s.fd, fcntl.F_GETFL)
                    #          fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
                    #
                    #        while s_in:
                    #          in_ready, _out_ready, _err_ready = select.select(s_in, [], [])
                    #          for s in in_ready:
                    #            buf = s.fd.read(4096)
                    #            if not buf:
                    #              s.fd.close()
                    #              s_in.remove(s)
                    #              continue
                    #
                    #            if not opt.verbose:
                    #              if s.fd != p.stdout:
                    #                errbuf += buf
                    #                continue
                    #
                    #            if empty:
                    #              if first:
                    #                first = False
                    #              else:
                    #                out.nl()
                    #              out.project('project %s/', project.relpath)
                    #              out.nl()
                    #              out.flush()
                    #              if errbuf:
                    #                sys.stderr.write(errbuf)
                    #                sys.stderr.flush()
                    #                errbuf = ''
                    #              empty = False
                    #
                    #            s.dest.write(buf)
                    #            s.dest.flush()

            r = p.wait()
            if r != 0:
                if r != rc:
                    rc = r
                if opt.abort_on_errors:
                    print("error: %s: Aborting due to previous error" % project.relpath,
                          file=sys.stderr)
                    sys.exit(r)
        if rc != 0:
            sys.exit(rc)
Exemple #15
0
  def __init__(self,
               project,
               cmdv,
               bare=False,
               provide_stdin=False,
               capture_stdout=False,
               capture_stderr=False,
               merge_output=False,
               disable_editor=False,
               ssh_proxy=False,
               cwd=None,
               gitdir=None):
    env = self._GetBasicEnv()

    # If we are not capturing std* then need to print it.
    self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}

    if disable_editor:
      env['GIT_EDITOR'] = ':'
    if ssh_proxy:
      env['REPO_SSH_SOCK'] = ssh_sock()
      env['GIT_SSH'] = _ssh_proxy()
      env['GIT_SSH_VARIANT'] = 'ssh'
    if 'http_proxy' in env and 'darwin' == sys.platform:
      s = "'http.proxy=%s'" % (env['http_proxy'],)
      p = env.get('GIT_CONFIG_PARAMETERS')
      if p is not None:
        s = p + ' ' + s
      env['GIT_CONFIG_PARAMETERS'] = s
    if 'GIT_ALLOW_PROTOCOL' not in env:
      env['GIT_ALLOW_PROTOCOL'] = (
          'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc')
    env['GIT_HTTP_USER_AGENT'] = user_agent.git

    if project:
      if not cwd:
        cwd = project.worktree
      if not gitdir:
        gitdir = project.gitdir

    command = [GIT]
    if bare:
      if gitdir:
        env[GIT_DIR] = gitdir
      cwd = None
    command.append(cmdv[0])
    # Need to use the --progress flag for fetch/clone so output will be
    # displayed as by default git only does progress output if stderr is a TTY.
    if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
      if '--progress' not in cmdv and '--quiet' not in cmdv:
        command.append('--progress')
    command.extend(cmdv[1:])

    if provide_stdin:
      stdin = subprocess.PIPE
    else:
      stdin = None

    stdout = subprocess.PIPE
    stderr = subprocess.STDOUT if merge_output else subprocess.PIPE

    if IsTrace():
      global LAST_CWD
      global LAST_GITDIR

      dbg = ''

      if cwd and LAST_CWD != cwd:
        if LAST_GITDIR or LAST_CWD:
          dbg += '\n'
        dbg += ': cd %s\n' % cwd
        LAST_CWD = cwd

      if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
        if LAST_GITDIR or LAST_CWD:
          dbg += '\n'
        dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
        LAST_GITDIR = env[GIT_DIR]

      dbg += ': '
      dbg += ' '.join(command)
      if stdin == subprocess.PIPE:
        dbg += ' 0<|'
      if stdout == subprocess.PIPE:
        dbg += ' 1>|'
      if stderr == subprocess.PIPE:
        dbg += ' 2>|'
      elif stderr == subprocess.STDOUT:
        dbg += ' 2>&1'
      Trace('%s', dbg)

    try:
      p = subprocess.Popen(command,
                           cwd=cwd,
                           env=env,
                           stdin=stdin,
                           stdout=stdout,
                           stderr=stderr)
    except Exception as e:
      raise GitError('%s: %s' % (command[1], e))

    if ssh_proxy:
      _add_ssh_client(p)

    self.process = p
    self.stdin = p.stdin