Add check for no-files-found

This commit is contained in:
Fred Boniface 2023-10-02 11:42:54 +01:00
parent 41c817f1fc
commit 79080d2752
2 changed files with 18 additions and 84 deletions

21
regain
View File

@ -22,16 +22,26 @@
# SOFTWARE. # SOFTWARE.
import os import os
import sys
import subprocess import subprocess
import logging import logging
import argparse import argparse
FORMATS = ["flac", "m4a", "aac", "alac", "mp3", "m3a", "ogg", "opus", "oga"] FORMATS = ["flac", "m4a", "aac", "alac", "mp3", "m3a", "ogg", "opus", "oga"]
PATH_TO_REGAINER = "./regainer.py" PATH_TO_REGAINER = "./regainer.py"
DESCRIPTION = """
regain - Calls regainer.py on any albums found in subdirectories.
\n\n
Run the regain command in a directory that contains albums and ReplayGain tags will be
added to the files, including album tags.
\n\n
regain will walk through all subdirectories so can be run in your 'Music' folder, or
individual artist/album folders and tag as many albums as neccessary.
"""
def parse_arguments(): def parse_arguments():
parser = argparse.ArgumentParser(description='Call regainer.py on subdirectories containing music files') parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('--debug', action='store_true', help='Enable debug logging') parser.add_argument('--debug', action='store_true', help='Enable debug logging (including regainer output)')
return parser.parse_args() return parser.parse_args()
def configureLogging(debug_mode = False): def configureLogging(debug_mode = False):
@ -64,7 +74,12 @@ if __name__ == "__main__":
logging.debug("Debug logs enabled") logging.debug("Debug logs enabled")
file_lists = find_files_by_extension(workdir) file_lists = find_files_by_extension(workdir)
logging.debug(f"File lists: {file_lists}") logging.debug(f"File lists: {file_lists}")
logging.info(f"Files for processing: {countFiles(file_lists)}") file_count = countFiles(file_lists)
if file_count == 0:
logging.error("No files to process")
print("You should run this command from the parent directory of any album folders")
sys.exit(1)
logging.info(f"Files for processing: {file_count}")
cmd = ["python", PATH_TO_REGAINER, "-f"] cmd = ["python", PATH_TO_REGAINER, "-f"]
for sublist in file_lists: for sublist in file_lists:

81
regains
View File

@ -1,81 +0,0 @@
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use File::Find;
use Getopt::Long;
my @FORMATS = ("flac", "m4a", "aac", "alac", "mp3", "m3a", "ogg", "opus", "oga");
my $PATH_TO_REGAINER = "./regainer.py";
my $debug = 0;
# Parse command-line arguments
GetOptions("debug" => \$debug);
# Configure logging
if ($debug) {
$| = 1; # Disable buffering for STDOUT
print "Debug logs enabled\n";
}
my $workdir = getcwd();
print "regain.pl WorkDir: $workdir\n";
my @file_lists = find_files_by_extension($workdir);
print "Files for processing: ", countFiles(\@file_lists), "\n";
my @cmd = ("python", $PATH_TO_REGAINER, "-f");
foreach my $sublist (@file_lists) {
push @cmd, "-a";
push @cmd, @$_ foreach @$sublist;
}
print "Running command: @cmd\n";
print "Running regainer.py\n";
if ($debug) {
open my $stdout_saved, ">&STDOUT";
open my $stderr_saved, ">&STDERR";
open STDOUT, ">", undef;
open STDERR, ">", undef;
my $result = system(@cmd);
open STDOUT, ">&", $stdout_saved;
open STDERR, ">&", $stderr_saved;
print "regainer.py has exited with code $result\n";
if ($result != 0) {
print "You can re-run with --debug to view the output from regainer\n";
} else {
print "Files successfully tagged\n";
}
} else {
system(@cmd);
}
sub find_files_by_extension {
my ($workdir) = @_;
my @file_lists;
find(sub {
if (-f && /\.(@FORMATS)$/) {
push @{$file_lists[-1]}, $File::Find::name;
}
}, $workdir);
return \@file_lists;
}
sub countFiles {
my ($file_list_of_lists) = @_;
my $count = 0;
foreach my $item (@$file_list_of_lists) {
$count += scalar(@$item);
}
return $count;
}