File size: 3,162 Bytes
5e1b738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!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)