diff options
| author | LaMont Jones <lamontjones@google.com> | 2022-11-10 02:31:19 +0000 |
|---|---|---|
| committer | LaMont Jones <lamontjones@google.com> | 2022-11-14 23:46:06 +0000 |
| commit | afd767103ee2b80a67beb6e5c4fa94dd025f6a75 (patch) | |
| tree | 03ee3372b124fc2fdc6c58d3a8ab3184dd7b7481 | |
| parent | b240d28bc065b94ea91383f58e7d55494d2691c1 (diff) | |
| download | git-repo-afd767103ee2b80a67beb6e5c4fa94dd025f6a75.tar.gz | |
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 <lamontjones@google.com>
Reviewed-by: Xin Li <delphij@google.com>
| -rw-r--r-- | man/repo.1 | 2 | ||||
| -rw-r--r-- | repo_trace.py | 70 |
2 files changed, 36 insertions, 36 deletions
| @@ -25,7 +25,7 @@ control color usage: auto, always, never | |||
| 25 | \fB\-\-trace\fR | 25 | \fB\-\-trace\fR |
| 26 | trace git command execution (REPO_TRACE=1) | 26 | trace git command execution (REPO_TRACE=1) |
| 27 | .TP | 27 | .TP |
| 28 | \fB\-\-trace-to-stderr\fR | 28 | \fB\-\-trace\-to\-stderr\fR |
| 29 | trace outputs go to stderr in addition to | 29 | trace outputs go to stderr in addition to |
| 30 | \&.repo/TRACE_FILE | 30 | \&.repo/TRACE_FILE |
| 31 | .TP | 31 | .TP |
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(): | |||
| 64 | 64 | ||
| 65 | class Trace(ContextDecorator): | 65 | class Trace(ContextDecorator): |
| 66 | 66 | ||
| 67 | def _time(self): | 67 | def _time(self): |
| 68 | """Generate nanoseconds of time in a py3.6 safe way""" | 68 | """Generate nanoseconds of time in a py3.6 safe way""" |
| 69 | return int(time.time()*1e+9) | 69 | return int(time.time() * 1e+9) |
| 70 | 70 | ||
| 71 | def __init__(self, fmt, *args, first_trace=False): | 71 | def __init__(self, fmt, *args, first_trace=False): |
| 72 | if not IsTrace(): | 72 | if not IsTrace(): |
| 73 | return | 73 | return |
| 74 | self._trace_msg = fmt % args | 74 | self._trace_msg = fmt % args |
| 75 | 75 | ||
| 76 | if not _TRACE_FILE: | 76 | if not _TRACE_FILE: |
| 77 | _SetTraceFile() | 77 | _SetTraceFile() |
| 78 | 78 | ||
| 79 | if first_trace: | 79 | if first_trace: |
| 80 | _ClearOldTraces() | 80 | _ClearOldTraces() |
| 81 | self._trace_msg = '%s %s' % (_NEW_COMMAND_SEP, self._trace_msg) | 81 | self._trace_msg = f'{_NEW_COMMAND_SEP} {self._trace_msg}' |
| 82 | 82 | ||
| 83 | def __enter__(self): | ||
| 84 | if not IsTrace(): | ||
| 85 | return self | ||
| 83 | 86 | ||
| 84 | def __enter__(self): | 87 | print_msg = f'PID: {os.getpid()} START: {self._time()} :{self._trace_msg}\n' |
| 85 | if not IsTrace(): | ||
| 86 | return self | ||
| 87 | |||
| 88 | print_msg = f'PID: {os.getpid()} START: {self._time()} :' + self._trace_msg + '\n' | ||
| 89 | 88 | ||
| 90 | with open(_TRACE_FILE, 'a') as f: | 89 | with open(_TRACE_FILE, 'a') as f: |
| 91 | print(print_msg, file=f) | 90 | print(print_msg, file=f) |
| 92 | 91 | ||
| 93 | if _TRACE_TO_STDERR: | 92 | if _TRACE_TO_STDERR: |
| 94 | print(print_msg, file=sys.stderr) | 93 | print(print_msg, file=sys.stderr) |
| 95 | 94 | ||
| 96 | return self | 95 | return self |
| 97 | 96 | ||
| 98 | def __exit__(self, *exc): | 97 | def __exit__(self, *exc): |
| 99 | if not IsTrace(): | 98 | if not IsTrace(): |
| 100 | return False | 99 | return False |
| 101 | 100 | ||
| 102 | print_msg = f'PID: {os.getpid()} END: {self._time()} :' + self._trace_msg + '\n' | 101 | print_msg = f'PID: {os.getpid()} END: {self._time()} :{self._trace_msg}\n' |
| 103 | 102 | ||
| 104 | with open(_TRACE_FILE, 'a') as f: | 103 | with open(_TRACE_FILE, 'a') as f: |
| 105 | print(print_msg, file=f) | 104 | print(print_msg, file=f) |
| 106 | 105 | ||
| 107 | if _TRACE_TO_STDERR: | 106 | if _TRACE_TO_STDERR: |
| 108 | print(print_msg, file=sys.stderr) | 107 | print(print_msg, file=sys.stderr) |
| 109 | 108 | ||
| 110 | return False | 109 | return False |
| 111 | 110 | ||
| 112 | 111 | ||
| 113 | def _GetTraceFile(): | 112 | def _GetTraceFile(): |
| @@ -115,9 +114,10 @@ def _GetTraceFile(): | |||
| 115 | # TODO: refactor to pass repodir to Trace. | 114 | # TODO: refactor to pass repodir to Trace. |
| 116 | repo_dir = os.path.dirname(os.path.dirname(__file__)) | 115 | repo_dir = os.path.dirname(os.path.dirname(__file__)) |
| 117 | trace_file = os.path.join(repo_dir, _TRACE_FILE_NAME) | 116 | trace_file = os.path.join(repo_dir, _TRACE_FILE_NAME) |
| 118 | print('Trace outputs in %s' % trace_file, file=sys.stderr) | 117 | print(f'Trace outputs in {trace_file}', file=sys.stderr) |
| 119 | return trace_file | 118 | return trace_file |
| 120 | 119 | ||
| 120 | |||
| 121 | def _ClearOldTraces(): | 121 | def _ClearOldTraces(): |
| 122 | """Clear the oldest commands if trace file is too big. | 122 | """Clear the oldest commands if trace file is too big. |
| 123 | 123 | ||
| @@ -126,13 +126,13 @@ def _ClearOldTraces(): | |||
| 126 | will not work precisely. | 126 | will not work precisely. |
| 127 | """ | 127 | """ |
| 128 | if os.path.isfile(_TRACE_FILE): | 128 | if os.path.isfile(_TRACE_FILE): |
| 129 | while os.path.getsize(_TRACE_FILE)/(1024*1024) > _MAX_SIZE: | 129 | while os.path.getsize(_TRACE_FILE) / (1024 * 1024) > _MAX_SIZE: |
| 130 | temp_file = _TRACE_FILE + '.tmp' | 130 | temp_file = _TRACE_FILE + '.tmp' |
| 131 | with open(_TRACE_FILE, 'r', errors='ignore') as fin: | 131 | with open(_TRACE_FILE, 'r', errors='ignore') as fin: |
| 132 | with open(temp_file, 'w') as tf: | 132 | with open(temp_file, 'w') as tf: |
| 133 | trace_lines = fin.readlines() | 133 | trace_lines = fin.readlines() |
| 134 | for i , l in enumerate(trace_lines): | 134 | for i, l in enumerate(trace_lines): |
| 135 | if 'END:' in l and _NEW_COMMAND_SEP in l: | 135 | if 'END:' in l and _NEW_COMMAND_SEP in l: |
| 136 | tf.writelines(trace_lines[i+1:]) | 136 | tf.writelines(trace_lines[i + 1:]) |
| 137 | break | 137 | break |
| 138 | platform_utils.rename(temp_file, _TRACE_FILE) | 138 | platform_utils.rename(temp_file, _TRACE_FILE) |
