lib/App/SeismicUnixGui/big_streams/RestoreProject.pl untested

filter: Statementstmt 0.0% 0 / 164 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 200 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1  
2 =head1 DOCUMENTATION
3  
4 =head2 SYNOPSIS
5  
6 PROGRAM NAME: RestoreProject
7 AUTHOR: Juan Lorenzo
8  
9 =head2 CHANGES and their DATES
10  
11 DATE: July 2024
12 Version 1.0
13
14  
15 =head2 DESCRIPTION
16  
17 Restore (tar -xzvf) a Project plus
18 its Project.config file
19 and restore the Project configuration
20 file and its associated directory
21 into the .L_SU foldder
22  
23 =head2 USE
24  
25 perl RestoreProject.pl
26  
27 =head2 Steps
28  
29 Tutorial Project
30  
31 1. Apply tar -xzvf to the compressed and tarred Project file in the user's
32 directory.
33  
34 2. Replace original user name (e.g., "gllore") with the current user name
35 in Project_config
36  
37 3. Update Project.config files in
38 .L_SU/configuration/Servilleta and .L_SU/configuration/active
39  
40 4. Replace folder names of original user name (e.g., "gllore") with
41 the current user's login name
42  
43  
44  
45 Another named Project
46  
47 1. Apply tar -xzvf to the compressed and tarred Project file in the user's
48 directory.
49  
50 2. Confirm that the Project.config directory is present. Otherwise output a warning.
51  
52 3. Replace original user name (e.g., "gllore") with the current user name in Project_config
53  
54 4. Replace folder names of original user name (e.g., "gllore") with
55 the current user's login name
56  
57 5. Message that process is complete
58  
59  
60 =head2 NOTES
61  
62 We are using Moose.
63 Moose already declares that you need debuggers turned on
64 so you don't need a line like the following:
65 use warnings;
66  
67 5. replace original user name (e.g., "gllore") with the current user name.
68 =cut
69  
70 use Moose;
71 our $VERSION = '0.1.0';
72  
73 use aliased 'App::SeismicUnixGui::misc::L_SU_local_user_constants';
74 use aliased 'App::SeismicUnixGui::configs::big_streams::RestoreProject_config';
75 use aliased 'App::SeismicUnixGui::misc::config_superflows';
76 use aliased 'App::SeismicUnixGui::misc::control';
77 use aliased 'App::SeismicUnixGui::misc::readfiles';
78 use File::Copy;
79  
80 =head2 Instantiate classes:
81  
82 Create a new versions of the packages
83  
84 =cut
85  
86 my $L_SU_local_user_constants = L_SU_local_user_constants->new();
87 my $RestoreProject_config = RestoreProject_config->new();
88 my $readfiles = readfiles->new();
89 my $config_superflows = config_superflows->new();
90 my $control = control->new();
91  
92 =head2
93  
94 Internal definitions
95
96 =cut
97  
98 my $success_counter = 0;
99 my $current_username = getpwuid($<);
100 my $prog_name = 'Project';
101 my $home_directory;
102 my $untar_options = '-xzvf';
103 my $default_tutorial_name = 'Servilleta';
104  
105 my $tutorial_name = '';
106  
107 my $false = 0;
108 my $true = 1;
109 my $PROJECT_exists = $false;
110  
111 my $HOME = $L_SU_local_user_constants->get_home();
112 my $CONFIGURATION = $L_SU_local_user_constants->get_CONFIGURATION();
113 my $ACTIVE_PROJECT = $L_SU_local_user_constants->get_ACTIVE_PROJECT();
114  
115 =head2 Restoration of Servilleta Tutorial file
116  
117 stdin is empty
118  
119 =cut
120  
121 if ( length( $ARGV[0] )
122 and $ARGV[0] eq $default_tutorial_name )
123 {
124  
125 $tutorial_name = $default_tutorial_name;
126  
127 }
128 else {
129 # NADA
130 # print("NADA RestoreProject.pl\n");
131 }
132  
133 =head2 STEP 1.
134  
135 Does configuration information exist?
136  
137 TODO: get_PROJECT_exists seems always=false ??
138  
139 =cut
140  
141 $PROJECT_exists = $L_SU_local_user_constants->get_PROJECT_exists();
142  
143 =head2 Create an ACTIVE_PROJECT
144  
145 This is the first time user runs SUG
146 and no .LSU directory exists
147  
148 =cut
149  
150 if ( not $PROJECT_exists
151 and $tutorial_name eq $default_tutorial_name )
152 {
153  
154 $L_SU_local_user_constants->makconfig();
155  
156 my $tar_input = $HOME . '/' . $tutorial_name;
157 # print("Confirmation: PROJECT does not exist\n");
158
159 my $old_Project_config_file = $tar_input . '/Project.config';
160 my $new_Project_config_file =
161 $CONFIGURATION . '/' . $tutorial_name . '/Project.config';
162  
163 =head2 Untar a project
164
165 =cut
166  
167 my $perl_instruction = ("cd $HOME; tar $untar_options $tar_input.tz");
168  
169 # print("$perl_instruction\n");
170 system($perl_instruction);
171  
172 =head2
173
174 Replace old username from backed-up Project.config file,
175 (new_project_config_file)
176 with the current user's name
177
178 config_superflows has a special configuration file
179 for program with a name=Project
180
181 =cut
182  
183 my ( $ref_parameter, $ref_value ) =
184 $readfiles->configs($old_Project_config_file);
185  
186 my @value = @$ref_value;
187 my $old_username_from_backup = $value[7];
188  
189 $config_superflows->set_program_name( \$prog_name ); # needed
190  
191 # remove ticks around critical values
192 $control->set_infection( $value[0] );
193 my $home_value = $control->get_ticksBgone();
194  
195 $control->set_infection( $value[1] );
196 my $project_value = $control->get_ticksBgone();
197  
198 $control->set_infection( $value[7] );
199 my $subuser_value = $control->get_ticksBgone();
200  
201 $control->set_infection($old_username_from_backup);
202 $old_username_from_backup = $control->get_ticksBgone();
203  
204 my $dir2find = $old_username_from_backup;
205  
206 my $instruction3;
207  
208 # for updating new_Project_config file
209 $home_value =~ s/$old_username_from_backup/$current_username/;
210 $project_value =~ s/$old_username_from_backup/$current_username/;
211 $subuser_value =~ s/$old_username_from_backup/$current_username/;
212  
213 # print(" home_value = $home_value\n");
214 # print(" project_value = $project_value\n");
215 # print(" subuser_value = $subuser_value\n");
216  
217 $value[0] = $home_value;
218 $value[1] = $project_value;
219 $value[7] = $subuser_value;
220  
221 my $hash_ref = {
222 _names_aref => $ref_parameter,
223 _values_aref => \@value,
224 _prog_name_sref => \$prog_name,
225 };
226  
227 =head2
228  
229 Updates new Project_config file
230 in ACTIVE_PROJECT (no ticks)
231 AND in the Servilleta
232 subdirectory (with ticks)
233
234 =cut
235  
236 $config_superflows->save($hash_ref);
237  
238 =pod
239  
240 Replace the Project_config file
241 within the ACTIVE_PROJECT,
242 because all its parameters have ticks
243
244 To do this, copy newly created Project.config,
245 created above
246 by config_superflows_>save, in Servilleta subdirectory
247 to .LSU/configuration/active/Project.config
248  
249 =cut
250  
251 # my $from1 = $new_Project_config_file; # has the current user name
252 # my $to1 = $ACTIVE_PROJECT;
253 #
254 ## print("Copying $from1 to $to1\n");
255 #
256 # copy( $from1, $to1 );
257  
258 if ( $old_username_from_backup ne $current_username ) {
259  
260 # Replace old folder names with current username
261 my $starting_point = $HOME . '/' . $tutorial_name;
262  
263 # print(" RestoreProject.pl,project_directory=$tutorial_name\n");
264  
265 # print(" Looking for directories within the project to be restored...\n");
266 # print("old_username_from_backup=$old_username_from_backup\n");
267  
268 my @original_list =
269 `(find $starting_point -name $dir2find -type d -print 2>/dev/null)`;
270 my $original_list_length = scalar @original_list;
271 my @new_list = @original_list;
272  
273 # print("list is $original_list_length\n");
274 print("@new_list\n");
275  
276 =pod
277  
278 Change folder names to the new user's
279  
280 =cut
281  
282 # print("current_username = $current_username\n");
283 # print("dir2find= $dir2find\n");
284  
285 for ( my $i = 0 ; $i < $original_list_length ; $i++ ) {
286  
287 my $old_directory = $original_list[$i];
288 my $new_directory = $original_list[$i];
289  
290 # remove ambiguous substitutions by making sure
291 # the directory name found has no extra letters
292 # at the start or the end
293 $new_directory =~
294 s/(?<=\b)(?=$dir2find\b)$dir2find/$current_username/g;
295 chomp $old_directory;
296 $instruction3 = `(mv $old_directory $new_directory 2>/dev/null)`;
297 system($instruction3);
298  
299 # print(" new_directory=$new_directory\n");
300 # print("$instruction3\n ");
301  
302 }
303 }
304  
305 }
306  
307 elsif ( not length $ARGV[0] ) {
308  
309 =head2 STEP 2
310  
311 Running Seismic Unix Gui's RestoreProject Tool option
312 Pre-existing project contains
313 RestoreProject.config file in ~pl/username directory
314  
315 =cut
316  
317 # collect new project name from local RestoreProject.config
318 my ( $CFG_h, $CFG_aref ) = $RestoreProject_config->get_values();
319 my $new_project_dir_name = $CFG_h->{RestoreProject}{1}{directory_name};
320  
321 $control->set_infection($new_project_dir_name);
322 $new_project_dir_name = $control->get_ticksBgone();
323  
324 # print("RestoreProject.pl, new_project_dir_name = $new_project_dir_name \n");
325  
326 my $ACTIVE_PROJECT = $L_SU_local_user_constants->get_ACTIVE_PROJECT();
327 my $tar_input = $HOME . '/' . $new_project_dir_name;
328 my $old_Project_config_file = $tar_input . '/Project.config';
329 my $new_Project_config_file =
330 $CONFIGURATION . '/' . $new_project_dir_name . '/Project.config';
331  
332 # print("RestoreProject.pl, ACTIVE_PROJECT = $ACTIVE_PROJECT\n");
333  
334 =head2 Untar a project
335
336 =cut
337  
338 my $perl_instruction = ("cd $HOME; tar $untar_options $tar_input.tz");
339 system($perl_instruction);
340  
341 =head2 Verify project is a true SUG project
342
343 1. Collect existant project names
344 2. Confirm that SUG configuration system exists
345
346 But, if SUG configuration folders do not exist then
347 this the first time that the user is running
348 SeismicUnixGui,and folders need to be generated.
349
350 =cut
351  
352 my @PROJECT_HOME_aref =
353 $L_SU_local_user_constants->get_PROJECT_HOMES_aref();
354 my @project_pathNname = @{ $PROJECT_HOME_aref[0] };
355 my $length_projects = scalar @project_pathNname;
356  
357 #print(
358 #"RestoreProject.pl,There are $length_projects existant projects in /.L_SU/configuration\n"
359 #);
360  
361 =pod
362
363 check to see that an old project directory does not exist
364 to prevent overwriting an older project.
365
366 =cut
367  
368 $L_SU_local_user_constants->set_PROJECT_name($new_project_dir_name);
369 my $same_project_exists = $L_SU_local_user_constants->get_PROJECT_exists();
370  
371 #print(
372 #"RestoreProject.pl,Does a new Project.config file exist for project $new_project_dir_name? ans=$same_project_exists\n"
373 #);
374  
375 =head2 prepare useful instructions
376  
377 =cut
378  
379 my $instruction1 = 'mkdir -p ' . $ACTIVE_PROJECT;
380 my $instruction2 =
381 'mkdir -p ' . $CONFIGURATION . '/' . $new_project_dir_name;
382  
383 =head2
384  
385 CASE 1: First time that any Project is built for this user
386 in their Home directory
387  
388 If there are no prior SeismicUnixGui configurations, then create
389 the directories
390  
391 =cut
392  
393 if ( $length_projects == 0 ) {
394  
395 system($instruction1);
396 system($instruction2);
397  
398 # print("$instruction1\n");
399 # print("$instruction2\n");
400  
401 # print("First time a SUG Project is created\n");
402  
403 $length_projects = 1;
404 }
405  
406 =head2
407  
408 CASE 2
409 N.B. At least one previous project directory
410 (just created or not) must exist
411 A Project.config file does not exist
412  
413 =cut
414  
415 if ( $length_projects >= 1
416 and not $same_project_exists )
417 {
418  
419 print("This seems like a real SeismicUnixGui project!\n");
420  
421 =head2
422  
423 For all newly restored folders,
424 replace folder names = username with the current username
425  
426 STEPS
427 1. Find old user name, in backed-up Project.config file
428 2. Find the current user's name ( see start of this module)
429 3. Replace the old with current user's name in the Project.config file
430 4. Replace any restored folder names to the current user's name
431 5. Create folder in the configuration directory
432 6. Copy new Project.config file to the configuration directory
433  
434  
435  
436 =pod
437 Make configuration directory
438 Copy the backed-up copy of
439 Project.config into the configuration
440 subfolder
441  
442 =cut
443  
444 system($instruction2);
445  
446 # print("$instruction2\n");
447 my $from = $old_Project_config_file;
448 my $to = $new_Project_config_file;
449 copy( $from, $to );
450  
451 print("RestoreProject.pl, Copying from $from to $to\n");
452  
453 my ( $ref_parameter, $ref_value ) =
454 $readfiles->configs($old_Project_config_file);
455 my @value = @$ref_value;
456 my $old_username_from_backup = $value[7];
457  
458 # print(" RestoreProject.pl,old username=$value[7]\n");
459 # print(" RestoreProject.pl,old old_username_from_backup=$old_username_from_backup\n");
460  
461 =head2
462
463 Replace old username from backup-up Project.config file
464 with the name of the current username
465
466 config_superflows has a special configuration file
467 for program with a name=Project
468
469 =cut
470  
471 $config_superflows->set_program_name( \$prog_name ); # needed
472  
473 $control->set_infection( $value[0] );
474 my $home_value = $control->get_ticksBgone();
475  
476 $control->set_infection( $value[1] );
477 my $project_value = $control->get_ticksBgone();
478  
479 $control->set_infection( $value[7] );
480 my $subuser_value = $control->get_ticksBgone();
481  
482 $control->set_infection($old_username_from_backup);
483 $old_username_from_backup = $control->get_ticksBgone();
484  
485 my $dir2find = $old_username_from_backup;
486  
487 $home_value =~ s/$old_username_from_backup/$current_username/;
488 $project_value =~ s/$old_username_from_backup/$current_username/;
489 $subuser_value =~ s/$old_username_from_backup/$current_username/;
490  
491 # print(" home_value = $home_value\n");
492 # print(" project_value= $project_value\n");
493 # print(" subuser_value = $subuser_value\n");
494  
495 # restore ticks to the strings
496 $control->set_first_name_string($home_value);
497 $home_value = $control->get_w_single_quotes();
498  
499 $control->set_first_name_string($project_value);
500 $project_value = $control->get_w_single_quotes();
501  
502 $control->set_first_name_string($subuser_value);
503 $subuser_value = $subuser_value;
504  
505 $value[0] = $home_value;
506 $value[1] = $project_value;
507 $value[7] = $subuser_value;
508  
509 my $hash_ref = {
510 _names_aref => $ref_parameter,
511 _values_aref => \@value,
512 _prog_name_sref => \$prog_name,
513 };
514  
515 $config_superflows->save($hash_ref);
516  
517  
518 =pod
519  
520 Replace the Project_config file
521 within the ACTIVE_PROJECT,
522 because all its parameters have ticks
523
524 To do this, copy newly created Project.config,
525 created above
526 by config_superflows_>save, in Servilleta subdirectory
527 to .LSU/configuration/active/Project.config
528  
529 =cut
530  
531  
532 # my $from1 = $new_Project_config_file; # has the current user name
533 # my $to1 = $ACTIVE_PROJECT;
534 #
535 ## print("Copying $from1 to $to1\n");
536 #
537 # copy( $from1, $to1 );
538  
539 # avoid extra work
540 if ( $old_username_from_backup ne $current_username ) {
541  
542 # Replace old folder names with current username
543 my $starting_point = $HOME . '/' . $new_project_dir_name;
544  
545 # print(" RestoreProject.pl,project_directory=$new_project_dir_name\n");
546  
547 # print(" Looking for directories within the project to be restored...\n");
548 # print("old_username_from_backup=$old_username_from_backup\n");
549  
550 my @original_list =
551 `(find $starting_point -name $dir2find -type d -print 2>/dev/null)`;
552 my $original_list_length = scalar @original_list;
553 my @new_list = @original_list;
554  
555 # print("list is $original_list_length\n");
556 my $instruction3;
557  
558 # perform replacement here
559  
560 # print("current_username = $current_username\n");
561 # print("dir2find= $dir2find\n");
562  
563 for ( my $i = 0 ; $i < $original_list_length ; $i++ ) {
564  
565 my $old_directory = $original_list[$i];
566 my $new_directory = $original_list[$i];
567  
568 # print("old_directory= $old_directory\n");
569  
570 # remove ambiguous substitutions by makign sure
571 # the directory name found has no extra letters
572 # at the start or the end
573 $new_directory =~
574 s/(?<=\b)(?=$dir2find\b)$dir2find/$current_username/g;
575 chomp $old_directory;
576 $instruction3 = ("mv $old_directory $new_directory ");
577 system($instruction3);
578  
579 # print(" new_directory=$new_directory\n");
580 # print("$instruction3\n");
581  
582 }
583  
584 # print("replaced list is @new_list\n");
585
586 } # if current suername ne old_username
587 }
588 else {
589 print("RestoreProject.pl,unsuccessful\n");
590 print("You either have never run SUG before or\n");
591 print(
592 "you are trying to overwrite another project with the same name\n");
593 }
594  
595 }
596 else {
597 print("RestoreProject.pl, possible missing project name to restore\n");
598  
599 }