#!/usr/bin/perl # author: Frank Michler # since: 21.12.2008 # purpose: generate new simulation project, use minimal as template # usage: makenewsim.pl [subdirname/]newsimname# # # history: # 23.12.2008 # works for new simulations in base dir ($HOME/prog/objsim) ########################################################### # todo: expand for new simulations in subdir of base dir # # 03.05.2009 # todo: expand for different simulation template # # 18.05.2010 # add a --NoAct option # command line parameter handling %ArgHash; # set default values $ArgHash{"TemplateSim"}="minimal"; #$ArgHash{"NewSimName"}="newsim"; # command line argument structure: --key=value foreach $ArgNum (0 .. $#ARGV) { if ($ARGV[$ArgNum] =~/--([a-zA-Z0-9]+)=(.+)/) { $ArgHash{$1}=$2; } } if (!exists($ArgHash{"NewSimName"})) { usage(); die "read the fucking manual\n"; } $TemplateSim=$ArgHash{"TemplateSim"}; $NewSimDir=$ArgHash{"NewSimName"}; print "TemplateSim= $TemplateSim\n"; print "NewSimDir = $NewSimName\n"; print "HOME=$ENV{'HOME'}\n"; $BaseDirName="$ENV{'HOME'}/prog/objsim/"; if ($NewSimDir=~/(.*\/)([a-zA-Z0-9]+)$/) { # new simulation in subdir $NewSimName=$2; $NewSimMetaDir=$1; } else { $NewSimName=$NewSimDir; $NewSimMetaDir=''; } $ParentJamfilePath=$BaseDirName.$NewSimMetaDir.'Jamfile'; print "creating new simulation.\nSimDir=$NewSimMetaDir \n"; print "NewSimName=$NewSimName\n"; print "BaseDirName=$BaseDirName \n"; $DefaultDataDirString='$HOME'."/data/sim/csim/$NewSimMetaDir$NewSimName"; $DefaultDataDir=$ENV{'HOME'}."/data/sim/csim/$NewSimMetaDir$NewSimName"; # check if simulation name already exists in parent directory Jamfile my $SimExists=system("grep 'SubInclude .* $NewSimName".'\s*$'."' $ParentJamfilePath"); print "ExistsResult=$SimExists \n"; if ($SimExists==0) { die "exiting script because Simulation name $NewSimName already exists in Jamfile\n"; } # check existence of directory if (-d "$BaseDirName/$NewSimDir" ) { die "exit script because directory $BaseDirName/$NewSimDir already exists\n"; } $Source="$BaseDirName/$TemplateSim"; $Target="$BaseDirName/$NewSimDir"; # create directory print "Create directory: $BaseDirName/$NewSimDir\n"; system("mkdir -p $BaseDirName/$NewSimDir \n"); # copy sim files %copyfiles=( 'Jamfile'=>'Jamfile' ,"$TemplateSim.cpp"=>"$NewSimName.cpp" , 'startsim'=>'startsim' , "simcontrol_$TemplateSim.tcl"=>"simcontrol_$NewSimName.tcl" , "simeval_$TemplateSim.tcl"=>"simeval_$NewSimName.tcl" , "settings_$TemplateSim.cfg"=>"settings_$NewSimName.cfg"); foreach $file (keys %copyfiles) { print "filename= $file -> $copyfiles{$file} \n"; system("cp $Source/$file $Target/$copyfiles{$file}"); } # modify files to change sim name my @RegExpArr=("s/(SimName\\(\")$TemplateSim(\"\\))/\$1$NewSimName\$2/"); ReplaceInFile("$Target/$NewSimName.cpp",\@RegExpArr); my @RegExpArr=('s/(set SimulationName )'.$TemplateSim.'/$1'.$NewSimName.'/' ,'s/(SetSimDirAndFileNames )'.$TemplateSim.'/$1'.$NewSimName.'/'); ReplaceInFile("$Target/simcontrol_$NewSimName.tcl",\@RegExpArr); my @RegExpArr=('s/(NAME=)'.$TemplateSim.'/$1'.$NewSimName.'/'); ReplaceInFile("$Target/startsim",\@RegExpArr); my @RegExpArr=('s/(DataDirectory : ).*/$1'.MaskString($DefaultDataDirString).'/'); ReplaceInFile("$Target/settings_$NewSimName.cfg",\@RegExpArr); # create default data directory system ("mkdir -p $DefaultDataDir"); my @RegExpArr=('s/(SimName = )'.$TemplateSim.'/$1'.$NewSimName.'/'); ReplaceInFile("$Target/Jamfile",\@RegExpArr); # find subdir path in Jamfile open (FILE, "< $ParentJamfilePath") or die "kann $ParentJamfilePath nicht öffnen\n"; my $ParentDirJam = ""; while () { if (/^\s*SubDir\s+(.*);/) { # print "SubDir is $1\n"; $ParentDirJam=$1; last; } } close (FILE); # add simulation to Jamfiles open (JAMFILE, ">> $ParentJamfilePath"); print JAMFILE "\nSubInclude $ParentDirJam $NewSimName ;\n"; close JAMFILE; my @RegExpArr=('s/(SubDir ).*(\$\(SimName\))/$1'.$ParentDirJam.'$2/'); ReplaceInFile("$Target/Jamfile",\@RegExpArr); ############################## sub usage { print "\n usage:\n"; print "makenewsim.pl --NewSimName= --TemplateSim=\n\n"; } sub ReadFile { my $File=shift; my @StringArray; while(<$File>) { $StringArray[$#StringArray+1]=$_; } return @StringArray; } #**************************** sub ReplaceInFile { my $filename=shift; # print "Replace in File: $filename \n"; my $RegExpArrRef=shift; # print "RegExpArrRef= $RegExpArrRef\n"; my @RegExpArr=@$RegExpArrRef; foreach my $r (@RegExpArr) { print "RexExp=$r \n"; } open (FILE, "< $filename") or die "kann Datei $filename nicht zum lesen öffnen \n"; my @tmp=ReadFile(FILE); my @out; foreach (@tmp) { # print "oldline: $_"; foreach my $RegExp (@RegExpArr) { # print "regexp=$RegExp\n"; eval($RegExp); # print "newline: $_"; } @out[$#out+1]=$_; } close(FILE); # write modified file open(FILE, "> $filename") or die "kann Datei $filename nicht zum schreiben öffnen \n"; print FILE @out; close(FILE); } sub MaskString { my $string=shift; $string=~s/\//\\\//g; $string=~s/\$/\\\$/g; return $string; }