-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_files.py
executable file
·129 lines (120 loc) · 4.62 KB
/
move_files.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python3
import os.path
import shutil
import pathlib
import anime
import configparser
import argparse
AID_PAD_WIDTH = 6
AID_CHUNK_SIZE = 2
def get_config(config_file: str = "weltschmerz.cfg"):
config = configparser.ConfigParser()
config.read_file(open(config_file))
parser = argparse.ArgumentParser(
description="Move known local files to target directory."
)
parser.add_argument(
"--database", help="database to use", default=config.get("client", "database")
)
parser.add_argument(
"--source-basedir",
help="source folder to process",
default=None,
dest="source_basedir",
)
parser.add_argument(
"--target-basedir",
help="target basedir to move files to",
default=None,
dest="target_basedir",
)
parser.add_argument(
"--dry-run",
"-n",
help="dry-run, no moving files",
default=False,
dest="dry_run",
action="store_true",
)
parser.add_argument(
"--target-crc32",
"-C",
help="add crc32 to target filename",
default=False,
dest="target_crc32",
action="store_true",
)
args = parser.parse_args()
return args
if __name__ == "__main__":
config = get_config()
dbs = anime.DatabaseSession(config.database, False)
known_files = (
dbs.session.query(anime.LocalFile)
.join(anime.File, anime.LocalFile.hash_ed2k == anime.File.hash_ed2k)
.filter(anime.LocalFile.directory.like(f'{config.source_basedir.rstrip("/")}%'))
.all()
)
print(len(known_files))
# sys.exit(0)
for local_file in known_files:
known_file = (
dbs.session.query(anime.File)
.filter(anime.File.hash_ed2k == local_file.hash_ed2k)
.one()
)
aid_padded = str(known_file.aid).zfill(AID_PAD_WIDTH)
aid_path = os.path.join(
*[
aid_padded[i : i + AID_CHUNK_SIZE]
for i in range(0, AID_PAD_WIDTH, AID_CHUNK_SIZE)
]
)
target_dir = os.path.join(config.target_basedir, "by-id", aid_path)
if local_file.directory not in [target_dir]:
if os.path.isfile(local_file.full_path):
print(
f'file not sorted yet "{local_file.filename}" is in "{local_file.directory}", should be "{target_dir}"'
)
if not os.path.isfile(os.path.join(target_dir, local_file.filename)):
pathlib.Path(target_dir).mkdir(parents=True, exist_ok=True)
print(
f'##### INFO: moving "{local_file.full_path}" to "{target_dir}"'
)
if not config.dry_run:
shutil.move(
os.path.join(local_file.directory, local_file.filename),
target_dir,
)
local_file.directory = target_dir
dbs.session.merge(local_file)
dbs.session.commit()
else:
print("##### INFO: dry-run requested, not moving file")
else:
if config.target_crc32:
print(
f'##### INFO: moving "{local_file.full_path}" to "{target_dir}", adding CRC32 to the filename'
)
target_filename = (
local_file.filename[
: -len(local_file.filename.split(".")[-1]) - 1
]
+ f'[{local_file.hash_crc}].{local_file.filename.split(".")[-1]}'
)
print(f'##### INFO: target filename "{target_filename}"')
if not config.dry_run:
shutil.move(
os.path.join(local_file.directory, local_file.filename),
os.path.join(target_dir, target_filename),
)
local_file.directory = target_dir
local_file.filename = target_filename
dbs.session.merge(local_file)
dbs.session.commit()
else:
print("##### INFO: dry-run requested, not moving file")
else:
print(
f'##### WARNING: "{os.path.join(target_dir, local_file.filename)}" already present'
)