|
Lines 23-29
marc_ordering_process.pl - cron script to retrieve MARC files and create order l
Link Here
|
| 23 |
|
23 |
|
| 24 |
=head1 SYNOPSIS |
24 |
=head1 SYNOPSIS |
| 25 |
|
25 |
|
| 26 |
./marc_ordering_process.pl [-c|--confirm] [-v|--verbose] [--dr|--delete-remote] [--rr|--rename-remote] ext] |
26 |
./marc_ordering_process.pl [-c|--confirm] [-v|--verbose] [-d|--delete] [-r|--rename ext] |
| 27 |
|
27 |
|
| 28 |
or, in crontab: |
28 |
or, in crontab: |
| 29 |
# Once every day |
29 |
# Once every day |
|
Lines 31-38
or, in crontab:
Link Here
|
| 31 |
|
31 |
|
| 32 |
=head1 DESCRIPTION |
32 |
=head1 DESCRIPTION |
| 33 |
|
33 |
|
| 34 |
This script searches for new MARC files in an SFTP location |
34 |
This script searches for new MARC files via configured file transports, |
| 35 |
If there are new files, it stages those files, adds bilbios/items and creates order lines |
35 |
stages those files, adds biblios/items and creates order lines. |
| 36 |
|
36 |
|
| 37 |
=head1 OPTIONS |
37 |
=head1 OPTIONS |
| 38 |
|
38 |
|
|
Lines 48-62
Without this parameter no changes will be made
Link Here
|
| 48 |
|
48 |
|
| 49 |
=item B<-d|--delete> |
49 |
=item B<-d|--delete> |
| 50 |
|
50 |
|
| 51 |
Delete the file once it has been processed |
51 |
Delete the remote source file once it has been processed |
| 52 |
|
52 |
|
| 53 |
=item B<--dr|--delete-remote> |
53 |
=item B<-r|--rename ext> |
| 54 |
|
54 |
|
| 55 |
Delete the remote file once it has been downloaded |
55 |
Rename the remote source file once it has been processed, adding the given file extension |
| 56 |
|
56 |
|
| 57 |
=item B<-rr|--rename-remove ext> |
57 |
=item B<-a|--archive dir> |
| 58 |
|
58 |
|
| 59 |
Rename the remote file once it has been downloaded, adding the given file extension |
59 |
Copy downloaded files into the specified local archive directory after processing |
| 60 |
|
60 |
|
| 61 |
=back |
61 |
=back |
| 62 |
|
62 |
|
|
Lines 65-71
Rename the remote file once it has been downloaded, adding the given file extens
Link Here
|
| 65 |
use Modern::Perl; |
65 |
use Modern::Perl; |
| 66 |
use Pod::Usage qw( pod2usage ); |
66 |
use Pod::Usage qw( pod2usage ); |
| 67 |
use Getopt::Long qw( GetOptions ); |
67 |
use Getopt::Long qw( GetOptions ); |
| 68 |
use File::Copy qw( copy move ); |
68 |
use File::Copy qw( copy ); |
|
|
69 |
use File::Temp qw( tempdir ); |
| 69 |
|
70 |
|
| 70 |
use Koha::Script -cron; |
71 |
use Koha::Script -cron; |
| 71 |
use Koha::MarcOrder; |
72 |
use Koha::MarcOrder; |
|
Lines 76-89
use C4::Log qw( cronlogaction );
Link Here
|
| 76 |
my $command_line_options = join( " ", @ARGV ); |
77 |
my $command_line_options = join( " ", @ARGV ); |
| 77 |
cronlogaction( { info => $command_line_options } ); |
78 |
cronlogaction( { info => $command_line_options } ); |
| 78 |
|
79 |
|
| 79 |
my ( $help, $verbose, $confirm, $delete, $rename_ext, $delete_remote ); |
80 |
my ( $help, $verbose, $confirm, $delete, $rename_ext, $archive_dir ); |
| 80 |
GetOptions( |
81 |
GetOptions( |
| 81 |
'h|help' => \$help, |
82 |
'h|help' => \$help, |
| 82 |
'v|verbose' => \$verbose, |
83 |
'v|verbose' => \$verbose, |
| 83 |
'c|confirm' => \$confirm, |
84 |
'c|confirm' => \$confirm, |
| 84 |
'd|delete' => \$delete, |
85 |
'd|delete' => \$delete, |
| 85 |
'dr|delete-remote' => \$delete_remote, |
86 |
'r|rename=s' => \$rename_ext, |
| 86 |
'rr|rename-remote=s' => \$rename_ext, |
87 |
'a|archive=s' => \$archive_dir, |
| 87 |
) || pod2usage(1); |
88 |
) || pod2usage(1); |
| 88 |
|
89 |
|
| 89 |
pod2usage(0) if $help; |
90 |
pod2usage(0) if $help; |
|
Lines 104-173
if ( scalar(@accounts) == 0 ) {
Link Here
|
| 104 |
my $valid_file_extensions = qr/\.(mrc|marcxml|mrk)$/i; |
105 |
my $valid_file_extensions = qr/\.(mrc|marcxml|mrk)$/i; |
| 105 |
|
106 |
|
| 106 |
foreach my $acct (@accounts) { |
107 |
foreach my $acct (@accounts) { |
|
|
108 |
my $file_transport = $acct->file_transport; |
| 109 |
unless ($file_transport) { |
| 110 |
say "No file transport configured for account: " . $acct->description if $verbose; |
| 111 |
next; |
| 112 |
} |
| 113 |
|
| 114 |
my $working_dir = $file_transport->download_directory; |
| 115 |
unless ($working_dir) { |
| 116 |
say "No download directory configured for file transport: " . $file_transport->id if $verbose; |
| 117 |
next; |
| 118 |
} |
| 119 |
|
| 107 |
if ($verbose) { |
120 |
if ($verbose) { |
| 108 |
say sprintf "Starting MARC ordering process for %s", $acct->vendor->name; |
121 |
say sprintf "Starting MARC ordering process for %s", $acct->vendor->name; |
| 109 |
say sprintf "Looking for new files in %s", $acct->download_directory; |
122 |
say sprintf "Looking for new files in %s", $working_dir; |
| 110 |
} |
123 |
} |
| 111 |
|
124 |
|
| 112 |
my $working_dir = $acct->download_directory; |
125 |
unless ( $file_transport->connect() ) { |
| 113 |
|
126 |
say "Failed to connect to file transport: " . $file_transport->id; |
| 114 |
my $file_transport = $acct->file_transport; |
127 |
next; |
| 115 |
if ($file_transport) { |
128 |
} |
| 116 |
if ( $file_transport->connect() ) { |
|
|
| 117 |
my $download_dir = $file_transport->download_directory; |
| 118 |
|
129 |
|
| 119 |
my $success = $download_dir ? $file_transport->change_directory($download_dir) : 1; |
130 |
my $success = $file_transport->change_directory($working_dir); |
| 120 |
if ( $download_dir && !$success ) { |
131 |
if ( !$success ) { |
| 121 |
say "Failed to change to download directory: $download_dir"; |
132 |
say "Failed to change to download directory: $working_dir"; |
| 122 |
} else { |
133 |
next; |
|
|
134 |
} |
| 123 |
|
135 |
|
| 124 |
# Get file list |
136 |
# Get file list |
| 125 |
my $file_list = $file_transport->list_files(); |
137 |
my $file_list = $file_transport->list_files(); |
| 126 |
if ($file_list) { |
138 |
unless ($file_list) { |
| 127 |
|
139 |
say "Failed to get file list from transport"; |
| 128 |
# Process files matching our criteria |
140 |
next; |
| 129 |
foreach my $file ( @{$file_list} ) { |
141 |
} |
| 130 |
my $filename = $file->{filename}; |
|
|
| 131 |
|
| 132 |
if ( $filename =~ $valid_file_extensions ) { |
| 133 |
|
| 134 |
my $local_file = "$working_dir/$filename"; |
| 135 |
|
| 136 |
# Download the file |
| 137 |
if ( $file_transport->download_file( $filename, $local_file ) ) { |
| 138 |
if ($rename_ext) { |
| 139 |
$file_transport->rename_file( $filename, "$filename.$rename_ext" ); |
| 140 |
} elsif ($delete_remote) { |
| 141 |
$file_transport->delete_file($filename); |
| 142 |
} |
| 143 |
} else { |
| 144 |
say "Failed to download file: $filename"; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
} else { |
| 150 |
say "Failed to get file list from transport"; |
| 151 |
} |
| 152 |
|
142 |
|
| 153 |
} |
143 |
my @files; |
| 154 |
} else { |
144 |
foreach my $file ( @{$file_list} ) { |
| 155 |
say "Failed to connect to file transport: " . $file_transport->id; |
145 |
my $filename = $file->{filename}; |
| 156 |
} |
146 |
next unless $filename =~ $valid_file_extensions; |
|
|
147 |
push @files, $filename; |
| 157 |
} |
148 |
} |
| 158 |
|
149 |
|
| 159 |
opendir my $dir, $working_dir or die "Can't open filepath"; |
|
|
| 160 |
my @files = grep { /$valid_file_extensions/ } readdir $dir; |
| 161 |
closedir $dir; |
| 162 |
print "No new files found\n" if scalar(@files) == 0; |
150 |
print "No new files found\n" if scalar(@files) == 0; |
| 163 |
|
151 |
|
| 164 |
my $files_processed = 0; |
152 |
my $files_processed = 0; |
|
|
153 |
my $local_dir = tempdir( CLEANUP => 1 ); |
| 165 |
|
154 |
|
| 166 |
foreach my $filename (@files) { |
155 |
foreach my $filename (@files) { |
| 167 |
my $full_path = "$working_dir/$filename"; |
156 |
my $local_file = "$local_dir/$filename"; |
| 168 |
my $args = { |
157 |
|
|
|
158 |
unless ( $file_transport->download_file( $filename, $local_file ) ) { |
| 159 |
say "Failed to download file: $filename"; |
| 160 |
next; |
| 161 |
} |
| 162 |
|
| 163 |
my $args = { |
| 169 |
filename => $filename, |
164 |
filename => $filename, |
| 170 |
filepath => $full_path, |
165 |
filepath => $local_file, |
| 171 |
profile => $acct, |
166 |
profile => $acct, |
| 172 |
agent => 'cron' |
167 |
agent => 'cron' |
| 173 |
}; |
168 |
}; |
|
Lines 182-194
foreach my $acct (@accounts) {
Link Here
|
| 182 |
if ( $result->{success} ) { |
177 |
if ( $result->{success} ) { |
| 183 |
$files_processed++; |
178 |
$files_processed++; |
| 184 |
say sprintf "Successfully processed file: %s", $filename if $verbose; |
179 |
say sprintf "Successfully processed file: %s", $filename if $verbose; |
|
|
180 |
if ($archive_dir) { |
| 181 |
mkdir $archive_dir unless -d $archive_dir; |
| 182 |
say sprintf "Archiving file: %s to %s", $filename, $archive_dir if $verbose; |
| 183 |
copy( $local_file, "$archive_dir/$filename" ); |
| 184 |
} |
| 185 |
if ($delete) { |
185 |
if ($delete) { |
| 186 |
say sprintf "Deleting processed file: %s", $filename if $verbose; |
186 |
say sprintf "Deleting file: %s", $filename if $verbose; |
| 187 |
unlink $full_path; |
187 |
$file_transport->delete_file($filename); |
| 188 |
} else { |
188 |
} elsif ($rename_ext) { |
| 189 |
mkdir "$working_dir/archive" unless -d "$working_dir/archive"; |
189 |
say sprintf "Renaming file: %s to %s.%s", $filename, $filename, $rename_ext if $verbose; |
| 190 |
say sprintf "Moving file to archive: %s", $filename if $verbose; |
190 |
$file_transport->rename_file( $filename, "$filename.$rename_ext" ); |
| 191 |
move( $full_path, "$working_dir/archive/$filename" ); |
|
|
| 192 |
} |
191 |
} |
| 193 |
} else { |
192 |
} else { |
| 194 |
say sprintf "Error processing file: %s", $filename if $verbose; |
193 |
say sprintf "Error processing file: %s", $filename if $verbose; |
| 195 |
- |
|
|