File: load_tools.pl

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (601 lines) | stat: -rwxr-xr-x 15,792 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#!/usr/local/apps/perl/current/bin/perl -I/usr/local/lib/metaps/perl -I.

use confUtils;
use Getopt::Long;
use strict;

#--------------------------------------------------
# 
#--------------------------------------------------

#The root page of examples in confluence!!! The root page must exit!!!!
my $unique;
my $binDir;
my $resMode="doc"; #can be: doc, example, both

my $preview=0;
my $previewStr=" - preview";

#---------------------------------
# Read arguments
#---------------------------------

GetOptions("unique=s" => \$unique,
           "binDir=s" => \$binDir,
           "confSpace=s" => \$confUtils::confSpace,
           "mode=s" => \$resMode)
 	or die("Error in command line arguments\n");
 
print "confluence space=".$confUtils::confSpace."\n";

#Check resmode
die("Mode (-m switch) must be \"doc\", \"example\" or \"both\"") if($resMode ne "both" && $resMode ne "doc" && $resMode ne "example"); 

#----------------------------------
# Dirs
#----------------------------------

#The tools shell scripts are located here
my $toolsSrcDir="../tools"; 

#The tools executables are located here. The shell scripts will be copied into
#this directory. The shell scipts are required to be in the directory of the executables.
my $toolsBinDir;
if ($binDir && -d $binDir) {
    $toolsBinDir=$binDir;
} else {
    die("Tools bin dir is not defined. Please use the -b switch to specify it!");
}

#Check definition path
my $defPath=$ENV{ECCODES_DEFINITION_PATH}; 
die "Env var ECCODES_DEFINITION_PATH should point to a valid directory" if ( $defPath eq "" || ! -d "$defPath" );

#The genareted html files are stored here
my $htmlDir=$ENV{TMPDIR}."/res_tools_html";

my %cmdSwitches=();
my %allTools=read_config();

#----------------------------------------
# Preparations
#----------------------------------------

unless ( -d  $htmlDir ) {
    mkdir $htmlDir	
}

if($resMode eq "both" || $resMode eq "doc") {

#==========================================================
#
# Documentation (including examples)
#
#==========================================================

my $rootPage="Command line tools";
if ($preview == 1) {
  $rootPage=$rootPage.$previewStr;
}

#---------------------------------------------------
# Loop for the code types (e.g. grib, bufr, etc.)
#---------------------------------------------------

foreach my $cType (keys %allTools) {
    
    print "----------------------------------------\n";
    print "  Processing tools for: $cType\n";
    print "----------------------------------------\n";
    
    #Get the examples
    my @tools=@{$allTools{$cType}};  
    
    if($#tools+1 == 0) {
        die "No tools are defined!\n";
    }

    #---------------------------------------------------
    # Create a parent page for the tools of this type
    #---------------------------------------------------

    print "--> Genarate parent page\n";

    my $parentPage=parentPageTitle($cType);
    
    unless($unique) {
    	makeParentPage($rootPage,$parentPage,$htmlDir,$cType,@tools);
    }
    	
    #--------------------------------
    # Loop for the tools
    #--------------------------------
    
    foreach my $name (@tools) {
    
        if($unique eq "" or $name eq $unique) {
        
        print "-------------------------------\n--> tool: ".$name."\n";
        
            my $fOut=$htmlDir."/".$cType."_".$name.".html";
            open(OUT,">$fOut") or die "$fOut: $!"; 
        
            my $str=getDoc($name);
        
            $str=$str.getExample($name);

            print OUT $str;
        
            close OUT;
        
            #Upload the file to confluence 
            my $pageTitle=toolsPageTitle($name);
            confUtils::loadToConf($fOut,$pageTitle,$parentPage); 
        }          
    }
}

} 

if($resMode eq "both" || $resMode eq "example")  {

#==========================================================
#
# Examples only
#
#==========================================================

my $rootPage="Command line tools examples";
if ($preview == 1) {
  $rootPage=$rootPage.$previewStr;
}
  
#---------------------------------------------------
# Loop for the code types (e.g. grib, bufr, etc.)
#---------------------------------------------------

foreach my $cType (keys %allTools) {

    if($cType eq "Generic") {continue;}

    print "----------------------------------------\n";
    print "  Processing tools for: $cType\n";
    print "----------------------------------------\n";
    
    #Get the examples
    my @tools=@{$allTools{$cType}};  
    
    if($#tools+1 == 0) {
        die "No tools are defined!\n";
    }

    #---------------------------------------------------
    # Create a parent page for the tools of this type
    #---------------------------------------------------

    #print "--> Genarate parent page\n";

    my $pageTitle=parentPageTitle($cType). " examples";
    my $parentPage=$rootPage;

    #unless($unique) {
    #    makeParentPage($rootPage,$parentPage,$htmlDir,$cType,@tools);
    #}
     
    #--------------------------------
    # Loop for the tools
    #--------------------------------
    
    my $fOut=$htmlDir."/".$cType."_all.html";
    open(OUT,">$fOut") or die "$fOut: $!"; 

    foreach my $name (@tools) {
    
        if($unique eq "" or $name eq $unique) {
        
            print "-------------------------------\n--> tool: ".$name."\n";
        
            my $str=getExample($name);

            print OUT $str;
        }          
    }
    confUtils::loadToConf($fOut,$pageTitle,$parentPage); 
    close OUT;
}    

}

#===========================================================
#===========================================================
#
# FUNCTIONS 
#
#===========================================================
#===========================================================

#====================================================
#
# Functions related to the examples
#
#====================================================

#------------------------------------------
# Read in example list
#------------------------------------------

sub read_config {

	my %res=();
	my $fIn="tools.par";
	open(IN,"<$fIn") or die "$fIn: $!";
	
    my $line;	
    my $actType;
    while (defined ($line = <IN>)) {
		chomp($line);
		if ($line =~ /.*#/) {next;}
		
        #print $line."\n";
    
        $line =~ s/\"//g;
    
        my ($a)=($line =~ /(\S+)\s*\(/);
        if($a and ($a !~ /\!/)) {
            $actType=$a;
            #print "type: ".$a."\n";
            #$res{$actType};
            next;
        }
        
        if( $line =~ /\)/) {
            $actType="";
            next;
        }
        
        if(length($actType) > 0) {  
            
            #If ! presents we skip this tool
            if($line =~ /^\s*!/) {
                next;
            
            #A switch is specified for the tool's shell script
            } elsif( $line =~ /\S+ \S+/) {
                      
                 my ($b,$c)=($line =~ /(\S+) (\S+)/);  
                 if($b) {
                        push(@{$res{$actType}},$b);
                        $cmdSwitches{$b}="$c";
                 }
            
            #The tools shell script has to be called without a switch          
            } else {          
                my ($b) =($line =~ /(\S+)/);
                #print "ex: ".$b."\n";
                if($b) {
                    push(@{$res{$actType}},$b);
                }    
            }
        }        
        
	}	
    
    close IN;
    
	return %res;
}

#-----------------------------------------------------------------------------
# Genarate the page title for the parent page for a given type of examples
#-----------------------------------------------------------------------------

sub parentPageTitle {
   
   my ($codeType) = @_;
   
   my $str=$codeType." tools";
   if($preview == 1) { $str=$str.$previewStr;}
   return $str;
}   

#--------------------------------------------------------
# Create parent page for a given type of examples
#--------------------------------------------------------

sub makeParentPage {
    my ($rootPage,$pageTitle,$outDir,$cType,@xmp) = @_;
    
    my $f=$outDir."/parent_".$cType."html";
  
    open(OUT,">$f") or die "$f: $!"; 
        
    my $str="<table><tbody>";
    $str=$str."<tr><th>Name</th><th>Description</th></tr>";
    
    foreach my $xm (@xmp) {
        $str=$str."<tr><td>".confUtils::linkToPage(toolsPageTitle($xm),toolsPageTitle($xm))."</td><td>".getDescription($xm)."</td></tr>";
    }        
    $str=$str."</tbody></table>";                   
    
    print OUT $str;
    
    close OUT;
    
    #Load it to confluence
    confUtils::loadToConf($f,$pageTitle,$rootPage);
}

#-----------------------------------------------------------------------------
# Genarate the page title for a given example
#-----------------------------------------------------------------------------
 
sub toolsPageTitle {
   
   my ($exName) = @_;
   
   my $str=$exName;
   if($preview == 1) { $str=$str.$previewStr;}
   return $str;
}    
  
#-----------------------------------------------------------
# Get the file path for an example with the given language 
#-----------------------------------------------------------

sub toolsBinPath {    
    my ($name) = @_;
    return $toolsBinDir."/".$name;
}

#-----------------------------------------------------------
# Check if the file for a given example exists.  
#-----------------------------------------------------------

sub hasExample {
    
    my ($name,$lang) = @_;
    #my $f=examplePath($name,$lang);
    #if( -e $f ) {return 1};
    
    #print "$f no!!!\n";
    
    return 0;
}

sub docPath {
    
    my ($name) = @_;
    
    my $fTmp=$htmlDir."/doc_".$name.".tmp";
    
    my $exe=$name;
    if( -e $toolsBinDir."/".$exe) {
        
        my $swt;
        if(defined($cmdSwitches{$name})) {
            $swt=$cmdSwitches{$name};
        }    
       
	my $cmd;
	
	#If the switch is desc it does not run the shell script but 
	#use the .desc file"
	if ($swt eq "desc") {
		$cmd="cp ".$toolsSrcDir."/".$name.".desc ".$fTmp;
	} else {
        $cmd=$toolsBinDir."/".$exe."  > ".$fTmp;
    }
	
	print "\tGenerate documentation with command:\n\t\t".$cmd."\n";
	
	system($cmd);
        return $fTmp;
    }
    
    return "";    
} 
   
#------------------------------------------------------------
# 
#------------------------------------------------------------

sub examplePath {
    
    my ($name) = @_;
    
    my $fTmp=$htmlDir."/ex_".$name.".tmp";
    
    my $script=$name.".sh";
    my $exe=$toolsBinDir."/".$script;
    
    #The shell script has to be in the bin dir. If it is not there we 
    #copy it into it.
    unless( -e $exe) {
        my $cmd="cp ".$toolsSrcDir."/".$script." ".$toolsBinDir;
        system($cmd);
    }

    if( -e $exe) {        
        my $cmd="cd ".$toolsBinDir.";./".$script."  > ".$fTmp."; cd -";
        print "\tGenerate examples text with command:\n\t\t".$cmd."\n";        
        system($cmd);
        return $fTmp;
    } else {
        print "\tShell script was not found for tool ".$name." as ".$exe." .Could not generate examples\n";
    }
    
    return "";    
}    

#------------------------------------------------------------
# Generate confluence formatted html string form a source file
#------------------------------------------------------------
sub getDoc {
    my ($name) = @_;
    my $fPath=docPath($name);
    my $str;
    my $tbody=0;

    open (IN,"<$fPath") or return "";
    while (defined (my $line = <IN>)) {
            
        $line =~ s/^\s+//g;
        $line=~ s/\n//g;
        
        if($line =~ /^NAME\s*/) {
            next;
        }
        
        if($line =~ /^DESCRIPTION|USAGE|OPTIONS\s*/) {
            
            if($tbody == 1) {
                $str=$str."</p>";
                $tbody=0;
            }    
            
            $str=$str."<h2>".$line."</h2>";
           
            unless($line =~ /OPTIONS\s*/)
            {
                $str=$str."<p>";
                $tbody=1;
            }    
            
        } elsif( $line =~ /^\s*-\w /) {
            
            if($tbody == 1) {
                $str=$str."</p>";
                $tbody=0;
            }    

            my @v=split(/\t|\s{2,}/,$line);

            if($#v+1 > 0) {
                $str=$str."<br /><b>".@v[0]."</b><br /><p>";
                if($#v+1 > 1) {
                    for(my $k=1; $k <= $#v; $k++) {
                        $str=$str.@v[$k]." ";
                    }
                }        
            }

            $tbody=1;
        
        } else {
            
            if($line =~ /\S/) {
                $str=$str.$line." ";
            } elsif ($tbody == 1) {
                $str=$str."</p>";
                $tbody=0;
            }
        }  
    } 

    close IN;
    if($tbody == 1) {
        $str=$str."</p>";
    }    

    return $str;
}    

#--------------------------------------------------------
# Extract the description of a given example
#--------------------------------------------------------

sub getDescription {
    my ($name) = @_;
    my $fPath=docPath($name);

    print "\tExtract desription from: ".$fPath."\n";

    my $str;

    local $/=undef;  
    open (IN,"<$fPath") or return "";
    $str=<IN>;
    close IN;

    ($str) = ($str =~ /DESCRIPTION\s*([\S|\s]+)USAGE/);
    $str =~ s/\n/ /g; 

    return $str;
}  

#--------------------------------------------------------
#
#--------------------------------------------------------
sub getExample {

    my ($name) = @_;
    my $fPath=examplePath($name);    

    unless($fPath) {
        return "";
    }   

    my $str;

    local $/=undef;  
    open (IN,"<$fPath") or return "";
    $str=<IN>;
    close IN;

    my @v=split(/-#/,$str);

    $str="<h2>".$name." examples</h2>";
    $str=$str."<ol>";
    foreach my $block (@v) {
        print "block=".$block."\n";
        
        #If there is text
        if($block =~ /\S+/) {
        
            $block =~ s/\\n//g;
            $block =~ s/\n>/\n> /g;
            $block =~ s/&&/&amp;&amp;/g;
            
            my $blockMod;
            my $prevLine;
            my $inList=0;
            foreach my $line (split(/\n/,$block)) {
                    
                if($line =~ /\\verbatim/) {
                    $line =~ s/\\verbatim/<ac:structured-macro ac:name="code"><ac:plain-text-body><!\[CDATA\[/g;  
                } elsif($line =~ /\\endverbatim/)  {
                    $line =~ s/\\endverbatim/\]\]><\/ac:plain-text-body><\/ac:structured-macro>/g;                    
                } elsif($line =~ /\\code/)  {    
                    $line =~ s/\\code/<ac:structured-macro ac:name="code"><ac:plain-text-body><!\[CDATA\[/g;
                } elsif($line =~ /\\endcode/)  {    
                    $line =~ s/\\endcode/\]\]><\/ac:plain-text-body><\/ac:structured-macro>/g;
                } elsif($line =~ /^\s*- /) {
                    $line =~ s/^\s*- /<li>/;
                    
                    if($inList ==  1) {
                        $line="</li>".$line;
                    } else {
                         $line="<ul>".$line;
                    }     
                         
                    $inList=1;
                }
                
                if($line =~ /^\s*\.\s*/ and $inList==1) {
                        $line="</li></ul>";
                        $inList=0;
                }       
                
                $prevLine=$line;
                
                $blockMod=$blockMod.$line."\n";    
            }        
          
            $str=$str."<li><p>".$blockMod."</p></li><br />";
        }

    }

    $str=$str."</ol>";
    return $str;
}