| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# ecaccess-file-mget: Download Multiple ECaccess Files at once |
| 4 |
# |
| 5 |
# [email protected] - 2010-10-15 |
| 6 |
|
| 7 |
use ECMWF::ECaccess; |
| 8 |
use Getopt::Long; |
| 9 |
use Pod::Usage; |
| 10 |
use MIME::Base64; |
| 11 |
use Term::Prompt; |
| 12 |
use Number::Bytes::Human qw(format_bytes); |
| 13 |
|
| 14 |
my %opt = ( progress => 0, force => 0, bufsize => 1048576, version => 0, help => 0, manual => 0, retry => 0, debug => 0 ); |
| 15 |
|
| 16 |
pod2usage( -noperldoc => 1, -exit => 1, verbose => 1 ) if !GetOptions( |
| 17 |
\%opt, |
| 18 |
qw( |
| 19 |
progress |
| 20 |
force |
| 21 |
bufsize=i |
| 22 |
version |
| 23 |
help|? |
| 24 |
manual |
| 25 |
retry=i |
| 26 |
debug |
| 27 |
) |
| 28 |
); |
| 29 |
|
| 30 |
# Display version if requested |
| 31 |
die ECMWF::ECaccess->VERSION . "\n" if ( $opt{version} ); |
| 32 |
|
| 33 |
my $targetLocalDirectory = pop(@ARGV); |
| 34 |
|
| 35 |
pod2usage( -noperldoc => 1, -exit => 1, verbose => 1 ) if ( $opt{help} ); |
| 36 |
pod2usage( -noperldoc => 1, -exit => 1, verbose => 2 ) if ( $opt{manual} ); |
| 37 |
pod2usage( -noperldoc => 1, -exit => 1, verbose => 0, -msg => "No source-ecaccess-file(s) specified!\n" ) if not(@ARGV) and not($targetLocalDirectory); |
| 38 |
pod2usage( -noperldoc => 1, -exit => 1, verbose => 0, -msg => "No target-local-directory specified!\n" ) if not(@ARGV); |
| 39 |
pod2usage( -noperldoc => 1, -exit => 1, verbose => 0, -msg => "Target-local-directory is not a directory!\n" ) if not( -d $targetLocalDirectory ); |
| 40 |
|
| 41 |
# Create the ECaccess Controler |
| 42 |
my $ecaccess = ECMWF::ECaccess->new( $opt{retry}, $opt{debug}); |
| 43 |
|
| 44 |
# Get the Token (using the Certificate in $HOME) |
| 45 |
my $token = $ecaccess->getToken(); |
| 46 |
|
| 47 |
# Get the Control Channel |
| 48 |
my $controlChannel = $ecaccess->getControlChannel(); |
| 49 |
|
| 50 |
# Process all source Files from the command-line |
| 51 |
foreach (@ARGV) { |
| 52 |
my $sourceFiles = $_; |
| 53 |
|
| 54 |
# Get the list of Files to download |
| 55 |
my $files = $controlChannel->getDirList( $token, $sourceFiles ); |
| 56 |
|
| 57 |
# Download each File |
| 58 |
foreach my $file ( $files->valueof('//getDirListResponse/return') ) { |
| 59 |
|
| 60 |
# Set source and target filenames |
| 61 |
my $source = $file->{domain} . "/" . $file->{name}; |
| 62 |
my $target = $targetLocalDirectory . "/" . $file->{name}; |
| 63 |
|
| 64 |
# Do we upload this file? (don't process directories and symbolic links) |
| 65 |
if ( not( substr( $file->{permissions}, 0, 1 ) eq 'd' ) |
| 66 |
&& not( substr( $file->{permissions}, 0, 1 ) eq 'l' ) |
| 67 |
&& $file->{size} >= '0' |
| 68 |
&& ( $opt{force} || &prompt( "y", "Download " . $source, "y/n", "y" ) ) ) |
| 69 |
{ |
| 70 |
print "Downloading " . $source . " -> " . $target . " (" . ( format_bytes( $file->{size} ) ) . ") ...\n"; |
| 71 |
|
| 72 |
# Get the file handle and open the target file |
| 73 |
$handle = $controlChannel->getInputFileHandle( $token, $source, 0 )->result; |
| 74 |
open FILE, ">", $target or die "Error creating file: " . $target . "\n"; |
| 75 |
|
| 76 |
# Progress bar |
| 77 |
my $progressbar; |
| 78 |
my $readCount = 0; |
| 79 |
my $readTotal = 0; |
| 80 |
if ( $opt{progress} && not( $^O =~ /^MSWin/ ) ) { |
| 81 |
eval "use Term::ProgressBar"; |
| 82 |
$progressbar = Term::ProgressBar->new( { count => $file->{size}, remove => 1 } ); |
| 83 |
$progressbar->update(0); |
| 84 |
} |
| 85 |
|
| 86 |
# Open the source file |
| 87 |
my $socket = $ecaccess->getFileInputStream($handle); |
| 88 |
|
| 89 |
# Read the file |
| 90 |
while (1) { |
| 91 |
$readCount = $socket->read_entity_body( $data, $opt{bufsize} ); |
| 92 |
die "Error downloading file: " . $target . "\n" unless defined $readCount; |
| 93 |
last unless $readCount; |
| 94 |
$readTotal += $readCount; |
| 95 |
print FILE $data; |
| 96 |
$progressbar->update($readTotal) if ( $opt{progress} && not( $^O =~ /^MSWin/ ) ); |
| 97 |
} |
| 98 |
|
| 99 |
# Close the file handles |
| 100 |
$controlChannel->closeHandle($handle); |
| 101 |
close FILE; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
# Logout |
| 107 |
END { |
| 108 |
$ecaccess->releaseToken($token) if ($token); |
| 109 |
} |
| 110 |
|
| 111 |
__END__ |
| 112 |
|
| 113 |
=head1 NAME |
| 114 |
|
| 115 |
ecaccess-file-mget - Download Multiple ECaccess Files at once |
| 116 |
|
| 117 |
=head1 SYNOPSIS |
| 118 |
|
| 119 |
B<ecaccess-file-mget -version|-help|-manual> |
| 120 |
|
| 121 |
B<ecaccess-file-mget [-debug] [-progress] [-force] [-bufsize> I<length>B<]> I<source-ecaccess-file> B<[>I<...>B<]> I<target-local-directory> |
| 122 |
|
| 123 |
=head1 DESCRIPTION |
| 124 |
|
| 125 |
Allow downloading Multiple ECaccess Files at once in the specified I<target-local-directory>. |
| 126 |
|
| 127 |
Each I<source-ecaccess-file> is in the form [domain:][/user-id/]path. Please read the "Shell commands -> File Management" |
| 128 |
section of the "ecaccess" guide for more information on the ECaccess File System. |
| 129 |
|
| 130 |
Please note this command is not recursive and will only download the plain-files in the I<source-ecaccess-file> |
| 131 |
directory (sub-directories are ignored). |
| 132 |
|
| 133 |
=head1 ARGUMENTS |
| 134 |
|
| 135 |
=over 8 |
| 136 |
|
| 137 |
=item I<source-ecaccess-file> B<[>I<...>B<]> |
| 138 |
|
| 139 |
The name(s) of the source ECaccess File(s). |
| 140 |
|
| 141 |
=item I<target-local-directory> |
| 142 |
|
| 143 |
The name of the target Local Directory. |
| 144 |
|
| 145 |
=back |
| 146 |
|
| 147 |
=head1 OPTIONS |
| 148 |
|
| 149 |
=over 8 |
| 150 |
|
| 151 |
=item B<-progress> |
| 152 |
|
| 153 |
Provide a progress meter on the standard terminal, allowing to monitor the file |
| 154 |
transmission in real-time. The progress bar is removed from the terminal when |
| 155 |
the transmission is done. This option is not supported and ignored on Windows |
| 156 |
platforms. |
| 157 |
|
| 158 |
=item B<-force> |
| 159 |
|
| 160 |
Overrides the interactive mode and download each file without prompting. |
| 161 |
|
| 162 |
=item B<-bufsize> I<length> |
| 163 |
|
| 164 |
Specify the I<length> of the buffer (in bytes) which is used to download the files. |
| 165 |
The larger the buffer the smaller the number of http/s requests. By default a |
| 166 |
buffer of 1048576 bytes (1MB) is used. |
| 167 |
|
| 168 |
=item B<-version> |
| 169 |
|
| 170 |
Display version number and exits. |
| 171 |
|
| 172 |
=item B<-help> |
| 173 |
|
| 174 |
Print a brief help message and exits. |
| 175 |
|
| 176 |
=item B<-manual> |
| 177 |
|
| 178 |
Prints the manual page and exits. |
| 179 |
|
| 180 |
=item B<-retry> I<count> |
| 181 |
|
| 182 |
Number of SSL connection retries per 5s to ECMWF. This parameter only apply to the |
| 183 |
initial SSL connection initiated by the command to the ECMWF server. It does not |
| 184 |
apply to all the subsequent requests made afteward as it is mainly targeting errors |
| 185 |
that can happen from time to time during the SSL handshake. Default is no retry. |
| 186 |
|
| 187 |
=item B<-debug> |
| 188 |
|
| 189 |
Display the SOAP and SSL messages exchanged. |
| 190 |
|
| 191 |
=back |
| 192 |
|
| 193 |
=head1 EXAMPLES |
| 194 |
|
| 195 |
B<ecaccess-file-mget> I<'home:/xyz/bin/*.bin'> I<$HOME/bin> |
| 196 |
|
| 197 |
Download the I<*.bin> Files in the $HOME/bin directory of the user xyz. The Files are downloaded |
| 198 |
in the local $HOME/bin directory. |
| 199 |
|
| 200 |
=head1 SEE ALSO |
| 201 |
|
| 202 |
B<ecaccess-file-delete>, B<ecaccess-file-get>, B<ecaccess-file-chmod>, B<ecaccess-file-modtime>, B<ecaccess-file-mput>, |
| 203 |
B<ecaccess-file-rmdir>, B<ecaccess-file-copy>, B<ecaccess-file-dir>, B<ecaccess-file-mdelete>, B<ecaccess-file-mkdir>, |
| 204 |
B<ecaccess-file-move>, B<ecaccess-file-put>, B<ecaccess-file-size> and B<ecaccess>. |
| 205 |
|
| 206 |
=cut |