Skip to content

Commit 5d32365

Browse files
author
Chris Putnam
committed
quieter output by default, improved error handling
1 parent 1e6cacd commit 5d32365

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

binmerge

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
#
77
# Please report any bugs on GitHub: https://github.com/putnam/binmerge
88

9-
import argparse, re, os, subprocess
9+
import argparse, re, os, subprocess, sys
10+
VERBOSE = False
11+
12+
def d(s):
13+
if VERBOSE:
14+
print("[DEBUG]\t%s" % s)
15+
16+
def e(s):
17+
print("[ERROR]\t%s" % s)
18+
19+
def p(s):
20+
print("[INFO]\t%s" % s)
1021

1122
class Track:
1223
globalBlocksize = None
@@ -37,7 +48,7 @@ class Track:
3748
Track.globalBlocksize = 2048
3849
elif track_type in ['MODE2/2336', 'CDI/2336']:
3950
Track.globalBlocksize = 2336
40-
print("Locked blocksize to %d" % Track.globalBlocksize)
51+
d("Locked blocksize to %d" % Track.globalBlocksize)
4152

4253
class File:
4354
def __init__(self, filename):
@@ -74,17 +85,17 @@ def read_cue_file(cue_path):
7485
next_item_offset = t.indexes[0]["file_offset"]
7586

7687
for f in files:
77-
print("-- File --")
78-
print("Filename: %s" % f.filename)
79-
print("Size: %d" % f.size)
80-
print("Tracks:")
88+
d("-- File --")
89+
d("Filename: %s" % f.filename)
90+
d("Size: %d" % f.size)
91+
d("Tracks:")
8192

8293
for t in f.tracks:
83-
print(" -- Track --")
84-
print(" Num: %d" % t.num)
85-
print(" Type: %s" % t.track_type)
86-
if t.sectors: print(" Sectors: %s" % t.sectors)
87-
print(" Indexes: %s" % repr(t.indexes))
94+
d(" -- Track --")
95+
d(" Num: %d" % t.num)
96+
d(" Type: %s" % t.track_type)
97+
if t.sectors: d(" Sectors: %s" % t.sectors)
98+
d(" Indexes: %s" % repr(t.indexes))
8899

89100
return files
90101

@@ -146,6 +157,10 @@ def gen_split_cuesheet(basename, merged_file):
146157

147158
# Merges files together to new file `merged_filename`, in listed order.
148159
def merge_files(merged_filename, files):
160+
if os.path.exists(merged_filename):
161+
e('Target merged bin path already exists: %s' % merged_filename)
162+
return False
163+
149164
# cat is actually a bit faster, but this is multi-platform and no special-casing
150165
chunksize = 1024 * 1024
151166
with open(merged_filename, 'wb') as outfile:
@@ -161,6 +176,13 @@ def merge_files(merged_filename, files):
161176
# Writes each track in a File to a new file
162177
def split_files(new_basename, merged_file):
163178
with open(merged_file.filename, 'rb') as infile:
179+
# Check all tracks for potential file-clobbering first before writing anything
180+
for t in merged_file.tracks:
181+
out_name = track_filename(new_basename, t.num, len(merged_file.tracks))
182+
if os.path.exists(out_name):
183+
e('Target bin path already exists: %s' % out_name)
184+
return False
185+
164186
for t in merged_file.tracks:
165187
chunksize = 1024 * 1024
166188
out_name = track_filename(new_basename, t.num, len(merged_file.tracks))
@@ -178,14 +200,19 @@ def split_files(new_basename, merged_file):
178200
return True
179201

180202
def main():
181-
parser = argparse.ArgumentParser(description="Using a cuesheet, merges numerous bin files into a single bin file and produces a new cuesheet with corrected offsets. Works great with Redump. Supports all block modes, but only binary track types. Should work on any python3 platform.")
182-
parser.add_argument('cuefile', help='path to source cuefile with multiple referenced bin tracks')
203+
parser = argparse.ArgumentParser(description="Using a cuesheet, merges numerous bin files into a single bin file and produces a new cuesheet with corrected offsets. Works great with Redump. Supports all block modes, but only binary track types.")
204+
parser.add_argument('cuefile', help='path to current cue file (bin files are expected in the same dir)')
183205
parser.add_argument('basename', help='name (without extension) for your new bin/cue files')
184-
parser.add_argument('--split', help='reverses operation, splitting merged files back to individual tracks', required=False, action="store_true")
206+
parser.add_argument('--split', help='reverses operation, splitting merged files back to individual tracks', required=False, action='store_true')
185207
parser.add_argument('-o', dest='outdir', required=False, default=False, help='output directory. defaults to the same directory as source cue')
208+
parser.add_argument('-v', dest='verbose', action='store_true', help='print more verbose messages')
186209
args = parser.parse_args()
187210

211+
if args.verbose:
212+
global VERBOSE
213+
VERBOSE = True
188214

215+
p("Opening cue: %s" % args.cuefile)
189216
cue_map = read_cue_file(args.cuefile)
190217
if args.split:
191218
cuesheet = gen_split_cuesheet(args.basename, cue_map[0])
@@ -197,24 +224,34 @@ def main():
197224
outdir = args.outdir
198225

199226
if not os.path.exists(args.outdir):
200-
print("Output dir does not exist")
227+
e("Output dir does not exist")
201228
return False
202229

203-
with open(os.path.join(outdir, args.basename+'.cue'), 'w', newline='\r\n') as f:
204-
f.write(cuesheet)
205-
print("Wrote %s" % args.basename+'.cue')
230+
new_cue_fn = os.path.join(outdir, args.basename+'.cue')
231+
if os.path.exists(new_cue_fn):
232+
e("Output cue file already exists. Quitting. Path: %s" % new_cue_fn)
233+
return False
206234

207235
if args.split:
208-
print("Splitting files...")
236+
p("Splitting files...")
209237
if split_files(os.path.join(outdir, args.basename), cue_map[0]):
210-
print("Wrote %d bin files" % len(cue_map[0].tracks))
238+
p("Wrote %d bin files" % len(cue_map[0].tracks))
211239
else:
212-
print("Unable to split bin files")
240+
e("Unable to split bin files.")
241+
return False
213242
else:
214-
print("Merging files...")
243+
p("Merging %d tracks..." % len(cue_map))
215244
if merge_files(os.path.join(outdir, args.basename+'.bin'), cue_map):
216-
print("Wrote %s" % args.basename+'.bin')
245+
p("Wrote %s" % args.basename+'.bin')
217246
else:
218-
print("Unable to merge bin files")
247+
e("Unable to merge bin files.")
248+
return False
249+
250+
with open(new_cue_fn, 'w', newline='\r\n') as f:
251+
f.write(cuesheet)
252+
p("Wrote new cue: %s" % new_cue_fn)
253+
254+
return True
219255

220-
main()
256+
if not main():
257+
sys.exit(1)

0 commit comments

Comments
 (0)