@@ -253,38 +253,40 @@ def process_log(out_path, concat_log):
253
253
stop += 1
254
254
255
255
256
- def concat_logs (step , out_path , listOfFiles ):
256
+ def concat_logs (step , out_path , listOfFiles , default_dir = None ):
257
+ if not os .path .isabs (out_path ):
258
+ out_path = os .path .abspath (out_path )
259
+
257
260
if '_tmp/' in out_path :
258
- # find parent directory for files in temporary directory
259
261
out_dir = os .path .dirname (os .path .dirname (out_path ))
260
262
else :
261
- # parent directory for out_path outputs
262
263
out_dir = os .path .dirname (out_path )
264
+
265
+ if not out_dir :
266
+ if default_dir :
267
+ out_dir = default_dir
268
+ else :
269
+ raise ValueError ("Output directory is empty and no default directory provided." )
270
+
271
+ if not os .path .exists (out_dir ):
272
+ raise FileNotFoundError (f"Output directory does not exist: { out_dir } " )
263
273
264
- # find all genotools created logs in out_dir
265
274
log_paths = []
266
275
for file in os .listdir (out_dir ):
267
276
if file .endswith ("_all_logs.log" ):
268
- # log_exists = True
269
277
log_paths .append (file )
270
278
271
- # if no genotools logs exist, create one
272
279
if len (log_paths ) == 0 :
273
- log_path = os .path .join (out_dir , os .path .split (out_path )[1 ])
280
+ log_path = os .path .join (out_dir , os .path .split (out_path )[1 ] + "_all_logs.log" )
274
281
275
- # if one genotools log exists, point to it
276
- if len (log_paths ) == 1 :
282
+ elif len (log_paths ) == 1 :
277
283
log_path = os .path .join (out_dir , log_paths [0 ])
278
284
279
- # if more than one genotools log exists, point to the one modified most recently
280
- if len (log_paths ) > 1 :
281
- mtimes = {}
282
- for path in log_paths :
283
- mtimes [path ] = os .path .getmtime (os .path .join (out_dir , path ))
284
- most_recent_log = max (zip (mtimes .values (), mtimes .keys ()))[1 ]
285
+ else :
286
+ mtimes = {path : os .path .getmtime (os .path .join (out_dir , path )) for path in log_paths }
287
+ most_recent_log = max (mtimes , key = mtimes .get )
285
288
log_path = os .path .join (out_dir , most_recent_log )
286
289
287
- # combine log files into 1 file
288
290
with open (log_path , "a+" ) as new_file :
289
291
for name in listOfFiles :
290
292
with open (name ) as file :
@@ -293,12 +295,10 @@ def concat_logs(step, out_path, listOfFiles):
293
295
for line in file :
294
296
new_file .write (line )
295
297
new_file .write ("\n " )
296
-
297
- # remove intermediate log files
298
+
298
299
for files in listOfFiles :
299
300
os .remove (files )
300
301
301
- # calls for processing
302
302
with open (log_path , 'r' ) as file :
303
303
out_path = log_path .replace ('_all_logs.log' , '' )
304
304
process_log (out_path , file .readlines ())
0 commit comments