blib/lib/oEdtk/libDev.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/oEdtk/Spool.pm ↑ Summary lib/oEdtk/AddrField.pm »
1 package oEdtk::libDev;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #[JW for editor]:mode=perl:tabSize=8:indentSize=2:noTabs=true:indentOnEnter=true:
6 #
7  
8 BEGIN {
9 1 ▶ use oEdtk::logger 2.1109;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use oEdtk::Main 0.42;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use oEdtk::trackEdtk qw (ini_Edtk_Conf conf_To_Env env_Var_Completion);
12 use Config::IniFiles;
13  
14 # use File::Copy; # to be provided to run dev in the prod context
15 use File::Basename;
16 use Date::Calc qw(Gmtime Today Compress);
17  
18 require oEdtk::tuiEdtk;
19  
20 use Exporter;
21 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %hListeId); # %EXPORT_TAGS);
22 use warnings;
23  
24 $VERSION = 2.1126;
25 @ISA = qw(Exporter);
26 @EXPORT = qw(
27 $NOK
28 run_Edtk_dev
29 IdUniqueSur7
30 lastLong
31 lastCourt
32 delSp
33 );
34  
35 @EXPORT_OK = qw(
36 check_EDTK_DIR wait_Enter
37 tree_Directory_Completion
38 clean_full_dir file_list
39 prep_Edtk_Data
40 )
41  
42 # catSp
43 }
44  
45 our $NOK = -1;
46  
47 #
48 # CODE - DOC AT THE END
49 #
50  
51 # limit the export of these functions (export OK)
52  
53 sub check_EDTK_DIR () {
54  
55 # check the Edtk environment,
56 # in particular the existence of the EDTK_DIR directories declared in the environment
57 while ( ( my $cle, my $valeur ) = each(%ENV) ) {
58 if ( $cle =~ /EDTK\_DIR/ ) {
59  
60 #print "$cle => $valeur\n";
61 env_Var_Completion($valeur);
62 tree_Directory_Completion($valeur);
63 }
64 }
65 1;
66 }
67  
68 sub tree_Directory_Completion {
69  
70 # complete, if needed, the whole tree of the path passed as a parameter
71 # by creating the directories of the path passed as a parameter
72 my $tree = shift;
73  
74 #print $tree."\n";
75  
76 my @listDir = split( /[\/\\]/, $tree );
77 $tree = "";
78 if ( -e "$listDir[0]/$listDir[1]" ) {
79 for ( my $i = 0 ; $i < ( scalar @listDir ) ; $i++ ) {
80 $tree .= $listDir[$i] . "/";
81  
82 #print "\t $i = $listDir[$i]\n";
83 if ( -e $tree ) {
84 }
85 elsif ( -d $tree ) {
86 }
87 else {
88 &logger( 6, "INFO: -> mkdir, create $tree\n" );
89 eval { mkdir $tree; };
90 die "ERROR: mkdir $tree $@" if $@;
91 }
92 }
93 }
94 1;
95 }
96  
97 sub run_Edtk_dev() {
98 import oEdtk::tuiEdtk;
99  
100 my $iniEdtk = ini_Edtk_Conf;
101 conf_To_Env( $iniEdtk, 'DEFAULT' );
102 conf_To_Env( $iniEdtk, 'ENVDESC' );
103  
104 # $ENV{EDTK_FTYP_DFLT} ||=$ENV{EDTK_FTYP_HOMOL};
105  
106 if ( $ENV{EDTK_DIR_BASE} eq "" ) {
107 not_Configured();
108 exit $NOK;
109 }
110 &check_EDTK_DIR;
111  
112 start_Screen();
113  
114 my $styleApp = $ENV{EDTK_DIR_SCRIPT} . "/" . $ENV{EDTK_PRGNAME};
115 env_Var_Completion($styleApp);
116 env_Var_Completion( $ENV{EDTK_DIR_APPTMP} );
117 warn "INFO: " . oe_now_time() . " -START- \n";
118  
119 my $doclean = 1;
120 {
121 # LaTeX case
122 $doclean = 0;
123 $ENV{EDTK_DOC_OUTPUT} = "$ENV{EDTK_FDATAOUT}.$ENV{EDTK_EXT_WORK}";
124  
125 # xxxxxxxx check, but we no longer need it now that it works (no need to check the intermediate files)
126 # $ENV{EDTK_EXT_DEFAULT}=$ENV{EDTK_EXT_WORK};
127  
128 chdir( $ENV{EDTK_DIR_APPTMP} )
129 or die "ERROR: Cannot change current directory: $!\n";
130 my $ctrl = &prep_Edtk_Data( $ENV{EDTK_FDATAIN} . "." . $ENV{EDTK_EXT_DATA} )
131 ; #, $ENV{EDTK_DOC_OUTPUT});
132 warn "INFO: " . oe_now_time() . " -END Extract- \n";
133  
134 if ( $ctrl eq $NOK ) {
135 warn "ERROR: return $? in prep_Edtk_Data\n";
136 &wait_Enter();
137 exit $NOK;
138 }
139 else {
140 #warn "INFO: data extraction seem good\n";
141 #$ENV{EDTK_DOC_OUTPUT} =$ctrl;
142 }
143 }
144  
145 warn "INFO: " . oe_now_time() . " -END- \n";
146  
147 # if everything ran correctly, empty the tmp dir
148 # $| = 1; # autoflush
149 if ($doclean) {
150 warn "INFO: clean temp\n";
151 clean_full_dir( $ENV{EDTK_DIR_APPTMP} );
152 }
153  
154 stop_Screen();
155 1;
156 }
157  
158 sub prep_Edtk_Data {
159  
160 # trigger the data extraction process
161 # in the context of the automated run run_Edtk_dev
162 my $command = "$ENV{EDTK_DIR_APP}/$ENV{EDTK_PRGNAME}/$ENV{EDTK_PRGNAME}.$ENV{EDTK_EXT_PERL}";
163 my $arg1 = shift;
164 my $arg2 = shift || "";
165 my $option = shift || "";
166  
167 env_Var_Completion($arg2);
168 warn "INFO: $command $arg1 $arg2\n";
169 env_Var_Completion($command);
170 env_Var_Completion($arg1);
171 env_Var_Completion($option);
172  
173 eval {
174 #system($command, $arg1, $arg2, $option);
175 system( $command, $arg1, $option );
176 };
177  
178 if ($@) {
179 warn "INFO: ERROR -> $@\n";
180 warn "INFO: ERROR $command $arg1 $arg2 return $? \n";
181  
182 return $NOK;
183 }
184  
185 1;
186 }
187  
188 sub wait_Enter() {
189 print "\nPause, press <enter> to continue...\n";
190 until (<STDIN>) {
191 }
192 1;
193 }
194  
195 sub delSp(\$) { ## no critic (ProhibitSubroutinePrototypes)
196  
197 #remove spaces
198 # the parameter is an implicit reference, example: delSp($chaine)
199 # returns the number of removed characters
200 my $rChaine = shift;
201 return ${$rChaine} =~ s/\s//go;
202 }
203  
204 sub IdUniqueSur6 () { # deprecated function
205 #format an id to 6 alphanumeric characters
206 # receives as a parameter the reference to an identifier
207 # duplicate handling internal to the function run
208 my $rId = shift;
209 my %hListeId;
210 my $cpt = 0;
211 ${$rId} = sprintf( "%-6.6s", ${$rId} );
212 ${$rId} =~ s/\s/x/g;
213  
214 while ( exists( $hListeId{ ${$rId} } ) ) {
215 ${$rId} = sprintf( "%-4.4s%0.2d", ${$rId}, $cpt++ );
216 }
217 $hListeId{ ${$rId} } = 1;
218 1;
219 }
220  
221 {
222 my $appelIUS7 = 0; # constant variable local to the function
223  
224 sub IdUniqueSur7 () {
225  
226 # define a unique identifier over 7 characters
227 # the first 6 characters of the passed key are extracted
228 # if the id is already known, take the first 4 and append a 3-digit counter (matches the call sequence)
229 # if it is still not unique, take the first 3 characters and pad the 3-digit counter with a character
230 # receives: - a reference to a key
231 # - optional: a reference to a counter value (3 digits)
232  
233 my ( $refId, $rInit ) = @_;
234 if ($rInit) { $appelIUS7 = ${$rInit} }
235 else { $appelIUS7++ }
236  
237 ${$refId} = sprintf( "%-7.7s", ${$refId} );
238 ${$refId} =~ s/\s/x/g;
239 if ( exists( $hListeId{ ${$refId} } ) ) {
240 ${$refId} = sprintf( "%-4.4s%0.3d", ${$refId}, $appelIUS7 );
241  
242 my $cpt = 97; # for the character "a"
243 while ( exists( $hListeId{ ${$refId} } ) ) {
244 ${$refId} = sprintf( "%-3.3s%0.3d%1.1s", ${$refId}, $appelIUS7, chr( $cpt++ ) );
245 die &logger( $NOK, "cannot create a unique key" ) if ( $cpt >= 123 );
246  
247 # use Log::Log4perl;
248 # Log::Log4perl->init("log.conf"); => read log.conf
249 # $logger = Log::Log4perl->get_logger("");
250 # $logger->logdie("cannot create a unique key") if ($cpt >= 123);
251 # $logger->trace("..."); # Log a trace message
252 # $logger->debug("..."); # Log a debug message
253 # $logger->info("..."); # Log a info message
254 # $logger->warn("..."); # Log a warn message / $logger->error_warn("..."); (includes the call to warn())
255 # $logger->error("..."); # Log a error message / $logger->logdie ("..."); (includes the call to die())
256 # $logger->fatal("..."); # Log a fatal message
257 }
258 }
259 $hListeId{ ${$refId} } = 1;
260 return 1;
261 }
262 }
263  
264 sub lastLong {
265  
266 # select the most significant alpha token of the string passed by reference
267 # example call: $mot=&lastLong ($chaine);
268 # the separator characters are spaces, _ or -
269  
270 my $chaine = shift;
271 $chaine =~ s/-/ /g;
272 $chaine =~ s/_/ /g;
273 oe_trimp_space($chaine);
274  
275 # If PATTERN contains parentheses (and therefore subpatterns), an extra element is created
276 # in the result array for each string matched by the subpattern.
277 # split(/([,-])/, "1-10,20", 3);
278 # produces the list of values
279 # (1, '-', 10, ',', 20)
280 # http://perl.enstimac.fr/DocFr/perlfunc.html#item_split
281 my @mots = split( " ", $chaine );
282 my ( $mot, $motLong );
283 my $taille = 0;
284  
285 while ( $mot = shift(@mots) ) {
286 if ( length($mot) >= $taille ) {
287 $taille = length($mot);
288 $motLong = $mot;
289 }
290 }
291  
292 return $motLong;
293 }
294  
295 sub lastCourt {
296  
297 # select the shortest alpha token of the string passed by reference
298 # example call: $mot=&lastCourt ($chaine);
299 my $chaine = shift;
300 $chaine =~ s/-/ /g;
301 $chaine =~ s/_/ /g;
302 oe_trimp_space($chaine);
303 my @mots = split( " ", $chaine );
304 my ( $mot, $motCourt );
305 my $taille = 1000;
306  
307 while ( $mot = shift(@mots) ) {
308 if ( length($mot) <= $taille ) {
309 $taille = length($mot);
310 $motCourt = $mot;
311 }
312 }
313  
314 #print "$chaine $taille $motCourt\n";
315 return $motCourt;
316 }
317  
318 sub clean_full_dir {
319  
320 # unlink alone does not work on windows with wildcards ???
321 # this function cleans the directory passed as a parameter
322 # and the subdirectories
323 my ( $membre, $option, $key, @listRep );
324 my $DIR;
325 $membre = shift;
326 $option = shift;
327 $option ||= "";
328  
329 # build the search pattern for the regular expression
330 my $suppr_motif;
331 $suppr_motif = ".*";
332  
333 # print "del. -> $suppr_motif -> $membre\n";
334  
335 # handle directory separators
336 # the standard perl separator (on *nix / windows ...) -> /
337 $membre .= "/";
338  
339 # the separator supplied on Dos is converted \ -> /
340 $membre =~ s/\\+/\//g;
341  
342 # remove repeated /+ -> /
343 $membre =~ s/\/+/\//g;
344  
345 unshift( @listRep, $membre );
346  
347 $key = pop @listRep;
348 ITEMS: for ( ; $key ; ) {
349  
350 # since listRep is interactive foreach ignores values added to listRep
351 # print "path= <$key> < \@listRep=>@listRep<\n";
352  
353 eval { opendir( $DIR, $key ); };
354 if ($?) {
355 warn "INFO: WARNING opendir(DIR, $key) return $?\n";
356 next ITEMS;
357 }
358  
359 $membre = readdir($DIR);
360 for ( ; $membre ; ) {
361 if ( $membre ne "." && $membre ne ".." ) {
362  
363 # if the member is a directory
364 if ( -d $key . $membre ) {
365  
366 # print "$key$membre \t (directory)\n";
367 push( @listRep, "$key$membre/" );
368  
369 }
370 else {
371 my $file = $key . $membre;
372  
373 # print "$file\n";
374 if ( $file =~ m{$suppr_motif} ) {
375 if ( $option ne "--dry-run" ) {
376 warn "INFO: deleting $file\n" if ( $option eq "--verbose" );
377 unlink($file);
378 }
379 else {
380 warn "INFO: --dry-run : $file\n";
381 }
382 }
383 } #end of if
384 } #end of if
385 $membre = readdir($DIR);
386 } #end of for
387  
388 closedir($DIR);
389 $key = pop @listRep;
390 } #end of for
391  
392 #close OUT;
393  
394 1;
395 }
396  
397 sub file_list {
398 my ( $key, @listRep, @listFile );
399 my $DIR;
400 my $membre = shift;
401 my $motif = shift;
402 my $opt = shift;
403 $opt ||= "";
404  
405 # handle directory separators
406 # the standard perl separator (on *nix / windows ...) -> /
407 $membre .= "/";
408  
409 # the separator supplied on Dos is converted \ -> /
410 $membre =~ s/\\+/\//g;
411  
412 # remove repeated /+ -> /
413 $membre =~ s/\/+/\//g;
414  
415 unshift( @listRep, $membre );
416  
417 $key = pop @listRep;
418 ITEMS: for ( ; $key ; ) {
419  
420 # since listRep is interactive foreach ignores values added to listRep
421 # print "path= <$key> < \@listRep=>@listRep<\n";
422  
423 eval { opendir( $DIR, $key ); };
424 if ($?) {
425 warn "INFO: WARNING opendir(DIR, $key) return $?\n";
426 next ITEMS;
427 }
428  
429 $membre = readdir($DIR);
430 for ( ; $membre ; ) {
431 if ( $membre ne "." && $membre ne ".." ) {
432  
433 # if the member is a directory
434 if ( -d $key . $membre ) {
435  
436 # print "$key$membre \t (directory)\n";
437 push( @listRep, "$key$membre/" );
438  
439 }
440 else {
441 my $file = $key . $membre;
442  
443 # print "$file\n";
444 if ( $file =~ m{$motif} ) {
445 push( @listFile, $file );
446 }
447 } #end of if
448 } #end of if
449 $membre = readdir($DIR);
450 } #end of for
451  
452 closedir($DIR);
453 $key = pop @listRep;
454 } #end of for
455  
456 #close OUT;
457  
458 return @listFile;
459 }
460  
461 END {
462 }
463 1;
464  
465 =head1 NAME
466  
467 oEdtk::libDev - helpers for running an application in the development environment
468  
469 =head1 SYNOPSIS
470  
471 use oEdtk::libDev;
472  
473 run_Edtk_dev();
474  
475 =head1 DESCRIPTION
476  
477 Development-run helpers: check the C<EDTK_DIR> directory tree, prepare the input
478 data, launch the composition in a development environment, and the small string
479 helpers used along the way (unique identifiers, longest and shortest token, space
480 removal).
481  
482 =head1 FUNCTIONS
483  
484 =head2 $NOK
485  
486 The conventional "not OK" return code, C<-1>.
487  
488 =head2 run_Edtk_dev
489  
490 run_Edtk_dev();
491  
492 Run an application in the development environment: load the configuration, check
493 the directory tree, prepare the data and launch the composition, with the start
494 and stop screens.
495  
496 =head2 IdUniqueSur7
497  
498 IdUniqueSur7(\$id);
499  
500 Make C<$id> unique over seven characters: the first six characters are taken, and
501 on a clash the first four followed by a three-digit counter are used, falling
502 back to the first three characters padded with a letter. The optional second
503 argument is a reference to the counter to seed.
504  
505 =head2 lastLong
506  
507 my $token = lastLong($string);
508  
509 Return the longest token of C<$string>, where tokens are separated by spaces,
510 underscores or hyphens.
511  
512 =head2 lastCourt
513  
514 my $token = lastCourt($string);
515  
516 Return the shortest token of C<$string>, with the same separators as
517 L</lastLong>.
518  
519 =head2 delSp
520  
521 my $count = delSp($string);
522  
523 Remove every space from C<$string>, in place, and return the number of characters
524 removed.
525  
526 =head2 check_EDTK_DIR
527  
528 check_EDTK_DIR();
529  
530 Expand every C<EDTK_DIR> environment variable and create the matching directory
531 tree.
532  
533 =head2 wait_Enter
534  
535 wait_Enter();
536  
537 Print a pause prompt and wait for the enter key.
538  
539 =head2 tree_Directory_Completion
540  
541 tree_Directory_Completion($path);
542  
543 Create, if needed, every directory of C<$path>.
544  
545 =head2 clean_full_dir
546  
547 clean_full_dir($dir, $option);
548  
549 Recursively delete the files under C<$dir>. With C<--dry-run> nothing is removed;
550 with C<--verbose> each deletion is reported.
551  
552 =head2 file_list
553  
554 my @files = file_list($dir, $pattern, $option);
555  
556 Recursively collect the files under C<$dir> whose path matches C<$pattern>.
557  
558 =head2 prep_Edtk_Data
559  
560 my $ok = prep_Edtk_Data($input, $output, $option);
561  
562 Trigger the data-extraction step of the current application; return C<$NOK> on
563 failure.
564  
565 The deprecated C<IdUniqueSur6> sub is kept only for reference and is not part of
566 the public API.
567  
568 =head1 AUTHOR
569  
570 The oEdtk team.
571  
572 =cut
« blib/lib/oEdtk/Spool.pm ↑ Summary lib/oEdtk/AddrField.pm »