-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_deleted.py
executable file
·61 lines (53 loc) · 1.76 KB
/
remove_deleted.py
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
#!/usr/bin/python3
import anime
import os.path
import configparser
import argparse
def get_config(config_file: str = "weltschmerz.cfg"):
config = configparser.ConfigParser()
config.read_file(open(config_file))
parser = argparse.ArgumentParser(description="Hash files and add info to database.")
parser.add_argument(
"--database", help="database to use", default=config.get("client", "database")
)
parser.add_argument(
"--log-file",
dest="log_file",
help="logfile to use",
default=config.get("client", "log"),
)
parser.add_argument("--folder", help="folder to query", default=None)
parser.add_argument(
"--dry-run",
"-n",
help="dry-run, no removing deleted files",
default=False,
dest="dry_run",
action="store_true",
)
args = parser.parse_args()
return args
if __name__ == "__main__":
config = get_config()
dbs = anime.DatabaseSession(config.database, False)
if config.folder:
known_files = (
dbs.session.query(anime.LocalFile)
.filter(anime.LocalFile.directory.like(f'{config.folder.rstrip("/")}%'))
.all()
)
else:
known_files = dbs.session.query(anime.LocalFile).all()
print(len(known_files))
for known_file in known_files:
if not os.path.isfile(known_file.full_path):
print(f"###### removing {known_file.full_path}")
if not config.dry_run:
dbs.session.delete(known_file)
else:
print("##### INFO: dry-run requested, not removing deleted file")
else:
continue
print(f"###### found {known_file.full_path}")
if not config.dry_run:
dbs.session.commit()