Skip to content

Commit 7759606

Browse files
authored
Merge pull request #186 from dvitale199/related_bug
check for abspath for concat_logs for relative path execution
2 parents 24ae4d8 + 8e5980b commit 7759606

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

genotools/utils.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -253,38 +253,40 @@ def process_log(out_path, concat_log):
253253
stop += 1
254254

255255

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+
257260
if '_tmp/' in out_path:
258-
# find parent directory for files in temporary directory
259261
out_dir = os.path.dirname(os.path.dirname(out_path))
260262
else:
261-
# parent directory for out_path outputs
262263
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}")
263273

264-
# find all genotools created logs in out_dir
265274
log_paths = []
266275
for file in os.listdir(out_dir):
267276
if file.endswith("_all_logs.log"):
268-
# log_exists = True
269277
log_paths.append(file)
270278

271-
# if no genotools logs exist, create one
272279
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")
274281

275-
# if one genotools log exists, point to it
276-
if len(log_paths) == 1:
282+
elif len(log_paths) == 1:
277283
log_path = os.path.join(out_dir, log_paths[0])
278284

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)
285288
log_path = os.path.join(out_dir, most_recent_log)
286289

287-
# combine log files into 1 file
288290
with open(log_path, "a+") as new_file:
289291
for name in listOfFiles:
290292
with open(name) as file:
@@ -293,12 +295,10 @@ def concat_logs(step, out_path, listOfFiles):
293295
for line in file:
294296
new_file.write(line)
295297
new_file.write("\n")
296-
297-
# remove intermediate log files
298+
298299
for files in listOfFiles:
299300
os.remove(files)
300301

301-
# calls for processing
302302
with open(log_path, 'r') as file:
303303
out_path = log_path.replace('_all_logs.log', '')
304304
process_log(out_path, file.readlines())

0 commit comments

Comments
 (0)