Perl script for defs: back up before changing

This commit is contained in:
Shahram Najm 2013-12-19 17:28:43 +00:00
parent 88a665d6a3
commit 5b693edd6b
1 changed files with 35 additions and 35 deletions

70
definitions/create_def.pl Normal file → Executable file
View File

@ -21,15 +21,15 @@ my $dbh = DBI->connect("dbi:mysql(RaiseError=>1):database=$db;host=$host",$user
my $tarfilesflag = 0;
sub create_def {
my $p; my %seen;
my ($key) =@_;
my $field=$key;
my $p; my %seen;
my ($key) =@_;
my $field=$key;
if ($key =~ /paramId/) { $field="param.id"; }
if ($key =~ /name/) { $field="param.name"; }
if ($key =~ /units/) { $field="units.name"; }
if ($key =~ /paramId/) { $field="param.id"; }
if ($key =~ /name/) { $field="param.name"; }
if ($key =~ /units/) { $field="units.name"; }
my $query= <<"EOF";
my $query= <<"EOF";
select $field,force128,edition,
centre.abbreviation,param_id,attribute.name,attribute_value,param.name,param.shortName
from param,grib,attribute,centre,units where param.hide_def=0 and param.id=grib.param_id
@ -37,8 +37,8 @@ sub create_def {
order by edition,centre,param.o,param.id,grib.param_version,attribute.o;
EOF
my $qh=$dbh->prepare($query);
$qh->execute();
my $qh=$dbh->prepare($query);
$qh->execute();
# file containing the list of grib api parameters files we want to tar and
# distribute to users for them to download and update their list of parameter
@ -46,13 +46,13 @@ EOF
open(TAR,$tarfilesflag ? ">>" : ">","tarfiles.txt") or die "Count not open file tarfiles.txt: $!";
$tarfilesflag=1;
while (my ($keyval,$force128,$edition,$centre,$paramId,$attribute,$value,$name,$shortName)=$qh->fetchrow_array )
{
if ($centre eq "all" ) { $conceptDir=""; }
while (my ($keyval,$force128,$edition,$centre,$paramId,$attribute,$value,$name,$shortName)=$qh->fetchrow_array )
{
if ($centre eq "all" ) { $conceptDir=""; }
else { $conceptDir="/localConcepts/$centre"; }
#if ($key =~ /paramId/ && $force128==1 && $keyval >1000) {
# $keyval= $keyval % 1000;
#}
#if ($key =~ /paramId/ && $force128==1 && $keyval >1000) {
# $keyval= $keyval % 1000;
#}
if ($filebase ne "$basedir/grib$edition$conceptDir") {
if ($filebase) {
@ -60,35 +60,35 @@ EOF
close $out;
}
$filebase="$basedir/grib$edition$conceptDir";
mkpath($filebase);
mkpath($filebase);
#copy("$filebase/$key.def","$filebase/$key.def.bkp")
# or die ("unable to copy $filebase/$key.def");
# or die ("unable to copy $filebase/$key.def");
print TAR "grib$edition$conceptDir/$key.def\n";
system("p4 edit $filebase/$key.def");
system("cp -f $filebase/$key.def $filebase/$key.def.orig");
#system("p4 edit $filebase/$key.def");
open($out,"> $filebase/$key.def")
or die "unable to open $filebase/$key.def";
print $out "# Automatically generated by $0 from database $db\@$host, do not edit\n";
$p=();
}
if ($p ne $paramId || exists($seen{$attribute}) ) {
if ($p) { print $out "\t}\n"; }
print $out "#$name\n" ;
print $out "\'".$keyval."\' = {\n" ;
$p=$paramId;
%seen=();
}
$seen{$attribute}=1;
print "($key=$keyval) $edition,$centre,$shortName,$paramId,$name,$attribute,$value\n";
# we need to allow strings in the attribute_value field
# for the moment we apply a patch here
if ($attribute =~ /stepType/ ) {
$value="\"accum\"";
}
print $out "\t $attribute = $value ;\n" ;
}
if ($p ne $paramId || exists($seen{$attribute}) ) {
if ($p) { print $out "\t}\n"; }
print $out "#$name\n" ;
print $out "\'".$keyval."\' = {\n" ;
$p=$paramId;
%seen=();
}
$seen{$attribute}=1;
print "($key=$keyval) $edition,$centre,$shortName,$paramId,$name,$attribute,$value\n";
# we need to allow strings in the attribute_value field
# for the moment we apply a patch here
if ($attribute =~ /stepType/ ) {
$value="\"accum\"";
}
print $out "\t $attribute = $value ;\n" ;
}
if ($filebase) {
print $out "}\n";
close $out;