Reorganize directory
This commit is contained in:
parent
12dce591bb
commit
10e3e1f745
26 changed files with 371 additions and 477 deletions
5
file_processing/README.md
Normal file
5
file_processing/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# archive_statements.pl and archive_taxes.pl
|
||||
|
||||
## Requirements
|
||||
|
||||
cpanm -S File::Find::Rule File::Copy::Recursive File::DirCompare Time::Piece Test2::V0 Test2::Plugin::NoWarnings Modern::Perl
|
||||
0
file_processing/lib/FinancialFileArchiver.pm
Normal file
0
file_processing/lib/FinancialFileArchiver.pm
Normal file
125
file_processing/lib/ProcessDirectories.pm
Normal file
125
file_processing/lib/ProcessDirectories.pm
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
package ProcessDirectories;
|
||||
use Modern::Perl;
|
||||
# VERSION
|
||||
# AUTHORITY
|
||||
# ABSTRACT: Get current subdirectories in current year directory and copy to archive directory
|
||||
use Carp qw( croak );
|
||||
use File::Basename;
|
||||
use File::Find::Rule;
|
||||
use File::Copy::Recursive qw(dircopy);
|
||||
use File::DirCompare;
|
||||
use Pod::Usage;
|
||||
# use Time::Piece qw/localtime/;
|
||||
|
||||
sub create_archive_dirs {
|
||||
my ( $source_dir, $dest_dir ) = @_;
|
||||
unless ( -d $source_dir ) {
|
||||
die "Source directory '$source_dir' does not exist, please create it";
|
||||
}
|
||||
|
||||
print("Copying current directories to archive folder\n");
|
||||
print("From: $source_dir\n");
|
||||
print("To: $dest_dir\n");
|
||||
dircopy( $source_dir, $dest_dir )
|
||||
or croak "Could not copy directories $!";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub compare_dirs {
|
||||
my ( $dir1, $dir2 ) = @_;
|
||||
my $same = 1;
|
||||
print("Making sure all directories and files match between source and destination\n");
|
||||
File::DirCompare->compare(
|
||||
$dir1, $dir2,
|
||||
sub {
|
||||
my ( $a, $b ) = @_;
|
||||
if ( !$b ) {
|
||||
printf "Only in %s: %s\n", dirname($a), basename($a);
|
||||
$same = 0;
|
||||
}
|
||||
elsif ( !$a ) {
|
||||
printf "Only in %s: %s\n", dirname($b), basename($b);
|
||||
$same = 0;
|
||||
}
|
||||
else {
|
||||
print "Files $a and $b differ\n";
|
||||
$same = 0;
|
||||
}
|
||||
}
|
||||
);
|
||||
return $same;
|
||||
}
|
||||
|
||||
sub clean_dirs {
|
||||
my ( $new_dir, $old_dir, $curr_year, $last_year ) = @_;
|
||||
|
||||
my @this_year_files =
|
||||
File::Find::Rule->file()->name("*$curr_year*")->in($old_dir);
|
||||
my @last_year_files =
|
||||
File::Find::Rule->file()->name("*$last_year*")->in($new_dir);
|
||||
|
||||
unlink @this_year_files;
|
||||
print("Deleted all files for $curr_year from the folder for $last_year\n");
|
||||
unlink @last_year_files;
|
||||
print("Deleted all files for $last_year from the folder for $curr_year\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=pod
|
||||
|
||||
=head1 NAME
|
||||
|
||||
ProcessDirectories
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Methods for copying and processing a directory of files for the past year and this year
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use ProcessDirectories
|
||||
|
||||
create_archive_dirs ( $source_dir, $dest_dir );
|
||||
compare_dirs( $source_dir, $dest_dir );
|
||||
clean_dirs( $source_dir, $dest_dir, $year, $lastyear );
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
=head2
|
||||
C<create_archive_dirs>: Copies the directory structure and contents of a source file to the archive / destination directory.
|
||||
|
||||
=head3
|
||||
Usage:
|
||||
create_archive_dirs( $source_dir, $destination_dir );
|
||||
|
||||
=head2
|
||||
C<compare_dirs>: Compares the content of two specified directories. Provides feedback on the results. Returns 1 if the source and destination match, 0 otherwise.
|
||||
|
||||
=head3
|
||||
Usage:
|
||||
compare_dirs( $source_dir, $destination_dir );
|
||||
|
||||
=head2
|
||||
C<clean_dirs>: This method accepts the following parameters:
|
||||
|
||||
=over 4
|
||||
|
||||
=item source_dir
|
||||
|
||||
=item destination_dir
|
||||
|
||||
=item this_year
|
||||
|
||||
=item last_year
|
||||
|
||||
=back
|
||||
|
||||
=head3
|
||||
Usage:
|
||||
clean_dirs( $source_dir, $destination_dir, 2023, 2022 );
|
||||
|
||||
=cut
|
||||
14
file_processing/t/00-load.t
Normal file
14
file_processing/t/00-load.t
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use Test2::V0;
|
||||
use Test2::Plugin::Times;
|
||||
use Test2::Plugin::ExitSummary;
|
||||
use Test2::Plugin::NoWarnings;
|
||||
use FindBin qw($Bin);
|
||||
use lib "$Bin/../lib";
|
||||
|
||||
|
||||
plan(1);
|
||||
BEGIN {
|
||||
use ProcessDirectories;
|
||||
|
||||
pass('Module loads correctly.');
|
||||
}
|
||||
79
file_processing/t/01-subroutines.t
Normal file
79
file_processing/t/01-subroutines.t
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
use Test2::V0;
|
||||
use Test2::Plugin::Times;
|
||||
use Test2::Plugin::ExitSummary;
|
||||
use Test2::Plugin::NoWarnings;
|
||||
use FindBin qw($Bin);
|
||||
use File::Path::Tiny;
|
||||
use Time::Piece qw/localtime/;
|
||||
|
||||
use lib "$Bin/../lib";
|
||||
use ProcessDirectories;
|
||||
|
||||
|
||||
plan(2);
|
||||
|
||||
# Preconditions
|
||||
my $now = localtime;
|
||||
my $year = $now->year;
|
||||
my $lastyear = $now->add_years(-1)->year;
|
||||
diag("Current year: $year Last year: $lastyear\n");
|
||||
|
||||
## Define and create test source folder
|
||||
my $current_year_dir = 't/current_year/';
|
||||
my $archive_folder = 't/1_archives/';
|
||||
my $prev_year_dir = $archive_folder . $lastyear . '_Archives';
|
||||
diag("Source test dir $current_year_dir Dest test dir $prev_year_dir");
|
||||
|
||||
if(!File::Path::Tiny::mk($current_year_dir)) {
|
||||
die "Could not create test source directory: '$current_year_dir': $!";
|
||||
}
|
||||
|
||||
my @test_files = qw( 2003_file-1 2003_file-2 2002_file-1 2002_file-2 2002_file-3 );
|
||||
for my $file (@test_files) {
|
||||
create_file($current_year_dir . '/' . $file);
|
||||
}
|
||||
|
||||
# Begin testing
|
||||
ProcessDirectories::create_archive_dirs( $current_year_dir, $prev_year_dir );
|
||||
ok( ( -d $prev_year_dir ), "Previous year archive directory was created" );
|
||||
|
||||
|
||||
ok( ProcessDirectories::compare_dirs( $current_year_dir, $prev_year_dir ), "Previous year archive directory contents matches current directory contents" );
|
||||
|
||||
|
||||
# clean_dirs
|
||||
|
||||
# End Testing
|
||||
|
||||
# Cleanup
|
||||
# if(!File::Path::Tiny::rm($current_year_dir)) {
|
||||
# die "Could not remove test source directory: '$current_year_dir': $!";
|
||||
# }
|
||||
# if(!File::Path::Tiny::rm($archive_folder)) {
|
||||
# die "Could not remove test archive directory: '$archive_folder': $!";
|
||||
# }
|
||||
|
||||
sub create_file {
|
||||
my $filepath = shift;
|
||||
open (my $fh, ">", $filepath) or die "Can't open file '$filepath' for writing: $!";
|
||||
|
||||
# you can control the range of characters here
|
||||
my $minimum = 4;
|
||||
my $range = 16;
|
||||
my $num_bytes = '100';
|
||||
|
||||
for (my $bytes = 0; $bytes < $num_bytes; $bytes += 4) {
|
||||
my $rand = int(rand($range ** 4));
|
||||
my $string = '';
|
||||
for (1..4) {
|
||||
$string .= chr($rand % $range + $minimum);
|
||||
$rand = int($rand / $range);
|
||||
}
|
||||
print $fh $string;
|
||||
}
|
||||
|
||||
close($fh) or warn "Can't close file '$filepath': $!";
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
19
file_processing/t/02-archive-files.t
Normal file
19
file_processing/t/02-archive-files.t
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use Modern::Perl;
|
||||
use Carp qw/carp croak/;
|
||||
|
||||
use Test2::V0;
|
||||
use Test2::Tools::Subtest qw/subtest_streamed/;
|
||||
use Test2::Plugin::ExitSummary;
|
||||
# use Test2::Tools::Exception qw/dies lives/;
|
||||
use Test2::Plugin::NoWarnings;
|
||||
# use Cwd qw(cwd);
|
||||
use File::Path qw(remove_tree make_path);
|
||||
use Time::Piece qw/localtime/;
|
||||
|
||||
# Order is important
|
||||
use FindBin qw($Bin);
|
||||
use lib "$Bin/../lib";
|
||||
use ProcessDirectories;
|
||||
|
||||
use File::Temp qw/ tempfile tempdir /;
|
||||
|
||||
7
file_processing/t/1_archives/2022_Archives/2002_file-1
Normal file
7
file_processing/t/1_archives/2022_Archives/2002_file-1
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
5
file_processing/t/1_archives/2022_Archives/2002_file-2
Normal file
5
file_processing/t/1_archives/2022_Archives/2002_file-2
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
9
file_processing/t/1_archives/2022_Archives/2002_file-3
Normal file
9
file_processing/t/1_archives/2022_Archives/2002_file-3
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
7
file_processing/t/1_archives/2022_Archives/2003_file-1
Normal file
7
file_processing/t/1_archives/2022_Archives/2003_file-1
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
8
file_processing/t/1_archives/2022_Archives/2003_file-2
Normal file
8
file_processing/t/1_archives/2022_Archives/2003_file-2
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
7
file_processing/t/current_year/2002_file-1
Normal file
7
file_processing/t/current_year/2002_file-1
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
5
file_processing/t/current_year/2002_file-2
Normal file
5
file_processing/t/current_year/2002_file-2
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
9
file_processing/t/current_year/2002_file-3
Normal file
9
file_processing/t/current_year/2002_file-3
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
7
file_processing/t/current_year/2003_file-1
Normal file
7
file_processing/t/current_year/2003_file-1
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
8
file_processing/t/current_year/2003_file-2
Normal file
8
file_processing/t/current_year/2003_file-2
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue