Skip to content
Snippets Groups Projects
Commit 1d24bd60 authored by Jens Steidl's avatar Jens Steidl :baby_chick:
Browse files

- moved argument checks to the beginning

- created main()
parent 388bea3f
No related branches found
No related tags found
No related merge requests found
......@@ -40,7 +40,58 @@ use XML::LibXSLT;
use XML::XPath;
use constant buffer => 100*1024*1024; # use 100MB as Buffer
my $with_debug = 0;
#===============================================================================
my $directory;
my $ppn;
my $noppn;
my $output;
my $url;
my $as_zip;
my $external_id;
my $external_workflow;
my $external_isil="";
my $external_value_descr;
my $external_conservation_flag;
my $with_debug;
my $help;
my $man;
our $VERSION = '1.1';
GetOptions(
"IE_directory=s" => \$directory,
"ppn=s" => \$ppn,
"noppn=s" => \$noppn,
"SIP_output_path=s" => \$output,
"as_zip" => \$as_zip,
"url=s" => \$url,
"external_id=s" => \$external_id,
"external_workflow=s" => \$external_workflow,
"external_ISIL=s" => \$external_isil,
"external_value_descr=s" => \$external_value_descr,
"external_conservation_flag" => \$external_conservation_flag,
"debug" => \$with_debug,
"help|?" => \$help,
"man" => \$man,
) or pod2usage(2);
if ($help) { pod2usage(1); }
if ($man) { pod2usage(-exitval => 0, -verbose => 2); }
if (!defined $directory) { confess ("you need to specify an IE directory, which needs to be archived"); }
if (!defined $ppn && !defined $noppn) { confess ("you need to specify a PPN, which exists in SWB catalogue"); }
if (defined $ppn && defined $noppn) {confess ("you could only use --ppn=foo or --noppn=bar"); }
if (!defined $output) { confess (" you need to specify an output path, where the SIP will be stored"); }
if ($output !~ m#^/#) { confess("you need to specify an output path using absoluet paths, $!"); }
if (!defined $url) { $url = "http://swb.bsz-bw.de/sru/DB=2.1/username=/password=/";}
if (!defined $external_conservation_flag) { $external_conservation_flag="false"; } else { $external_conservation_flag="true"; }
# additional checks
if (! -d $directory) { confess("you need to specify an IE directory, which needs to be archived, $!"); }
if ($directory !~ m#^/#) { confess("you need to specify an IE directory using absoluet paths, $!"); }
#if (! -d $output) { confess("you need to specify an output path, where the SIP will be stored, $!"); }
#===============================================================================
# write data to file (UTF-8)
sub write_file ($$) { #
my $filename = $_[0];
......@@ -183,60 +234,21 @@ sub get_mods_from ($$) { # $mods = ($url, $ppn)
#===============================================================================
my $directory;
my $ppn;
my $noppn;
my $output;
my $url;
my $as_zip;
my $external_id;
my $external_workflow;
my $external_isil="";
my $external_value_descr;
my $external_conservation_flag;
our $VERSION = '1.0';
GetOptions(
"IE_directory=s" => \$directory,
"ppn=s" => \$ppn,
"noppn=s" => \$noppn,
"SIP_output_path=s" => \$output,
"as_zip" => \$as_zip,
"url=s" => \$url,
"external_id=s" => \$external_id,
"external_workflow=s" => \$external_workflow,
"external_ISIL=s" => \$external_isil,
"external_value_descr=s" => \$external_value_descr,
"external_conservation_flag" => \$external_conservation_flag,
"debug" => \$with_debug,
"help" => sub { pod2usage(1); exit(0); },
) or pod2usage(2);
if (!defined $directory) { confess ("you need to specify an IE directory, which needs to be archived"); }
if (!defined $ppn && !defined $noppn) { confess ("you need to specify a PPN, which exists in SWB catalogue"); }
if (defined $ppn && defined $noppn) {confess ("you could only use --ppn=foo or --noppn=bar"); }
if (!defined $output) { confess (" you need to specify an output path, where the SIP will be stored"); }
if ($output !~ m#^/#) { confess("you need to specify an output path using absoluet paths, $!"); }
if (!defined $url) { $url = "http://swb.bsz-bw.de/sru/DB=2.1/username=/password=/";}
if (!defined $external_conservation_flag) { $external_conservation_flag="false"; } else { $external_conservation_flag="true"; }
# additional checks
if (! -d $directory) { confess("you need to specify an IE directory, which needs to be archived, $!"); }
if ($directory !~ m#^/#) { confess("you need to specify an IE directory using absoluet paths, $!"); }
#if (! -d $output) { confess("you need to specify an output path, where the SIP will be stored, $!"); }
sub main {
# get date
my $export_to_archive_date = DateTime->now->iso8601();#
my $file_date = $export_to_archive_date;
$file_date =~ s/T/_/g;
$file_date =~ s/:/-/g;
# create output dir
mkpath "$output" || confess("could not create SIP directory for '$output', $!");
if (! -d $output) {
mkpath("$output") || confess("could not create SIP directory for '$output', $!");
}
my $sip_root_dir = "PPN-${ppn}_${file_date}";
my $content = "$output/$sip_root_dir/data";
if (!defined $as_zip) {
mkpath "$output/$sip_root_dir" || confess("could not create SIP directory for '$output/$sip_root_dir', $!");
mkpath "$content" || confess("could not create SIP subdirectory for '$content', $!");
mkpath("$output/$sip_root_dir") || confess("could not create SIP directory for '$output/$sip_root_dir', $!");
mkpath("$content") || confess("could not create SIP subdirectory for '$content', $!");
}
......@@ -245,7 +257,7 @@ my $mods;
if (defined $ppn) {
$mods = get_mods_from($url, $ppn);
if (1 == $with_debug) {
write_file("DEBUG_${ppn}_mods.xml", {binmode => ':utf8'}, $mods);
write_file("DEBUG_${ppn}_mods.xml", $mods);
}
# remove the <xml /> from beginning of the answer
$mods=~ s#<\?xml version="1.0" encoding="UTF-8"\?>#<!-- removed xml header from mods part -->#;
......@@ -377,14 +389,14 @@ METS
# compress if needed
if (!defined $as_zip) {
write_file( "${output}/${sip_root_dir}/sip.xml",{binmode => ':utf8'}, $sip );
write_file( "${output}/${sip_root_dir}/sip.xml", $sip );
# copy source to target
foreach my $source (sort keys (%filecopyhash)) {
my $target = $filecopyhash{$source}->{"target"};
my $basename = dirname($target);
#say "cp $source, $target ($basename)";
if (! -d $basename) {
mkpath $basename || confess ("could not mkdir '$basename', $!");
mkpath($basename) || confess ("could not mkdir '$basename', $!");
}
cp($source, $target, buffer) || confess ("could not copy from '$source' to '$target', $!");
}
......@@ -406,8 +418,15 @@ if (!defined $as_zip) {
}
say "SIP '$sip_root_dir' build successfully in '$zip_file_path'";
}
}
#===============================================================================
main();
#===============================================================================
__END__
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment