Debian Codegraph @

script/ecaccess-file-put

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