6
6
import glob
7
7
import json
8
8
import pandas as pd
9
+ import sys
9
10
from datetime import datetime
10
11
12
+ class Tee :
13
+ """Tee output to both stdout/stderr and a log file"""
14
+ def __init__ (self , data_dir , name = 'output.log' ):
15
+ self .log_path = os .path .join (data_dir , name )
16
+ self .file = open (self .log_path , 'a' )
17
+ self .stdout = sys .stdout
18
+ self .stderr = sys .stderr
19
+ sys .stdout = self
20
+ sys .stderr = self
21
+
22
+ def write (self , data ):
23
+ self .file .write (data )
24
+ self .stdout .write (data )
25
+ self .file .flush ()
26
+
27
+ def flush (self ):
28
+ self .file .flush ()
29
+
30
+ def close (self ):
31
+ sys .stdout = self .stdout
32
+ sys .stderr = self .stderr
33
+ self .file .close ()
34
+
11
35
class path_handler ():
12
36
13
37
def __init__ (self ):
@@ -28,9 +52,10 @@ class pipeline_graph_check():
28
52
"""
29
53
30
54
def __init__ (self , data_dir ):
55
+ self .data_dir = data_dir
56
+ self .tee = Tee (data_dir )
31
57
self .load_graph ()
32
58
self .check_graph ()
33
- self .data_dir = data_dir
34
59
35
60
def load_graph (self ):
36
61
"""
@@ -129,10 +154,19 @@ def write_to_log(self, script_path, type = 'attempted'):
129
154
if 'attempted' not in log_dict .keys ():
130
155
log_dict ['attempted' ] = {}
131
156
log_dict ['attempted' ][script_path ] = current_datetime
157
+ print ('============================================================' )
158
+ print (f'Attempting { os .path .basename (script_path )} , started at { current_datetime } ' )
159
+ print ('============================================================' )
132
160
elif type == 'completed' :
133
161
if 'completed' not in log_dict .keys ():
134
162
log_dict ['completed' ] = {}
135
163
log_dict ['completed' ][script_path ] = current_datetime
164
+ print ('============================================================' )
165
+ print (f'Completed { os .path .basename (script_path )} , ended at { current_datetime } ' )
166
+ print ('============================================================' )
167
+ # Close tee when completing
168
+ if hasattr (self , 'tee' ):
169
+ self .tee .close ()
136
170
# log_dict[script_path] = current_datetime
137
171
with open (self .log_path , 'w' ) as log_file_connect :
138
172
json .dump (log_dict , log_file_connect , indent = 4 )
0 commit comments