Added debug mode, modified some outputs.

This commit is contained in:
Uroš Golja
2016-11-13 21:40:33 +01:00
parent 1a7507c6bc
commit 050b0a5802

View File

@@ -14,17 +14,38 @@ use Math::Round;
my $conf = {
src_dir => "./pics",
dst_dir => "./links",
debug => 0,
};
sub msg {
my $text = shift;
print $text;
}
sub msg_error {
my $text = shift;
print STDERR "ERROR: $text";
}
sub msg_warn {
my $text = shift;
print STDERR "warning: $text";
}
sub msg_debug {
my $text = shift;
print "$text" if $conf->{debug};
}
# verify the source dir
if (! -d $conf->{src_dir}) {
print "ERROR: Source directory '$conf->{src_dir}' does not exist. Aborting.\n" ;
msg_error "Source directory '$conf->{src_dir}' does not exist. Aborting.\n" ;
exit 1;
}
# verify the destination dir
if (! -d $conf->{dst_dir}) {
print "ERROR: Destination directory '$conf->{dst_dir}' does not exist. Aborting.\n";
msg_error "Destination directory '$conf->{dst_dir}' does not exist. Aborting.\n";
exit 1;
}
@@ -34,7 +55,7 @@ my @list = find_wanted(
$conf->{src_dir}
);
if (! @list) {
print "No files found.\n";
msg "No files found.\n";
exit 0
}
@@ -45,14 +66,14 @@ my $ars = {};
my $files = {}; # a hashref that holds all the files, their exif data, and their calculcated aspect ratio
foreach my $f (@list) {
$count++;
print "Gathering info for image in file: '$f'\n";
msg_debug "Gathering info for image in file: '$f'\n";
my $e = Image::EXIF->new($f);
my $exif = $e->get_image_info();
my ($x, $y, $error_flag);
# Check for the existence of x
if (!defined $exif->{"Image Width"}) {
print "WARNING: Could not find attribute 'Image Width'.\n";
msg "Could not find attribute 'Image Width' for image '$f'.\n";
$error_flag = 1;
}
else {
@@ -61,7 +82,7 @@ foreach my $f (@list) {
# Check for the existence of y
if (!defined $exif->{"Image Height"}) {
print "WARNING: Could not find attribute 'Image Height'.\n";
msg "Could not find attribute 'Image Height' for image '$f'.\n";
$error_flag = 1;
}
else {
@@ -70,7 +91,7 @@ foreach my $f (@list) {
# Calculate aspect ratio and append it into the hash
if ($error_flag) {
print "WARNING: Unable to calculate aspect ratio for this image. Skipping it."
msg "Unable to calculate aspect ratio for image '$f'. Skipping it.\n"
}
else {
my $ar;
@@ -87,45 +108,46 @@ foreach my $f (@list) {
}
# Dump the EXIF data of all images
print "All images and their EXIF data:\n";
print Dumper $files;
print "\n";
if ($conf->{debug}) {
msg_debug "All images and their EXIF data:\n";
print Dumper $files;
msg_debug "\n";
}
# Dump some statistics
print "Total images found: $count. Images with invalid EXIF data: $errors\n";
print "Aspect ratio information for all images (aspect ratio => number of occurences):\n";
msg "Total images found: $count. Images with invalid EXIF data: $errors\n";
msg "Aspect ratio information for all images (aspect ratio => number of occurences):\n";
print Dumper $ars;
print "\n";
# Create destination directories. TODO: use something smarter than just string
# concatenation.
print "Creating destination directories: ";
msg_debug "Creating destination directories: ";
foreach my $d (keys %$ars) {
my $dirname = $conf->{dst_dir} . "/" . $d;
print "'$dirname' ";
msg_debug "'$dirname' ";
mkdir $dirname;
}
print "\n";
msg_debug "\n";
# Symlink
print "Symlinking source files into destination directories.\n";
msg "Symlinking source files into destination directories.\n";
foreach my $f (keys %$files) {
next unless exists $files->{$f}->{ar};
# Construct the symlink path. TODO: use something smarter than just string
# concatenation.
print "\$f='$f' ";
my $ar = $files->{$f}->{ar}; print "\$ar='$ar' ";
my $symlink_dir = $conf->{dst_dir} . "/" . $ar; print "\$symlink_dir='$symlink_dir' ";
my $symlink = $symlink_dir . "/" . basename($f); print "\$symlink='$symlink' ";
msg_debug "\$f='$f' ";
my $ar = $files->{$f}->{ar}; msg_debug "\$ar='$ar' ";
my $symlink_dir = $conf->{dst_dir} . "/" . $ar; msg_debug "\$symlink_dir='$symlink_dir' ";
my $symlink = $symlink_dir . "/" . basename($f); msg_debug "\$symlink='$symlink' ";
my $symlink_dst = File::Spec->abs2rel(
File::Spec->rel2abs($f),
File::Spec->rel2abs($symlink_dir)
); print "\$symlink_dst='$symlink_dst' ";
print "\n";
); msg_debug "\$symlink_dst='$symlink_dst' ";
msg_debug "\n";
# symlink
print "symlink($symlink_dst, $symlink);\n";
msg_debug "symlink($symlink_dst, $symlink);\n";
symlink($symlink_dst, $symlink);
}