Script at least properly setting up loops and finding right dirs

This commit is contained in:
Bradley M. Kuhn 2023-06-02 21:14:14 -07:00
parent 88260520a4
commit 5ec6214da2

View file

@ -1,34 +1,81 @@
#!/usr/bin/perl #!/usr/bin/perl
# This expects a directory organized this way: # This expects both the input and output directory to be organized this way:
# rfp-NN/ # rfp-NN/BUCKET/TYPE/
# Note that items from input will be moved to output if they are included in group
#
# output directory ends up with two subdirectories: native/ and numbered/
# native is where things are just moved over at the end
# numbered is where the numbered items go
use strict; use strict;
use warnings; use warnings;
use autodie qw(:all); use autodie qw(:all);
use Getopt::Long; use Getopt::Long;
use File::Spec::Functions;
use File::Spec; use File::Spec;
use File::Path qw(make_path);
my %GROUP_NAMES_BY_DIR = ( confidential => 'CONFIDENTIAL', privilege => 'PRIVILEGE', privileged => 'PRIVILEGE',
'journalist-privilege' => 'PRIVILEGE' );
sub UsageAndExit($) { sub UsageAndExit($) {
print STDERR "usage: $0 --inputToplevelDir==/path/to/inputdir [ --verbose=N ]\n"; print STDERR "usage: $0 --inputToplevelDir=/path/to/inputdir --outputToplevelDir=/path/to/outputdir --group=group [ --verbose=N ]\n";
print STDERR "\n $_[0]\n"; print STDERR "\n $_[0]\n";
exit 2; exit 2;
} }
my($VERBOSE, $INPUT_TOPLEVEL_DIR) = my($VERBOSE, $INPUT_TOPLEVEL_DIR, $OUTPUT_TOPLEVEL_DIR, $GROUP) =
(0, undef, undef, undef, undef, undef); (0, undef, undef, undef, undef, undef);
GetOptions("verbose=i" => \$VERBOSE, "inputToplevelDir=s", \$INPUT_TOPLEVEL_DIR) or UsageAndExit("invalid options"); GetOptions("verbose=i" => \$VERBOSE, "inputToplevelDir=s", \$INPUT_TOPLEVEL_DIR, 'group=s' => \$GROUP,
"outputToplevelDir=s", \$OUTPUT_TOPLEVEL_DIR) or UsageAndExit("invalid options");
{ no warnings 'uninitialized';
UsageAndExit("\"$INPUT_TOPLEVEL_DIR\" is not a readable directory") UsageAndExit("\"$INPUT_TOPLEVEL_DIR\" is not a readable directory")
unless -r $INPUT_TOPLEVEL_DIR and -d $INPUT_TOPLEVEL_DIR; unless defined $INPUT_TOPLEVEL_DIR and -r $INPUT_TOPLEVEL_DIR and -d $INPUT_TOPLEVEL_DIR;
UsageAndExit("\"$OUTPUT_TOPLEVEL_DIR\" is not a readable directory")
unless defined $OUTPUT_TOPLEVEL_DIR and -r $OUTPUT_TOPLEVEL_DIR and -d $OUTPUT_TOPLEVEL_DIR;
UsageAndExit("\"$GROUP\" is not a valid group")
unless defined $GROUP and defined $GROUP_NAMES_BY_DIR{$GROUP};
}
my $upiNumberFile = File::Spec->rel2abs(catfile($OUTPUT_TOPLEVEL_DIR, "upi-number-start.txt"));
UsageAndExit("\"$upiNumberFile\" is not a readable file")
unless -r $upiNumberFile and -f $upiNumberFile;
open(my $upiFH, '<', $upiNumberFile);
my $count = 0;
my $upiStart = -1;
while (my $line = <$upiFH>) {
$count++;
chomp $line;
UsageAndExit("\"$upiNumberFile\" must contain a number only!") unless $line =~ /^\s*(\d+)\s*$/;
$upiStart = $1;
}
close $upiFH;
UsageAndExit("Error reading \"$upiNumberFile\"") unless $count == 1 and $upiStart > 0;
my $upiCurrentNum = $upiStart;
opendir(my $topDH, $INPUT_TOPLEVEL_DIR); opendir(my $topDH, $INPUT_TOPLEVEL_DIR);
while (my $rfp = readdir $topDH) { while (my $rfp = readdir $topDH) {
next unless $rfp =~ /^\s*rfp-(\d+)\s*$/; next unless $rfp =~ /^\s*rfp-(\d+)\s*$/;
my $rfpDirName = File::Spec->catfile($INPUT_TOPLEVEL_DIR, $rfp); my $inRfpDirName = catfile($INPUT_TOPLEVEL_DIR, $rfp);
print "$rfpDirName\n"; opendir(my $rfpDH, $inRfpDirName);
while (my $bucketName = readdir $rfpDH) {
next unless $bucketName eq $GROUP;
my $inBucketDirName = catfile($INPUT_TOPLEVEL_DIR, $rfp, $bucketName);
print " $inBucketDirName\n";
} }
closedir $rfpDH;
}
closedir $topDH;
#make_path(, {
# verbose => 1,
# mode => 0755,
#});