]> mj.ucw.cz Git - gallery.git/blob - gal/gal
UCW::Implemented try_get method
[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 $threads = 4;
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 --threads=<n>   Run in <n> threads (default=4)
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');
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         "threads=i" => \$threads,
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
70 my $running = 0;
71 my $errors = 0;
72 my %pid_to_dir = ();
73
74 while ($running || @dirs) {
75         if ($running == $threads || !@dirs) {
76                 # Wait for children
77                 my $pid = wait; die if $pid < 0;
78                 my $dir = $pid_to_dir{$pid} or die;
79                 if ($?) {
80                         print "!! $dir FAILED [see $dir/gallery.log]";
81                         $errors++;
82                 } else {
83                         print "++ $dir";
84                         unlink "$dir/gallery.log";
85                 }
86                 delete $pid_to_dir{$pid};
87                 $running--;
88                 $done++;
89                 print " (done $done/$need)\n";
90         } else {
91                 my $dir = shift @dirs;
92                 print "<< $dir\n";
93                 my $pid = fork;
94                 if (!$pid) {
95                         close STDOUT;
96                         open STDOUT, '>', "$dir/gallery.log" or die;
97                         chdir $dir or die "Cannot chdir to $dir: $!\n";
98                         exec $sub_path, @ARGV;
99                         die "Cannot execute $sub_path: $!\n";
100                 }
101                 $pid_to_dir{$pid} = $dir;
102                 $running++;
103         }
104 }
105
106 print "$done jobs, $errors errors.\n";
107 exit ($errors > 0);