Spaces:
Sleeping
Sleeping
#!C:\Python312\python.exe | |
# encoding: utf-8 | |
"""Find and (optionally) delete corrupt Whisper data files""" | |
import argparse | |
import os | |
import sys | |
import logging | |
try: | |
import whisper | |
except ImportError: | |
raise SystemExit("[ERROR] Please make sure Whisper is installed properly") | |
def setup_logging(verbose=False): | |
"""Configure logging.""" | |
logging.basicConfig( | |
level=logging.DEBUG if verbose else logging.INFO, | |
format="%(asctime)s [%(levelname)s]: %(message)s", | |
datefmt="%Y-%m-%d %H:%M:%S", | |
) | |
def walk_dir(base_dir, delete_corrupt=False, backup_corrupt=False): | |
"""Walk through directories to find and handle corrupt Whisper files.""" | |
total_files = 0 | |
corrupt_files = 0 | |
deleted_files = 0 | |
for dirpath, _, filenames in os.walk(base_dir): | |
logging.info("Scanning %s...", dirpath) | |
whisper_files = (os.path.join(dirpath, f) for f in filenames if f.endswith(".wsp")) | |
for f in whisper_files: | |
total_files += 1 | |
try: | |
info = whisper.info(f) | |
logging.debug("%s: %d points", f, sum(i["points"] for i in info.get("archives", {}))) | |
except whisper.CorruptWhisperFile: | |
corrupt_files += 1 | |
if backup_corrupt: | |
backup_path = f + ".bak" | |
try: | |
os.rename(f, backup_path) | |
logging.warning("Backed up corrupt file: %s -> %s", f, backup_path) | |
except OSError as e: | |
logging.error("Failed to back up %s: %s", f, e) | |
continue | |
if delete_corrupt: | |
try: | |
os.unlink(f) | |
deleted_files += 1 | |
logging.warning("Deleted corrupt file: %s", f) | |
except OSError as e: | |
logging.error("Failed to delete %s: %s", f, e) | |
else: | |
logging.error("Corrupt Whisper file: %s", f) | |
logging.info("Summary: Scanned %d files, Found %d corrupt, Deleted %d", total_files, corrupt_files, deleted_files) | |
return total_files, corrupt_files, deleted_files | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description=__doc__.strip()) | |
parser.add_argument("--delete-corrupt", action="store_true", help="Delete reported corrupt files") | |
parser.add_argument("--backup-corrupt", action="store_true", help="Back up corrupt files before deletion") | |
parser.add_argument("--verbose", action="store_true", help="Display detailed progress") | |
parser.add_argument("directories", type=str, nargs="+", metavar="WHISPER_DIR", help="Directory containing Whisper files") | |
args = parser.parse_args() | |
setup_logging(verbose=args.verbose) | |
for d in args.directories: | |
d = os.path.realpath(d) | |
if not os.path.isdir(d): | |
logging.error("%s is not a directory!", d) | |
continue | |
walk_dir(d, delete_corrupt=args.delete_corrupt, backup_corrupt=args.backup_corrupt) | |