makenewsim.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/perl
  2. # author: Frank Michler
  3. # since: 21.12.2008
  4. # purpose: generate new simulation project, use minimal as template
  5. # usage: makenewsim.pl [subdirname/]newsimname#
  6. #
  7. # history:
  8. # 23.12.2008
  9. # works for new simulations in base dir ($HOME/prog/objsim)
  10. ###########################################################
  11. # todo: expand for new simulations in subdir of base dir
  12. #
  13. # 03.05.2009
  14. # todo: expand for different simulation template
  15. #
  16. # 18.05.2010
  17. # add a --NoAct option
  18. # command line parameter handling
  19. %ArgHash;
  20. # set default values
  21. $ArgHash{"TemplateSim"}="minimal";
  22. #$ArgHash{"NewSimName"}="newsim";
  23. # command line argument structure: --key=value
  24. foreach $ArgNum (0 .. $#ARGV) {
  25. if ($ARGV[$ArgNum] =~/--([a-zA-Z0-9]+)=(.+)/) {
  26. $ArgHash{$1}=$2;
  27. }
  28. }
  29. if (!exists($ArgHash{"NewSimName"})) {
  30. usage();
  31. die "read the fucking manual\n";
  32. }
  33. $TemplateSim=$ArgHash{"TemplateSim"};
  34. $NewSimDir=$ArgHash{"NewSimName"};
  35. print "TemplateSim= $TemplateSim\n";
  36. print "NewSimDir = $NewSimName\n";
  37. print "HOME=$ENV{'HOME'}\n";
  38. $BaseDirName="$ENV{'HOME'}/prog/objsim/";
  39. if ($NewSimDir=~/(.*\/)([a-zA-Z0-9]+)$/) {
  40. # new simulation in subdir
  41. $NewSimName=$2;
  42. $NewSimMetaDir=$1;
  43. } else {
  44. $NewSimName=$NewSimDir;
  45. $NewSimMetaDir='';
  46. }
  47. $ParentJamfilePath=$BaseDirName.$NewSimMetaDir.'Jamfile';
  48. print "creating new simulation.\nSimDir=$NewSimMetaDir \n";
  49. print "NewSimName=$NewSimName\n";
  50. print "BaseDirName=$BaseDirName \n";
  51. $DefaultDataDirString='$HOME'."/data/sim/csim/$NewSimMetaDir$NewSimName";
  52. $DefaultDataDir=$ENV{'HOME'}."/data/sim/csim/$NewSimMetaDir$NewSimName";
  53. # check if simulation name already exists in parent directory Jamfile
  54. my $SimExists=system("grep 'SubInclude .* $NewSimName".'\s*$'."' $ParentJamfilePath");
  55. print "ExistsResult=$SimExists \n";
  56. if ($SimExists==0) {
  57. die "exiting script because Simulation name $NewSimName already exists in Jamfile\n";
  58. }
  59. # check existence of directory
  60. if (-d "$BaseDirName/$NewSimDir" ) {
  61. die "exit script because directory $BaseDirName/$NewSimDir already exists\n";
  62. }
  63. $Source="$BaseDirName/$TemplateSim";
  64. $Target="$BaseDirName/$NewSimDir";
  65. # create directory
  66. print "Create directory: $BaseDirName/$NewSimDir\n";
  67. system("mkdir -p $BaseDirName/$NewSimDir \n");
  68. # copy sim files
  69. %copyfiles=(
  70. 'Jamfile'=>'Jamfile'
  71. ,"$TemplateSim.cpp"=>"$NewSimName.cpp"
  72. , 'startsim'=>'startsim'
  73. , "simcontrol_$TemplateSim.tcl"=>"simcontrol_$NewSimName.tcl"
  74. , "simeval_$TemplateSim.tcl"=>"simeval_$NewSimName.tcl"
  75. , "settings_$TemplateSim.cfg"=>"settings_$NewSimName.cfg");
  76. foreach $file (keys %copyfiles) {
  77. print "filename= $file -> $copyfiles{$file} \n";
  78. system("cp $Source/$file $Target/$copyfiles{$file}");
  79. }
  80. # modify files to change sim name
  81. my @RegExpArr=("s/(SimName\\(\")$TemplateSim(\"\\))/\$1$NewSimName\$2/");
  82. ReplaceInFile("$Target/$NewSimName.cpp",\@RegExpArr);
  83. my @RegExpArr=('s/(set SimulationName )'.$TemplateSim.'/$1'.$NewSimName.'/'
  84. ,'s/(SetSimDirAndFileNames )'.$TemplateSim.'/$1'.$NewSimName.'/');
  85. ReplaceInFile("$Target/simcontrol_$NewSimName.tcl",\@RegExpArr);
  86. my @RegExpArr=('s/(NAME=)'.$TemplateSim.'/$1'.$NewSimName.'/');
  87. ReplaceInFile("$Target/startsim",\@RegExpArr);
  88. my @RegExpArr=('s/(DataDirectory : ).*/$1'.MaskString($DefaultDataDirString).'/');
  89. ReplaceInFile("$Target/settings_$NewSimName.cfg",\@RegExpArr);
  90. # create default data directory
  91. system ("mkdir -p $DefaultDataDir");
  92. my @RegExpArr=('s/(SimName = )'.$TemplateSim.'/$1'.$NewSimName.'/');
  93. ReplaceInFile("$Target/Jamfile",\@RegExpArr);
  94. # find subdir path in Jamfile
  95. open (FILE, "< $ParentJamfilePath") or die "kann $ParentJamfilePath nicht öffnen\n";
  96. my $ParentDirJam = "";
  97. while (<FILE>) {
  98. if (/^\s*SubDir\s+(.*);/) {
  99. # print "SubDir is $1\n";
  100. $ParentDirJam=$1;
  101. last;
  102. }
  103. }
  104. close (FILE);
  105. # add simulation to Jamfiles
  106. open (JAMFILE, ">> $ParentJamfilePath");
  107. print JAMFILE "\nSubInclude $ParentDirJam $NewSimName ;\n";
  108. close JAMFILE;
  109. my @RegExpArr=('s/(SubDir ).*(\$\(SimName\))/$1'.$ParentDirJam.'$2/');
  110. ReplaceInFile("$Target/Jamfile",\@RegExpArr);
  111. ##############################
  112. sub usage {
  113. print "\n usage:\n";
  114. print "makenewsim.pl --NewSimName=<newsimname> --TemplateSim=<templatename>\n\n";
  115. }
  116. sub ReadFile {
  117. my $File=shift;
  118. my @StringArray;
  119. while(<$File>) {
  120. $StringArray[$#StringArray+1]=$_;
  121. }
  122. return @StringArray;
  123. }
  124. #****************************
  125. sub ReplaceInFile {
  126. my $filename=shift;
  127. # print "Replace in File: $filename \n";
  128. my $RegExpArrRef=shift;
  129. # print "RegExpArrRef= $RegExpArrRef\n";
  130. my @RegExpArr=@$RegExpArrRef;
  131. foreach my $r (@RegExpArr) {
  132. print "RexExp=$r \n";
  133. }
  134. open (FILE, "< $filename")
  135. or die "kann Datei $filename nicht zum lesen öffnen \n";
  136. my @tmp=ReadFile(FILE);
  137. my @out;
  138. foreach (@tmp) {
  139. # print "oldline: $_";
  140. foreach my $RegExp (@RegExpArr) {
  141. # print "regexp=$RegExp\n";
  142. eval($RegExp);
  143. # print "newline: $_";
  144. }
  145. @out[$#out+1]=$_;
  146. }
  147. close(FILE);
  148. # write modified file
  149. open(FILE, "> $filename")
  150. or die "kann Datei $filename nicht zum schreiben öffnen \n";
  151. print FILE @out;
  152. close(FILE);
  153. }
  154. sub MaskString {
  155. my $string=shift;
  156. $string=~s/\//\\\//g;
  157. $string=~s/\$/\\\$/g;
  158. return $string;
  159. }