Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.

Commit b7dbce1

Browse files
committed
Return non zero if at least one error occured (fixes #10)
1 parent 0874fb0 commit b7dbce1

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

r128gain/__init__.py

+39-23
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def get_r128_loudness(audio_filepaths, *, calc_peak=True, enable_ffmpeg_threadin
157157

158158

159159
def scan(audio_filepaths, *, album_gain=False, skip_tagged=False, thread_count=None, ffmpeg_path=None, executor=None,
160-
progress=None):
160+
progress=None, boxed_error_count=None):
161161
""" Analyze files, and return a dictionary of filepath to loudness metadata or filepath to future if executor is not None. """
162162
r128_data = {}
163163

@@ -235,6 +235,8 @@ def scan(audio_filepaths, *, album_gain=False, skip_tagged=False, thread_count=N
235235
logger().warning("Failed to analyze file '%s': %s %s" % (audio_filepath,
236236
e.__class__.__qualname__,
237237
e))
238+
if boxed_error_count is not None:
239+
boxed_error_count[0] += 1
238240
else:
239241
if result is not None: # track/album gain was not skipped
240242
r128_data[audio_filepath] = result
@@ -348,9 +350,8 @@ def tag(filepath, loudness, peak, *,
348350
mf["----:COM.APPLE.ITUNES:REPLAYGAIN_ALBUM_PEAK"] = mutagen.mp4.MP4FreeForm(("%.6f" % (album_peak)).encode())
349351

350352
else:
351-
logger().warning("Unhandled '%s' tag format for file '%s'" % (mf.__class__.__name__,
352-
filepath))
353-
return
353+
raise RuntimeError("Unhandled '%s' tag format for file '%s'" % (mf.__class__.__name__,
354+
filepath))
354355

355356
mf.save()
356357

@@ -437,6 +438,8 @@ def show_scan_report(audio_filepaths, album_dir, r128_data):
437438
def process(audio_filepaths, *, album_gain=False, opus_output_gain=False, mtime_second_offset=None, skip_tagged=False,
438439
thread_count=None, ffmpeg_path=None, dry_run=False, report=False):
439440
""" Analyze and tag input audio files. """
441+
error_count = 0
442+
440443
with contextlib.ExitStack() as cm:
441444
if sys.stderr.isatty() and logging.getLogger().isEnabledFor(logging.INFO):
442445
progress = cm.enter_context(tqdm.tqdm(total=len(audio_filepaths) + int(album_gain),
@@ -453,7 +456,8 @@ def process(audio_filepaths, *, album_gain=False, opus_output_gain=False, mtime_
453456
skip_tagged=skip_tagged,
454457
thread_count=thread_count,
455458
ffmpeg_path=ffmpeg_path,
456-
progress=progress)
459+
progress=progress,
460+
boxed_error_count=[error_count])
457461

458462
if report:
459463
show_scan_report(audio_filepaths,
@@ -486,11 +490,16 @@ def process(audio_filepaths, *, album_gain=False, opus_output_gain=False, mtime_
486490
logger().error("Failed to tag file '%s': %s %s" % (audio_filepath,
487491
e.__class__.__qualname__,
488492
e))
493+
error_count += 1
494+
495+
return error_count
489496

490497

491498
def process_recursive(directories, *, album_gain=False, opus_output_gain=False, mtime_second_offset=None,
492499
skip_tagged=False, thread_count=None, ffmpeg_path=None, dry_run=False, report=False):
493500
""" Analyze and tag all audio files recursively found in input directories. """
501+
error_count = 0
502+
494503
if thread_count is None:
495504
try:
496505
thread_count = len(os.sched_getaffinity(0))
@@ -568,6 +577,7 @@ def process_recursive(directories, *, album_gain=False, opus_output_gain=False,
568577
logger().warning("Failed to analyze file '%s': %s %s" % (key,
569578
e.__class__.__qualname__,
570579
e))
580+
error_count += 1
571581
else:
572582
if result is not None:
573583
r128_data[key] = result
@@ -601,6 +611,7 @@ def process_recursive(directories, *, album_gain=False, opus_output_gain=False,
601611
logger().error("Failed to tag file '%s': %s %s" % (audio_filepath,
602612
e.__class__.__qualname__,
603613
e))
614+
error_count += 1
604615

605616
to_del_futures.add(done_future)
606617
for f in other_dir_futures:
@@ -609,6 +620,8 @@ def process_recursive(directories, *, album_gain=False, opus_output_gain=False,
609620
for to_del_future in to_del_futures:
610621
del futures[to_del_future]
611622

623+
return error_count
624+
612625

613626
def cl_main():
614627
# parse args
@@ -704,25 +717,28 @@ def cl_main():
704717

705718
# main
706719
if args.recursive:
707-
process_recursive(args.path,
708-
album_gain=args.album_gain,
709-
opus_output_gain=args.opus_output_gain,
710-
mtime_second_offset=args.mtime_second_offset,
711-
skip_tagged=args.skip_tagged,
712-
thread_count=args.thread_count,
713-
ffmpeg_path=args.ffmpeg_path,
714-
dry_run=args.dry_run,
715-
report=logging.getLogger().isEnabledFor(logging.INFO) or args.dry_run)
720+
err_count = process_recursive(args.path,
721+
album_gain=args.album_gain,
722+
opus_output_gain=args.opus_output_gain,
723+
mtime_second_offset=args.mtime_second_offset,
724+
skip_tagged=args.skip_tagged,
725+
thread_count=args.thread_count,
726+
ffmpeg_path=args.ffmpeg_path,
727+
dry_run=args.dry_run,
728+
report=logging.getLogger().isEnabledFor(logging.INFO) or args.dry_run)
716729
else:
717-
process(args.path,
718-
album_gain=args.album_gain,
719-
opus_output_gain=args.opus_output_gain,
720-
mtime_second_offset=args.mtime_second_offset,
721-
skip_tagged=args.skip_tagged,
722-
thread_count=args.thread_count,
723-
ffmpeg_path=args.ffmpeg_path,
724-
dry_run=args.dry_run,
725-
report=logging.getLogger().isEnabledFor(logging.INFO) or args.dry_run)
730+
err_count = process(args.path,
731+
album_gain=args.album_gain,
732+
opus_output_gain=args.opus_output_gain,
733+
mtime_second_offset=args.mtime_second_offset,
734+
skip_tagged=args.skip_tagged,
735+
thread_count=args.thread_count,
736+
ffmpeg_path=args.ffmpeg_path,
737+
dry_run=args.dry_run,
738+
report=logging.getLogger().isEnabledFor(logging.INFO) or args.dry_run)
739+
740+
if err_count > 0:
741+
exit(1)
726742

727743

728744
if getattr(sys, "frozen", False):

0 commit comments

Comments
 (0)