@@ -64,60 +64,60 @@ def _SetTraceFile():
64
64
65
65
class Trace (ContextDecorator ):
66
66
67
- def _time (self ):
68
- """Generate nanoseconds of time in a py3.6 safe way"""
69
- return int (time .time ()* 1e+9 )
67
+ def _time (self ):
68
+ """Generate nanoseconds of time in a py3.6 safe way"""
69
+ return int (time .time () * 1e+9 )
70
70
71
- def __init__ (self , fmt , * args , first_trace = False ):
72
- if not IsTrace ():
73
- return
74
- self ._trace_msg = fmt % args
71
+ def __init__ (self , fmt , * args , first_trace = False ):
72
+ if not IsTrace ():
73
+ return
74
+ self ._trace_msg = fmt % args
75
75
76
- if not _TRACE_FILE :
77
- _SetTraceFile ()
76
+ if not _TRACE_FILE :
77
+ _SetTraceFile ()
78
78
79
- if first_trace :
80
- _ClearOldTraces ()
81
- self ._trace_msg = '%s %s' % ( _NEW_COMMAND_SEP , self ._trace_msg )
79
+ if first_trace :
80
+ _ClearOldTraces ()
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 ):
85
- if not IsTrace ():
86
- return self
87
-
88
- print_msg = f'PID: { os .getpid ()} START: { self ._time ()} :' + self ._trace_msg + '\n '
87
+ print_msg = f'PID: { os .getpid ()} START: { self ._time ()} :{ self ._trace_msg } \n '
89
88
90
- with open (_TRACE_FILE , 'a' ) as f :
91
- print (print_msg , file = f )
89
+ with open (_TRACE_FILE , 'a' ) as f :
90
+ print (print_msg , file = f )
92
91
93
- if _TRACE_TO_STDERR :
94
- print (print_msg , file = sys .stderr )
92
+ if _TRACE_TO_STDERR :
93
+ print (print_msg , file = sys .stderr )
95
94
96
- return self
95
+ return self
97
96
98
- def __exit__ (self , * exc ):
99
- if not IsTrace ():
100
- return False
97
+ def __exit__ (self , * exc ):
98
+ if not IsTrace ():
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 :
105
- print (print_msg , file = f )
103
+ with open (_TRACE_FILE , 'a' ) as f :
104
+ print (print_msg , file = f )
106
105
107
- if _TRACE_TO_STDERR :
108
- print (print_msg , file = sys .stderr )
106
+ if _TRACE_TO_STDERR :
107
+ print (print_msg , file = sys .stderr )
109
108
110
- return False
109
+ return False
111
110
112
111
113
112
def _GetTraceFile ():
114
113
"""Get the trace file or create one."""
115
114
# TODO: refactor to pass repodir to Trace.
116
115
repo_dir = os .path .dirname (os .path .dirname (__file__ ))
117
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
118
return trace_file
120
119
120
+
121
121
def _ClearOldTraces ():
122
122
"""Clear the oldest commands if trace file is too big.
123
123
@@ -126,13 +126,13 @@ def _ClearOldTraces():
126
126
will not work precisely.
127
127
"""
128
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
130
temp_file = _TRACE_FILE + '.tmp'
131
131
with open (_TRACE_FILE , 'r' , errors = 'ignore' ) as fin :
132
132
with open (temp_file , 'w' ) as tf :
133
133
trace_lines = fin .readlines ()
134
- for i , l in enumerate (trace_lines ):
134
+ for i , l in enumerate (trace_lines ):
135
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
137
break
138
138
platform_utils .rename (temp_file , _TRACE_FILE )
0 commit comments