From afd767103ee2b80a67beb6e5c4fa94dd025f6a75 Mon Sep 17 00:00:00 2001 From: LaMont Jones Date: Thu, 10 Nov 2022 02:31:19 +0000 Subject: repo_trace: adjust formatting, update man page. No behavior change in this CL. Change-Id: Iab1eb01864ea8a5aec3a683200764d20786b42de Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/351474 Tested-by: LaMont Jones Reviewed-by: Xin Li --- repo_trace.py | 70 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'repo_trace.py') diff --git a/repo_trace.py b/repo_trace.py index 9016cb0c..fcd8650e 100644 --- a/repo_trace.py +++ b/repo_trace.py @@ -64,50 +64,49 @@ def _SetTraceFile(): class Trace(ContextDecorator): - def _time(self): - """Generate nanoseconds of time in a py3.6 safe way""" - return int(time.time()*1e+9) + def _time(self): + """Generate nanoseconds of time in a py3.6 safe way""" + return int(time.time() * 1e+9) - def __init__(self, fmt, *args, first_trace=False): - if not IsTrace(): - return - self._trace_msg = fmt % args + def __init__(self, fmt, *args, first_trace=False): + if not IsTrace(): + return + self._trace_msg = fmt % args - if not _TRACE_FILE: - _SetTraceFile() + if not _TRACE_FILE: + _SetTraceFile() - if first_trace: - _ClearOldTraces() - self._trace_msg = '%s %s' % (_NEW_COMMAND_SEP, self._trace_msg) + if first_trace: + _ClearOldTraces() + self._trace_msg = f'{_NEW_COMMAND_SEP} {self._trace_msg}' + def __enter__(self): + if not IsTrace(): + return self - def __enter__(self): - if not IsTrace(): - return self - - print_msg = f'PID: {os.getpid()} START: {self._time()} :' + self._trace_msg + '\n' + print_msg = f'PID: {os.getpid()} START: {self._time()} :{self._trace_msg}\n' - with open(_TRACE_FILE, 'a') as f: - print(print_msg, file=f) + with open(_TRACE_FILE, 'a') as f: + print(print_msg, file=f) - if _TRACE_TO_STDERR: - print(print_msg, file=sys.stderr) + if _TRACE_TO_STDERR: + print(print_msg, file=sys.stderr) - return self + return self - def __exit__(self, *exc): - if not IsTrace(): - return False + def __exit__(self, *exc): + if not IsTrace(): + return False - print_msg = f'PID: {os.getpid()} END: {self._time()} :' + self._trace_msg + '\n' + print_msg = f'PID: {os.getpid()} END: {self._time()} :{self._trace_msg}\n' - with open(_TRACE_FILE, 'a') as f: - print(print_msg, file=f) + with open(_TRACE_FILE, 'a') as f: + print(print_msg, file=f) - if _TRACE_TO_STDERR: - print(print_msg, file=sys.stderr) + if _TRACE_TO_STDERR: + print(print_msg, file=sys.stderr) - return False + return False def _GetTraceFile(): @@ -115,9 +114,10 @@ def _GetTraceFile(): # TODO: refactor to pass repodir to Trace. repo_dir = os.path.dirname(os.path.dirname(__file__)) trace_file = os.path.join(repo_dir, _TRACE_FILE_NAME) - print('Trace outputs in %s' % trace_file, file=sys.stderr) + print(f'Trace outputs in {trace_file}', file=sys.stderr) return trace_file + def _ClearOldTraces(): """Clear the oldest commands if trace file is too big. @@ -126,13 +126,13 @@ def _ClearOldTraces(): will not work precisely. """ 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_file = _TRACE_FILE + '.tmp' with open(_TRACE_FILE, 'r', errors='ignore') as fin: with open(temp_file, 'w') as tf: 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: - tf.writelines(trace_lines[i+1:]) + tf.writelines(trace_lines[i + 1:]) break platform_utils.rename(temp_file, _TRACE_FILE) -- cgit v1.2.3-54-g00ecf