« lib/App/SeismicUnixGui/developer/code/sunix/README.pm
↑ Summary
lib/App/SeismicUnixGui/developer/code/sunix/replacelines.pl »
| 1 | |||
| 2 | =head2 SYNOPSIS | ||
| 3 | |||
| 4 | PERL PROGRAM NAME: convert2V07 | ||
| 5 | |||
| 6 | AUTHOR: | ||
| 7 | |||
| 8 | DATE: Jun 15 2022 | ||
| 9 | |||
| 10 | DESCRIPTION: Convert perl gui scripts | ||
| 11 | by including full path to modules | ||
| 12 | Version: 0.1 | ||
| 13 | V 0.2 August 4, 2022 | ||
| 14 | V 0.3 Dec. 2022 | ||
| 15 | |||
| 16 | =head2 USE | ||
| 17 | |||
| 18 | =head3 NOTES | ||
| 19 | |||
| 20 | Before conversion: use L_SU_global_constants: | ||
| 21 | After conversion: use App:SeismicUnixGui::misc::SeismicUnixGui::misc::L_SU_global_constants; | ||
| 22 | |||
| 23 | V0.2 adapted to use App:SeismicUnixGui::misc::SeismicUnixGui | ||
| 24 | first, change use module; to use aliased 'App::SeismicUnixGui::path::to::module | ||
| 25 | next,change new module(); to module->new(); | ||
| 26 | |||
| 27 | V 0.3 use search_directories.pm instead of L_SU_global_constants.pm | ||
| 28 | |||
| 29 | =head4 Examples | ||
| 30 | |||
| 31 | =head2 SYNOPSIS | ||
| 32 | |||
| 33 | changed files: | ||
| 34 | |||
| 35 | configs: | ||
| 36 | |||
| 37 | |||
| 38 | correct oop_text | ||
| 39 | oop_run_flows | ||
| 40 | |||
| 41 | |||
| 42 | =head2 CHANGES and their DATES | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | use Moose; | ||
| 47 | our $VERSION = '0.0.1'; | ||
| 48 | |||
| 49 | use aliased 'App::SeismicUnixGui::misc::manage_files_by2'; | ||
| 50 | use aliased 'App::SeismicUnixGui::misc::L_SU_global_constants'; | ||
| 51 | use aliased 'App::SeismicUnixGui::misc::dirs'; | ||
| 52 | use Carp; | ||
| 53 | |||
| 54 | my $L_SU_global_constants = L_SU_global_constants->new(); | ||
| 55 | my $manage_files_by2 = manage_files_by2->new(); | ||
| 56 | my $dirs = dirs->new(); | ||
| 57 | |||
| 58 | =head2 Define | ||
| 59 | |||
| 60 | local variables | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | my ( @INBOUND, @SUBDIR ); | ||
| 65 | my @directories; | ||
| 66 | my @line; | ||
| 67 | my @path4gui; | ||
| 68 | my @file4gui; | ||
| 69 | my @path4su; | ||
| 70 | my @file4su; | ||
| 71 | my @path4gen; | ||
| 72 | my @file4gen; | ||
| 73 | |||
| 74 | my $line2find_use = '\s*use\s'; | ||
| 75 | my $line2find_new = '\s=\snew\s'; | ||
| 76 | my $line2find_App = 'use\ App::'; | ||
| 77 | my $line2find_SeismicUnix = 'SeismicUnix\ qw'; | ||
| 78 | |||
| 79 | my $ans = 'n'; | ||
| 80 | |||
| 81 | print("Enter file name to convert\n"); | ||
| 82 | my $file_name = <>; | ||
| 83 | chomp $file_name; | ||
| 84 | |||
| 85 | print("You entered $file_name. Correct? y/n\n"); | ||
| 86 | $ans = <>; | ||
| 87 | chomp $ans; | ||
| 88 | |||
| 89 | while ( ( $ans eq 'N' ) or ( $ans eq 'n' ) ) { | ||
| 90 | |||
| 91 | print("Enter file name again\n"); | ||
| 92 | $file_name = <>; | ||
| 93 | chomp $file_name; | ||
| 94 | |||
| 95 | print("You entered $file_name. Correct? y/n\n"); | ||
| 96 | $ans = <>; | ||
| 97 | chomp $ans; | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | print "OK, correct file name is $file_name\n"; | ||
| 102 | |||
| 103 | #my $file_name = 'test_file.pl'; | ||
| 104 | #print("parent_directory_gui_number_of=$parent_directory_gui_number_of\n"); | ||
| 105 | #print("child_directory_gui_number_of=$child_directory_gui_number_of\n"); | ||
| 106 | |||
| 107 | =head2 private shared hash | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | my $convert2V07 = { | ||
| 112 | |||
| 113 | _file_name => '', | ||
| 114 | |||
| 115 | }; | ||
| 116 | |||
| 117 | =head2 CASE # 1A 1B | ||
| 118 | |||
| 119 | when only use module; | ||
| 120 | is present | ||
| 121 | |||
| 122 | use SeismicUnix qw () | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | $manage_files_by2->set_file_in($file_name); | ||
| 127 | $manage_files_by2->set_directory('./'); | ||
| 128 | my $slurp_ref = manage_files_by2->get_whole($file_name); | ||
| 129 | |||
| 130 | my @slurp = @$slurp_ref; | ||
| 131 | my $length_of_slurp = scalar @slurp; | ||
| 132 | |||
| 133 | for ( my $j = 0 ; $j < $length_of_slurp ; $j++ ) { | ||
| 134 | |||
| 135 | my $raw_string = $slurp[$j]; | ||
| 136 | chomp $raw_string; # remove all newlines | ||
| 137 | my @temp_string; | ||
| 138 | |||
| 139 | if ( $raw_string =~ m/$line2find_use/ ) { | ||
| 140 | |||
| 141 | my $module_name; | ||
| 142 | my $string = $raw_string; | ||
| 143 | |||
| 144 | $string =~ s/;//; | ||
| 145 | $string =~ s/\(\)//; | ||
| 146 | @temp_string = split( /\s+/, $string ); | ||
| 147 | my $line = $j + 1; | ||
| 148 | |||
| 149 | if ( $temp_string[1] eq 'use' ) { | ||
| 150 | |||
| 151 | $module_name = $temp_string[2]; | ||
| 152 | |||
| 153 | print( | ||
| 154 | "Cases 1A or 1B for:use, convert2V07, module name with $module_name...\n" | ||
| 155 | ); | ||
| 156 | |||
| 157 | if ( $module_name ne 'Moose' | ||
| 158 | and $module_name ne 'SeismicUnix' | ||
| 159 | and $module_name ne 'aliased' | ||
| 160 | and length $module_name | ||
| 161 | and $module_name ne '' | ||
| 162 | and $module_name ne 'null' ) | ||
| 163 | { | ||
| 164 | |||
| 165 | # CASE 1B looking for use model | ||
| 166 | # print("line to substitute=$slurp[$j]\n"); | ||
| 167 | my $var = $L_SU_global_constants->var(); | ||
| 168 | my $separation = $var->{_SeismicUnixGui}; | ||
| 169 | |||
| 170 | my $module_name_pm = $module_name . '.pm'; | ||
| 171 | |||
| 172 | # print("module name_pm=$module_name_pm\n"); | ||
| 173 | |||
| 174 | $dirs->set_file_name($module_name_pm); | ||
| 175 | my $path = $dirs->get_path4convert_file(); | ||
| 176 | |||
| 177 | if ( length $path ) { | ||
| 178 | |||
| 179 | my $pathNmodule_pm = $path . '/' . $module_name_pm; | ||
| 180 | my @next_string = split( $separation, $pathNmodule_pm ); | ||
| 181 | |||
| 182 | # print("Case 1A: convert2V07, 'b4:' . $next_string[0]\n"); | ||
| 183 | # print("'After:' . $next_string[1]\n"); | ||
| 184 | # print("segment3: $next_string[2]\n"); | ||
| 185 | |||
| 186 | # substitute "/" with ":" | ||
| 187 | $next_string[2] =~ s/(\/)+/::/g; | ||
| 188 | $next_string[2] =~ s/.pm//g; | ||
| 189 | $next_string[2] = "\t" | ||
| 190 | . 'use aliased \'App::' | ||
| 191 | . $var->{_SeismicUnixGui} | ||
| 192 | . $next_string[2] . '\';'; | ||
| 193 | |||
| 194 | # warn 'After...' . $next_string[1]; | ||
| 195 | $raw_string = $next_string[2]; | ||
| 196 | $slurp[$j] = $raw_string; | ||
| 197 | |||
| 198 | print("substituted line=$slurp[$j]\n"); | ||
| 199 | |||
| 200 | } | ||
| 201 | else { | ||
| 202 | warn 'Warning: variable missing'; | ||
| 203 | print 'path=' . $path . "\n"; | ||
| 204 | print("module name_pm=$module_name_pm\n\n"); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | elsif ( $module_name eq 'SeismicUnix' | ||
| 208 | and $module_name ne 'Moose' | ||
| 209 | and length $module_name | ||
| 210 | and $module_name ne '' | ||
| 211 | and $module_name ne 'null' ) | ||
| 212 | { | ||
| 213 | # CASE 1B looking for use SeismicUnix qw ( ...) | ||
| 214 | # When bad module names are avoided | ||
| 215 | # print("line to substitute=$slurp[$j]\n"); | ||
| 216 | my $separation_qw = 'qw'; | ||
| 217 | my @for_variables_only = split( $separation_qw, $slurp[$j] ); | ||
| 218 | # print( | ||
| 219 | #"Case 1B, convert2V07,for_variables_only 'b4:'$for_variables_only[0]\n" | ||
| 220 | # ); | ||
| 221 | # print( | ||
| 222 | #"Case 1B, convert2V07,for_variables_only 'After4:'$for_variables_only[1]\n" | ||
| 223 | # ); | ||
| 224 | |||
| 225 | my $var = $L_SU_global_constants->var(); | ||
| 226 | my $separation = $var->{_SeismicUnixGui}; | ||
| 227 | |||
| 228 | my $module_name_pm = $module_name . '.pm'; | ||
| 229 | |||
| 230 | # print("module name_pm=$module_name_pm\n"); | ||
| 231 | |||
| 232 | $dirs->set_file_name($module_name_pm); | ||
| 233 | my $slash_path = | ||
| 234 | $dirs->get_path4convert_file(); | ||
| 235 | |||
| 236 | if ( length $slash_path ) { | ||
| 237 | |||
| 238 | my $slash_pathNmodule_pm = | ||
| 239 | $slash_path . '/' . $module_name_pm; | ||
| 240 | my @next_string = | ||
| 241 | split( $separation, $slash_pathNmodule_pm ); | ||
| 242 | |||
| 243 | # print("Case 1B, convert2V07, 'b4:' . $next_string[0]\n"); | ||
| 244 | # print("'After:' . $next_string[1]\n"); | ||
| 245 | # print("next_string[3]: $next_string[2]\n"); | ||
| 246 | |||
| 247 | # substitute "/" with ":" | ||
| 248 | $next_string[2] =~ s/(\/)+/::/g; | ||
| 249 | $next_string[2] =~ s/.pm//g; | ||
| 250 | $next_string[2] = 'use App::' | ||
| 251 | . $var->{_SeismicUnixGui} | ||
| 252 | . $next_string[2] . ' qw' | ||
| 253 | . $for_variables_only[1]; | ||
| 254 | |||
| 255 | # warn 'After...' . $next_string[1]; | ||
| 256 | $raw_string = $next_string[2]; | ||
| 257 | $slurp[$j] = $raw_string; | ||
| 258 | |||
| 259 | print("substituted line=$slurp[$j]\n"); | ||
| 260 | |||
| 261 | } | ||
| 262 | else { | ||
| 263 | warn 'Warning: variable missing'; | ||
| 264 | print 'slash_path=' . $slash_path . "\n"; | ||
| 265 | print("module name_pm=$module_name_pm\n\n"); | ||
| 266 | } | ||
| 267 | |||
| 268 | } | ||
| 269 | else { | ||
| 270 | print("convert2V07, bad module: $module_name avoided\n"); | ||
| 271 | } | ||
| 272 | |||
| 273 | } | ||
| 274 | elsif ( $temp_string[0] eq 'use' ) { | ||
| 275 | |||
| 276 | # Catches cases of strange modules | ||
| 277 | |||
| 278 | # $module_name = $temp_string[1]; | ||
| 279 | # print("module name within $file_name=$module_name...\n"); | ||
| 280 | print("use is in unexpected location WARNING...\n"); | ||
| 281 | |||
| 282 | } | ||
| 283 | else { | ||
| 284 | # CATCH the UNUSUAL | ||
| 285 | my $string_number_of = scalar @temp_string; | ||
| 286 | print("convert2V07, unexpected module name\n"); | ||
| 287 | print("\nconvert2V07, string=$string \n"); | ||
| 288 | print("convert2V07, string_number_of=$string_number_of\n"); | ||
| 289 | print("convert2V07, temp_string=@temp_string \n"); | ||
| 290 | print("convert2V07, temp_string[0]=$temp_string[0] \n"); | ||
| 291 | print("convert2V07, temp_string[1]=$temp_string[1] \n"); | ||
| 292 | $module_name = 'null'; | ||
| 293 | } | ||
| 294 | |||
| 295 | } | ||
| 296 | else { # for each line containing "use" | ||
| 297 | # print("convert2V07, skip line\n"); | ||
| 298 | } | ||
| 299 | |||
| 300 | } # for each line in a slurped file | ||
| 301 | |||
| 302 | =head2 Write out | ||
| 303 | |||
| 304 | the corrected or uncorrected file | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | my $outbound = $file_name; | ||
| 309 | |||
| 310 | print("Case 1 for 'use' only, writing to $outbound\n"); | ||
| 311 | #print("Case 1 number of lines in output file = $length_of_slurp\n"); | ||
| 312 | |||
| 313 | if ( $length_of_slurp == 0 ) { | ||
| 314 | |||
| 315 | print("convert2V07, unexpected empty file\n"); | ||
| 316 | print("Hit Enter to continue\n"); | ||
| 317 | <STDIN>; | ||
| 318 | |||
| 319 | } | ||
| 320 | elsif ( $length_of_slurp > 0 ) { | ||
| 321 | |||
| 322 | # print "Press Writing a new file with a changed line"; | ||
| 323 | # <STDIN>; | ||
| 324 | |||
| 325 | open( OUT, ">$outbound" ) | ||
| 326 | or die("File $file_name not found"); | ||
| 327 | |||
| 328 | # add \n!!!! | ||
| 329 | for ( my $i = 0 ; $i < $length_of_slurp ; $i++ ) { | ||
| 330 | |||
| 331 | print OUT $slurp[$i] . "\n"; | ||
| 332 | |||
| 333 | # print $slurp[$i] . "\n"; | ||
| 334 | } | ||
| 335 | |||
| 336 | close(OUT); | ||
| 337 | } | ||
| 338 | |||
| 339 | =head2 CASE #2 | ||
| 340 | |||
| 341 | re-arrange new | ||
| 342 | |||
| 343 | =cut | ||
| 344 | |||
| 345 | $manage_files_by2->set_file_in($file_name); | ||
| 346 | $manage_files_by2->set_directory('./'); | ||
| 347 | $slurp_ref = manage_files_by2->get_whole($file_name); | ||
| 348 | |||
| 349 | @slurp = @$slurp_ref; | ||
| 350 | $length_of_slurp = scalar @slurp; | ||
| 351 | |||
| 352 | for ( my $j = 0 ; $j < $length_of_slurp ; $j++ ) { | ||
| 353 | |||
| 354 | my $raw_string = $slurp[$j]; | ||
| 355 | chomp $raw_string; # remove all newlines | ||
| 356 | my @temp_string; | ||
| 357 | |||
| 358 | if ( $raw_string =~ m/$line2find_new/ ) { | ||
| 359 | |||
| 360 | my $module_name; | ||
| 361 | my $string = $raw_string; | ||
| 362 | # print("Case 2, B4:$raw_string\n"); | ||
| 363 | $string =~ s/\(\);//; | ||
| 364 | @temp_string = split( 'new ' , $string ); | ||
| 365 | # print ("temp string is $temp_string[0]\n"); | ||
| 366 | # print ("temp string is $temp_string[1]\n"); | ||
| 367 | # my $line = $j + 1; | ||
| 368 | my $new_line = $temp_string[0] . $temp_string[1] . '->new();'; | ||
| 369 | # print("After:$new_line\n"); | ||
| 370 | $slurp[$j] = $new_line; | ||
| 371 | |||
| 372 | } | ||
| 373 | else { # for each line containing "new" | ||
| 374 | # print print("convert2V07, skip line\n"); | ||
| 375 | } | ||
| 376 | |||
| 377 | } # for each line in a slurped file | ||
| 378 | |||
| 379 | # write out the corrected or uncorrected file | ||
| 380 | |||
| 381 | $outbound = $file_name; | ||
| 382 | |||
| 383 | print("Case 2 for 'new'; writing to $outbound\n"); | ||
| 384 | #print("number of lines in output file = $length_of_slurp\n"); | ||
| 385 | |||
| 386 | if ( $length_of_slurp == 0 ) { | ||
| 387 | |||
| 388 | print("convert2V07, unexpected empty file\n"); | ||
| 389 | print("Hit Enter to continue\n"); | ||
| 390 | <STDIN>; | ||
| 391 | |||
| 392 | } | ||
| 393 | elsif ( $length_of_slurp > 0 ) { | ||
| 394 | |||
| 395 | # print "Press Writing a new file with a changed line\n"; | ||
| 396 | # <STDIN>; | ||
| 397 | |||
| 398 | open( OUT, ">$outbound" ) | ||
| 399 | or die("File $file_name not found"); | ||
| 400 | |||
| 401 | # add \n!!!! | ||
| 402 | for ( my $i = 0 ; $i < $length_of_slurp ; $i++ ) { | ||
| 403 | |||
| 404 | print OUT $slurp[$i] . "\n"; | ||
| 405 | } | ||
| 406 | |||
| 407 | close(OUT); | ||
| 408 | } | ||
| 409 | |||
| 410 | =head3 CASE#3 | ||
| 411 | |||
| 412 | convert use App:: to | ||
| 413 | use aliased 'App:: | ||
| 414 | |||
| 415 | except for the case of SeismicUnix | ||
| 416 | |||
| 417 | =cut | ||
| 418 | |||
| 419 | $manage_files_by2->set_file_in($file_name); | ||
| 420 | $manage_files_by2->set_directory('./'); | ||
| 421 | $slurp_ref = manage_files_by2->get_whole($file_name); | ||
| 422 | |||
| 423 | @slurp = @$slurp_ref; | ||
| 424 | $length_of_slurp = scalar @slurp; | ||
| 425 | |||
| 426 | for ( my $j = 0 ; $j < $length_of_slurp ; $j++ ) { | ||
| 427 | |||
| 428 | my $raw_string = $slurp[$j]; | ||
| 429 | chomp $raw_string; # remove all newlines | ||
| 430 | my @temp_string; | ||
| 431 | |||
| 432 | if ( $raw_string =~ m/$line2find_App/ | ||
| 433 | and $raw_string !~ m/SeismicUnix\ qw/ ) | ||
| 434 | { | ||
| 435 | |||
| 436 | my $module_name; | ||
| 437 | my $string = $raw_string; | ||
| 438 | # print("Case3, B4:$raw_string\n"); | ||
| 439 | $string =~ s/use\ App/use\ aliased\ 'App/; | ||
| 440 | $string =~ s/;/';/; | ||
| 441 | |||
| 442 | # my $line = $j + 1; | ||
| 443 | my $new_line = $string; | ||
| 444 | # print("After:$new_line\n\n"); | ||
| 445 | $slurp[$j] = $new_line; | ||
| 446 | |||
| 447 | } | ||
| 448 | else { # for each line containing "use" | ||
| 449 | # print("convert2V07, skip line\n"); | ||
| 450 | } | ||
| 451 | |||
| 452 | } # for each line in a slurped file | ||
| 453 | |||
| 454 | # write out the corrected or uncorrected file | ||
| 455 | |||
| 456 | $outbound = $file_name; | ||
| 457 | |||
| 458 | print("Case 3 for 'aliased'; writing to $outbound\n"); | ||
| 459 | #print("number of lines in output file = $length_of_slurp\n"); | ||
| 460 | |||
| 461 | if ( $length_of_slurp == 0 ) { | ||
| 462 | |||
| 463 | print("convert2V07, unexpected empty file\n"); | ||
| 464 | print("Hit Enter to continue\n"); | ||
| 465 | <STDIN>; | ||
| 466 | |||
| 467 | } | ||
| 468 | elsif ( $length_of_slurp > 0 ) { | ||
| 469 | |||
| 470 | # print "Press Writing a new file with a changed line"; | ||
| 471 | # <STDIN>; | ||
| 472 | |||
| 473 | open( OUT, ">$outbound" ) | ||
| 474 | or die("File $file_name not found"); | ||
| 475 | |||
| 476 | # add \n!!!! | ||
| 477 | for ( my $i = 0 ; $i < $length_of_slurp ; $i++ ) { | ||
| 478 | |||
| 479 | print OUT $slurp[$i] . "\n"; | ||
| 480 | } | ||
| 481 | |||
| 482 | close(OUT); | ||
| 483 | } |