| 1 | #!/usr/bin/perl -w |
| 2 | ######################################################################## |
| 3 | # Copyright 2004 by Malcolm Nooning |
| 4 | # This program does not impose any |
| 5 | # licensing restrictions on files generated by their execution, in |
| 6 | # accordance with the 8th article of the Artistic License: |
| 7 | # |
| 8 | # "Aggregation of this Package with a commercial distribution is |
| 9 | # always permitted provided that the use of this Package is embedded; |
| 10 | # that is, when no overt attempt is made to make this Package's |
| 11 | # interfaces visible to the end user of the commercial distribution. |
| 12 | # Such use shall not be construed as a distribution of this Package." |
| 13 | # |
| 14 | # Therefore, you are absolutely free to place any license on the resulting |
| 15 | # executable(s), as long as the packed 3rd-party libraries are also available |
| 16 | # under the Artistic License. |
| 17 | # |
| 18 | # This program is free software; you can redistribute it and/or modify it |
| 19 | # under the same terms as Perl itself. |
| 20 | # |
| 21 | # See F<LICENSE>. |
| 22 | # |
| 23 | # |
| 24 | ######################################################################## |
| 25 | our $VERSIONscip-perl cpan PAR-Packer 1.064 main/$VERSION. = '0.06'; |
| 26 | |
| 27 | |
| 28 | ######################################################################## |
| 29 | # Usage: |
| 30 | # $error = |
| 31 | # prior_to_test($test_number, |
| 32 | # $startdir, |
| 33 | # $os, |
| 34 | # \$test_sub_dir_to_use_this_test, |
| 35 | # $verbose, |
| 36 | # \$message); |
| 37 | # |
| 38 | # $error will be one of POSIX (EXIT_SUCCESS EXIT_FAILURE) |
| 39 | # |
| 40 | ######################################################################## |
| 41 | # Outline |
| 42 | # ------- |
| 43 | # . chdir to the base directory. |
| 44 | # . Decide which of three possible sub dirs to wipe out, |
| 45 | # which will be tempn where the 'n' is test number mod 3. |
| 46 | # . Wipe out the temp dir and all it's files and sub dirs |
| 47 | # . Recreate the temp dir and four further sub dirs. |
| 48 | # . Assign the temp dir name (the one used by the caller) |
| 49 | # to be passed back up. |
| 50 | # |
| 51 | ######################################################################## |
| 52 | # |
| 53 | # There are three temp directories used so that we can inspect prior |
| 54 | # test results if there is a crash, as well as the current test |
| 55 | # results. The rationale is that it may be helpful to know what |
| 56 | # we were doing prior to the present test. There should never be |
| 57 | # a relationship, but, ... |
| 58 | # |
| 59 | ######################################################################## |
| 60 | |
| 61 | package prior_to_testscip-perl cpan PAR-Packer 1.064 prior_to_test/; |
| 62 | |
| 63 | |
| 64 | use Exporterscip-perl cpan PAR-Packer 1.064 Exporter/; |
| 65 | @ISAscip-perl cpan PAR-Packer 1.064 prior_to_test/`@ISA`. = qw(Exporter); |
| 66 | @EXPORTscip-perl cpan PAR-Packer 1.064 prior_to_test/`@EXPORT`. = ("prior_to_test"); |
| 67 | |
| 68 | use POSIXscip-perl cpan PAR-Packer 1.064 POSIX/ qw(EXIT_SUCCESS EXIT_FAILURE); |
| 69 | use File::Pathscip-perl cpan PAR-Packer 1.064 File/Path/; |
| 70 | use File::Findscip-perl cpan PAR-Packer 1.064 File/Find/; |
| 71 | use Cwdscip-perl cpan PAR-Packer 1.064 Cwd/ qw(cwd); |
| 72 | |
| 73 | use strictscip-perl cpan PAR-Packer 1.064 strict/; |
| 74 | |
| 75 | ############################################################## |
| 76 | # The find command does not seem to like globals. Hence |
| 77 | # the need for these two globals. |
| 78 | my @global_files = (); |
| 79 | my @global_dirs = (); |
| 80 | |
| 81 | ############################################################## |
| 82 | # This sub is used in conjunction with the perl "find" module. |
| 83 | sub push_to_file_or_dir_arrayscip-perl cpan PAR-Packer 1.064 prior_to_test/push_to_file_or_dir_array(). { |
| 84 | |
| 85 | my $file_or_dir = $File::Find::namescip-perl cpan PAR-Packer 1.064 File/Find/$name.; |
| 86 | |
| 87 | return if ($file_or_dir =~ /^\.+$/); |
| 88 | |
| 89 | if (-d($file_or_dir)) { |
| 90 | if ($file_or_dir =~ m/\w+/) { |
| 91 | pushscip-perl cpan PAR-Packer 1.064 prior_to_test/push(). (@global_dirs, ($file_or_dir)); |
| 92 | } |
| 93 | } else { |
| 94 | pushscip-perl cpan PAR-Packer 1.064 prior_to_test/push(). (@global_files, ($file_or_dir)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | ######################################################################## |
| 99 | sub remove_windows_treescip-perl cpan PAR-Packer 1.064 prior_to_test/remove_windows_tree(). { |
| 100 | my ($test_sub_dir, $message_ref) = @_scip-perl cpan PAR-Packer 1.064 prior_to_test/`@_`.; |
| 101 | my $file; |
| 102 | my $dir; |
| 103 | my $MAX_FILES_TO_DELETE = 100; |
| 104 | my $actual_num_files = 0; |
| 105 | my $cwd = cwd; |
| 106 | |
| 107 | $$message_ref = ""; |
| 108 | # There should never be more than just files, or at most |
| 109 | # files and subdirectories that contain no further |
| 110 | # subdirectories. Thus we can use the find command without |
| 111 | # using up too much ram. |
| 112 | |
| 113 | @global_files = (); |
| 114 | @global_dirs = (); |
| 115 | |
| 116 | findscip-perl cpan PAR-Packer 1.064 prior_to_test/find().(\&push_to_file_or_dir_arrayscip-perl cpan PAR-Packer 1.064 prior_to_test/push_to_file_or_dir_array()., ($test_sub_dir)); |
| 117 | |
| 118 | #.................................................................... |
| 119 | # Before we start deleting files, make sure there are less than, oh, |
| 120 | # some small number. There is not supposed to be many files or |
| 121 | # directories. We can up the number if we need to but we want to |
| 122 | # prevent an inadvertant disaster. |
| 123 | |
| 124 | $actual_num_files = @global_files; |
| 125 | |
| 126 | if ($actual_num_files >= $MAX_FILES_TO_DELETE) { |
| 127 | # Ouch. Something is wrong |
| 128 | $$message_ref = "ptt_055: " . |
| 129 | "In preparation for a test, I am not permitted " . |
| 130 | "to delete more than $MAX_FILES_TO_DELETE files\n" . |
| 131 | "however, there are $actual_num_files files to " . |
| 132 | "be deleted. I will not do it.\n" . |
| 133 | "Please research and fix\n"; |
| 134 | return(EXIT_FAILURE); |
| 135 | |
| 136 | } |
| 137 | #.................................................................... |
| 138 | |
| 139 | # Delete the files first. Then we can delete the dirs |
| 140 | # without worring about whether or not they are empty. |
| 141 | foreach $file (@global_files) { |
| 142 | if (!(unlinkscip-perl cpan PAR-Packer 1.064 prior_to_test/unlink(). ("$file"))) { |
| 143 | $$message_ref = "ptt_060: " . |
| 144 | "Cannot unlink $file:$!:\n"; |
| 145 | return (EXIT_FAILURE); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | # Remove the last dir first |
| 150 | foreach $dir (reversescip-perl cpan PAR-Packer 1.064 prior_to_test/reverse(). @global_dirs) { |
| 151 | if (!(rmdirscip-perl cpan PAR-Packer 1.064 prior_to_test/rmdir().($dir))) { |
| 152 | $$message_ref = "ptt_065: " . |
| 153 | "I am in dir $cwd and I " . |
| 154 | "cannot rmdir $dir:$!:\n" . |
| 155 | "Are you using it in another window?\n"; |
| 156 | return (EXIT_FAILURE); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return (EXIT_SUCCESS); |
| 161 | } |
| 162 | |
| 163 | ######################################################################## |
| 164 | sub prior_to_testscip-perl cpan PAR-Packer 1.064 prior_to_test/prior_to_test(). { |
| 165 | my ( |
| 166 | $test_number, |
| 167 | $base_directory, |
| 168 | $os, |
| 169 | $test_sub_dir_to_use_ref, |
| 170 | $verbose, |
| 171 | $message_ref, |
| 172 | ) = @_scip-perl cpan PAR-Packer 1.064 prior_to_test/`@_`.; |
| 173 | |
| 174 | my $MODULUS = 3; |
| 175 | my $temp_num = ($test_number % $MODULUS); |
| 176 | my $error = EXIT_FAILURE; |
| 177 | my $test_sub_dir = ""; |
| 178 | my $permission = 509; # 509 decimal is octal 0775 |
| 179 | my $further_subdir = ""; |
| 180 | my @further_subdirs = qw(subdir1 subdir2 subdir3 subdir4); |
| 181 | my $further_subdir_to_create = ""; |
| 182 | |
| 183 | $$message_ref = ""; |
| 184 | |
| 185 | chdirscip-perl cpan PAR-Packer 1.064 prior_to_test/chdir().($base_directory); |
| 186 | |
| 187 | # Remove the test directory, if present, |
| 188 | if ($os =~ m!^Win!i) { |
| 189 | $test_sub_dir = $base_directory . "\\temp" . "$temp_num"; |
| 190 | if (-e("$test_sub_dir")) { |
| 191 | $error = remove_windows_treescip-perl cpan PAR-Packer 1.064 prior_to_test/remove_windows_tree().($test_sub_dir, $message_ref); |
| 192 | return $error if ($error == EXIT_FAILURE); |
| 193 | } |
| 194 | } else { |
| 195 | $test_sub_dir = $base_directory . "/temp" . "$temp_num"; |
| 196 | if (-e("$test_sub_dir")) { |
| 197 | if (systemscip-perl cpan PAR-Packer 1.064 prior_to_test/system().("rm -rf \"$test_sub_dir\"")) { |
| 198 | $$message_ref = ( "ptt_075: " . |
| 199 | ":$!:$?:\n"); |
| 200 | return (EXIT_FAILURE); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | # mkpath assuming unix. Windows defaults to read/write itself. |
| 206 | if (!(mkpathscip-perl cpan PAR-Packer 1.064 prior_to_test/mkpath(). ("$test_sub_dir", 0, $permission))) { |
| 207 | $$message_ref = "ptt_080: Cannot create dir $test_sub_dir:$!:\n"; |
| 208 | return (EXIT_FAILURE); |
| 209 | } |
| 210 | |
| 211 | $$test_sub_dir_to_use_ref = $test_sub_dir; |
| 212 | |
| 213 | |
| 214 | #................................................................. |
| 215 | # Create subdirs underneath our test_sub_dir, just in case |
| 216 | #................................................................. |
| 217 | foreach $further_subdir (@further_subdirs) { |
| 218 | $further_subdir_to_create = $test_sub_dir . "/$further_subdir"; |
| 219 | if (!(mkpathscip-perl cpan PAR-Packer 1.064 prior_to_test/mkpath(). ("$further_subdir_to_create", 0, $permission))) { |
| 220 | $$message_ref = "ptt_085: " . |
| 221 | "Cannot create dir $further_subdir_to_create:$!:\n"; |
| 222 | return (EXIT_FAILURE); |
| 223 | } |
| 224 | } |
| 225 | #................................................................. |
| 226 | |
| 227 | return (EXIT_SUCCESS); |
| 228 | |
| 229 | } |
| 230 | ######################################################################## |
| 231 | 1; |