| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use Getopt::Long; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use Pod::Usage; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use File::Copy; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use File::Path; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use encoding 'utf8'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | use open ':utf8'; | ||
| 12 | use Time::HiRes qw(gettimeofday tv_interval); | ||
| 13 | |||
| 14 | use Alvis::Utils qw(absolutize_path open_file get_files); | ||
| 15 | |||
| 16 | ####################### global vars | ||
| 17 | my $VERBOSE = 1; | ||
| 18 | my $DEBUG = 0; | ||
| 19 | |||
| 20 | ################################################################################ | ||
| 21 | # main sub | ||
| 22 | |||
| 23 | # TODO: optimize reqexp | ||
| 24 | # TODO: performance | ||
| 25 | my ($orig_dir, $extra_dir, $out_dir, $config_file, $bzip2, $extra_file) = | ||
| 26 | read_params(); | ||
| 27 | my %config = read_config($config_file); | ||
| 28 | my %extra_all = read_extra_file($extra_file, keys %config) | ||
| 29 | if (defined($extra_file)); | ||
| 30 | |||
| 31 | for my $file (get_files($orig_dir)) { | ||
| 32 | merge_file($file); | ||
| 33 | } | ||
| 34 | |||
| 35 | ################################################################################ | ||
| 36 | sub read_params | ||
| 37 | { | ||
| 38 | my ($orig_dir, $extra_dir, $out_dir, $config_file, $bzip2, $extra_file); | ||
| 39 | GetOptions( | ||
| 40 | '<>' => sub {$orig_dir = shift}, | ||
| 41 | 'man' => sub {pod2usage(-exitstatus => 0, -verbose => 2)}, | ||
| 42 | 'e|extra=s' => \$extra_dir, | ||
| 43 | 'extra-file=s' => \$extra_file, | ||
| 44 | 'o|out=s' => \$out_dir, | ||
| 45 | 'c|conf=s' => \$config_file, | ||
| 46 | 'z|bzip2' => \$bzip2, | ||
| 47 | 'h|help' => sub {pod2usage(1)} | ||
| 48 | ); | ||
| 49 | pod2usage(-message => "ERROR: input dir is not specified") | ||
| 50 | if (!defined($orig_dir)); | ||
| 51 | pod2usage(-message => "ERROR: extra dir or file is not specified") | ||
| 52 | if (!defined($extra_dir) and !defined($extra_file)); | ||
| 53 | pod2usage(-message => "ERROR: $extra_file is not a file") | ||
| 54 | if (defined($extra_file) and !(-f $extra_file)); | ||
| 55 | pod2usage(-message => "ERROR: output dir is not specified") | ||
| 56 | if (!defined($out_dir)); | ||
| 57 | pod2usage(-message => "ERROR: cofiguration file is not specified") | ||
| 58 | if (!defined($config_file)); | ||
| 59 | |||
| 60 | $orig_dir = absolutize_path($orig_dir); | ||
| 61 | pod2usage(-message => "ERROR: input dir does not exist") | ||
| 62 | if (!defined($orig_dir)); | ||
| 63 | if (defined($extra_dir)) { | ||
| 64 | $extra_dir = absolutize_path($extra_dir); | ||
| 65 | pod2usage(-message => "ERROR: extra dir does not exist") | ||
| 66 | if (!defined($extra_dir)); | ||
| 67 | } elsif (defined($extra_file)) { | ||
| 68 | $extra_file = absolutize_path($extra_file); | ||
| 69 | pod2usage(-message => "ERROR: extra file does not exist") | ||
| 70 | if (!defined($extra_file)); | ||
| 71 | } else { | ||
| 72 | pod2usage( | ||
| 73 | -message => "ERROR: extra file or extra dir must be specified"); | ||
| 74 | } | ||
| 75 | |||
| 76 | $out_dir = absolutize_path($out_dir); | ||
| 77 | pod2usage(-message => "ERROR: out dir does not exist") | ||
| 78 | if (!defined($out_dir)); | ||
| 79 | |||
| 80 | $config_file = absolutize_path($config_file); | ||
| 81 | pod2usage(-message => "ERROR: cofig file does not exist") | ||
| 82 | if (!defined($config_file)); | ||
| 83 | |||
| 84 | return ($orig_dir, $extra_dir, $out_dir, $config_file, $bzip2, $extra_file); | ||
| 85 | } | ||
| 86 | |||
| 87 | ################################################################################ | ||
| 88 | sub merge_file | ||
| 89 | { | ||
| 90 | my $orig_filename = shift; | ||
| 91 | my ($extra_filename, $out_filename) = mk_paths($orig_filename); | ||
| 92 | |||
| 93 | if ($VERBOSE) { | ||
| 94 | print "\n"; | ||
| 95 | print "input file: $orig_filename\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | my $is_merged = 0; | ||
| 99 | |||
| 100 | if (defined($extra_file)) { | ||
| 101 | print "extra file: $extra_file\n"; | ||
| 102 | merge($orig_filename, $out_filename, \%extra_all); | ||
| 103 | compress($out_filename) | ||
| 104 | if ($bzip2 || $orig_filename =~ /\.bz2$/); | ||
| 105 | $is_merged = 1; | ||
| 106 | } else { | ||
| 107 | my @extra_filenames = guess_filename($extra_filename); | ||
| 108 | for my $extra_filename (@extra_filenames) { | ||
| 109 | if (-e $extra_filename) { | ||
| 110 | print "extra file: $extra_filename\n" if ($VERBOSE); | ||
| 111 | my %extra = read_extra_file($extra_filename, keys %config); | ||
| 112 | |||
| 113 | my $start_time = [gettimeofday] if ($DEBUG); | ||
| 114 | merge($orig_filename, $out_filename, \%extra); | ||
| 115 | print "merge time: ", tv_interval($start_time, [gettimeofday]), | ||
| 116 | "\n" | ||
| 117 | if ($DEBUG); | ||
| 118 | |||
| 119 | compress($out_filename) | ||
| 120 | if ($bzip2 || $orig_filename =~ /\.bz2$/); | ||
| 121 | $is_merged = 1; | ||
| 122 | last; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | unless ($is_merged) { | ||
| 127 | print "Extra file not found. Output file will be a copy of input.\n" | ||
| 128 | if ($VERBOSE); | ||
| 129 | copy($orig_filename, $out_filename); | ||
| 130 | if ($bzip2) { | ||
| 131 | print "Compressing output file ...\n" if ($VERBOSE); | ||
| 132 | compress($out_filename); | ||
| 133 | } | ||
| 134 | |||
| 135 | #print "Output file: $out_filename\n" if ($VERBOSE); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | ################################################################################ | ||
| 140 | sub merge | ||
| 141 | { | ||
| 142 | my ($orig_filename, $out_filename, $extra) = @_; | ||
| 143 | my @nodes = keys %config; | ||
| 144 | my $IN = Alvis::Utils::open_file("$orig_filename"); | ||
| 145 | |||
| 146 | #$out_filename =~ s/\.bz2$// if ($orig_filename =~ /\.bz2$/); | ||
| 147 | open(OUT, ">$out_filename") | ||
| 148 | or die "Cannot open file for write '$out_filename': $!"; | ||
| 149 | |||
| 150 | my ($id, $skip) = (); | ||
| 151 | my %printed = (); | ||
| 152 | my $n = 0; | ||
| 153 | while (defined(my $str = <$IN>)) { | ||
| 154 | |||
| 155 | if (!defined($skip) && defined($id) && defined($extra->{$id})) { | ||
| 156 | $skip = 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | # trim | ||
| 160 | my $oring_str = $str; | ||
| 161 | $str =~ s/^\s+//; | ||
| 162 | $str =~ s/\s+$//; | ||
| 163 | |||
| 164 | if (defined($skip)) { | ||
| 165 | for my $node (@nodes) { | ||
| 166 | if (!defined($printed{$node}) | ||
| 167 | && defined($extra->{$id}{$node})) | ||
| 168 | { | ||
| 169 | |||
| 170 | #if ($str =~ /^\s*<\/$node>/) { # node already defined in xml | ||
| 171 | if ($str eq "<\/$node>") { | ||
| 172 | my $x = $extra->{$id}{$node}; | ||
| 173 | $x =~ s/\n*<$node>\n*//s; | ||
| 174 | $x =~ s/\n*<\/$node>\n*//s; | ||
| 175 | print OUT $x; | ||
| 176 | $printed{$node} = 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | #elsif ($str =~ /^\s*$config{$node}/) { | ||
| 180 | elsif ($str eq $config{$node}) { | ||
| 181 | print OUT $extra->{$id}{$node}; | ||
| 182 | $printed{$node} = 1; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | print OUT $oring_str; | ||
| 189 | |||
| 190 | #if ($str =~ /^\s*<documentRecord id="(.+)">/) { | ||
| 191 | if ($str =~ /^<documentRecord id="([^"]+)"/) { | ||
| 192 | $id = $1; | ||
| 193 | undef $skip; | ||
| 194 | %printed = (); | ||
| 195 | print "$n records merged\n" if ($VERBOSE && (++$n % 100 == 0)); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | close OUT; | ||
| 200 | close $IN; | ||
| 201 | } | ||
| 202 | |||
| 203 | ################################################################################ | ||
| 204 | # TODO: move this to Alvis::Utils | ||
| 205 | sub mk_paths | ||
| 206 | { | ||
| 207 | my $orig_filename = shift; | ||
| 208 | my $uorig_filename = $orig_filename; | ||
| 209 | $uorig_filename =~ s/\.bz2$//; | ||
| 210 | $uorig_filename =~ s/\.gz$//; | ||
| 211 | my $extra_filename = $uorig_filename; | ||
| 212 | my $out_filename = $uorig_filename; | ||
| 213 | |||
| 214 | my $orig_dir_p = escape($orig_dir); | ||
| 215 | my $out_dir_p = escape($out_dir); | ||
| 216 | my $extra_dir_p = escape($extra_dir) unless (defined($extra_file)); | ||
| 217 | $out_filename =~ s/$orig_dir_p/$out_dir_p/; | ||
| 218 | $extra_filename =~ s/$orig_dir_p/$extra_dir_p/ | ||
| 219 | unless (defined($extra_file)); | ||
| 220 | |||
| 221 | $out_filename =~ /(.+)\/(.+?)$/; | ||
| 222 | my $out_dir = $1; | ||
| 223 | mkpath($out_dir) unless (-e $out_dir); | ||
| 224 | |||
| 225 | return ($extra_filename, $out_filename); | ||
| 226 | } | ||
| 227 | |||
| 228 | # TODO: move this to Alvis::Utils | ||
| 229 | sub escape | ||
| 230 | { | ||
| 231 | my $val = shift; | ||
| 232 | $val =~ s/(['|"\+?*])/\\$1/g; | ||
| 233 | return $val; | ||
| 234 | } | ||
| 235 | |||
| 236 | ################################################################################ | ||
| 237 | sub read_extra_file | ||
| 238 | { | ||
| 239 | my $filename = shift; | ||
| 240 | my @nodes = @_; | ||
| 241 | my %extra = (); | ||
| 242 | |||
| 243 | my $IN = Alvis::Utils::open_file("$filename"); | ||
| 244 | my ($id, $tag, $xml) = (); | ||
| 245 | while (defined(my $str = <$IN>)) { | ||
| 246 | $id = $1 if ($str =~ /^\s*<documentRecord id="([^"]+)"/); | ||
| 247 | |||
| 248 | unless (defined $tag) { | ||
| 249 | for my $node (@nodes) { | ||
| 250 | $tag = $node | ||
| 251 | if ($str =~ /.*<$node>.*/); # TODO: optimize reqexp | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | if (defined $tag) { | ||
| 256 | $xml .= $str; | ||
| 257 | if ($str =~ /.*<\/$tag>.*/) { # TODO: optimize reqexp | ||
| 258 | $extra{$id}{$tag} = $xml; | ||
| 259 | undef $xml; | ||
| 260 | undef $tag; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | } | ||
| 264 | close $IN; | ||
| 265 | |||
| 266 | return %extra; | ||
| 267 | } | ||
| 268 | |||
| 269 | ################################################################################ | ||
| 270 | sub read_config | ||
| 271 | { | ||
| 272 | my %config = (); | ||
| 273 | my $config_file = shift; | ||
| 274 | open(FP, $config_file) or die $!; | ||
| 275 | |||
| 276 | while (defined(my $str = <FP>)) { | ||
| 277 | $str =~ /(\S+?)\s+"\^(.+)"/; | ||
| 278 | $config{$1} = $2; | ||
| 279 | } | ||
| 280 | close FP; | ||
| 281 | |||
| 282 | return %config; | ||
| 283 | } | ||
| 284 | |||
| 285 | ################################################################################ | ||
| 286 | sub compress | ||
| 287 | { | ||
| 288 | my $file = shift; | ||
| 289 | unless ($file =~ /\.bz2$/) { | ||
| 290 | `bzip2 $file`; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | ################################################################################ | ||
| 295 | sub guess_filename | ||
| 296 | { | ||
| 297 | my %guessed = (); | ||
| 298 | my $filename = shift; | ||
| 299 | $guessed{$filename} = 1; | ||
| 300 | $guessed{$1} = 1 if ($filename =~ /(.+)\.bz2$/); | ||
| 301 | $guessed{$1} = 1 if ($filename =~ /(.+)\.gz$/); | ||
| 302 | $guessed{$1} = 1 if ($filename =~ /(.+)\.zip$/); | ||
| 303 | |||
| 304 | if ($filename =~ /(.+)\.xml$/) { | ||
| 305 | $guessed{$filename . '.bz2'} = 1; | ||
| 306 | $guessed{$filename . '.gz'} = 1; | ||
| 307 | $guessed{$filename . '.zip'} = 1; | ||
| 308 | } | ||
| 309 | |||
| 310 | return keys %guessed; | ||
| 311 | } | ||
| 312 | |||
| 313 | __END__ | ||