Debian Codegraph @

script/ecaccess-file-get

raw
1 #!/usr/bin/perl -w
2 #
3 # ecaccess-file-get: Download an ECaccess File
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
12 my %opt = ( progress => 0, offset => 0, bufsize => 1048576, version => 0, help => 0, manual => 0, retry => 0, debug => 0 );
13
14 pod2usage( -noperldoc => 1, -exit => 1, verbose => 1 ) if !GetOptions(
15 \%opt,
16 qw(
17 progress
18 offset=i
19 bufsize=i
20 version
21 help|?
22 manual
23 retry=i
24 debug
25 )
26 );
27
28 # Display version if requested
29 die ECMWF::ECaccess->VERSION . "\n" if ( $opt{version} );
30
31 my $sourceECaccessFile = $ARGV[0];
32 my $targetLocalFile = $ARGV[1];
33
34 pod2usage( -noperldoc => 1, -exit => 1, verbose => 1 ) if ( $opt{help} );
35 pod2usage( -noperldoc => 1, -exit => 1, verbose => 2 ) if ( $opt{manual} );
36 pod2usage( -noperldoc => 1, -exit => 1, verbose => 0, -msg => "No source-ecaccess-file specified!\n" ) if not($sourceECaccessFile);
37
38 # Create the ECaccess Controler
39 my $ecaccess = ECMWF::ECaccess->new( $opt{retry}, $opt{debug});
40
41 # Get the Token (using the Certificate in $HOME)
42 my $token = $ecaccess->getToken();
43
44 # Get the Control Channel
45 my $controlChannel = $ecaccess->getControlChannel();
46
47 # Get the details of the remote file
48 my @files = $controlChannel->getDirList( $token, $sourceECaccessFile, 'false' )->valueof('//getDirListResponse/return');
49 my $size;
50
51 # Reject if it is not a regular file
52 die "Source-ecaccess-file is not a regular file!\n" if not( @files eq '1' ) or ( $size = $files[0]->{size} ) eq '-1';
53
54 # If no target-file is specified then take the source-file name
55 $targetLocalFile = $files[0]->{name} if not($targetLocalFile);
56
57 # Get the file handle
58 $handle = $controlChannel->getInputFileHandle( $token, $sourceECaccessFile, $opt{offset} )->result;
59
60 # Open the target file
61 open FILE, ">", $targetLocalFile or die "Error creating file: " . $targetLocalFile . "\n";
62 binmode FILE;
63
64 # Open the source file
65 my $socket = $ecaccess->getFileInputStream($handle);
66
67 # Progress bar
68 my $progressbar;
69 my $readCount = 0;
70 my $readTotal = 0;
71 if ( $opt{progress} && not( $^O =~ /^MSWin/ ) ) {
72 eval "use Term::ProgressBar";
73 $progressbar = Term::ProgressBar->new( { count => $size, remove => 1 } );
74 $progressbar->update(0);
75 }
76
77 # Read the file
78 while (1) {
79 $readCount = $socket->read_entity_body( $data, $opt{bufsize} );
80 die "Error downloading file: " . $targetLocalFile . "\n" unless defined $readCount;
81 last unless $readCount;
82 $readTotal += $readCount;
83 print FILE $data;
84 $progressbar->update($readTotal) if ( $opt{progress} && not( $^O =~ /^MSWin/ ) );
85 }
86
87 # Close the file handles
88 $controlChannel->closeHandle($handle);
89 close FILE;
90
91 # Logout
92 END {
93 $ecaccess->releaseToken($token) if ($token);
94 }
95
96 __END__
97
98 =head1 NAME
99
100 ecaccess-file-get - Download an ECaccess File
101
102 =head1 SYNOPSIS
103
104 B<ecaccess-file-get -version|-help|-manual>
105
106 B<ecaccess-file-get [-debug] [-progress] [-offset> I<number>B<] [-bufsize> I<length>B<]> I<source-ecaccess-file> B<[>I<target-local-file>B<]>
107
108 =head1 DESCRIPTION
109
110 Allow downloading the I<source-ecaccess-file>. If no I<target-local-file> is specified then the
111 I<source-ecaccess-file> name is used.
112
113 The I<source-ecaccess-file> is in the form [domain:][/user-id/]path. Please read the "Shell commands -> File Management"
114 section of the "ecaccess" guide for more information on the ECaccess File System.
115
116 =head1 ARGUMENTS
117
118 =over 8
119
120 =item I<source-ecaccess-file>
121
122 The name of the Source ECaccess File.
123
124 =item I<target-local-file> (optional)
125
126 The name of the Target Local File.
127
128 =back
129
130 =head1 OPTIONS
131
132 =over 8
133
134 =item B<-progress>
135
136 Provide a progress meter on the standard terminal, allowing to monitor the file
137 transmission in real-time. The progress bar is removed from the terminal when
138 the transmission is done. This option is not supported and ignored on Windows
139 platforms.
140
141 =item B<-offset> I<number>
142
143 Every open file has an associated file offset, which determines where the next
144 read operation will start. This I<number> is set to 0 by default.
145
146 =item B<-bufsize> I<length>
147
148 Specify the I<length> of the buffer (in bytes) which is used to download the file.
149 The larger the buffer the smaller the number of http/s requests. By default a
150 buffer of 1048576 bytes (1MB) is used.
151
152 =item B<-version>
153
154 Display version number and exits.
155
156 =item B<-help>
157
158 Print a brief help message and exits.
159
160 =item B<-manual>
161
162 Prints the manual page and exits.
163
164 =item B<-retry> I<count>
165
166 Number of SSL connection retries per 5s to ECMWF. This parameter only apply to the
167 initial SSL connection initiated by the command to the ECMWF server. It does not
168 apply to all the subsequent requests made afteward as it is mainly targeting errors
169 that can happen from time to time during the SSL handshake. Default is no retry.
170
171 =item B<-debug>
172
173 Display the SOAP and SSL messages exchanged.
174
175 =back
176
177 =head1 EXAMPLES
178
179 B<ecaccess-file-get -progress> I<bin/a.out>
180
181 Download the I<a.out> File in the $HOME/bin directory of the authenticated user. The Target File Name
182 is I<a.out>. A progress bar is displayed during the transmission.
183
184 =head1 SEE ALSO
185
186 B<ecaccess-file-delete>, B<ecaccess-file-chmod>, B<ecaccess-file-mget>, B<ecaccess-file-modtime>, B<ecaccess-file-mput>,
187 B<ecaccess-file-rmdir>, B<ecaccess-file-copy>, B<ecaccess-file-dir>, B<ecaccess-file-mdelete>, B<ecaccess-file-mkdir>,
188 B<ecaccess-file-move>, B<ecaccess-file-put>, B<ecaccess-file-size> and B<ecaccess>.
189
190 =cut