repo: add trace support to the launcher

Now that we have a central run_command point, we can easily add
tracing support to the launcher script.

Change-Id: I9e0335c196cafd6263ff501925abfe835f036c5e
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/254755
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
This commit is contained in:
Mike Frysinger 2020-02-12 09:39:23 -05:00 committed by David Pursehouse
parent 62285d22c1
commit 6fb0cb5c80

24
repo
View File

@ -17,8 +17,29 @@ import subprocess
import sys
# Keep basic logic in sync with repo_trace.py.
class Trace(object):
"""Trace helper logic."""
REPO_TRACE = 'REPO_TRACE'
def __init__(self):
self.set(os.environ.get(self.REPO_TRACE) == '1')
def set(self, value):
self.enabled = bool(value)
def print(self, *args, **kwargs):
if self.enabled:
print(*args, **kwargs)
trace = Trace()
def exec_command(cmd):
"""Execute |cmd| or return None on failure."""
trace.print(':', ' '.join(cmd))
try:
if platform.system() == 'Windows':
ret = subprocess.call(cmd)
@ -309,6 +330,7 @@ def run_command(cmd, **kwargs):
stdout = stdout.decode('utf-8')
if stderr is not None:
stderr = stderr.decode('utf-8')
trace.print(':', ' '.join(cmd))
ret = RunResult(proc.returncode, stdout, stderr)
# If things failed, print useful debugging output.
@ -810,6 +832,8 @@ def _ParseArguments(args):
opt.help = True
elif a == '--version':
opt.version = True
elif a == '--trace':
trace.set(True)
elif not a.startswith('-'):
cmd = a
arg = args[i + 1:]