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