| 1 | package VUser::Batch; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # Copyright 2004 Randy Smith | ||
| 6 | # $Id: Batch.pm,v 1.7 2007-09-24 19:31:53 perlstalker Exp $ | ||
| 7 | |||
| 8 | use vars qw(@ISA); | ||
| 9 | |||
| 10 | our $REVISION = (split (' ', '$Revision: 1.7 $'))[1]; | ||
| 11 | our $VERSION = "0.3.1"; | ||
| 12 | |||
| 13 | use VUser::Log qw(:levels); | ||
| 14 | use Pod::Usage; | ||
| 15 | |||
| 16 | use VUser::Extension; | ||
| 17 | push @ISA, 'VUser::Extension'; | ||
| 18 | |||
| 19 | my $log; | ||
| 20 | |||
| 21 | sub config_file | ||
| 22 | { | ||
| 23 | my $cfg = shift; | ||
| 24 | my $opts = shift; | ||
| 25 | |||
| 26 | print ("Current config file: ", tied (%$cfg)->GetFileName, "\n"); | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | sub config_sample | ||
| 31 | { | ||
| 32 | my $cfg = shift; | ||
| 33 | my $opts = shift; | ||
| 34 | |||
| 35 | my $fh; | ||
| 36 | if (defined $opts->{file}) { | ||
| 37 | open ($fh, ">".$opts->{file}) | ||
| 38 | or die "Can't open '".$opts->{file}."': $!\n"; | ||
| 39 | } else { | ||
| 40 | $fh = \*STDOUT; | ||
| 41 | } | ||
| 42 | |||
| 43 | print $fh <<'CONFIG'; | ||
| 44 | [vuser] | ||
| 45 | # To enable batch mode | ||
| 46 | extensions = Batch | ||
| 47 | |||
| 48 | CONFIG | ||
| 49 | |||
| 50 | if (defined $opts->{file}) { | ||
| 51 | close $fh; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub version | ||
| 56 | { | ||
| 57 | my $cfg = shift; | ||
| 58 | my $opts = shift; | ||
| 59 | |||
| 60 | my $rs = VUser::ResultSet->new(); | ||
| 61 | $rs->add_meta(VUser::Meta->new('name' => 'extension', | ||
| 62 | 'type' => 'string', | ||
| 63 | 'description' => 'Extension name')); | ||
| 64 | $rs->add_meta(VUser::Meta->new('name' => 'version', | ||
| 65 | 'type' => 'string', | ||
| 66 | 'description' => 'Version number')); | ||
| 67 | $rs->add_data(['Batch', $VERSION]); | ||
| 68 | return $rs; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub revision | ||
| 72 | { | ||
| 73 | my $cfg = shift; | ||
| 74 | my $opts = shift; | ||
| 75 | |||
| 76 | my $rs = VUser::ResultSet->new(); | ||
| 77 | $rs->add_meta(VUser::Meta->new('name' => 'extension', | ||
| 78 | 'type' => 'string', | ||
| 79 | 'description' => 'Extension name')); | ||
| 80 | $rs->add_meta(VUser::Meta->new('name' => 'version', | ||
| 81 | 'type' => 'string', | ||
| 82 | 'description' => 'Revision number')); | ||
| 83 | $rs->add_data(['Batch', $REVISION]); | ||
| 84 | return $rs; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub init | ||
| 88 | { | ||
| 89 | my $eh = shift; # ExtHandler | ||
| 90 | my %cfg = @_; | ||
| 91 | |||
| 92 | if (defined $main::log) { | ||
| 93 | $log = $main::log; | ||
| 94 | } else { | ||
| 95 | $log = VUser::Log->new(\%cfg, 'vuser') | ||
| 96 | } | ||
| 97 | |||
| 98 | $eh->register_task('version', '', \&version); | ||
| 99 | |||
| 100 | # Batch | ||
| 101 | $eh->register_keyword('batch', 'Run in batch mode.'); | ||
| 102 | $eh->register_action('batch', '*', ''); | ||
| 103 | $eh->register_option('batch', '*', 'flag', '=s', 0, 'touch this file when finished with the batch.'); | ||
| 104 | $eh->register_task('batch', '*', \&batch_mode); | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | sub process_event_dir | ||
| 109 | { | ||
| 110 | my $cfg = shift; | ||
| 111 | my $opts = shift; | ||
| 112 | my $eh = shift; | ||
| 113 | my $dir = shift; | ||
| 114 | |||
| 115 | opendir DIR, $dir or die "Unable to open $dir: $!\n"; | ||
| 116 | my @files = grep { ! (/^\.\.?$/ | ||
| 117 | or /^error-/ | ||
| 118 | or /^new-/ | ||
| 119 | ) | ||
| 120 | } readdir DIR; | ||
| 121 | closedir DIR; | ||
| 122 | |||
| 123 | foreach my $file (sort { | ||
| 124 | my @astat = stat("$dir/$a"); | ||
| 125 | my @bstat = stat("$dir/$b"); | ||
| 126 | $astat[9] <=> $bstat[9]; # Sort on mtime | ||
| 127 | } @files) { | ||
| 128 | if (-d "$dir/$file") { | ||
| 129 | eval { process_event_dir($cfg, $opts, $eh, "$dir/$file"); }; | ||
| 130 | die $@ if $@; | ||
| 131 | } elsif (-f "$dir/$file") { | ||
| 132 | eval { process_event_file($cfg, $opts, $eh, $dir, $file); }; | ||
| 133 | if ($@) { | ||
| 134 | warn $@; | ||
| 135 | rename "$dir/$file", "$dir/error-$file" | ||
| 136 | or warn "Can't rename $dir/$file to error-$file: $!"; | ||
| 137 | } | ||
| 138 | } else { | ||
| 139 | warn "File $dir/$file is not a plain file. Skipping.\n"; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | sub process_event_file | ||
| 145 | { | ||
| 146 | my $cfg = shift; | ||
| 147 | my $opts = shift; | ||
| 148 | my $eh = shift; | ||
| 149 | my $dir = shift; | ||
| 150 | my $file = shift; | ||
| 151 | |||
| 152 | my ($keyword, $action, $garbage) = split (/[_-]/, $file); | ||
| 153 | |||
| 154 | my %opts = (); | ||
| 155 | open FILE, "$dir/$file" or die "Unable to open $dir/$file: $!"; | ||
| 156 | while (<FILE>) { | ||
| 157 | chomp; | ||
| 158 | next unless /^\s*(\S+)\s*=>\s*(.*?)\s*$/; | ||
| 159 | my $key = $1; | ||
| 160 | my $val = $2; | ||
| 161 | |||
| 162 | if (not defined $opts{$key}) { | ||
| 163 | $opts{$key} = $val; | ||
| 164 | } elsif (ref $opts{$key} eq 'SCALAR') { | ||
| 165 | # We have hit a second option for this key. | ||
| 166 | # Convert the value into a list | ||
| 167 | $opts{$key} = [$opts{$key}, $val]; | ||
| 168 | } elsif (ref $opts{$key} eq 'ARRAY') { | ||
| 169 | # We have hit an Nth (N > 2) value for this key. | ||
| 170 | # Add it to the list. | ||
| 171 | push (@{$opts{$key}}, $val); | ||
| 172 | } else { | ||
| 173 | # This should never happen. | ||
| 174 | warn "Unknown error processing $file: We should never get here."; | ||
| 175 | next; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | close FILE; | ||
| 179 | |||
| 180 | # print STDERR "Keyword: $keyword; Action: $action; File: $file\n"; | ||
| 181 | # use Data::Dumper; print Dumper \%opts; | ||
| 182 | |||
| 183 | # All the data has been read, time to run the task. | ||
| 184 | $log->log(LOG_NOTICE, "Batch running $keyword|$action"); | ||
| 185 | eval { $eh->run_tasks($keyword, $action, $cfg, %opts); }; | ||
| 186 | die "$file: ".$@ if $@; | ||
| 187 | |||
| 188 | if ($opts->{flag}) { | ||
| 189 | eval { VUser::ExtLib::touch($opts->{flag}); }; | ||
| 190 | die "$@\n" if $@; | ||
| 191 | } | ||
| 192 | |||
| 193 | unlink "$dir/$file"; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub batch_mode | ||
| 197 | { | ||
| 198 | my $cfg = shift; | ||
| 199 | my $opts = shift; | ||
| 200 | my $directory = shift; # The 'action' for batch is the dir/file to process | ||
| 201 | my $eh = shift; | ||
| 202 | |||
| 203 | eval { process_event_dir($cfg, $opts, $eh, $directory); }; | ||
| 204 | die $@ if $@; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub unload { } | ||
| 208 | |||
| 209 | 1; | ||
| 210 | |||
| 211 | __END__ |