diff options
author | Joanna Wang <jojwang@google.com> | 2022-11-08 18:56:52 -0500 |
---|---|---|
committer | Joanna Wang <jojwang@google.com> | 2022-11-09 01:24:49 +0000 |
commit | 24c6314fca400f86fd893053c79788a8ab681477 (patch) | |
tree | f54c5f6a6b58800b3005b38644f25085c16e03b4 /repo_trace.py | |
parent | 7efab539f0fc78aa759000a922c30ac45e84530b (diff) | |
download | git-repo-24c6314fca400f86fd893053c79788a8ab681477.tar.gz |
Fix TRACE_FILE renaming.v2.29.8
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>
Diffstat (limited to 'repo_trace.py')
-rw-r--r-- | repo_trace.py | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/repo_trace.py b/repo_trace.py index 03542950..86cbfc62 100644 --- a/repo_trace.py +++ b/repo_trace.py | |||
@@ -22,10 +22,11 @@ To also include trace outputs in stderr do `repo --trace_to_stderr ...` | |||
22 | 22 | ||
23 | import sys | 23 | import sys |
24 | import os | 24 | import os |
25 | import tempfile | ||
26 | import time | 25 | import time |
27 | from contextlib import ContextDecorator | 26 | from contextlib import ContextDecorator |
28 | 27 | ||
28 | import platform_utils | ||
29 | |||
29 | # Env var to implicitly turn on tracing. | 30 | # Env var to implicitly turn on tracing. |
30 | REPO_TRACE = 'REPO_TRACE' | 31 | REPO_TRACE = 'REPO_TRACE' |
31 | 32 | ||
@@ -38,7 +39,7 @@ _TRACE_FILE = None | |||
38 | 39 | ||
39 | _TRACE_FILE_NAME = 'TRACE_FILE' | 40 | _TRACE_FILE_NAME = 'TRACE_FILE' |
40 | 41 | ||
41 | _MAX_SIZE = 5 # in mb | 42 | _MAX_SIZE = 70 # in mb |
42 | 43 | ||
43 | _NEW_COMMAND_SEP = '+++++++++++++++NEW COMMAND+++++++++++++++++++' | 44 | _NEW_COMMAND_SEP = '+++++++++++++++NEW COMMAND+++++++++++++++++++' |
44 | 45 | ||
@@ -123,7 +124,7 @@ def _GetTraceFile(): | |||
123 | return trace_file | 124 | return trace_file |
124 | 125 | ||
125 | def _ClearOldTraces(): | 126 | def _ClearOldTraces(): |
126 | """Clear traces from old commands if trace file is too big. | 127 | """Clear the oldest commands if trace file is too big. |
127 | 128 | ||
128 | Note: If the trace file contains output from two `repo` | 129 | Note: If the trace file contains output from two `repo` |
129 | commands that were running at the same time, this | 130 | commands that were running at the same time, this |
@@ -131,12 +132,12 @@ def _ClearOldTraces(): | |||
131 | """ | 132 | """ |
132 | if os.path.isfile(_TRACE_FILE): | 133 | if os.path.isfile(_TRACE_FILE): |
133 | while os.path.getsize(_TRACE_FILE)/(1024*1024) > _MAX_SIZE: | 134 | while os.path.getsize(_TRACE_FILE)/(1024*1024) > _MAX_SIZE: |
134 | temp = tempfile.NamedTemporaryFile(mode='w', delete=False) | 135 | temp_file = _TRACE_FILE + '.tmp' |
135 | with open(_TRACE_FILE, 'r', errors='ignore') as fin: | 136 | with open(_TRACE_FILE, 'r', errors='ignore') as fin: |
136 | trace_lines = fin.readlines() | 137 | with open(temp_file, 'w') as tf: |
137 | for i , l in enumerate(trace_lines): | 138 | trace_lines = fin.readlines() |
138 | if 'END:' in l and _NEW_COMMAND_SEP in l: | 139 | for i , l in enumerate(trace_lines): |
139 | temp.writelines(trace_lines[i+1:]) | 140 | if 'END:' in l and _NEW_COMMAND_SEP in l: |
140 | break | 141 | tf.writelines(trace_lines[i+1:]) |
141 | temp.close() | 142 | break |
142 | os.replace(temp.name, _TRACE_FILE) | 143 | platform_utils.rename(temp_file, _TRACE_FILE) |