Fix TRACE_FILE renaming.

Bug: b/258073923

Change-Id: I997961056388e1550711f73a6310788b5c7ad4d4
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/350934
Tested-by: Joanna Wang <jojwang@google.com>
Reviewed-by: LaMont Jones <lamontjones@google.com>
This commit is contained in:
Joanna Wang 2022-11-08 18:56:52 -05:00
parent 7efab539f0
commit 24c6314fca
3 changed files with 14 additions and 13 deletions

View File

@ -109,7 +109,7 @@ global_options.add_option('--color',
global_options.add_option('--trace', global_options.add_option('--trace',
dest='trace', action='store_true', dest='trace', action='store_true',
help='trace git command execution (REPO_TRACE=1)') help='trace git command execution (REPO_TRACE=1)')
global_options.add_option('--trace_to_stderr', global_options.add_option('--trace-to-stderr',
dest='trace_to_stderr', action='store_true', dest='trace_to_stderr', action='store_true',
help='trace outputs go to stderr in addition to .repo/TRACE_FILE') help='trace outputs go to stderr in addition to .repo/TRACE_FILE')
global_options.add_option('--trace-python', global_options.add_option('--trace-python',

View File

@ -25,7 +25,7 @@ control color usage: auto, always, never
\fB\-\-trace\fR \fB\-\-trace\fR
trace git command execution (REPO_TRACE=1) trace git command execution (REPO_TRACE=1)
.TP .TP
\fB\-\-trace_to_stderr\fR \fB\-\-trace-to-stderr\fR
trace outputs go to stderr in addition to trace outputs go to stderr in addition to
\&.repo/TRACE_FILE \&.repo/TRACE_FILE
.TP .TP

View File

@ -22,10 +22,11 @@ To also include trace outputs in stderr do `repo --trace_to_stderr ...`
import sys import sys
import os import os
import tempfile
import time import time
from contextlib import ContextDecorator from contextlib import ContextDecorator
import platform_utils
# Env var to implicitly turn on tracing. # Env var to implicitly turn on tracing.
REPO_TRACE = 'REPO_TRACE' REPO_TRACE = 'REPO_TRACE'
@ -38,7 +39,7 @@ _TRACE_FILE = None
_TRACE_FILE_NAME = 'TRACE_FILE' _TRACE_FILE_NAME = 'TRACE_FILE'
_MAX_SIZE = 5 # in mb _MAX_SIZE = 70 # in mb
_NEW_COMMAND_SEP = '+++++++++++++++NEW COMMAND+++++++++++++++++++' _NEW_COMMAND_SEP = '+++++++++++++++NEW COMMAND+++++++++++++++++++'
@ -123,7 +124,7 @@ def _GetTraceFile():
return trace_file return trace_file
def _ClearOldTraces(): def _ClearOldTraces():
"""Clear traces from old commands if trace file is too big. """Clear the oldest commands if trace file is too big.
Note: If the trace file contains output from two `repo` Note: If the trace file contains output from two `repo`
commands that were running at the same time, this commands that were running at the same time, this
@ -131,12 +132,12 @@ def _ClearOldTraces():
""" """
if os.path.isfile(_TRACE_FILE): if os.path.isfile(_TRACE_FILE):
while os.path.getsize(_TRACE_FILE)/(1024*1024) > _MAX_SIZE: while os.path.getsize(_TRACE_FILE)/(1024*1024) > _MAX_SIZE:
temp = tempfile.NamedTemporaryFile(mode='w', delete=False) temp_file = _TRACE_FILE + '.tmp'
with open(_TRACE_FILE, 'r', errors='ignore') as fin: with open(_TRACE_FILE, 'r', errors='ignore') as fin:
with open(temp_file, 'w') as tf:
trace_lines = fin.readlines() trace_lines = fin.readlines()
for i , l in enumerate(trace_lines): for i , l in enumerate(trace_lines):
if 'END:' in l and _NEW_COMMAND_SEP in l: if 'END:' in l and _NEW_COMMAND_SEP in l:
temp.writelines(trace_lines[i+1:]) tf.writelines(trace_lines[i+1:])
break break
temp.close() platform_utils.rename(temp_file, _TRACE_FILE)
os.replace(temp.name, _TRACE_FILE)