improvements

This commit is contained in:
Mayeul Aubin 2025-03-06 15:29:15 +01:00
parent 3dc03cec4f
commit e488275523
11 changed files with 640 additions and 125 deletions

View file

@ -193,8 +193,64 @@ def wait_until_file_exists(filename:str, verbose:int=1, limit:int=24*3600):
from os.path import isfile
k=0
while not isfile(filename):
while not isfile(filename) and k<limit:
if k%60 == -1:
print_message(f"Waiting for {filename} to exist. {k//60} minutes elapsed.", 3, "low level", verbose=verbose)
sleep(1)
print_message(f"File {filename} exists. {k//60} minutes elapsed.", 3, "low level", verbose=verbose)
if k>=limit:
raise TimeoutError(f"File {filename} not found after {limit} seconds.")
if k>60:
print_message(f"File {filename} exists. {k//60} minutes elapsed.", 3, "low level", verbose=verbose)
def get_progress_from_logfile(filename):
"""
Get the number of operations done and the total number of operations from a log file.
"""
current_operation = 0
total_operations = 0
from os.path import isfile
if not isfile(filename):
raise FileNotFoundError(f"Log file {filename} not found.")
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
if " operation " in line:
try:
splitted_line = line.split(" operation ")[1]
splitted_line = splitted_line.split(":")[0]
current_operation = int(splitted_line.split("/")[0])
total_operations = int(splitted_line.split("/")[1])
except:
pass
return current_operation, total_operations
def progress_bar_from_logfile(filename:str, desc:str="", verbose:int=1, **kwargs):
"""
Print a progress bar from a log file.
"""
from tqdm import tqdm
from time import sleep
k=0
limit=3600
update_interval=0.2
sleep(2) # Wait for the process to be launched, and for the previous log file to be overwritten if necessary.
wait_until_file_exists(filename, verbose=verbose, limit=limit)
current_operation, total_operations = get_progress_from_logfile(filename)
previous_operation = 0
if current_operation == total_operations:
# print_message("Progress bar not needed, the process is already finished.", 3, "low level", verbose=verbose)
return
with tqdm(desc=desc, total=total_operations, disable=(verbose==0), **kwargs) as pbar:
while current_operation < total_operations and k/update_interval < limit:
sleep(update_interval)
current_operation, total_operations = get_progress_from_logfile(filename)
if current_operation > previous_operation:
pbar.update(current_operation-previous_operation)
previous_operation = current_operation
k+=1
if k/update_interval >= limit:
print_message(f"Progress bar timed out after {limit} seconds.", 3, "low level", verbose=verbose)