Skip to content

Commit

Permalink
more debug
Browse files Browse the repository at this point in the history
  • Loading branch information
RichieHakim committed Apr 2, 2024
1 parent 39884c6 commit 8004f8b
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions bnpm/path_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,28 +372,14 @@ def check_files_openable(dir_outer, depth=2, time_limit_per_file=1, verbose=Fals
indicating whether the file can be opened.
"""
import os
from concurrent.futures import ThreadPoolExecutor, TimeoutError
from multiprocessing import Pool, TimeoutError
from contextlib import contextmanager

def check_file_openable(file_path):
"""
Check if a file can be opened.
"""
try:
with open(file_path, 'rb') as f:
## Read a maximum of 1024 bytes. If file is smaller, read the whole file.
f.read(1024)
print(f"File {file_path} can be opened.") if verbose > 1 else None
return True
except Exception as e:
print(f"File {file_path} could not be opened: {e}") if verbose > 0 else None
return False

def check_with_timeout(file_path, time_limit):
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(check_file_openable, file_path)
with Pool(processes=1) as executor:
result = executor.apply_async(_check_file_openable, (file_path, verbose,))
try:
return future.result(timeout=time_limit)
return result.get(timeout=time_limit)
except TimeoutError:
print(f"File {file_path} took too long to open.") if verbose > 0 else None
return False
Expand All @@ -414,4 +400,16 @@ def walk_files(dir_outer, depth):
files = walk_files(dir_outer, depth)
file_openable = {file: check_with_timeout(file, time_limit=time_limit_per_file) for file in files}
return file_openable

def _check_file_openable(file_path, verbose=False):
"""
Check if a file can be opened.
"""
try:
with open(file_path, 'rb') as f:
## Read a maximum of 1024 bytes. If file is smaller, read the whole file.
f.read(1024)
print(f"File {file_path} can be opened.") if verbose > 1 else None
return True
except Exception as e:
print(f"File {file_path} could not be opened: {e}") if verbose > 0 else None
return False

0 comments on commit 8004f8b

Please sign in to comment.