forked from FozzTexx/viddin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenumber
executable file
·108 lines (90 loc) · 3.28 KB
/
renumber
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
#!/usr/bin/env python3
import argparse
import os, sys
import tempfile
def build_argparser():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("first", help="number of first episode")
parser.add_argument("files", nargs="+", help="files to rename", default=".")
parser.add_argument("--skip", type=int, help="use only every nth file")
parser.add_argument("--dryrun", action="store_true", help="Don't actually rename files")
return parser
def main():
args = build_argparser().parse_args()
start = args.first.split('x')
season = int(start[0])
episode = int(start[1])
orig_name = args.files
new_name = []
skip = 1
if args.skip:
skip = args.skip
for idx, old_file in enumerate(orig_name):
path = os.path.dirname(old_file)
base = os.path.basename(old_file)
if idx % skip != 0:
continue
_, ext = os.path.splitext(base)
new_file = "%ix%02i" % (season, episode + idx / skip) + ext
new_path = os.path.join(path, new_file)
new_name.append(new_path)
# Don't try to rename anything that ended up with the same name
old_to_new = list(zip(orig_name, new_name))
for row in old_to_new:
if row[0] == row[1]:
orig_name.remove(row[0])
new_name.remove(row[1])
old_to_new = list(zip(orig_name, new_name))
old_set = set(orig_name)
new_set = set(new_name)
# Sanity check - make sure all the from files exist
for path in old_set:
if not os.path.exists(path):
print(path, "does not exist - aborting", file=sys.stderr)
exit(1)
# Sanity check - make sure that files that exist are ones that will be renamed
for path in new_set:
if os.path.exists(path) and path not in old_set:
print(path, "already exists and is not going to be renamed", file=sys.stderr)
exit(1)
if len(old_to_new) > 0:
ordered = []
while True:
no_collision = list(new_set - old_set)
if len(no_collision) == 0:
move_name = old_to_new[0][0]
_, ext = os.path.splitext(move_name)
move_abs = os.path.abspath(move_name)
dpath = os.path.dirname(move_abs)
tf = tempfile.NamedTemporaryFile(suffix=ext, dir=dpath, delete=False)
prefix = os.path.commonprefix([move_abs, tf.name])
new_name = tf.name[len(prefix):]
old_to_new[0] = (new_name, old_to_new[0][1])
old_set = old_set - set([move_name])
os.remove(new_name)
if not args.dryrun:
print("Renamed " + move_name + " to " + new_name)
os.rename(move_name, new_name)
continue
no_collision.sort()
will_rename = []
p = no_collision[0]
for o in old_to_new:
if p == o[1]:
will_rename.append(o)
old_to_new = [i for i in old_to_new if i not in will_rename]
ordered.extend(will_rename)
old_set.difference_update(set([i[0] for i in will_rename]))
new_set.difference_update(set([i[1] for i in will_rename]))
if len(old_to_new) == 0:
break
for old_path, new_path in ordered:
if not os.path.exists(new_path):
if not args.dryrun:
os.rename(old_path, new_path)
print("Renamed " + old_path + " to " + new_path)
else:
print("ALREADY EXISTS", new_path)
return
if __name__ == '__main__':
exit(main() or 0)