| 1 | package oEdtk::Run; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Archive::Zip qw(:ERROR_CODES); | ||
| 7 | use Cwd; | ||
| 8 | use File::Copy; | ||
| 9 | use File::Path qw(rmtree); | ||
| 10 | use POSIX qw(WNOHANG); | ||
| 11 | use Text::CSV; | ||
| 12 | use oEdtk::Config qw(config_read); | ||
| 13 | use oEdtk::DBAdmin qw(db_connect); | ||
| 14 | use oEdtk::EDMS qw(EDMS_prepare EDMS_process EDMS_import); | ||
| 15 | use oEdtk::FixIdx qw(make_docs_even_numbered); | ||
| 16 | use oEdtk::Outmngr qw(omgr_import omgr_export); | ||
| 17 | use oEdtk::TexDoc; | ||
| 18 | |||
| 19 | use Carp qw(croak); | ||
| 20 | use File::Spec::Functions qw(catfile curdir rootdir updir); | ||
| 21 | use oEdtk::logger qw(logger); | ||
| 22 | use PDF::Tk; | ||
| 23 | |||
| 24 | use Exporter; | ||
| 25 | |||
| 26 | our $VERSION = 2.1129; | ||
| 27 | our @ISA = qw(Exporter); | ||
| 28 | our @EXPORT_OK = qw( | ||
| 29 | oe_status_to_msg | ||
| 30 | oe_compo_run | ||
| 31 | oe_after_compo | ||
| 32 | oe_csv_to_doc | ||
| 33 | oe_outmngr_output_run_tex | ||
| 34 | oe_cmd_run | ||
| 35 | ); | ||
| 36 | |||
| 37 | sub oe_cmd_run { | ||
| 38 | my $cmd = shift; | ||
| 39 | |||
| 40 | # Redirect stdout to stderr so the output of the command doesn't confuse | ||
| 41 | # the scripts which parse stdout to get the list of files we generated. | ||
| 42 | $cmd .= ' >&2'; | ||
| 43 | |||
| 44 | warn "INFO: Running command \"$cmd\"\n"; | ||
| 45 | eval { system($cmd); }; | ||
| 46 | return if $? == 0; | ||
| 47 | |||
| 48 | my $reason = oe_status_to_msg($?); | ||
| 49 | die "ERROR: Command failed : $reason\n"; | ||
| 50 | |||
| 51 | return 1; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub oe_cmd_run_bg { | ||
| 55 | my ( $cmd, $ref ) = @_; | ||
| 56 | |||
| 57 | if ( ref($ref) ne 'SCALAR' ) { | ||
| 58 | die "ERROR: oe_cmd_run_bg() expects a SCALAR reference\n"; | ||
| 59 | } | ||
| 60 | |||
| 61 | # Wait mode: blocking by default (the mainline behaviour), non-blocking | ||
| 62 | # when the configuration sets EDTK_CMD_RUN_BG_NOWAIT (the fork's mode). In | ||
| 63 | # the mainline mode the first reap blocks; the loop then drains the | ||
| 64 | # remaining children without blocking, so no zombie is left behind. | ||
| 65 | my $flags = config_read('COMPO')->{'EDTK_CMD_RUN_BG_NOWAIT'} ? WNOHANG : 0; | ||
| 66 | |||
| 67 | # Install a signal handler first so that we are notified if our | ||
| 68 | # child exits prematurely, which should happen fairly rarely. | ||
| 69 | $SIG{'CHLD'} = sub { | ||
| 70 | my $wait = $flags; | ||
| 71 | my $pid; | ||
| 72 | while ( ( $pid = waitpid( -1, $wait ) ) > 0 ) { | ||
| 73 | if ( $? != 0 ) { | ||
| 74 | my $msg = oe_status_to_msg($?); | ||
| 75 | warn "INFO: LaTex process exited prematurely : $msg\n"; | ||
| 76 | die "ERROR: LaTeX process exited prematurely : $msg\n" | ||
| 77 | ; ##### xxxxxx capture of die events by the tracker ... | ||
| 78 | } | ||
| 79 | $$ref = 0; | ||
| 80 | $wait = WNOHANG; | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | |||
| 84 | $cmd .= ' >&2'; | ||
| 85 | warn "INFO: start cmd = $cmd \n"; | ||
| 86 | my $pid = fork(); | ||
| 87 | if ( !defined($pid) ) { | ||
| 88 | die "ERROR: Cannot fork process: $!\n"; | ||
| 89 | } | ||
| 90 | if ($pid) { | ||
| 91 | |||
| 92 | # Parent process. | ||
| 93 | warn "INFO: Successfully started subprocess, pid $pid - look log for details\n"; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | # Child process. | ||
| 97 | exec($cmd); | ||
| 98 | |||
| 99 | # NOT REACHED | ||
| 100 | exit; | ||
| 101 | } | ||
| 102 | return $pid; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub oe_status_to_msg { | ||
| 106 | my ($status) = @_; | ||
| 107 | |||
| 108 | return if $status == 0; | ||
| 109 | my $msg = ''; | ||
| 110 | if ( $? == -1 ) { | ||
| 111 | $msg = "failed to execute: $!"; | ||
| 112 | } | ||
| 113 | elsif ( $? & 127 ) { | ||
| 114 | $msg = sprintf( "child died with signal %d", $? & 127 ); | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | $msg = sprintf( "child exited with value %d", $? >> 8 ); | ||
| 118 | } | ||
| 119 | return $msg; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub oe_compo_run { | ||
| 123 | my ( $app, $options ) = @_; | ||
| 124 | |||
| 125 | my $cfg = config_read( ['COMPO'], $app ); | ||
| 126 | my $mode = $options->{'mode'} || 'pdf'; | ||
| 127 | |||
| 128 | my $exe; | ||
| 129 | if ( $mode =~ /^dvi/ ) { | ||
| 130 | $exe = $cfg->{'EDTK_COMPO_CMD_DVI'}; | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | $exe = $cfg->{'EDTK_COMPO_CMD_PDF'}; | ||
| 134 | } | ||
| 135 | |||
| 136 | # XXXXXXXXXXXXX THIS IS WHERE WE SWITCH THE EXECUTION MODE TO INCLUDE MODE | ||
| 137 | # Usage: pdftex [OPTION]... [TEXNAME[.tex]] [COMMANDS] | ||
| 138 | # or: pdftex [OPTION]... \FIRST-LINE | ||
| 139 | # or: pdftex [OPTION]... &FMT ARGS | ||
| 140 | # Run pdfTeX on TEXNAME, usually creating TEXNAME.pdf. | ||
| 141 | # Any remaining COMMANDS are processed as pdfTeX input, after TEXNAME is read. | ||
| 142 | # If the first line of TEXNAME is %&FMT, and FMT is an existing .fmt file, | ||
| 143 | # use it. Else use `NAME.fmt', where NAME is the program invocation name, | ||
| 144 | # most commonly `pdftex'. | ||
| 145 | # | ||
| 146 | # Alternatively, if the first non-option argument begins with a backslash, | ||
| 147 | # interpret all non-option arguments as a line of pdfTeX input. | ||
| 148 | # | ||
| 149 | # Alternatively, if the first non-option argument begins with a &, the | ||
| 150 | # next word is taken as the FMT to read, overriding all else. Any | ||
| 151 | # remaining arguments are processed as above. | ||
| 152 | # | ||
| 153 | # If no arguments or options are specified, prompt for input. | ||
| 154 | |||
| 155 | my $param; | ||
| 156 | |||
| 157 | # if (defined $cfg->{'EDTK_COMPO_INCLUDE'} && $cfg->{'EDTK_COMPO_INCLUDE'}=~/yes/i) { | ||
| 158 | # # DOES NOT WORK, REVIEW ACCORDING TO pdftex --help | ||
| 159 | # $param = "./$app." . $cfg->{'EDTK_EXT_WORK'}; | ||
| 160 | # } else { | ||
| 161 | $param = $cfg->{'EDTK_DIR_SCRIPT'} . "/$app." . $cfg->{'EDTK_EXT_COMPO'}; | ||
| 162 | |||
| 163 | # } | ||
| 164 | |||
| 165 | # Use the \edExtra mechanism to include additional packages if needed. | ||
| 166 | if ( defined( $options->{'extrapkgs'} ) ) { | ||
| 167 | my $extra = '\newcommand{\edExtra}{'; | ||
| 168 | foreach ( @{ $options->{'extrapkgs'} } ) { | ||
| 169 | $extra .= '\RequirePackage'; | ||
| 170 | my ( $pkg, $opts ); | ||
| 171 | if ( ref($_) eq 'ARRAY' ) { | ||
| 172 | ( $pkg, $opts ) = @$_; | ||
| 173 | $extra .= "[$opts]"; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | $pkg = $_; | ||
| 177 | } | ||
| 178 | $extra .= "{$pkg}"; | ||
| 179 | } | ||
| 180 | $param = $extra . '}\input{' . $param . '}'; | ||
| 181 | } | ||
| 182 | if ( defined( $options->{'jobname'} ) ) { | ||
| 183 | $exe .= " -jobname=$options->{'jobname'}"; | ||
| 184 | } | ||
| 185 | |||
| 186 | my $cmd = $cfg->{'EDTK_BIN_COMPO'} . "/$exe \"$param\""; | ||
| 187 | |||
| 188 | # Handle additional include directories. | ||
| 189 | my @incdirs = (); | ||
| 190 | if ( defined( $options->{'incdirs'} ) ) { | ||
| 191 | @incdirs = @{ $options->{'incdirs'} }; | ||
| 192 | } | ||
| 193 | |||
| 194 | # Add EDTK_DIR_DATA_IN to the list of directories LaTeX will | ||
| 195 | # look into for when we run via runEdtk.pl. | ||
| 196 | push( @incdirs, $cfg->{'EDTK_DIR_DATA_IN'} ); | ||
| 197 | my $old = $ENV{'TEXINPUTS'}; | ||
| 198 | if ( defined($old) ) { | ||
| 199 | push( @incdirs, $old ); | ||
| 200 | } | ||
| 201 | $ENV{'TEXINPUTS'} = ';' . join( ';', @incdirs ); | ||
| 202 | |||
| 203 | my $pid; | ||
| 204 | |||
| 205 | # In FIFO mode we need to run the LaTeX process asynchronously. | ||
| 206 | if ( $options->{'fifo'} ) { | ||
| 207 | $pid = oe_cmd_run_bg( $cmd, \$options->{'cldstatus'} ); | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | oe_cmd_run($cmd); | ||
| 211 | } | ||
| 212 | |||
| 213 | # Restore the old environment. | ||
| 214 | if ( defined($old) ) { | ||
| 215 | $ENV{'TEXINPUTS'} = $old; | ||
| 216 | } | ||
| 217 | else { | ||
| 218 | delete $ENV{'TEXINPUTS'}; | ||
| 219 | } | ||
| 220 | |||
| 221 | return $pid if $options->{'fifo'}; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub oe_after_compo { | ||
| 225 | my ( $app, $options ) = @_; | ||
| 226 | |||
| 227 | my $cfg = config_read( ['EDTK_DB'], $app ); | ||
| 228 | my $mode = $options->{'mode'} || 'pdf'; | ||
| 229 | |||
| 230 | # Run dvipdfm if we were running in DVI+PDF mode. | ||
| 231 | if ( $mode eq 'dvipdf' ) { | ||
| 232 | my $exe = $cfg->{'EDTK_COMPO_CMD_DVIPDF'}; | ||
| 233 | my $cmd = "$cfg->{'EDTK_BIN_COMPO'}/$exe $app.dvi"; | ||
| 234 | oe_cmd_run($cmd); | ||
| 235 | } | ||
| 236 | |||
| 237 | # Output the full path to the generated PDF file (used by the | ||
| 238 | # composition.sh script to determine what we have generated). | ||
| 239 | my $pdf = "$app.pdf"; | ||
| 240 | if ( defined( $options->{'jobname'} ) ) { | ||
| 241 | $pdf = $options->{'jobname'} . ".pdf"; | ||
| 242 | } | ||
| 243 | if ( !-f $pdf ) { | ||
| 244 | die "ERROR: Could not find the generated PDF file ($pdf)\n"; | ||
| 245 | } | ||
| 246 | my $cwd = getcwd(); | ||
| 247 | print "$cwd/$pdf\n"; | ||
| 248 | |||
| 249 | # Cleanup? Keep the generated output file when the configuration asks for | ||
| 250 | # it (EDTK_KEEP_OUT_FILE present and not NONE) or in the Test environment; | ||
| 251 | # remove it otherwise. | ||
| 252 | unlink("$app.aux"); | ||
| 253 | my $keep = ( exists $cfg->{'EDTK_KEEP_OUT_FILE'} && $cfg->{'EDTK_KEEP_OUT_FILE'} ne 'NONE' ); | ||
| 254 | if ( $keep || $cfg->{'EDTK_TYPE_ENV'} eq 'Test' ) { | ||
| 255 | warn "INFO: Keeping outfile '$options->{'outfile'}'\n" if ( $options->{'outfile'} ); | ||
| 256 | } | ||
| 257 | elsif ( $options->{'outfile'} ) { | ||
| 258 | unlink( $options->{'outfile'} ); | ||
| 259 | } | ||
| 260 | |||
| 261 | # If no index was requested, we are done. | ||
| 262 | if ( !$options->{'index'} ) { | ||
| 263 | warn "INFO: No index file was requested, done.\n"; | ||
| 264 | return; | ||
| 265 | } | ||
| 266 | |||
| 267 | # If an index file was requested, see if we need to import it | ||
| 268 | # for later processing, and/or if we need to produce a GED pack. | ||
| 269 | my $index = "$app." . $options->{'idldoc'} . ".idx1"; | ||
| 270 | if ( !-f $index ) { | ||
| 271 | die "ERROR: Could not find the generated index file ($index)\n"; | ||
| 272 | } | ||
| 273 | |||
| 274 | my $corp = $options->{'corp'}; | ||
| 275 | if ( !defined($corp) ) { | ||
| 276 | die "ERROR: No corporation name given\n"; | ||
| 277 | } | ||
| 278 | |||
| 279 | my $dbh = db_connect( $cfg, 'EDTK_DBI_PARAM', { RaiseError => 1 } ); | ||
| 280 | my $appdata = $dbh->selectrow_hashref( | ||
| 281 | "SELECT * FROM EDTK_REFIDDOC " . "WHERE ED_REFIDDOC = ? AND (ED_CORP = ? OR ED_CORP = '%')", | ||
| 282 | undef, $app, $corp | ||
| 283 | ); | ||
| 284 | |||
| 285 | if ( !defined($appdata) ) { | ||
| 286 | warn "INFO: Application $app was not found in EDTK_REFIDDOC\n"; | ||
| 287 | } | ||
| 288 | |||
| 289 | # Do we need to import the index ? | ||
| 290 | # if ($options->{'massmail'} or (defined($appdata) && $appdata->{'ED_MASSMAIL'} eq 'Y')) { | ||
| 291 | if ( ( defined($appdata) && $appdata->{'ED_MASSMAIL'} eq 'Y' ) | ||
| 292 | or ( defined($appdata) && $appdata->{'ED_MASSMAIL'} eq 'C' && $options->{'massmail'} ) ) | ||
| 293 | { | ||
| 294 | my $doclib = "$cfg->{'EDTK_DIR_DOCLIB'}/$options->{'doclib'}"; | ||
| 295 | warn "INFO: Moving $pdf into $doclib\n"; | ||
| 296 | copy( $pdf, $doclib ); | ||
| 297 | warn "INFO: Importing index into database...\n"; | ||
| 298 | omgr_import( $app, $index, $corp ); | ||
| 299 | } | ||
| 300 | |||
| 301 | # Do we need to prepare for GED processing? | ||
| 302 | # if (defined($appdata) && $appdata->{'ED_EDOCSHARE'} eq 'Y') { | ||
| 303 | if ( ( defined($appdata) && $appdata->{'ED_EDOCSHARE'} eq 'Y' ) | ||
| 304 | or ( defined($appdata) && $appdata->{'ED_EDOCSHARE'} eq 'C' && $options->{'edms'} ) | ||
| 305 | or ( defined($appdata) && $options->{'bpdf'} ) ) | ||
| 306 | { | ||
| 307 | |||
| 308 | if ( $options->{'cgi'} && $options->{'cgiged'} ) { | ||
| 309 | warn "INFO: Direct GED processing...\n"; | ||
| 310 | my ( $index, @pdfs ) = EDMS_process( $app, $options->{'idldoc'}, $pdf, $index ); | ||
| 311 | EDMS_import( $index, @pdfs ) | ||
| 312 | or die "ERROR: EDMS_import failed\n"; | ||
| 313 | } | ||
| 314 | elsif ( !$options->{'cgi'} ) { | ||
| 315 | if ( $options->{'bpdf'} ) { | ||
| 316 | $pdf = oe_background_pdf( $cfg, $options, $pdf, $index ); | ||
| 317 | } | ||
| 318 | warn "INFO: Preparing ZIP archive for GED...\n"; | ||
| 319 | EDMS_prepare( $app, $options->{'idldoc'}, $pdf, $index ); | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | # Now we can remove the index file. | ||
| 324 | if ( $cfg->{'EDTK_TYPE_ENV'} ne 'Test' ) { | ||
| 325 | unlink($index); | ||
| 326 | } | ||
| 327 | |||
| 328 | return 1; | ||
| 329 | } | ||
| 330 | |||
| 331 | # List the entries of a directory whose name matches a regexp, sorted. | ||
| 332 | sub listing_depository { | ||
| 333 | my ( $depo_path, $rgxp_name ) = @_; | ||
| 334 | |||
| 335 | opendir( my $depot, $depo_path ) | ||
| 336 | or croak "Can't open depository : $depo_path\n"; | ||
| 337 | my @contents = grep { /$rgxp_name/xms } readdir($depot); | ||
| 338 | closedir($depot); | ||
| 339 | |||
| 340 | my @sorted = sort @contents; | ||
| 341 | return @sorted; | ||
| 342 | } | ||
| 343 | |||
| 344 | # Double every line of an index file, then renumber the ";n;m;" markers so the | ||
| 345 | # two copies carry consecutive numbers. | ||
| 346 | sub update_idx { | ||
| 347 | my $idx_file = shift; | ||
| 348 | |||
| 349 | my @tab_line; | ||
| 350 | open my $fh, '<', $idx_file | ||
| 351 | or croak "ERROR: Cannot open index file \"$idx_file\": $!\n"; | ||
| 352 | foreach (<$fh>) { | ||
| 353 | push @tab_line, $_, $_; | ||
| 354 | } | ||
| 355 | close $fh; | ||
| 356 | |||
| 357 | open $fh, '>', $idx_file | ||
| 358 | or croak "ERROR: Cannot write index file \"$idx_file\": $!\n"; | ||
| 359 | for my $i ( 0 .. $#tab_line ) { | ||
| 360 | my $num_line = $i + 1; | ||
| 361 | $tab_line[$i] =~ s/;\d+;(\d+);/;$num_line;$1;/xms; | ||
| 362 | print {$fh} $tab_line[$i]; | ||
| 363 | } | ||
| 364 | close $fh; | ||
| 365 | |||
| 366 | return 1; | ||
| 367 | } | ||
| 368 | |||
| 369 | # Overlay the configured background PDF on every page of $pdf (the fork's | ||
| 370 | # "background pdf" feature). Kept as API only: oe_after_compo is not wired to | ||
| 371 | # it in this step. | ||
| 372 | sub oe_background_pdf { | ||
| 373 | my ( $cfg, $params, $pdf, $index ) = @_; | ||
| 374 | |||
| 375 | my $pdftk_path = config_read('EDOCMNGR')->{'EDMS_BIN_PDFTK'}; | ||
| 376 | my $pdftk = PDF::Tk->new( pdftk => $pdftk_path ); | ||
| 377 | |||
| 378 | my $blank_pdf = $cfg->{'EDTK_DIR_BACKGROUNDS'} . q{/} . 'blank' . q{.} . $cfg->{'EDTK_EXT_PDF'}; | ||
| 379 | croak "ERROR: $blank_pdf don't exist" if !-e $blank_pdf; | ||
| 380 | |||
| 381 | $pdftk->call_pdftk( $pdf, '%04d_split.pdf', 'burst' ); | ||
| 382 | |||
| 383 | my @contents = listing_depository( q{./}, '[0-9]*_split\.pdf$' ); | ||
| 384 | |||
| 385 | if ( ( scalar @{ $params->{'backgrounds'} } ) ne 1 | ||
| 386 | && @contents ne ( scalar @{ $params->{'backgrounds'} } ) ) | ||
| 387 | { | ||
| 388 | croak "ERROR: received " | ||
| 389 | . ( scalar @{ $params->{'backgrounds'} } ) | ||
| 390 | . " but needed 1 or " | ||
| 391 | . @contents | ||
| 392 | . "\n Quit cause can't continue"; | ||
| 393 | } | ||
| 394 | |||
| 395 | my $index_background = 0; | ||
| 396 | foreach (@contents) { | ||
| 397 | my $background_pdf = | ||
| 398 | $cfg->{'EDTK_DIR_BACKGROUNDS'} . q{/} | ||
| 399 | . $params->{'backgrounds'}->[$index_background] . q{_} | ||
| 400 | . $cfg->{'EDTK_CORP'} . q{.} | ||
| 401 | . $cfg->{'EDTK_EXT_PDF'}; | ||
| 402 | if ( !-e $background_pdf ) { | ||
| 403 | if ( $cfg->{'EDTK_TYPE_ENV'} eq 'Production' ) { | ||
| 404 | croak "ERROR: background $background_pdf doesn't exist"; | ||
| 405 | } | ||
| 406 | else { | ||
| 407 | &logger( 4, "Can't find $background_pdf so I put a blank background\n" ); | ||
| 408 | $background_pdf = catfile( $cfg->{'EDTK_DIR_BACKGROUNDS'}, 'blank.pdf' ); | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | my @cat_tab = ( $_, $blank_pdf ); | ||
| 413 | $pdftk->call_pdftk( \@cat_tab, $_ . q{.} . 'blank', 'cat' ); | ||
| 414 | $pdftk->call_pdftk( $_ . q{.} . 'blank', $_, 'multibackground', $background_pdf ); | ||
| 415 | |||
| 416 | if ( ( $index_background + 1 ) < ( scalar @{ $params->{'backgrounds'} } ) ) { | ||
| 417 | $index_background++; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | my $pdf_out = | ||
| 422 | q{./} . $params->{'app-ref'} . q{.} . $params->{'idldoc'} . q{.} . $cfg->{'EDTK_EXT_PDF'}; | ||
| 423 | $pdftk->call_pdftk( \@contents, $pdf_out, 'cat' ); | ||
| 424 | |||
| 425 | unlink glob '*split.pdf*' or croak "ERROR: can't delete useless pdf"; | ||
| 426 | |||
| 427 | update_idx($index); | ||
| 428 | |||
| 429 | return $pdf_out; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub oe_csv_to_doc { | ||
| 433 | my ( $input, $endtag ) = @_; | ||
| 434 | |||
| 435 | my $doc = oEdtk::TexDoc->new; | ||
| 436 | open( my $fh, '<', $input ) or die "Cannot open \"$input\": $!\n"; | ||
| 437 | |||
| 438 | my $csv = Text::CSV->new( { binary => 1 } ); | ||
| 439 | my @cols = map { s/_//gr } @{ $csv->getline($fh) }; | ||
| 440 | |||
| 441 | while ( my $vals = $csv->getline($fh) ) { | ||
| 442 | my %lists = (); | ||
| 443 | for ( my $i = 0 ; $i < $#cols ; $i++ ) { | ||
| 444 | my ( $key, $val ) = ( $cols[$i], $vals->[$i] ); | ||
| 445 | if ( $key !~ /^(\D+)\d+$/ ) { | ||
| 446 | $doc->append( $key, $val ); | ||
| 447 | } | ||
| 448 | elsif ( $val ne '' ) { | ||
| 449 | push( @{ $lists{$1} }, $val ); | ||
| 450 | } | ||
| 451 | } | ||
| 452 | while ( my ( $key, $val ) = each %lists ) { | ||
| 453 | $doc->append_table( $key, @$val ); | ||
| 454 | } | ||
| 455 | $doc->append($endtag); | ||
| 456 | } | ||
| 457 | close($fh); | ||
| 458 | return $doc; | ||
| 459 | } | ||
| 460 | |||
| 461 | # True when a lot's filiere (the part of its name after the last dot) is | ||
| 462 | # declared recto-verso in the configuration. EDTK_FILIERES_RECTO_VERSO is a | ||
| 463 | # whitespace-separated list of filieres, empty by default (no lot is padded). | ||
| 464 | sub _filiere_is_rectoverso { | ||
| 465 | my ( $lot, $cfg ) = @_; | ||
| 466 | my $filiere = substr( $lot, rindex( $lot, '.' ) + 1 ); | ||
| 467 | my @declared = split ' ', ( $cfg->{'EDTK_FILIERES_RECTO_VERSO'} // '' ); | ||
| 468 | return scalar grep { $_ eq $filiere } @declared; | ||
| 469 | } | ||
| 470 | |||
| 471 | sub oe_outmngr_output_run_tex { | ||
| 472 | my ( $filter, $type ) = @_; | ||
| 473 | |||
| 474 | # With the distinction between composition applications and batching processing, the following test no longer makes sense | ||
| 475 | # if ($type !~ /[MTD]/) { | ||
| 476 | # # oe_outmngr_output_run: we only go through index_output for Mass, Debug or batching Test | ||
| 477 | # warn "INFO: OM processing '$type' -> batching suspended\n"; | ||
| 478 | # return 1; | ||
| 479 | # } | ||
| 480 | |||
| 481 | my $cfg = config_read('COMPO'); | ||
| 482 | my $type_env = $cfg->{'EDTK_TYPE_ENV'}; | ||
| 483 | my $basedir = $cfg->{'EDTK_DIR_OUTMNGR'}; | ||
| 484 | |||
| 485 | warn "INFO: Calling omgr_export\n"; | ||
| 486 | my @lots = omgr_export(%$filter); | ||
| 487 | |||
| 488 | foreach (@lots) { | ||
| 489 | my ( $lot, @doclibs ) = @$_; | ||
| 490 | |||
| 491 | my $lotdir = "$basedir/$lot"; | ||
| 492 | chdir($lotdir) or die "Cannot change directory to \"$lotdir\": $!\n"; | ||
| 493 | warn "INFO: Preparing job ticket $lot for compo (doclibs = @doclibs)\n"; | ||
| 494 | |||
| 495 | # Create the intermediate stream. | ||
| 496 | my $doc = oEdtk::TexDoc->new; | ||
| 497 | $doc->append( oe_csv_to_doc( "$lot.job", 'edStartPg' ) ); | ||
| 498 | |||
| 499 | warn "INFO: Preparing index $lot for compo\n"; | ||
| 500 | $doc->append( oe_csv_to_doc( "$lot.idx", 'xFLigne' ) ); | ||
| 501 | $doc->append('edEndPg'); | ||
| 502 | $doc->append('xFinFlux'); | ||
| 503 | |||
| 504 | # The intermediate stream is written in UTF-8, the encoding the | ||
| 505 | # composition expects. | ||
| 506 | open( my $txt, '>:encoding(UTF-8)', 'LOTPDF.txt' ) | ||
| 507 | or die "Cannot open \"LOT.txt\": $!\n"; | ||
| 508 | print $txt "$doc"; | ||
| 509 | close($txt); | ||
| 510 | |||
| 511 | warn "INFO: Composition $lot in $basedir/$lotdir\n"; | ||
| 512 | my $options = { | ||
| 513 | jobname => $lot, | ||
| 514 | incdirs => [ $cfg->{'EDTK_DIR_DOCLIB'} ] | ||
| 515 | }; | ||
| 516 | oe_compo_run( 'LOTPDF', $options ); | ||
| 517 | oe_after_compo( 'LOTPDF', $options ); | ||
| 518 | |||
| 519 | # Generate the final PDF file. | ||
| 520 | if ( !-f "$lot.pdf" ) { | ||
| 521 | die "ERROR: Composition did not create PDF file\n"; | ||
| 522 | } | ||
| 523 | |||
| 524 | # Recto-verso: pad the index to an even number of pages when the lot's | ||
| 525 | # filiere is declared recto-verso in the configuration. | ||
| 526 | if ( _filiere_is_rectoverso( $lot, $cfg ) ) { | ||
| 527 | make_docs_even_numbered("$lot.idx"); | ||
| 528 | } | ||
| 529 | |||
| 530 | warn "INFO: Packaging $basedir $lot\n"; | ||
| 531 | my $zip = Archive::Zip->new(); | ||
| 532 | $zip->addFile( "$lotdir/$lot.idx", "$lot.idx" ); | ||
| 533 | $zip->addFile( "$lotdir/$lot.pdf", "$lot.pdf" ); | ||
| 534 | die "ERROR: Could not create zip archive\n" | ||
| 535 | unless $zip->writeToFileNamed("$basedir/$lot.zip") == AZ_OK; | ||
| 536 | } | ||
| 537 | |||
| 538 | # Change the current working directory to the base directory, otherwise | ||
| 539 | # we wouldn't be able to remove the temporary directory we are still in. | ||
| 540 | chdir($basedir); | ||
| 541 | foreach (@lots) { | ||
| 542 | my ($lot) = @$_; | ||
| 543 | |||
| 544 | print "$basedir/$lot.zip\n"; | ||
| 545 | if ( $type_env !~ /^De/i ) { # not done for the Dev or Debug environments | ||
| 546 | warn "INFO: removing intermediate files ($type_env)\n"; | ||
| 547 | rmtree("$basedir/$lot"); | ||
| 548 | } | ||
| 549 | } | ||
| 550 | return @lots; | ||
| 551 | } | ||
| 552 | |||
| 553 | 1; | ||
| 554 | |||
| 555 | =head1 NAME | ||
| 556 | |||
| 557 | oEdtk::Run - orchestrate a composition run: LaTeX, post-processing and output | ||
| 558 | |||
| 559 | =head1 SYNOPSIS | ||
| 560 | |||
| 561 | use oEdtk::Run qw(oe_compo_run oe_after_compo); | ||
| 562 | |||
| 563 | oe_compo_run($app, { mode => 'pdf' }); | ||
| 564 | oe_after_compo($app, { outfile => $pdf }); | ||
| 565 | |||
| 566 | =head1 DESCRIPTION | ||
| 567 | |||
| 568 | Runs one composition: it launches the LaTeX executable in the configured mode, | ||
| 569 | post-processes the result (dvipdfm, cleanup, index import), and packs or exports | ||
| 570 | the output through L<oEdtk::Outmngr> and L<oEdtk::EDMS>. It also provides the | ||
| 571 | command helpers used throughout the oEdtk runtime. | ||
| 572 | |||
| 573 | =head1 FUNCTIONS | ||
| 574 | |||
| 575 | =head2 oe_status_to_msg | ||
| 576 | |||
| 577 | my $msg = oe_status_to_msg($status); | ||
| 578 | |||
| 579 | Translate a process wait status into a human message, or return nothing when the | ||
| 580 | status is zero. | ||
| 581 | |||
| 582 | =head2 oe_compo_run | ||
| 583 | |||
| 584 | oe_compo_run($app, \%options); | ||
| 585 | |||
| 586 | Run the LaTeX composition of C<$app> in the configured mode. C<%options> may | ||
| 587 | carry C<mode>, C<jobname>, C<extrapkgs>, C<incdirs>, C<fifo> and C<cldstatus>; | ||
| 588 | in FIFO mode the process runs in the background and its pid is returned. | ||
| 589 | |||
| 590 | =head2 oe_after_compo | ||
| 591 | |||
| 592 | oe_after_compo($app, \%options); | ||
| 593 | |||
| 594 | Post-process the composition: run C<dvipdfm> in DVI+PDF mode, print the path of | ||
| 595 | the generated PDF, clean up the intermediate files, and import the index or build | ||
| 596 | the EDMS pack when one was requested. | ||
| 597 | |||
| 598 | =head2 oe_csv_to_doc | ||
| 599 | |||
| 600 | my $doc = oe_csv_to_doc($input, $endtag); | ||
| 601 | |||
| 602 | Build an L<oEdtk::TexDoc> from a two-column CSV: each numbered group becomes a | ||
| 603 | table, and C<$endtag> ends every record. | ||
| 604 | |||
| 605 | =head2 oe_outmngr_output_run_tex | ||
| 606 | |||
| 607 | my @lots = oe_outmngr_output_run_tex(\%filter, $type); | ||
| 608 | |||
| 609 | Export the matching lots, compose each one into a PDF, pad recto-verso lots, and | ||
| 610 | pack each lot's index and PDF into a zip archive; return the lots. | ||
| 611 | |||
| 612 | =head2 oe_cmd_run | ||
| 613 | |||
| 614 | oe_cmd_run($cmd); | ||
| 615 | |||
| 616 | Run C<$cmd>, with its stdout redirected to stderr, and die with the translated | ||
| 617 | status when it fails. | ||
| 618 | |||
| 619 | The following subs are internal helpers and are not part of the public API: | ||
| 620 | C<oe_cmd_run_bg>, C<listing_depository>, C<update_idx>, C<oe_background_pdf> | ||
| 621 | and C<_filiere_is_rectoverso>. | ||
| 622 | |||
| 623 | =head1 AUTHOR | ||
| 624 | |||
| 625 | The oEdtk team. | ||
| 626 | |||
| 627 | =cut |