2009-04-18 16:54:51 +00:00
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2019-08-27 04:26:15 +00:00
|
|
|
"""Logic for tracing repo interactions.
|
|
|
|
|
|
|
|
Activated via `repo --trace ...` or `REPO_TRACE=1 repo ...`.
|
2022-11-03 20:51:19 +00:00
|
|
|
|
|
|
|
Temporary: Tracing is always on. Set `REPO_TRACE=0` to turn off.
|
|
|
|
To also include trace outputs in stderr do `repo --trace_to_stderr ...`
|
2019-08-27 04:26:15 +00:00
|
|
|
"""
|
|
|
|
|
2009-04-18 16:54:51 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2022-11-03 20:51:19 +00:00
|
|
|
import time
|
2022-12-09 22:49:07 +00:00
|
|
|
import tempfile
|
2022-11-03 20:51:19 +00:00
|
|
|
from contextlib import ContextDecorator
|
2019-08-27 04:26:15 +00:00
|
|
|
|
2022-11-08 23:56:52 +00:00
|
|
|
import platform_utils
|
|
|
|
|
2019-08-27 04:26:15 +00:00
|
|
|
# Env var to implicitly turn on tracing.
|
2009-04-18 16:54:51 +00:00
|
|
|
REPO_TRACE = 'REPO_TRACE'
|
|
|
|
|
2022-11-03 20:51:19 +00:00
|
|
|
# Temporarily set tracing to always on unless user expicitly sets to 0.
|
|
|
|
_TRACE = os.environ.get(REPO_TRACE) != '0'
|
|
|
|
_TRACE_TO_STDERR = False
|
|
|
|
_TRACE_FILE = None
|
|
|
|
_TRACE_FILE_NAME = 'TRACE_FILE'
|
2022-12-09 22:49:07 +00:00
|
|
|
_MAX_SIZE = 70 # in MiB
|
2022-11-03 20:51:19 +00:00
|
|
|
_NEW_COMMAND_SEP = '+++++++++++++++NEW COMMAND+++++++++++++++++++'
|
|
|
|
|
|
|
|
|
2022-11-10 00:11:51 +00:00
|
|
|
def IsTraceToStderr():
|
2022-12-09 22:49:07 +00:00
|
|
|
"""Whether traces are written to stderr."""
|
2022-11-03 20:51:19 +00:00
|
|
|
return _TRACE_TO_STDERR
|
2009-04-18 16:54:51 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2009-04-18 16:54:51 +00:00
|
|
|
def IsTrace():
|
2022-12-09 22:49:07 +00:00
|
|
|
"""Whether tracing is enabled."""
|
2009-04-18 16:54:51 +00:00
|
|
|
return _TRACE
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2022-11-03 20:51:19 +00:00
|
|
|
def SetTraceToStderr():
|
2022-12-09 22:49:07 +00:00
|
|
|
"""Enables tracing logging to stderr."""
|
2022-11-03 20:51:19 +00:00
|
|
|
global _TRACE_TO_STDERR
|
|
|
|
_TRACE_TO_STDERR = True
|
|
|
|
|
|
|
|
|
2009-04-18 16:54:51 +00:00
|
|
|
def SetTrace():
|
2022-12-09 22:49:07 +00:00
|
|
|
"""Enables tracing."""
|
2009-04-18 16:54:51 +00:00
|
|
|
global _TRACE
|
|
|
|
_TRACE = True
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
def _SetTraceFile(quiet):
|
2022-12-09 22:49:07 +00:00
|
|
|
"""Sets the trace file location."""
|
2022-11-03 20:51:19 +00:00
|
|
|
global _TRACE_FILE
|
2022-11-10 02:31:19 +00:00
|
|
|
_TRACE_FILE = _GetTraceFile(quiet)
|
2022-11-03 20:51:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Trace(ContextDecorator):
|
2022-12-09 22:49:07 +00:00
|
|
|
"""Used to capture and save git traces."""
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
def _time(self):
|
|
|
|
"""Generate nanoseconds of time in a py3.6 safe way"""
|
|
|
|
return int(time.time() * 1e+9)
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
def __init__(self, fmt, *args, first_trace=False, quiet=True):
|
|
|
|
"""Initialize the object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fmt: The format string for the trace.
|
|
|
|
*args: Arguments to pass to formatting.
|
|
|
|
first_trace: Whether this is the first trace of a `repo` invocation.
|
|
|
|
quiet: Whether to suppress notification of trace file location.
|
|
|
|
"""
|
2022-11-10 02:31:19 +00:00
|
|
|
if not IsTrace():
|
|
|
|
return
|
|
|
|
self._trace_msg = fmt % args
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
if not _TRACE_FILE:
|
2022-11-10 02:31:19 +00:00
|
|
|
_SetTraceFile(quiet)
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
if first_trace:
|
|
|
|
_ClearOldTraces()
|
|
|
|
self._trace_msg = f'{_NEW_COMMAND_SEP} {self._trace_msg}'
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
def __enter__(self):
|
|
|
|
if not IsTrace():
|
|
|
|
return self
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
print_msg = f'PID: {os.getpid()} START: {self._time()} :{self._trace_msg}\n'
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
with open(_TRACE_FILE, 'a') as f:
|
|
|
|
print(print_msg, file=f)
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
if _TRACE_TO_STDERR:
|
|
|
|
print(print_msg, file=sys.stderr)
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
return self
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
def __exit__(self, *exc):
|
|
|
|
if not IsTrace():
|
|
|
|
return False
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
print_msg = f'PID: {os.getpid()} END: {self._time()} :{self._trace_msg}\n'
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
with open(_TRACE_FILE, 'a') as f:
|
|
|
|
print(print_msg, file=f)
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
if _TRACE_TO_STDERR:
|
|
|
|
print(print_msg, file=sys.stderr)
|
2022-11-03 20:51:19 +00:00
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
return False
|
2022-11-03 20:51:19 +00:00
|
|
|
|
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
def _GetTraceFile(quiet):
|
2022-11-03 20:51:19 +00:00
|
|
|
"""Get the trace file or create one."""
|
|
|
|
# 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)
|
2022-11-10 02:31:19 +00:00
|
|
|
if not quiet:
|
|
|
|
print(f'Trace outputs in {trace_file}', file=sys.stderr)
|
2022-11-03 20:51:19 +00:00
|
|
|
return trace_file
|
|
|
|
|
2022-11-10 02:31:19 +00:00
|
|
|
|
2022-11-03 20:51:19 +00:00
|
|
|
def _ClearOldTraces():
|
2022-12-09 22:49:07 +00:00
|
|
|
"""Clear the oldest commands if trace file is too big."""
|
|
|
|
try:
|
|
|
|
with open(_TRACE_FILE, 'r', errors='ignore') as f:
|
|
|
|
if os.path.getsize(f.name) / (1024 * 1024) <= _MAX_SIZE:
|
|
|
|
return
|
|
|
|
trace_lines = f.readlines()
|
|
|
|
except FileNotFoundError:
|
|
|
|
return
|
|
|
|
|
|
|
|
while sum(len(x) for x in trace_lines) / (1024 * 1024) > _MAX_SIZE:
|
|
|
|
for i, line in enumerate(trace_lines):
|
|
|
|
if 'END:' in line and _NEW_COMMAND_SEP in line:
|
|
|
|
trace_lines = trace_lines[i + 1:]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# The last chunk is bigger than _MAX_SIZE, so just throw everything away.
|
|
|
|
trace_lines = []
|
|
|
|
|
|
|
|
while trace_lines and trace_lines[-1] == '\n':
|
|
|
|
trace_lines = trace_lines[:-1]
|
|
|
|
# Write to a temporary file with a unique name in the same filesystem
|
|
|
|
# before replacing the original trace file.
|
|
|
|
temp_dir, temp_prefix = os.path.split(_TRACE_FILE)
|
|
|
|
with tempfile.NamedTemporaryFile('w',
|
|
|
|
dir=temp_dir,
|
|
|
|
prefix=temp_prefix,
|
|
|
|
delete=False) as f:
|
|
|
|
f.writelines(trace_lines)
|
|
|
|
platform_utils.rename(f.name, _TRACE_FILE)
|