#!/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 $rootPage="Command line tools";
my $unique;
#---------------------------------
# Read arguments
#---------------------------------
GetOptions("unique=s" => \$unique)
or die("Error in command line arguments\n");
#----------------------------------
# Dirs
#----------------------------------
#The tools executales and shell scripts
my $toolsSrcDir="../tools";
my $toolsBinDir="/var/tmp/cgr/build/eccodes/develop/bin"; #"../tools";/var/tmp/cgr/build/eccodes/develop
#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
}
#---------------------------------------------------
# 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);
}
}
}
#===========================================================
#===========================================================
#
# 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 = ";
$str=$str."
";
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) = @_;
return $exName;
}
#-----------------------------------------------------------
# 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;
if( -e $exe) {
my $cmd="cd ".$toolsBinDir.";./".$script." > ".$fTmp;
print "\tGenerate examples text with command:\n\t\t".$cmd."\n";
system($cmd);
return $fTmp;
}
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 = ";
foreach my $xm (@xmp) {
$str=$str."Name Description ";
}
$str=$str."".confUtils::linkToPage($xm,toolsPageTitle($xm))." ".getDescription($xm)."
"; $tbody=1; } } elsif( $line =~ /^\s*-\w /) { if($tbody == 1) { $str=$str."
"; $tbody=0; } my @v=split(/\t|\s{2,}/,$line); if($#v+1 > 0) { $str=$str.""; 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."
"; $tbody=0; } } } close IN; if($tbody == 1) { $str=$str.""; } 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=".$blockMod."