lib/oEdtk/Run.pm

filter: Statementstmt 19.6% 61 / 311 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 61.2% 19 / 31 Podpod 60.0% 6 / 10 totaltotal 16.0% 86 / 537 CC 81 SCAR 58.4
CC
81
Cov
13%
oe_after_compo
1056.0
oe_compo_run
156.0
oe_background_pdf
90.0
CRAP 343.7
« lib/oEdtk/Outmngr.pm ↑ Summary blib/lib/oEdtk/Tracking2.pm »
1 package oEdtk::Run;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use Archive::Zip qw(:ERROR_CODES);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use File::Path qw(rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use oEdtk::Config qw(config_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use oEdtk::DBAdmin qw(db_connect);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use oEdtk::EDMS qw(EDMS_prepare EDMS_process EDMS_import);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use oEdtk::FixIdx qw(make_docs_even_numbered);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use oEdtk::Outmngr qw(omgr_import omgr_export);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use oEdtk::TexDoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 20 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use File::Spec::Functions qw(catfile curdir rootdir updir);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 20 ▶use oEdtk::logger qw(logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 20 ▶use PDF::Tk;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 20 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 20 our $VERSION = 2.1131;
27 20 our @ISA = qw(Exporter);
28 20 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 0 ▶sub oe_cmd_run {
Subroutine oe_cmd_run: not called CC=2 SCAR=17.9
Pod documented
38 0 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 0 $cmd .= ' >&2';
43  
44 0 warn "INFO: Running command \"$cmd\"\n";
45 0 eval { system($cmd); };
46 0 ▶ return if $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 0 0 0
47  
48 0 my $reason = oe_status_to_msg($?);
49 0 die "ERROR: Command failed : $reason\n";
50  
51 0 return 1;
52 }
53  
54 0 ▶sub oe_cmd_run_bg {
Subroutine oe_cmd_run_bg: not called CC=5 SCAR=34.0
Pod undocumented
55 0 my ( $cmd, $ref ) = @_;
56  
57 0 ▶ if ( ref($ref) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref ne "SCALAR") 0 0 0
58 0 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 0 ▶ my $flags = config_read('COMPO')->{'EDTK_CMD_RUN_BG_NOWAIT'} ? WNOHANG : 0;
Branch: 1 branch
BranchTrueFalseTotal
config_read('COMPO')->{'EDTK_CMD_RUN_BG_NOWAIT'} ? : 0 0 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 0 ▶ $SIG{'CHLD'} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
70 0 my $wait = $flags;
71 0 my $pid;
72 0 ▶ while ( ( $pid = waitpid( -1, $wait ) ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($pid = waitpid(-1, $wait)) > 0) 0 0 0
73 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
74 0 my $msg = oe_status_to_msg($?);
75 0 warn "INFO: LaTex process exited prematurely : $msg\n";
76 0 die "ERROR: LaTeX process exited prematurely : $msg\n"
77 ; ##### xxxxxx capture of die events by the tracker ...
78 }
79 0 $$ref = 0;
80 0 $wait = WNOHANG;
81 }
82 0 };
83  
84 0 $cmd .= ' >&2';
85 0 warn "INFO: start cmd = $cmd \n";
86 0 my $pid = fork();
87 0 ▶ if ( !defined($pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
88 0 die "ERROR: Cannot fork process: $!\n";
89 }
90 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
91  
92 # Parent process.
93 0 warn "INFO: Successfully started subprocess, pid $pid - look log for details\n";
94 }
95 else {
96 # Child process.
97 0 exec($cmd);
98  
99 # NOT REACHED
100 0 exit;
101 }
102 0 return $pid;
103 }
104  
105 0 ▶sub oe_status_to_msg {
Subroutine oe_status_to_msg: not called CC=4 SCAR=30.0
Pod documented
106 0 my ($status) = @_;
107  
108 0 ▶ return if $status == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 0 0 0 0
109 0 my $msg = '';
110 0 ▶ if ( $? == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 0 0 0
elsif ($? & 127) { } 0 0 0
111 0 $msg = "failed to execute: $!";
112 }
113 elsif ( $? & 127 ) {
114 0 $msg = sprintf( "child died with signal %d", $? & 127 );
115 }
116 else {
117 0 $msg = sprintf( "child exited with value %d", $? >> 8 );
118 }
119 0 return $msg;
120 }
121  
122 0 ▶sub oe_compo_run {
Subroutine oe_compo_run: not called CC=12 SCAR=50.5
Pod documented
123 0 my ( $app, $options ) = @_;
124  
125 0 my $cfg = config_read( ['COMPO'], $app );
126 0 ▶ my $mode = $options->{'mode'} || 'pdf';
Condition0%0/2
$options->{'mode'} || 'pdf'
l!l
00
MC/DC0%0/1
$options->{'mode'} || 'pdf'
$options->{'mode'}
Truth table0%0/2
A
A= $options->{'mode'}
Aresult
11
00
127  
128 0 my $exe;
129 0 ▶ if ( $mode =~ /^dvi/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /^dvi/) { } 0 0 0
130 0 $exe = $cfg->{'EDTK_COMPO_CMD_DVI'};
131 }
132 else {
133 0 $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 0 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 0 $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 0 ▶ if ( defined( $options->{'extrapkgs'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"extrapkgs"}) 0 0 0
167 0 my $extra = '\newcommand{\edExtra}{';
168 0 foreach ( @{ $options->{'extrapkgs'} } ) {
169 0 $extra .= '\RequirePackage';
170 0 my ( $pkg, $opts );
171 0 ▶ if ( ref($_) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 0 0 0
172 0 ( $pkg, $opts ) = @$_;
173 0 $extra .= "[$opts]";
174 }
175 else {
176 0 $pkg = $_;
177 }
178 0 $extra .= "{$pkg}";
179 }
180 0 $param = $extra . '}\input{' . $param . '}';
181 }
182 0 ▶ if ( defined( $options->{'jobname'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"jobname"}) 0 0 0
183 0 $exe .= " -jobname=$options->{'jobname'}";
184 }
185  
186 0 my $cmd = $cfg->{'EDTK_BIN_COMPO'} . "/$exe \"$param\"";
187  
188 # Handle additional include directories.
189 0 my @incdirs = ();
190 0 ▶ if ( defined( $options->{'incdirs'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"incdirs"}) 0 0 0
191 0 @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 0 push( @incdirs, $cfg->{'EDTK_DIR_DATA_IN'} );
197 0 my $old = $ENV{'TEXINPUTS'};
198 0 ▶ if ( defined($old) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 0 0 0
199 0 push( @incdirs, $old );
200 }
201 0 $ENV{'TEXINPUTS'} = ';' . join( ';', @incdirs );
202  
203 0 my $pid;
204  
205 # In FIFO mode we need to run the LaTeX process asynchronously.
206 0 ▶ if ( $options->{'fifo'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'fifo'}) { } 0 0 0
207 0 $pid = oe_cmd_run_bg( $cmd, \$options->{'cldstatus'} );
208 }
209 else {
210 0 oe_cmd_run($cmd);
211 }
212  
213 # Restore the old environment.
214 0 ▶ if ( defined($old) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) { } 0 0 0
215 0 $ENV{'TEXINPUTS'} = $old;
216 }
217 else {
218 0 delete $ENV{'TEXINPUTS'};
219 }
220  
221 0 ▶ return $pid if $options->{'fifo'};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"fifo"} 0 0 0
222 }
223  
224 0 ▶sub oe_after_compo {
Subroutine oe_after_compo: not called CC=32 SCAR=69.6
Pod documented
225 0 my ( $app, $options ) = @_;
226  
227 0 my $cfg = config_read( ['EDTK_DB'], $app );
228 0 ▶ my $mode = $options->{'mode'} || 'pdf';
Condition0%0/2
$options->{'mode'} || 'pdf'
l!l
00
MC/DC0%0/1
$options->{'mode'} || 'pdf'
$options->{'mode'}
Truth table0%0/2
A
A= $options->{'mode'}
Aresult
11
00
229  
230 # Run dvipdfm if we were running in DVI+PDF mode.
231 0 ▶ if ( $mode eq 'dvipdf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "dvipdf") 0 0 0
232 0 my $exe = $cfg->{'EDTK_COMPO_CMD_DVIPDF'};
233 0 my $cmd = "$cfg->{'EDTK_BIN_COMPO'}/$exe $app.dvi";
234 0 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 0 my $pdf = "$app.pdf";
240 0 ▶ if ( defined( $options->{'jobname'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"jobname"}) 0 0 0
241 0 $pdf = $options->{'jobname'} . ".pdf";
242 }
243 0 ▶ if ( !-f $pdf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $pdf) 0 0 0
244 0 die "ERROR: Could not find the generated PDF file ($pdf)\n";
245 }
246 0 my $cwd = getcwd();
247 0 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 0 unlink("$app.aux");
253 0 ▶ my $keep = ( exists $cfg->{'EDTK_KEEP_OUT_FILE'} && $cfg->{'EDTK_KEEP_OUT_FILE'} ne 'NONE' );
Condition0%0/3
exists $cfg->{'EDTK_KEEP_OUT_FILE'} && $cfg->{'EDTK_KEEP_OUT_FILE'} ne 'NONE'
!ll&&!rl&&r
000
MC/DC0%0/2
exists $cfg->{'EDTK_KEEP_OUT_FILE'} && $cfg->{'EDTK_KEEP_OUT_FILE'} ne 'NONE'
exists $cfg->{'EDTK_KEEP_OUT_FILE'} $cfg->{'EDTK_KEEP_OUT_FILE'} ne 'NONE'
Truth table0%0/3
A && B
A= exists $cfg->{'EDTK_KEEP_OUT_FILE'}B= $cfg->{'EDTK_KEEP_OUT_FILE'} ne 'NONE'
ABresult
0X0
100
111
254 0 ▶ if ( $keep || $cfg->{'EDTK_TYPE_ENV'} eq 'Test' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($keep or $cfg->{'EDTK_TYPE_ENV'} eq 'Test') { } 0 0 0
elsif ($options->{'outfile'}) { } 0 0 0
Condition0%0/3
$keep or $cfg->{"EDTK_TYPE_ENV"} eq "Test"
l!l&&r!l&&!r
000
MC/DC0%0/2
$keep or $cfg->{"EDTK_TYPE_ENV"} eq "Test"
$keep $cfg->{"EDTK_TYPE_ENV"} eq "Test"
Truth table0%0/3
A or B
A= $keepB= $cfg->{"EDTK_TYPE_ENV"} eq "Test"
ABresult
1X1
011
000
255 0 ▶ warn "INFO: Keeping outfile '$options->{'outfile'}'\n" if ( $options->{'outfile'} );
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"outfile"} 0 0 0
256 }
257 elsif ( $options->{'outfile'} ) {
258 0 unlink( $options->{'outfile'} );
259 }
260  
261 # If no index was requested, we are done.
262 0 ▶ if ( !$options->{'index'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"index"}) 0 0 0
263 0 warn "INFO: No index file was requested, done.\n";
264 0 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 0 my $index = "$app." . $options->{'idldoc'} . ".idx1";
270 0 ▶ if ( !-f $index ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $index) 0 0 0
271 0 die "ERROR: Could not find the generated index file ($index)\n";
272 }
273  
274 0 my $corp = $options->{'corp'};
275 0 ▶ if ( !defined($corp) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $corp) 0 0 0
276 0 die "ERROR: No corporation name given\n";
277 }
278  
279 0 my $dbh = db_connect( $cfg, 'EDTK_DBI_PARAM', { RaiseError => 1 } );
280 0 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 0 ▶ if ( !defined($appdata) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $appdata) 0 0 0
286 0 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 0 ▶ if ( ( defined($appdata) && $appdata->{'ED_MASSMAIL'} eq 'Y' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $appdata and $appdata->{"ED_MASSMAIL"} eq "Y" or defined $appdata and $appdata->{"ED_MASSMAIL"} eq "C" and $options->{"massmail"}) 0 0 0
Condition0%0/12
defined $appdata and $appdata->{"ED_MASSMAIL"} eq "Y" or defined $appdata and $appdata->{"ED_MASSMAIL"} eq "C" and $options->{"massmail"}
l!l&&r!l&&!r
000
defined $appdata and $appdata->{"ED_MASSMAIL"} eq "Y"
!ll&&!rl&&r
000
defined $appdata and $appdata->{"ED_MASSMAIL"} eq "C" and $options->{"massmail"}
!ll&&!rl&&r
000
defined $appdata and $appdata->{"ED_MASSMAIL"} eq "C"
!ll&&!rl&&r
000
MC/DC0%0/5
defined $appdata and $appdata->{"ED_MASSMAIL"} eq "Y" or defined $appdata and $appdata->{"ED_MASSMAIL"} eq "C" and $options->{"massmail"}
defined $appdata $appdata->{"ED_MASSMAIL"} eq "Y" defined $appdata $appdata->{"ED_MASSMAIL"} eq "C" $options->{"massmail"}
Truth table0%0/9
A and B or C and D and E
A= defined $appdataB= $appdata->{"ED_MASSMAIL"} eq "Y"C= defined $appdataD= $appdata->{"ED_MASSMAIL"} eq "C"E= $options->{"massmail"}
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
292 or ( defined($appdata) && $appdata->{'ED_MASSMAIL'} eq 'C' && $options->{'massmail'} ) )
293 {
294 0 my $doclib = "$cfg->{'EDTK_DIR_DOCLIB'}/$options->{'doclib'}";
295 0 warn "INFO: Moving $pdf into $doclib\n";
296 0 copy( $pdf, $doclib );
297 0 warn "INFO: Importing index into database...\n";
298 0 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 0 ▶ if ( ( defined($appdata) && $appdata->{'ED_EDOCSHARE'} eq 'Y' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "Y" or defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "C" and $options->{"edms"} or defined $appdata and $options->{"bpdf"}) 0 0 0
Condition0%0/18
defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "Y" or defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "C" and $options->{"edms"} or defined $appdata and $options->{"bpdf"}
l!l&&r!l&&!r
000
defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "Y" or defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "C" and $options->{"edms"}
l!l&&r!l&&!r
000
defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "Y"
!ll&&!rl&&r
000
defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "C" and $options->{"edms"}
!ll&&!rl&&r
000
defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "C"
!ll&&!rl&&r
000
defined $appdata and $options->{"bpdf"}
!ll&&!rl&&r
000
MC/DC0%0/7
defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "Y" or defined $appdata and $appdata->{"ED_EDOCSHARE"} eq "C" and $options->{"edms"} or defined $appdata and $options->{"bpdf"}
defined $appdata $appdata->{"ED_EDOCSHARE"} eq "Y" defined $appdata $appdata->{"ED_EDOCSHARE"} eq "C" $options->{"edms"} defined $appdata $options->{"bpdf"}
Truth table0%0/21
A and B or C and D and E or F and G
A= defined $appdataB= $appdata->{"ED_EDOCSHARE"} eq "Y"C= defined $appdataD= $appdata->{"ED_EDOCSHARE"} eq "C"E= $options->{"edms"}F= defined $appdataG= $options->{"bpdf"}
ABCDEFGresult
11XXXXX1
0X111XX1
10111XX1
0X0XX111
0X10X111
0X110111
100XX111
1010X111
10110111
0X0XX0X0
0X0XX100
0X10X0X0
0X10X100
0X1100X0
0X110100
100XX0X0
100XX100
1010X0X0
1010X100
101100X0
10110100
304 or ( defined($appdata) && $appdata->{'ED_EDOCSHARE'} eq 'C' && $options->{'edms'} )
305 or ( defined($appdata) && $options->{'bpdf'} ) )
306 {
307  
308 0 ▶ if ( $options->{'cgi'} && $options->{'cgiged'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'cgi'} and $options->{'cgiged'}) { } 0 0 0
elsif (not $options->{'cgi'}) { } 0 0 0
Condition0%0/3
$options->{"cgi"} and $options->{"cgiged"}
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"cgi"} and $options->{"cgiged"}
$options->{"cgi"} $options->{"cgiged"}
Truth table0%0/3
A and B
A= $options->{"cgi"}B= $options->{"cgiged"}
ABresult
0X0
100
111
309 0 warn "INFO: Direct GED processing...\n";
310 0 my ( $index, @pdfs ) = EDMS_process( $app, $options->{'idldoc'}, $pdf, $index );
311 0 ▶ EDMS_import( $index, @pdfs )
Branch: 1 branch
BranchTrueFalseTotal
EDMS_import($index, @pdfs) or die "ERROR: EDMS_import failed\n" 0 0 0
312 or die "ERROR: EDMS_import failed\n";
313 }
314 elsif ( !$options->{'cgi'} ) {
315 0 ▶ if ( $options->{'bpdf'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"bpdf"}) 0 0 0
316 0 $pdf = oe_background_pdf( $cfg, $options, $pdf, $index );
317 }
318 0 warn "INFO: Preparing ZIP archive for GED...\n";
319 0 EDMS_prepare( $app, $options->{'idldoc'}, $pdf, $index );
320 }
321 }
322  
323 # Now we can remove the index file.
324 0 ▶ if ( $cfg->{'EDTK_TYPE_ENV'} ne 'Test' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{"EDTK_TYPE_ENV"} ne "Test") 0 0 0
325 0 unlink($index);
326 }
327  
328 0 return 1;
329 }
330  
331 # List the entries of a directory whose name matches a regexp, sorted.
332 0 ▶sub listing_depository {
Subroutine listing_depository: not called CC=2 SCAR=17.9
Pod undocumented
333 0 my ( $depo_path, $rgxp_name ) = @_;
334  
335 0 ▶ opendir( my $depot, $depo_path )
Branch: 1 branch
BranchTrueFalseTotal
opendir my $depot, $depo_path or croak("Can't open depository : $depo_path\n") 0 0 0
336 or croak "Can't open depository : $depo_path\n";
337 0 my @contents = grep { /$rgxp_name/xms } readdir($depot);
338 0 closedir($depot);
339  
340 0 my @sorted = sort @contents;
341 0 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 0 ▶sub update_idx {
Subroutine update_idx: not called CC=5 SCAR=34.0
Pod undocumented
347 0 my $idx_file = shift;
348  
349 0 my @tab_line;
350 0 ▶ open my $fh, '<', $idx_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $idx_file or croak(qq[ERROR: Cannot open index file "$idx_file": $!\n]) 0 0 0
351 or croak "ERROR: Cannot open index file \"$idx_file\": $!\n";
352 0 foreach (<$fh>) {
353 0 push @tab_line, $_, $_;
354 }
355 0 close $fh;
356  
357 0 ▶ open $fh, '>', $idx_file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $idx_file or croak(qq[ERROR: Cannot write index file "$idx_file": $!\n]) 0 0 0
358 or croak "ERROR: Cannot write index file \"$idx_file\": $!\n";
359 0 for my $i ( 0 .. $#tab_line ) {
360 0 my $num_line = $i + 1;
361 0 $tab_line[$i] =~ s/;\d+;(\d+);/;$num_line;$1;/xms;
362 0 print {$fh} $tab_line[$i];
363 }
364 0 close $fh;
365  
366 0 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 0 ▶sub oe_background_pdf {
Subroutine oe_background_pdf: not called CC=9 SCAR=45.0
Pod undocumented
373 0 my ( $cfg, $params, $pdf, $index ) = @_;
374  
375 0 my $pdftk_path = config_read('EDOCMNGR')->{'EDMS_BIN_PDFTK'};
376 0 my $pdftk = PDF::Tk->new( pdftk => $pdftk_path );
377  
378 0 my $blank_pdf = $cfg->{'EDTK_DIR_BACKGROUNDS'} . q{/} . 'blank' . q{.} . $cfg->{'EDTK_EXT_PDF'};
379 0 ▶ croak "ERROR: $blank_pdf don't exist" if !-e $blank_pdf;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $blank_pdf 0 0 0
380  
381 0 $pdftk->call_pdftk( $pdf, '%04d_split.pdf', 'burst' );
382  
383 0 my @contents = listing_depository( q{./}, '[0-9]*_split\.pdf$' );
384  
385 0 ▶ if ( ( scalar @{ $params->{'backgrounds'} } ) ne 1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$params->{"backgrounds"};} ne 1 and @contents ne scalar @{$params->{"backgrounds"};}) 0 0 0
Condition0%0/3
scalar @{$params->{"backgrounds"};} ne 1 and @contents ne scalar @{$params->{"backgrounds"};}
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @{$params->{"backgrounds"};} ne 1 and @contents ne scalar @{$params->{"backgrounds"};}
scalar @{$params->{"backgrounds"};} ne 1 @contents ne scalar @{$params->{"backgrounds"};}
Truth table0%0/3
A and B
A= scalar @{$params->{"backgrounds"};} ne 1B= @contents ne scalar @{$params->{"backgrounds"};}
ABresult
0X0
100
111
386 0 && @contents ne ( scalar @{ $params->{'backgrounds'} } ) )
387 {
388 croak "ERROR: received "
389 0 . ( scalar @{ $params->{'backgrounds'} } )
390 . " but needed 1 or "
391 . @contents
392 . "\n Quit cause can't continue";
393 }
394  
395 0 my $index_background = 0;
396 0 foreach (@contents) {
397 my $background_pdf =
398 $cfg->{'EDTK_DIR_BACKGROUNDS'} . q{/}
399 . $params->{'backgrounds'}->[$index_background] . q{_}
400 . $cfg->{'EDTK_CORP'} . q{.}
401 0 . $cfg->{'EDTK_EXT_PDF'};
402 0 ▶ if ( !-e $background_pdf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $background_pdf) 0 0 0
403 0 ▶ if ( $cfg->{'EDTK_TYPE_ENV'} eq 'Production' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{'EDTK_TYPE_ENV'} eq 'Production') { } 0 0 0
404 0 croak "ERROR: background $background_pdf doesn't exist";
405 }
406 else {
407 0 &logger( 4, "Can't find $background_pdf so I put a blank background\n" );
408 0 $background_pdf = catfile( $cfg->{'EDTK_DIR_BACKGROUNDS'}, 'blank.pdf' );
409 }
410 }
411  
412 0 my @cat_tab = ( $_, $blank_pdf );
413 0 $pdftk->call_pdftk( \@cat_tab, $_ . q{.} . 'blank', 'cat' );
414 0 $pdftk->call_pdftk( $_ . q{.} . 'blank', $_, 'multibackground', $background_pdf );
415  
416 0 ▶ if ( ( $index_background + 1 ) < ( scalar @{ $params->{'backgrounds'} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_background + 1 < scalar @{$params->{"backgrounds"};}) 0 0 0
417 0 $index_background++;
418 }
419 }
420  
421 my $pdf_out =
422 0 q{./} . $params->{'app-ref'} . q{.} . $params->{'idldoc'} . q{.} . $cfg->{'EDTK_EXT_PDF'};
423 0 $pdftk->call_pdftk( \@contents, $pdf_out, 'cat' );
424  
425 0 ▶ unlink glob '*split.pdf*' or croak "ERROR: can't delete useless pdf";
Branch: 1 branch
BranchTrueFalseTotal
unlink glob("*split.pdf*") or croak("ERROR: can't delete useless pdf") 0 0 0
426  
427 0 update_idx($index);
428  
429 0 return $pdf_out;
430 }
431  
432 0 ▶sub oe_csv_to_doc {
Subroutine oe_csv_to_doc: not called CC=7 SCAR=40.3
Pod documented
433 0 my ( $input, $endtag ) = @_;
434  
435 0 my $doc = oEdtk::TexDoc->new;
436 0 ▶ open( my $fh, '<', $input ) or die "Cannot open \"$input\": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $input or die qq[Cannot open "$input": $!\n] 0 0 0
437  
438 0 my $csv = Text::CSV->new( { binary => 1 } );
439 0 my @cols = map { s/_//gr } @{ $csv->getline($fh) };
440  
441 0 ▶ while ( my $vals = $csv->getline($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vals = $csv->getline($fh)) 0 0 0
442 0 my %lists = ();
443 0 ▶ for ( my $i = 0 ; $i < $#cols ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#cols) 0 0 0
444 0 my ( $key, $val ) = ( $cols[$i], $vals->[$i] );
445 0 ▶ if ( $key !~ /^(\D+)\d+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $key =~ /^(\D+)\d+$/) { } 0 0 0
elsif ($val ne '') { } 0 0 0
446 0 $doc->append( $key, $val );
447 }
448 elsif ( $val ne '' ) {
449 0 push( @{ $lists{$1} }, $val );
450 }
451 }
452 0 ▶ while ( my ( $key, $val ) = each %lists ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %lists) 0 0 0
453 0 $doc->append_table( $key, @$val );
454 }
455 0 $doc->append($endtag);
456 }
457 0 close($fh);
458 0 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 0 ▶sub _filiere_is_rectoverso {
Subroutine _filiere_is_rectoverso: not called CC=2 SCAR=17.9
465 0 my ( $lot, $cfg ) = @_;
466 0 my $filiere = substr( $lot, rindex( $lot, '.' ) + 1 );
467 0 ▶ my @declared = split ' ', ( $cfg->{'EDTK_FILIERES_RECTO_VERSO'} // '' );
Condition0%0/2
$cfg->{"EDTK_FILIERES_RECTO_VERSO"} // ""
l!l
00
MC/DC0%0/1
$cfg->{"EDTK_FILIERES_RECTO_VERSO"} // ""
$cfg->{"EDTK_FILIERES_RECTO_VERSO"}
Truth table0%0/2
A
A= $cfg->{"EDTK_FILIERES_RECTO_VERSO"}
Aresult
11
00
468 0 return scalar grep { $_ eq $filiere } @declared;
469 }
470  
471 0 ▶sub oe_outmngr_output_run_tex {
Subroutine oe_outmngr_output_run_tex: not called CC=9 SCAR=45.0
Pod documented
472 0 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 0 my $cfg = config_read('COMPO');
482 0 my $type_env = $cfg->{'EDTK_TYPE_ENV'};
483 0 my $basedir = $cfg->{'EDTK_DIR_OUTMNGR'};
484  
485 0 warn "INFO: Calling omgr_export\n";
486 0 my @lots = omgr_export(%$filter);
487  
488 0 foreach (@lots) {
489 0 my ( $lot, @doclibs ) = @$_;
490  
491 0 my $lotdir = "$basedir/$lot";
492 0 ▶ chdir($lotdir) or die "Cannot change directory to \"$lotdir\": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $lotdir or die qq[Cannot change directory to "$lotdir": $!\n] 0 0 0
493 0 warn "INFO: Preparing job ticket $lot for compo (doclibs = @doclibs)\n";
494  
495 # Create the intermediate stream.
496 0 my $doc = oEdtk::TexDoc->new;
497 0 $doc->append( oe_csv_to_doc( "$lot.job", 'edStartPg' ) );
498  
499 0 warn "INFO: Preparing index $lot for compo\n";
500 0 $doc->append( oe_csv_to_doc( "$lot.idx", 'xFLigne' ) );
501 0 $doc->append('edEndPg');
502 0 $doc->append('xFinFlux');
503  
504 # The intermediate stream is written in UTF-8, the encoding the
505 # composition expects.
506 0 ▶ open( my $txt, '>:encoding(UTF-8)', 'LOTPDF.txt' )
Branch: 1 branch
BranchTrueFalseTotal
open my $txt, ">:encoding(UTF-8)", "LOTPDF.txt" or die qq[Cannot open "LOT.txt": $!\n] 0 0 0
507 or die "Cannot open \"LOT.txt\": $!\n";
508 0 print $txt "$doc";
509 0 close($txt);
510  
511 0 warn "INFO: Composition $lot in $basedir/$lotdir\n";
512 my $options = {
513 jobname => $lot,
514 0 incdirs => [ $cfg->{'EDTK_DIR_DOCLIB'} ]
515 };
516 0 oe_compo_run( 'LOTPDF', $options );
517 0 oe_after_compo( 'LOTPDF', $options );
518  
519 # Generate the final PDF file.
520 0 ▶ if ( !-f "$lot.pdf" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f "$lot.pdf") 0 0 0
521 0 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 0 ▶ if ( _filiere_is_rectoverso( $lot, $cfg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_filiere_is_rectoverso($lot, $cfg)) 0 0 0
527 0 make_docs_even_numbered("$lot.idx");
528 }
529  
530 0 warn "INFO: Packaging $basedir $lot\n";
531 0 my $zip = Archive::Zip->new();
532 0 $zip->addFile( "$lotdir/$lot.idx", "$lot.idx" );
533 0 $zip->addFile( "$lotdir/$lot.pdf", "$lot.pdf" );
534 0 ▶ die "ERROR: Could not create zip archive\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $zip->writeToFileNamed("$basedir/$lot.zip") == 0 0 0 0
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 0 chdir($basedir);
541 0 foreach (@lots) {
542 0 my ($lot) = @$_;
543  
544 0 print "$basedir/$lot.zip\n";
545 0 ▶ if ( $type_env !~ /^De/i ) { # not done for the Dev or Debug environments
Branch: 1 branch
BranchTrueFalseTotal
unless ($type_env =~ /^De/i) 0 0 0
546 0 warn "INFO: removing intermediate files ($type_env)\n";
547 0 rmtree("$basedir/$lot");
548 }
549 }
550 0 return @lots;
551 }
552  
553 20 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
« lib/oEdtk/Outmngr.pm ↑ Summary blib/lib/oEdtk/Tracking2.pm »