summaryrefslogtreecommitdiffstats
path: root/repo_trace.py
diff options
context:
space:
mode:
Diffstat (limited to 'repo_trace.py')
-rw-r--r--repo_trace.py70
1 files changed, 35 insertions, 35 deletions
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
65class Trace(ContextDecorator): 65class 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
113def _GetTraceFile(): 112def _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
121def _ClearOldTraces(): 121def _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)