-
Notifications
You must be signed in to change notification settings - Fork 0
/
renseq
159 lines (139 loc) · 4.28 KB
/
renseq
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/env python
import argparse
import itertools
import os
import shutil
import sys
def create_parser():
description = ("Rename and/or renumber a file.\n"
"Set the padding on the source and destination files using c-style formatting "
"(e.g. \"%04d\" for 4 number padding)."
)
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"source",
help="The source sequence template."
)
parser.add_argument(
"--destination",
"-d",
required=False,
help="The destination template. If not passed, uses the source and assumes renumbering of files."
)
parser.add_argument(
"--first",
"-f",
required=True,
type=int,
help="The first frame of the original sequence."
)
parser.add_argument(
"--last",
"-l",
required=True,
type=int,
help="The last frame of the original sequence."
)
parser.add_argument(
"--start",
"-s",
required=True,
type=int,
help="The new start frame."
)
parser.add_argument(
"--step",
"-x",
default=1,
type=int,
help="The step between frames (default=1)."
)
parser.add_argument(
"--respect-breaks",
"-b",
action="store_true",
help="Respect the breaks in source sequences, otherwise create a contiguous sequence (default)."
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Dry run mode."
)
return parser
def rename(src, dest, dry_run):
print "Copying '{}' -> '{}'".format(src, dest)
if not dry_run:
try:
shutil.copy2(src, dest)
except:
print "A copying error has occured", sys.exc_info[1]
raise
else:
print "Copy complete, removing source."
os.remove(src)
def try_rename(sources, destinations, dry_run):
visited = []
for source, dest in itertools.izip(sources, destinations):
try:
rename(source, dest, dry_run)
visited.append((dest, source))
except Exception as err:
print str(err)
print "Reverting changes..."
for src, dst in visited:
rename(src, dst, dry_run)
return False
return True
def rename_files(src, dest, first, last, start, step, breaks, dry_run):
if not dest:
dest = src
use_temp = dest == src and start >= first and start <= last
new_frame = itertools.count(start, step)
frames = xrange(first, last+1)
sources = []
temp_destinations = []
destinations = []
for frame in frames:
source = src % frame
if not os.path.exists(source):
print "'{}' does not exist. Skipping...".format(source)
if breaks:
print "Advancing new frame number to respect sequence break"
new_frame.next()
continue
sources.append(source)
destination = dest % new_frame.next()
destinations.append(destination)
temp_dest = "TMP" + destination
temp_destinations.append(temp_dest)
if any(map(os.path.exists, set(destinations) - set(sources))):
result = raw_input("Some destination paths are going to overwrite existing files. Continue anyway? y/n")
while result not in ["y", "n"]:
result = input("ERROR: Response must be either 'y' or 'n'")
if result == "n":
return
if use_temp:
print "Copying to temp location..."
if not try_rename(sources, temp_destinations, dry_run):
return
else:
temp_destinations = sources
print "Copying to final location..."
if not try_rename(temp_destinations, destinations, dry_run) and use_temp:
for source, temp in itertools.izip(sources, temp_destinations):
rename(temp, source, dry_run)
print "COMPLETE!"
if __name__ == "__main__":
parser = create_parser()
args = parser.parse_args()
rename_files(
args.source,
args.destination,
args.first,
args.last,
args.start,
args.step,
args.respect_breaks,
args.dry_run
)
sys.exit()