]> mj.ucw.cz Git - gallery.git/blob - gal/gal
Gallery2: The driver script supports sequential mode, which shows full output
[gallery.git] / gal / gal
1 #!/usr/bin/perl
2 # UCW Gallery -- Master Program
3 # (c) 2012 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7 use Getopt::Long;
8
9 use FindBin;
10 my $gallery_root = $FindBin::Bin;
11
12 my $all;
13 my $parallel;
14
15 sub show_help() {
16         print <<AMEN ;
17 Usage: gal <general-options> <command> <command-options> <args>
18
19 General options:
20     --all               Run on all galleries in subdirectories
21 -p, --parallel=<n>      Run in parallel and use <n> processes
22
23 Common commands:
24 scan                    Scan directory and obtain originals
25 gen                     Generate web photos from originals
26 cache                   Rebuild thumbnails and other cached info
27
28 Other commands:
29 dump-config             Show configuration settings
30 dump-meta               Show contents of metadata files
31 from-gqview             Parse gqview/geeqie collections
32 AMEN
33         exit 0;
34 }
35
36 Getopt::Long::Configure('require_order', 'bundling');
37 GetOptions(
38         "help" => \&show_help,
39         "version" => sub {
40                         print "UCW Gallery 2.0 (c) 2004-2012 Martin Mares <mj\@ucw.cz>\n";
41                 },
42         "all!" => \$all,
43         "p|parallel=i" => \$parallel,
44 ) or die "Try `gal help' for more information.\n";
45 Getopt::Long::Configure('default');
46
47 @ARGV or die "Missing subcommand.\n";
48 my $sub = shift @ARGV;
49 $sub =~ /^[0-9a-zA-Z-]+$/ or die "Invalid subcommand $sub\n";
50
51 if ($sub eq 'help') { show_help(); }
52
53 my $sub_path = "$gallery_root/bin/gal-$sub";
54 -x $sub_path or die "Unknown subcommand $sub\n";
55
56 $ENV{"GALLERY_ROOT"} = $gallery_root;
57 $ENV{"PERL5LIB"} = join(":", $gallery_root, $ENV{"PERL5LIB"} // ());
58
59 if (!$all) {
60         exec $sub_path, @ARGV;
61         die "Cannot execute $sub_path: $!\n";
62 }
63
64 ### Parallel execution ###
65
66 my @dirs = sort map { chomp; s{^\./}{}; s{\/gallery.cf}{}; $_; } `find . -mindepth 2 -name gallery.cf`;
67 my $done = 0;
68 my $need = @dirs;
69 my $logging = $parallel ? 1 : 0;
70 my $threads = $parallel // 1;
71
72 my $running = 0;
73 my $errors = 0;
74 my %pid_to_dir = ();
75
76 while ($running || @dirs) {
77         if ($running == $threads || !@dirs) {
78                 # Wait for children
79                 my $pid = wait; die if $pid < 0;
80                 my $dir = $pid_to_dir{$pid} or die;
81                 if ($?) {
82                         print "!! $dir FAILED";
83                         print " [see $dir/gallery.log]" if $logging;
84                         $errors++;
85                 } else {
86                         print "++ $dir";
87                         unlink "$dir/gallery.log" if $logging;
88                 }
89                 delete $pid_to_dir{$pid};
90                 $running--;
91                 $done++;
92                 print " (done $done/$need)\n";
93         } else {
94                 my $dir = shift @dirs;
95                 print "<< $dir\n";
96                 my $pid = fork;
97                 if (!$pid) {
98                         if ($logging) {
99                                 close STDOUT;
100                                 open STDOUT, '>', "$dir/gallery.log" or die;
101                                 close STDERR;
102                                 open STDERR, '>&STDOUT';
103                         }
104                         chdir $dir or die "Cannot chdir to $dir: $!\n";
105                         exec $sub_path, @ARGV;
106                         die "Cannot execute $sub_path: $!\n";
107                 }
108                 $pid_to_dir{$pid} = $dir;
109                 $running++;
110         }
111 }
112
113 print "$done jobs, $errors errors.\n";
114 exit ($errors > 0);