#!/usr/bin/perl # UCW Gallery -- Master Program # (c) 2012 Martin Mares use strict; use warnings; use Getopt::Long; use FindBin; my $gallery_root = $FindBin::Bin; my $all; my $parallel; sub show_help() { print < General options: --all Run on all galleries in subdirectories -p, --parallel= Run in parallel and use processes Common commands: scan Scan directory and obtain originals gen Generate web photos from originals cache Rebuild thumbnails and other cached info Other commands: dump-config Show configuration settings dump-meta Show contents of metadata files from-gqview Parse gqview/geeqie collections AMEN exit 0; } Getopt::Long::Configure('require_order', 'bundling'); GetOptions( "help" => \&show_help, "version" => sub { print "UCW Gallery 2.0 (c) 2004-2012 Martin Mares \n"; }, "all!" => \$all, "p|parallel=i" => \$parallel, ) or die "Try `gal help' for more information.\n"; Getopt::Long::Configure('default'); @ARGV or die "Missing subcommand.\n"; my $sub = shift @ARGV; $sub =~ /^[0-9a-zA-Z-]+$/ or die "Invalid subcommand $sub\n"; if ($sub eq 'help') { show_help(); } my $sub_path = "$gallery_root/bin/gal-$sub"; -x $sub_path or die "Unknown subcommand $sub\n"; $ENV{"GALLERY_ROOT"} = $gallery_root; $ENV{"PERL5LIB"} = join(":", $gallery_root . "/lib", $ENV{"PERL5LIB"} // ()); if (!$all) { exec $sub_path, @ARGV; die "Cannot execute $sub_path: $!\n"; } ### Parallel execution ### my @dirs = sort map { chomp; s{^\./}{}; s{\/gallery.cf}{}; $_; } `find . -mindepth 2 -name gallery.cf`; my $done = 0; my $need = @dirs; my $logging = $parallel ? 1 : 0; my $threads = $parallel // 1; my $running = 0; my $errors = 0; my %pid_to_dir = (); while ($running || @dirs) { if ($running == $threads || !@dirs) { # Wait for children my $pid = wait; die if $pid < 0; my $dir = $pid_to_dir{$pid} or die; if ($?) { print "!! $dir FAILED"; print " [see $dir/gallery.log]" if $logging; $errors++; } else { print "++ $dir"; unlink "$dir/gallery.log" if $logging; } delete $pid_to_dir{$pid}; $running--; $done++; print " (done $done/$need)\n"; } else { my $dir = shift @dirs; print "<< $dir\n"; my $pid = fork; if (!$pid) { if ($logging) { close STDOUT; open STDOUT, '>', "$dir/gallery.log" or die; close STDERR; open STDERR, '>&STDOUT'; } chdir $dir or die "Cannot chdir to $dir: $!\n"; exec $sub_path, @ARGV; die "Cannot execute $sub_path: $!\n"; } $pid_to_dir{$pid} = $dir; $running++; } } print "$done jobs, $errors errors.\n"; exit ($errors > 0);