PML/IO.pm

filter: Statementstmt 44.7% 195 / 436 Branchbran 20.5% 58 / 282 Conditioncond 15.7% 29 / 184 MC/DCmcdc 4.7% 5 / 106 Subroutinesub 60.6% 37 / 61 Podpod 96.1% 25 / 26 totaltotal 31.8% 349 / 1095 CC 207 SCAR 44.5
CC
207
Cov
31%
_fetch_file_posix
342.0
_get_upload_fh_posix
272.0
is_same_filename
182.0
CRAP 85.6
« PML/Schema/Seq.pm ↑ Summary PML/Backend/TrXML.pm »
1 # -*- cperl -*-
2  
3 =head1 NAME
4  
5 Treex::PML::IO - I/O support functions used by Treex::PML
6  
7 =head1 DESCRIPTION
8  
9 This module implements various I/O and filesystem related functions
10 used by L<Treex::PML>.
11  
12 The current implementation supports the following protocols for
13 reading:
14  
15 http, https, ftp, gopher, news - reading (POSIX and Windows)
16  
17 ssh, fish, sftp - reading/writing on POSIX systems via secure shell copy
18 or the kioclient from KDE.
19  
20 The module attempts to handle GNU Zip-compressed files (suffix .gz)
21 transparently.
22  
23 =head1 FUNCTIONS
24  
25 =cut
26  
27 package Treex::PML::IO;
28 9 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use File::Temp 0.14 qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶use IO::Pipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 9 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 9 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 9 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 9 ▶use vars qw(@ISA $VERSION @EXPORT @EXPORT_OK
Subroutine BEGIN: called CC=1 SCAR=0.0
46 %UNLINK_ON_CLOSE
47 $Debug
48 $kioclient $kioclient_opts
49 $ssh $ssh_opts
50 $curl $curl_opts
51 $gzip $gzip_opts
52 $zcat $zcat_opts
53 $reject_proto
54 $lwp_user_agent
55 9 );
56  
57 0 ▶sub DOES {
Subroutine DOES: not called CC=3 SCAR=24.8
Pod undocumented
58 0 my ($self,$role)=@_;
59 0 ▶ if ($role eq 'IO' or $role eq __PACKAGE__) { # backward compatibility
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq 'IO' or $role eq __PACKAGE__) { } 0 0 0
Condition0%0/3
$role eq "IO" or $role eq __PACKAGE__
l!l&&r!l&&!r
000
MC/DC0%0/2
$role eq "IO" or $role eq __PACKAGE__
$role eq "IO" $role eq __PACKAGE__
Truth table0%0/3
A or B
A= $role eq "IO"B= $role eq __PACKAGE__
ABresult
1X1
011
000
60 0 return 1;
61 } else {
62 0 return $self->SUPER::DOES($role);
63 }
64 }
65  
66 {
67 package Treex::PML::IO::UserAgent;
68 9 ▶ use base qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
69 }
70  
71 #$Debug=0;
72 9 my %input_protocol_handler;
73  
74 9 ▶BEGIN {
Subroutine BEGIN: called CC=9 SCAR=25.9
75 *_find_exe = eval {
76 9 require File::Which;
77 9 \&File::Which::which
78 9 ▶ } || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
eval { do { require File::Which; \&File::Which::which } } || sub { }
l!l&&r!l&&!r
900
MC/DC0%0/2
eval { do { require File::Which; \&File::Which::which } } || sub { }
eval { do { require File::Which; \&File::Which::which } } sub { }
Truth table33%1/3
A || B
A= eval { do { require File::Which; \&File::Which::which } }B= sub { }
ABresult
1X1
011
000
79  
80 9 $VERSION = '2.29'; # version template
81 9 @ISA=qw(Exporter);
82 9 @EXPORT_OK = qw($kioclient $kioclient_opts
83 $ssh $ssh_opts
84 $curl $curl_opts
85 $gzip $gzip_opts
86 $zcat $zcat_opts
87 &set_encoding
88 &open_backend &open_uri &close_backend &close_uri
89 &get_protocol &quote_filename
90 &rename_uri);
91  
92 9 ▶ $zcat ||= _find_exe('zcat');
Condition33%1/3
$Treex::PML::IO::zcat ||= _find_exe("zcat")
l!l&&r!l&&!r
090
MC/DC0%0/2
$Treex::PML::IO::zcat ||= _find_exe("zcat")
$Treex::PML::IO::zcat _find_exe("zcat")
Truth table33%1/3
A ||= B
A= $Treex::PML::IO::zcatB= _find_exe("zcat")
ABresult
1X1
011
000
93 9 ▶ $gzip ||= _find_exe('gzip');
Condition33%1/3
$Treex::PML::IO::gzip ||= _find_exe("gzip")
l!l&&r!l&&!r
090
MC/DC0%0/2
$Treex::PML::IO::gzip ||= _find_exe("gzip")
$Treex::PML::IO::gzip _find_exe("gzip")
Truth table33%1/3
A ||= B
A= $Treex::PML::IO::gzipB= _find_exe("gzip")
ABresult
1X1
011
000
94 9 ▶ $kioclient ||= _find_exe('kioclient');
Condition33%1/3
$Treex::PML::IO::kioclient ||= _find_exe("kioclient")
l!l&&r!l&&!r
009
MC/DC0%0/2
$Treex::PML::IO::kioclient ||= _find_exe("kioclient")
$Treex::PML::IO::kioclient _find_exe("kioclient")
Truth table33%1/3
A ||= B
A= $Treex::PML::IO::kioclientB= _find_exe("kioclient")
ABresult
1X1
011
000
95 9 ▶ $ssh ||= _find_exe('ssh');
Condition33%1/3
$Treex::PML::IO::ssh ||= _find_exe("ssh")
l!l&&r!l&&!r
009
MC/DC0%0/2
$Treex::PML::IO::ssh ||= _find_exe("ssh")
$Treex::PML::IO::ssh _find_exe("ssh")
Truth table33%1/3
A ||= B
A= $Treex::PML::IO::sshB= _find_exe("ssh")
ABresult
1X1
011
000
96 9 ▶ $curl ||= _find_exe('curl');
Condition33%1/3
$Treex::PML::IO::curl ||= _find_exe("curl")
l!l&&r!l&&!r
090
MC/DC0%0/2
$Treex::PML::IO::curl ||= _find_exe("curl")
$Treex::PML::IO::curl _find_exe("curl")
Truth table33%1/3
A ||= B
A= $Treex::PML::IO::curlB= _find_exe("curl")
ABresult
1X1
011
000
97 9 ▶ $ssh_opts ||= '-C';
Condition50%1/2
$Treex::PML::IO::ssh_opts ||= "-C"
l!l
09
MC/DC0%0/1
$Treex::PML::IO::ssh_opts ||= "-C"
$Treex::PML::IO::ssh_opts
Truth table50%1/2
A
A= $Treex::PML::IO::ssh_opts
Aresult
11
00
98 9 ▶ $reject_proto ||= '^(pop3?s?|imaps?)\$';
Condition50%1/2
$Treex::PML::IO::reject_proto ||= "^(pop3?s?|imaps?)\\\$"
l!l
09
MC/DC0%0/1
$Treex::PML::IO::reject_proto ||= "^(pop3?s?|imaps?)\\\$"
$Treex::PML::IO::reject_proto
Truth table50%1/2
A
A= $Treex::PML::IO::reject_proto
Aresult
11
00
99 9 $lwp_user_agent = Treex::PML::IO::UserAgent->new(keep_alive=>1);
100 9 $lwp_user_agent->agent("Treex::PML_IO/$VERSION");
101 };
102  
103  
104 =over 4
105  
106 =item C<DirPart($path)>
107  
108 Returns directory part of a given path (including volume).
109  
110 =cut
111  
112 11 ▶sub DirPart {
Subroutine DirPart: called CC=1 SCAR=0.0
Pod documented
113 11 return File::Spec->catpath(
114 (File::Spec->splitpath($_[0]))[0,1],''
115 );
116 }
117  
118 =item C<CallerDir($rel_path?)>
119  
120 If called without an argument, returns the directory of the perl
121 module or macro-file that invoked this macro.
122  
123 If a relative path is given as an argument, a respective absolute path
124 is computed based on the caller's directory and returned.
125  
126 =cut
127  
128 11 ▶sub CallerDir {
Subroutine CallerDir: called CC=2 SCAR=7.6
Pod documented
129 return
130 11 ▶ @_>0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 ? : 11 0 11
131 ? File::Spec->rel2abs($_[0], DirPart( (caller)[1] ))
132 : DirPart( (caller)[1] );
133 }
134  
135 =item C<register_input_protocol_handler($scheme,$callback)>
136  
137 Register a callback to fetch URIs of a given protocol. C<$scheme> is
138 the URI scheme of the protocol (i.e. the first part of an URI
139 preceding the comma, e.g. 'ftp' or 'https'). <$callback> is either a
140 CODE reference or an ARRAY reference whose first element is a CODE
141 reference and the other elements are additional arguments to be passed
142 to the callback prior to the standard arguments.
143  
144 When the library attempts to fetch a resource from an URI matching
145 given scheme, the callback is invoked with the (optional) user
146 parameters followed by the URI.
147  
148 The callback function must either return a new URI (typically a
149 file:// URI pointing to a temporary file) and a boolean flag
150 indicating whether the library should attempt to delete the
151 returned file after it finished reading.
152  
153 If the callback returns the same or another URI with the C<$scheme>,
154 the callback is not reinvoked, but passed on to further processing
155 (i.e. by Treex::PML I/O backends).
156  
157 =cut
158  
159 0 ▶sub register_input_protocol_handler {
Subroutine register_input_protocol_handler: not called CC=4 SCAR=30.0
Pod documented
160 0 my ($proto,$handler)=@_;
161 0 ▶ if (ref($handler) eq 'CODE' or ref($handler) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq 'CODE' or ref $handler eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $handler eq "CODE" or ref $handler eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $handler eq "CODE" or ref $handler eq "ARRAY"
ref $handler eq "CODE" ref $handler eq "ARRAY"
Truth table0%0/3
A or B
A= ref $handler eq "CODE"B= ref $handler eq "ARRAY"
ABresult
1X1
011
000
162 0 ▶ if (exists($input_protocol_handler{$proto})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $input_protocol_handler{$proto}) 0 0 0
163 0 carp(__PACKAGE__."::register_input_protocol_handler: WARNING: redefining protocol handler for '$proto'");
164 }
165 0 $input_protocol_handler{$proto}=$handler;
166 } else {
167 0 croak("Wrong arguments. Usage: Treex::PML::IO::register_input_protocol_handler(protocol=>callback)");
168 }
169 }
170  
171 =item unregister_input_protocol_handler($scheme)
172  
173 Unregister a handler for a given URI scheme.
174  
175 =cut
176  
177 0 ▶sub unregister_input_protocol_handler {
Subroutine unregister_input_protocol_handler: not called CC=1 SCAR=6.9
Pod documented
178 0 my ($proto)=@_;
179 0 return delete $input_protocol_handler{$proto};
180 }
181  
182 =item get_input_protocol_handler($scheme)
183  
184 Returns the user-defined handler registered for a given URI scheme; if
185 none, undef is returned.
186  
187 =cut
188  
189 0 ▶sub get_input_protocol_handler {
Subroutine get_input_protocol_handler: not called CC=1 SCAR=6.9
Pod documented
190 0 my ($proto)=@_;
191 0 return $input_protocol_handler{$proto};
192 }
193  
194 =item set_encoding($filehandle, $encoding)
195  
196 Safely resets Perl I/O-layer on a given filehandle to decode or encode
197 from/to a given encoding. This is equivalent to:
198  
199 binmode($filehandle,":raw:perlio:encoding($encoding)");
200  
201 except that errors are turned into warnings.
202  
203 =cut
204  
205 347 ▶sub set_encoding {
Subroutine set_encoding: called CC=5 SCAR=21.6
Pod documented
206 347 my ($fh,$encoding) = @_;
207 9 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 347 ▶ if (defined($fh) and defined($encoding) and ($]>=5.008)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh and defined $encoding and $] >= "5.008") 0 347 347
Condition33%2/6
defined $fh and defined $encoding and $] >= "5.008"
!ll&&!rl&&r
34700
defined $fh and defined $encoding
!ll&&!rl&&r
03470
MC/DC0%0/3
defined $fh and defined $encoding and $] >= "5.008"
defined $fh defined $encoding $] >= "5.008"
Truth table25%1/4
A and B and C
A= defined $fhB= defined $encodingC= $] >= "5.008"
ABCresult
0XX0
10X0
1100
1111
209 0 eval {
210 0 binmode($fh,":raw:perlio:encoding($encoding)");
211 };
212 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
213 }
214 347 return $fh;
215 }
216  
217 =item get_protocol($filename_or_URI)
218  
219 If the argument is a filename, returns 'file'; if the argument is an
220 URI, returns the URI's scheme. Note: unless the argument is an URI
221 object, a heuristic is used to determine the scheme. To avoid
222 reporting Windows drive names as URI schemes, only URI schemes
223 consisting of at least two characters are supported, i.e. C:foo is
224 considered a file name wheres CC:foo would be an URI with the scheme
225 'CC'.
226  
227 =cut
228  
229 # to avoid collision with Win32 drive-names, we only support protocols
230 # with at least two letters
231 460 ▶sub get_protocol {
Subroutine get_protocol: called CC=5 SCAR=16.1
Pod documented
232 460 my ($uri) = @_;
233 460 ▶ if (blessed($uri) and $uri->isa('URI')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri) and $uri->isa("URI")) 107 353 460
Condition66%2/3
builtin::blessed($uri) and $uri->isa("URI")
!ll&&!rl&&r
3530107
MC/DC50%1/2
builtin::blessed($uri) and $uri->isa("URI")
builtin::blessed($uri) $uri->isa("URI")
Truth table66%2/3
A and B
A= builtin::blessed($uri)B= $uri->isa("URI")
ABresult
0X0
100
111
234 107 ▶ return $uri->scheme || 'file';
Condition100%2/2
$uri->scheme || 'file'
l!l
1043
MC/DC100%1/1
$uri->scheme || 'file'
$uri->scheme
Truth table100%2/2
A
A= $uri->scheme
Aresult
11
00
235 }
236 353 ▶ if ($uri =~ m{^\s*([[:alnum:]][[:alnum:]]+):}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /^\s*([[:alnum:]][[:alnum:]]+):/) { } 26 327 353
237 26 return $1;
238 } else {
239 327 return 'file';
240 }
241 }
242  
243 =item quote_filename($string)
244  
245 Returns given string in shell-quotes with special characters (\, $, ")
246 escaped.
247  
248 =cut
249  
250 0 ▶sub quote_filename {
Subroutine quote_filename: not called CC=1 SCAR=6.9
Pod documented
251 0 my ($uri)=@_;
252 0 $uri =~ s{\\}{\\\\}g;
253 0 $uri =~ s{\$}{\\\$}g;
254 0 $uri =~ s{"}{\\"}g;
255 0 return '"'.$uri.'"';
256 }
257  
258 =item get_filename($URI_or_filename)
259  
260 Upgrades given string to an URI and if the resulting URI is in the
261 'file' scheme (e.g. file:///bar/baz), returns the file-name portion of
262 the URI (e.g. /bar/baz). Otherwise returns nothing.
263  
264 =cut
265  
266 501 ▶sub get_filename {
Subroutine get_filename: called CC=3 SCAR=11.2
Pod documented
267 501 my ($uri)=@_;
268 501 $uri=make_URI($uri); # cast to URI or make a copy
269 501 ▶ $uri->scheme('file') if !$uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme 142 359 501
270 501 ▶ if ($uri->scheme eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme eq "file") 501 0 501
271 501 return $uri->file;
272 }
273 0 return;
274 }
275  
276 =item make_abs_URI($URL_or_filename)
277  
278 Upgrades a given string (URL or filename) into an URI object with
279 absolute path (relative URIs are resolved using the current working
280 directory obtained via Cwd::getcwd())
281  
282 =cut
283  
284 226 ▶sub make_abs_URI {
Subroutine make_abs_URI: called CC=3 SCAR=11.2
Pod documented
285 226 my ($url)=@_;
286 226 my $uri = make_URI($url);
287 226 my $cwd = getcwd();
288 226 ▶ $cwd = VMS::Filespec::unixpath($cwd) if $^O eq 'VMS';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "VMS" 0 226 226
289 226 $cwd = URI::file->new($cwd);
290 226 ▶ $cwd .= "/" unless substr($cwd, -1, 1) eq "/";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($cwd, -1, 1) eq "/" 226 0 226
291 226 return $uri->abs($cwd);
292 }
293  
294 =item make_URI($URL_or_filename)
295  
296 Upgrades a given string (URL or filename) into an URI object.
297  
298 =cut
299  
300 1117 ▶sub make_URI {
Subroutine make_URI: called CC=6 SCAR=18.5
Pod documented
301 1117 my ($url)=@_;
302 1117 my $uri = URI->new($url);
303 1117 ▶ return $uri if blessed($url) and $url->isa('URI'); # return a copy if was URI already
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($url) and $url->isa("URI") 734 383 1117
Condition66%2/3
builtin::blessed($url) and $url->isa("URI")
!ll&&!rl&&r
3830734
MC/DC50%1/2
builtin::blessed($url) and $url->isa("URI")
builtin::blessed($url) $url->isa("URI")
Truth table66%2/3
A and B
A= builtin::blessed($url)B= $url->isa("URI")
ABresult
0X0
100
111
304 383 ▶ if (($uri eq $url or URI::Escape::uri_unescape($uri) eq $url)
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq $url || URI::Escape::uri_unescape($uri) eq $url and $url =~ m[^\s*[[:alnum:]]+://]) { } 27 356 383
Condition50%3/6
$uri eq $url || URI::Escape::uri_unescape($uri) eq $url and $url =~ m[^\s*[[:alnum:]]+://]
!ll&&!rl&&r
035627
$uri eq $url or URI::Escape::uri_unescape($uri) eq $url
l!l&&r!l&&!r
38300
MC/DC33%1/3
$uri eq $url || URI::Escape::uri_unescape($uri) eq $url and $url =~ m[^\s*[[:alnum:]]+://]
$uri eq $url URI::Escape::uri_unescape($uri) eq $url $url =~ m[^\s*[[:alnum:]]+://]
Truth table40%2/5
A or B and C
A= $uri eq $urlB= URI::Escape::uri_unescape($uri) eq $urlC= $url =~ m[^\s*[[:alnum:]]+://]
ABCresult
00X0
1X00
0100
1X11
0111
305 and $url =~ m(^\s*[[:alnum:]]+://)) { # looks like it is URL already
306 27 return $uri;
307 } else {
308 356 return URI::file->new($url);
309 }
310 }
311  
312 =item make_relative_URI($URL,$baseURI)
313  
314 Returns a relative URI based in a given base URI. The arguments
315 are automatically upgraded using make_URI() if necessary.
316  
317 =cut
318  
319 32 ▶sub make_relative_URI {
Subroutine make_relative_URI: called CC=3 SCAR=12.0
Pod documented
320 32 my ($href,$base)=@_;
321 # if (Treex::PML::_is_url($href)) {
322 32 ▶ $href = URI->new(make_URI($href)) unless blessed($href) and $href->isa('URI');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($href) and $href->isa("URI") 0 32 32
Condition33%1/3
builtin::blessed($href) and $href->isa("URI")
!ll&&!rl&&r
0032
MC/DC0%0/2
builtin::blessed($href) and $href->isa("URI")
builtin::blessed($href) $href->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($href)B= $href->isa("URI")
ABresult
0X0
100
111
323 32 $base = make_URI($base);
324 ### $href = $href->abs($base)->rel($base);
325 32 $href = $href->rel($base);
326 }
327  
328 =item strip_protocol($URI)
329  
330 Returns the scheme-specific part of the URI (everything between the
331 scheme and the fragment). If the scheme of the URI was 'file', returns
332 the URI as a file name.
333  
334 =cut
335  
336 0 ▶sub strip_protocol {
Subroutine strip_protocol: not called CC=3 SCAR=24.8
Pod documented
337 0 my ($uri)=@_;
338 0 $uri=make_URI($uri); # make a copy
339 0 ▶ $uri->scheme('file') if !$uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme 0 0 0
340 0 ▶ if ($uri->scheme eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme eq "file") 0 0 0
341 0 return $uri->file;
342 }
343 0 return $uri->opaque;
344 }
345  
346 # =item is_gzip($filename)
347  
348 # Auxiliary:
349 # Returns true if the filename ends with the suffix .gz or .gz~.
350  
351 # =cut
352  
353 403 ▶sub _is_gzip {
Subroutine _is_gzip: called CC=2 SCAR=6.9
354 403 ▶ ($_[0] =~/.gz~?$/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /.gz~?$/ ? : 20 383 403
355 }
356  
357 =item is_same_filename($URI_1,$URI_2)
358  
359 Checks if $URI_1 and $URI_2 point to the same resource. For filenames
360 and URIs in the 'file' scheme checks that the referred files (if
361 exist) are the same using is_same_file(); for other schemes simply
362 checks for string equality on canonical versions of the URIs (see
363 URI->canonical).
364  
365 =cut
366  
367 0 ▶sub is_same_filename {
Subroutine is_same_filename: not called CC=13 SCAR=52.0
Pod documented
368 0 my ($f1,$f2)=@_;
369 0 ▶ return 1 if $f1 eq $f2;
Branch: 1 branch
BranchTrueFalseTotal
if $f1 eq $f2 0 0 0
370 0 ▶ my $u1 = (blessed($f1) and $f1->isa('URI')) ? $f1 : make_URI($f1);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($f1) && $f1->isa('URI') ? : 0 0 0
Condition0%0/3
builtin::blessed($f1) and $f1->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($f1) and $f1->isa("URI")
builtin::blessed($f1) $f1->isa("URI")
Truth table0%0/3
A and B
A= builtin::blessed($f1)B= $f1->isa("URI")
ABresult
0X0
100
111
371 0 ▶ my $u2 = (blessed($f2) and $f2->isa('URI')) ? $f2 : make_URI($f2);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($f2) && $f2->isa('URI') ? : 0 0 0
Condition0%0/3
builtin::blessed($f2) and $f2->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($f2) and $f2->isa("URI")
builtin::blessed($f2) $f2->isa("URI")
Truth table0%0/3
A and B
A= builtin::blessed($f2)B= $f2->isa("URI")
ABresult
0X0
100
111
372 0 ▶ return 1 if $u1 eq $u2;
Branch: 1 branch
BranchTrueFalseTotal
if $u1 eq $u2 0 0 0
373 0 ▶ return 1 if $u1->canonical eq $u2->canonical;
Branch: 1 branch
BranchTrueFalseTotal
if $u1->canonical eq $u2->canonical 0 0 0
374 0 ▶ if (!ref($f1) and !ref($f2) and $^O ne 'MSWin32' and -f $f1 and -f $f2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $f1 || ref $f2 and $^O ne "MSWin32" and -f $f1 and -f $f2) 0 0 0
Condition0%0/12
not ref $f1 || ref $f2 and $^O ne "MSWin32" and -f $f1 and -f $f2
!ll&&!rl&&r
000
not ref $f1 || ref $f2 and $^O ne "MSWin32" and -f $f1
!ll&&!rl&&r
000
not ref $f1 || ref $f2 and $^O ne "MSWin32"
!ll&&!rl&&r
000
ref $f1 || ref $f2
l!l&&r!l&&!r
000
MC/DC0%0/5
not ref $f1 || ref $f2 and $^O ne "MSWin32" and -f $f1 and -f $f2
ref $f1 ref $f2 $^O ne "MSWin32" -f $f1 -f $f2
Truth table0%0/6
not(A || B) and C and D and E
A= ref $f1B= ref $f2C= $^O ne "MSWin32"D= -f $f1E= -f $f2
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
375 0 return is_same_file($f1,$f2);
376 }
377 0 return 0;
378 }
379  
380 =item is_same_file($filename_1,$filename_2)
381  
382 Uses device and i-node numbers (reported by stat()) to check if the
383 two filenames point to the same file on the filesystem. Returns 1 if
384 yes, 0 otherwise.
385  
386 =cut
387  
388 0 ▶sub is_same_file {
Subroutine is_same_file: not called CC=5 SCAR=34.0
Pod documented
389 0 my ($f1,$f2) = @_;
390 0 ▶ return 1 if $f1 eq $f2;
Branch: 1 branch
BranchTrueFalseTotal
if $f1 eq $f2 0 0 0
391 0 my ($d1,$i1)=stat($f1);
392 0 my ($d2,$i2)=stat($f2);
393 0 ▶ return ($d1==$d2 and $i1!=0 and $i1==$i2) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d1 == $d2 && $i1 != 0 && $i1 == $i2 ? : 0 0 0
Condition0%0/6
$d1 == $d2 and $i1 != 0 and $i1 == $i2
!ll&&!rl&&r
000
$d1 == $d2 and $i1 != 0
!ll&&!rl&&r
000
MC/DC0%0/3
$d1 == $d2 and $i1 != 0 and $i1 == $i2
$d1 == $d2 $i1 != 0 $i1 == $i2
Truth table0%0/4
A and B and C
A= $d1 == $d2B= $i1 != 0C= $i1 == $i2
ABCresult
0XX0
10X0
1100
1111
394 }
395  
396 =item open_pipe($filename,$mode,$command)
397  
398 Returns a filehandle of a newly open pipe in a given mode.
399  
400 In write mode ($mode = 'w'), opens a writing pipe to a given
401 command redirecting the standard output of the command to a given
402 file. Moreover, if the last suffix of the $filename is '.gz' or
403 '.gz~', the output of the command is gzipped before saving to
404 $filename.
405  
406 In read mode ($mode = 'r'), opens a reading pipe to a given
407 command redirecting the content of the given file to the standard
408 input of the command. Moreover, if the last suffix of the $filename is
409 '.gz' or '.gz~', the output of the command is un-gzipped before it is passed
410 to the command.
411  
412 =cut
413  
414 0 ▶sub open_pipe {
Subroutine open_pipe: not called CC=10 SCAR=47.0
Pod documented
415 0 my ($file,$rw,$pipe) = @_;
416 0 my $fh;
417 0 ▶ if (_is_gzip($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_gzip($file)) { } 0 0 0
418 0 ▶ if (-x $gzip && -x $zcat) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $Treex::PML::IO::gzip and -x $Treex::PML::IO::zcat) { } 0 0 0
Condition0%0/3
-x $Treex::PML::IO::gzip and -x $Treex::PML::IO::zcat
!ll&&!rl&&r
000
MC/DC0%0/2
-x $Treex::PML::IO::gzip and -x $Treex::PML::IO::zcat
-x $Treex::PML::IO::gzip -x $Treex::PML::IO::zcat
Truth table0%0/3
A and B
A= -x $Treex::PML::IO::gzipB= -x $Treex::PML::IO::zcat
ABresult
0X0
100
111
419 0 ▶ if ($rw eq 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rw eq 'w') { } 0 0 0
420 0 ▶ open $fh, "| $pipe | $gzip $gzip_opts > ".quote_filename($file) || undef $fh;
Condition0%0/2
"| $pipe | $Treex::PML::IO::gzip $Treex::PML::IO::gzip_opts > " . quote_filename($file) || undef $fh
l!l
00
MC/DC0%0/1
"| $pipe | $Treex::PML::IO::gzip $Treex::PML::IO::gzip_opts > " . quote_filename($file) || undef $fh
"| $pipe | $Treex::PML::IO::gzip $Treex::PML::IO::gzip_opts > " . quote_filename($file)
Truth table0%0/2
A
A= "| $pipe | $Treex::PML::IO::gzip $Treex::PML::IO::gzip_opts > " . quote_filename($file)
Aresult
11
00
421 } else {
422 0 ▶ open $fh, "$zcat $zcat_opts < ".quote_filename($file)." | $pipe |" || undef $fh;
Condition0%0/2
"$Treex::PML::IO::zcat $Treex::PML::IO::zcat_opts < " . quote_filename($file) . " | $pipe |" || undef $fh
l!l
00
MC/DC0%0/1
"$Treex::PML::IO::zcat $Treex::PML::IO::zcat_opts < " . quote_filename($file) . " | $pipe |" || undef $fh
"$Treex::PML::IO::zcat $Treex::PML::IO::zcat_opts < " . quote_filename($file) . " | $pipe |"
Truth table0%0/2
A
A= "$Treex::PML::IO::zcat $Treex::PML::IO::zcat_opts < " . quote_filename($file) . " | $pipe |"
Aresult
11
00
423 }
424 } else {
425 0 warn "Need a functional gzip and zcat to open this file\n";
426 }
427 } else {
428 0 ▶ if ($rw eq 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rw eq 'w') { } 0 0 0
429 0 ▶ open $fh, "| $pipe > ".quote_filename($file) || undef $fh;
Condition0%0/2
"| $pipe > " . quote_filename($file) || undef $fh
l!l
00
MC/DC0%0/1
"| $pipe > " . quote_filename($file) || undef $fh
"| $pipe > " . quote_filename($file)
Truth table0%0/2
A
A= "| $pipe > " . quote_filename($file)
Aresult
11
00
430 } else {
431 0 ▶ open $fh, "$pipe < ".quote_filename($file)." |" || undef $fh;
Condition0%0/2
"$pipe < " . quote_filename($file) . ' |' || undef $fh
l!l
00
MC/DC0%0/1
"$pipe < " . quote_filename($file) . ' |' || undef $fh
"$pipe < " . quote_filename($file) . ' |'
Truth table0%0/2
A
A= "$pipe < " . quote_filename($file) . ' |'
Aresult
11
00
432 }
433 }
434 0 return $fh;
435 }
436  
437 # _open_file_zcat:
438 #
439 # Note: This function represents the original strategy used on POSIX
440 # systems. It turns out, however, that the calls to zcat/gzip cause
441 # serious penalty on btred when loading large amount of files and also
442 # cause the process' priority to lessen. It also turns out that we
443 # cannot use IO::Zlib filehandles directly with some backends, such as
444 # StorableBackend.
445 #
446 # I'm leaving the function here, but it is not used anymore.
447  
448 0 ▶sub _open_file_zcat {
Subroutine _open_file_zcat: not called CC=10 SCAR=47.0
449 0 my ($file,$rw) = @_;
450 0 my $fh;
451 0 ▶ if (_is_gzip($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_gzip($file)) { } 0 0 0
452 0 ▶ if (-x $gzip) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $Treex::PML::IO::gzip) 0 0 0
453 0 $fh = new IO::Pipe();
454 0 ▶ if ($rw eq 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rw eq 'w') { } 0 0 0
455 0 ▶ $fh->writer("$gzip $gzip_opts > ".quote_filename($file)) || undef $fh;
Branch: 1 branch
BranchTrueFalseTotal
$fh->writer("$Treex::PML::IO::gzip $Treex::PML::IO::gzip_opts > " . quote_filename($file)) or undef $fh 0 0 0
456 } else {
457 0 ▶ $fh->reader("$zcat $zcat_opts < ".quote_filename($file)) || undef $fh;
Branch: 1 branch
BranchTrueFalseTotal
$fh->reader("$Treex::PML::IO::zcat $Treex::PML::IO::zcat_opts < " . quote_filename($file)) or undef $fh 0 0 0
458 }
459 }
460 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
461 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require IO::Zlib; $fh = "IO::Zlib"->new } } or return 0 0 0
462 0 require IO::Zlib;
463 0 $fh = new IO::Zlib;
464 } || return;
465 0 ▶ $fh->open($file,$rw."b") || undef $fh;
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($file, $rw . 'b') or undef $fh 0 0 0
466 }
467 } else {
468 0 $fh = new IO::File();
469 0 ▶ $fh->open($file,$rw) || undef $fh;
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($file, $rw) or undef $fh 0 0 0
470 }
471 0 return $fh;
472 }
473  
474 =item open_file($filename,$mode)
475  
476 Opens a given file for reading ($mode = 'r') or writing ($mode =
477 'w'). If the last suffix of the filename is '.gz' or '.gz~', the data
478 are transparently un-gzipped (when reading) or gzipped (when writing).
479  
480 =cut
481  
482 215 ▶sub open_file {
Subroutine open_file: called CC=11 SCAR=33.7
Pod documented
483 215 my ($file,$rw) = @_;
484 215 my $fh;
485 215 ▶ if (_is_gzip($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_gzip($file)) { } 5 210 215
486 5 eval {
487 5 $fh = File::Temp->new(UNLINK => 1);
488 };
489 5 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
490 5 ▶ return unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 5 5
491 5 ▶ if ($rw eq 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rw eq 'w') { } 5 0 5
492 5 ▶ print STDERR __PACKAGE__.": Storing ZIPTOFILE: $rw\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 5 5
493 5 ${*$fh}{'ZIPTOFILE'}=$file;
494 } else {
495 0 my $tmp;
496 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } and $tmp or return 0 0 0
Condition0%0/3
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } and $tmp
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } and $tmp
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } $tmp
Truth table0%0/3
A and B
A= eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } }B= $tmp
ABresult
0X0
100
111
497 0 require IO::Zlib;
498 0 $tmp = new IO::Zlib();
499 } && $tmp || return;
500 0 ▶ $tmp->open($file,"rb") || return;
Branch: 1 branch
BranchTrueFalseTotal
$tmp->open($file, "rb") or return 0 0 0
501 0 my $buffer;
502 0 my $length = 1024*1024;
503 0 ▶ while (read($tmp,$buffer,$length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $tmp, $buffer, $length) 0 0 0
504 0 $fh->print($buffer);
505 }
506 0 $tmp->close();
507 0 seek($fh,0,SEEK_SET);
508 }
509 5 return $fh;
510 } else {
511 210 $fh = new IO::File();
512 210 ▶ $fh->open($file,$rw) || return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($file, $rw) or return 210 0 210
513 }
514 210 return $fh;
515 }
516  
517 0 ▶sub _callback {
Subroutine _callback: not called CC=3 SCAR=24.8
518 0 my $callback = shift;
519 0 ▶ if (ref($callback) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $callback eq 'CODE') { } 0 0 0
elsif (ref $callback eq 'ARRAY') { } 0 0 0
520 0 return $callback->(@_);
521 } elsif (ref($callback) eq 'ARRAY') {
522 0 my ($cb,@args)=@{$callback};
523 0 $cb->(@args,@_);
524 }
525 }
526  
527 189 ▶sub _fetch_file {
Subroutine _fetch_file: called CC=11 SCAR=36.7
528 189 my ($uri) = @_;
529 189 my $proto = get_protocol($uri);
530 189 ▶ if ($proto eq 'file') {
Branch: 3 branches
BranchTrueFalseTotal
if ($proto eq 'file') { } 189 0 189
elsif ($proto eq 'ntred' or $proto =~ /$Treex::PML::IO::reject_proto/) { } 0 0 0
elsif (exists $input_protocol_handler{$proto}) { } 0 0 0
531 189 my $file = get_filename($uri);
532 189 ▶ print STDERR __PACKAGE__.": _fetch_file: $file\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 189 189
533 189 ▶ die("File does not exist: $file\n") unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 188 189
534 188 ▶ die("File is not readable: $file\n") unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 188 188
535 188 ▶ die("File is empty: $file\n") if -z $file;
Branch: 1 branch
BranchTrueFalseTotal
if -z $file 0 188 188
536 188 return ($file,0);
537 ▶ } elsif ($proto eq 'ntred' or $proto =~ /$reject_proto/) {
Condition0%0/3
$proto eq "ntred" or $proto =~ /$Treex::PML::IO::reject_proto/
l!l&&r!l&&!r
000
MC/DC0%0/2
$proto eq "ntred" or $proto =~ /$Treex::PML::IO::reject_proto/
$proto eq "ntred" $proto =~ /$Treex::PML::IO::reject_proto/
Truth table0%0/3
A or B
A= $proto eq "ntred"B= $proto =~ /$Treex::PML::IO::reject_proto/
ABresult
1X1
011
000
538 0 return ($uri,0);
539 } elsif (exists($input_protocol_handler{$proto})) {
540 0 my ($new_uri,$unlink) = _callback($input_protocol_handler{$proto},$uri);
541 0 my $new_proto = get_protocol($new_uri);
542 0 ▶ if ($new_proto ne $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_proto ne $proto) { } 0 0 0
543 0 return _fetch_file($new_uri);
544 } else {
545 0 return ($new_uri,$unlink);
546 }
547 } else {
548 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
549 0 return _fetch_file_win32($uri,$proto);
550 } else {
551 0 return _fetch_file_posix($uri,$proto);
552 }
553 }
554 }
555  
556  
557 =item fetch_file($uri)
558  
559 Fetches a resource from a given URI and returns a path to a local file
560 with the content of the resource and a boolean unlink flag. If the
561 unlink flag is true, the caller is responsible for removing the local
562 file when finished using it. Otherwise, the caller should not remove
563 the file (usually when it points to the original resource). The
564 caller may assume that the resource is already un-gzipped if the URI
565 had the '.gz' or '.gz~' suffix.
566  
567 =cut
568  
569 189 ▶sub fetch_file {
Subroutine fetch_file: called CC=9 SCAR=22.6
Pod documented
570 189 my ($uri) = @_;
571 189 my ($file,$unlink) = &_fetch_file;
572 188 ▶ if (get_protocol($file) eq 'file' and _is_gzip($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_protocol($file) eq 'file' and _is_gzip($uri)) { } 15 173 188
Condition66%2/3
get_protocol($file) eq "file" and _is_gzip($uri)
!ll&&!rl&&r
017315
MC/DC50%1/2
get_protocol($file) eq "file" and _is_gzip($uri)
get_protocol($file) eq "file" _is_gzip($uri)
Truth table66%2/3
A and B
A= get_protocol($file) eq "file"B= _is_gzip($uri)
ABresult
0X0
100
111
573 15 my ($fh,$ungzfile) = File::Temp::tempfile("tredgzioXXXXXX",
574 DIR => File::Spec->tmpdir(),
575 UNLINK => 0,
576 );
577 15 ▶ die "Cannot create temporary file: $!" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 15 15
578 15 my $tmp;
579 15 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } and $tmp or die "Cannot load IO::Zlib: $@" 15 0 15
Condition33%1/3
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } and $tmp
!ll&&!rl&&r
0015
MC/DC0%0/2
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } and $tmp
eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } } $tmp
Truth table33%1/3
A and B
A= eval { do { require IO::Zlib; $tmp = "IO::Zlib"->new } }B= $tmp
ABresult
0X0
100
111
580 15 require IO::Zlib;
581 15 $tmp = new IO::Zlib();
582 } && $tmp || die "Cannot load IO::Zlib: $@";
583 15 ▶ $tmp->open($file,"rb") || die "Cannot read $uri ($file)";
Branch: 1 branch
BranchTrueFalseTotal
$tmp->open($file, "rb") or die "Cannot read $uri ($file)" 15 0 15
584 15 my $buffer;
585 15 my $length = 1024*1024;
586 15 ▶ while (read($tmp,$buffer,$length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $tmp, $buffer, $length) 15 15 30
587 15 $fh->print($buffer);
588 }
589 15 $tmp->close();
590 15 $fh->close;
591 15 ▶ unlink $file if $unlink;
Branch: 1 branch
BranchTrueFalseTotal
if $unlink 0 15 15
592 15 return ($ungzfile,1);
593 } else {
594 173 return ($file,$unlink);
595 }
596 }
597  
598  
599 0 ▶sub _fetch_cmd {
Subroutine _fetch_cmd: not called CC=3 SCAR=24.8
600 0 my ($cmd, $filename)=@_;
601 0 ▶ print STDERR __PACKAGE__.": _fetch_cmd: $cmd\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
602 0 ▶ if (system($cmd." > ".$filename)==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system($cmd . ' > ' . $filename) == 0) { } 0 0 0
603 0 return ($filename,1);
604 } else {
605 0 warn "$cmd > $filename failed (code $?): $!\n";
606 0 return $filename,0;
607 }
608 }
609  
610 0 ▶sub _fetch_with_lwp {
Subroutine _fetch_with_lwp: not called CC=5 SCAR=34.0
611 0 my ($uri,$fh,$filename)=@_;
612 0 my $status = $lwp_user_agent->get($uri, ':content_file' => $filename);
613 0 ▶ if ($status and $status->is_error and $status->code == 401) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status and $status->is_error and $status->code == 401) 0 0 0
Condition0%0/6
$status and $status->is_error and $status->code == 401
!ll&&!rl&&r
000
$status and $status->is_error
!ll&&!rl&&r
000
MC/DC0%0/3
$status and $status->is_error and $status->code == 401
$status $status->is_error $status->code == 401
Truth table0%0/4
A and B and C
A= $statusB= $status->is_errorC= $status->code == 401
ABCresult
0XX0
10X0
1100
1111
614 # unauthorized
615 # Got authorization error 401, maybe the nonce is stale, let's try again...
616 0 $status = $lwp_user_agent->get($uri, ':content_file' => $filename);
617 }
618 0 ▶ if ($status->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_success) { } 0 0 0
619 0 close $fh;
620 0 return ($filename,1);
621 } else {
622 0 unlink $fh;
623 0 close $fh;
624 0 die "Error occured while fetching URL $uri $@\n".
625 $status->status_line()."\n";
626 }
627 }
628  
629 0 ▶sub _fetch_file_win32 {
Subroutine _fetch_file_win32: not called CC=4 SCAR=30.0
630 0 my ($uri,$proto)=@_;
631 0 ▶ my ($fh,$filename) = File::Temp::tempfile("tredioXXXXXX",
Branch: 1 branch
BranchTrueFalseTotal
_is_gzip($uri) ? : 0 0 0
632 DIR => File::Spec->tmpdir(),
633 SUFFIX => (_is_gzip($uri) ? ".gz" : ""),
634 UNLINK => 0,
635 );
636 0 ▶ print STDERR __PACKAGE__.": fetching URI $uri as proto $proto to $filename\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
637 0 ▶ if ($proto=~m(^https?|ftp|gopher|news)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ /^https?|ftp|gopher|news/) 0 0 0
638 0 return _fetch_with_lwp($uri,$fh,$filename);
639 }
640 0 return($uri,0);
641 }
642  
643 0 ▶sub _fetch_file_posix {
Subroutine _fetch_file_posix: not called CC=18 SCAR=58.3
644 0 my ($uri,$proto)=@_;
645 0 ▶ print STDERR __PACKAGE__.": fetching file using protocol $proto ($uri)\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
646 0 ▶ my ($fh,$tempfile) = File::Temp::tempfile("tredioXXXXXX",
Branch: 1 branch
BranchTrueFalseTotal
_is_gzip($uri) ? : 0 0 0
647 DIR => File::Spec->tmpdir(),
648 SUFFIX => (_is_gzip($uri) ? ".gz" : ""),
649 UNLINK => 0,
650 );
651 0 ▶ print STDERR __PACKAGE__.": tempfile: $tempfile\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
652 0 ▶ if ($proto=~m(^https?|ftp|gopher|news)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ /^https?|ftp|gopher|news/) 0 0 0
653 0 return _fetch_with_lwp($uri,$fh,$tempfile);
654 }
655 0 close($fh);
656 0 ▶ if ($ssh and -x $ssh and $proto =~ /^(ssh|fish|sftp)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/) 0 0 0
Condition0%0/6
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
!ll&&!rl&&r
000
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh
!ll&&!rl&&r
000
MC/DC0%0/3
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
$Treex::PML::IO::ssh -x $Treex::PML::IO::ssh $proto =~ /^(ssh|fish|sftp)$/
Truth table0%0/4
A and B and C
A= $Treex::PML::IO::sshB= -x $Treex::PML::IO::sshC= $proto =~ /^(ssh|fish|sftp)$/
ABCresult
0XX0
10X0
1100
1111
657 0 ▶ print STDERR __PACKAGE__.": using plain ssh\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
658 0 ▶ if ($uri =~ m{^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$]) { } 0 0 0
659 0 my ($host,$file) = ($1,$2);
660 0 ▶ print STDERR __PACKAGE__.": tempfile: $tempfile\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
661 return
662 0 _fetch_cmd($ssh." ".$ssh_opts." ".quote_filename($host).
663 " /bin/cat ".quote_filename(quote_filename($file)),$tempfile);
664 } else {
665 0 die "failed to parse URI for ssh $uri\n";
666 }
667 }
668 0 ▶ if ($kioclient and -x $kioclient) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient) 0 0 0
Condition0%0/3
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
!ll&&!rl&&r
000
MC/DC0%0/2
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
$Treex::PML::IO::kioclient -x $Treex::PML::IO::kioclient
Truth table0%0/3
A and B
A= $Treex::PML::IO::kioclientB= -x $Treex::PML::IO::kioclient
ABresult
0X0
100
111
669 0 ▶ print STDERR __PACKAGE__.": using kioclient\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
670 # translate ssh protocol to fish protocol
671 0 ▶ if ($proto eq 'ssh') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "ssh") 0 0 0
672 0 ($uri =~ s{^\s*ssh:(?://)?([/:]*)[:/]}{fish://$1/});
673 }
674 0 return _fetch_cmd($kioclient." ".$kioclient_opts.
675 " cat ".quote_filename($uri),$tempfile);
676 }
677 0 ▶ if ($curl and -x $curl and $proto =~ /^(?:https?|ftps?|gopher)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::curl and -x $Treex::PML::IO::curl and $proto =~ /^(?:https?|ftps?|gopher)$/) 0 0 0
Condition0%0/6
$Treex::PML::IO::curl and -x $Treex::PML::IO::curl and $proto =~ /^(?:https?|ftps?|gopher)$/
!ll&&!rl&&r
000
$Treex::PML::IO::curl and -x $Treex::PML::IO::curl
!ll&&!rl&&r
000
MC/DC0%0/3
$Treex::PML::IO::curl and -x $Treex::PML::IO::curl and $proto =~ /^(?:https?|ftps?|gopher)$/
$Treex::PML::IO::curl -x $Treex::PML::IO::curl $proto =~ /^(?:https?|ftps?|gopher)$/
Truth table0%0/4
A and B and C
A= $Treex::PML::IO::curlB= -x $Treex::PML::IO::curlC= $proto =~ /^(?:https?|ftps?|gopher)$/
ABCresult
0XX0
10X0
1100
1111
678 0 return _fetch_cmd($curl." ".$curl_opts." ".quote_filename($uri),$tempfile);
679 }
680 0 warn "No handlers for protocol $proto\n";
681 0 return ($uri,0);
682 }
683  
684 0 ▶sub _open_upload_pipe {
Subroutine _open_upload_pipe: not called CC=7 SCAR=40.3
685 0 my ($need_gzip,$user_pipe,$upload_pipe)=@_;
686 0 my $fh;
687 0 ▶ $user_pipe="| ".$user_pipe if defined($user_pipe) and $user_pipe !~ /^\|/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user_pipe and not $user_pipe =~ /^\|/ 0 0 0
Condition0%0/3
defined $user_pipe and not $user_pipe =~ /^\|/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user_pipe and not $user_pipe =~ /^\|/
defined $user_pipe not $user_pipe =~ /^\|/
Truth table0%0/3
A and not(B)
A= defined $user_pipeB= not $user_pipe =~ /^\|/
ABresult
0X0
100
111
688 0 $user_pipe.=" ";
689 0 my $cmd;
690 0 ▶ if ($need_gzip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_gzip) { } 0 0 0
691 0 ▶ if (-x $gzip) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $Treex::PML::IO::gzip) { } 0 0 0
692 0 $cmd = $user_pipe."| $gzip $gzip_opts | $upload_pipe ";
693 } else {
694 0 die "Need a functional gzip and zcat to open this file\n";
695 }
696 } else {
697 0 $cmd = $user_pipe."| $upload_pipe ";
698 }
699 0 ▶ print STDERR __PACKAGE__.": upload: $cmd\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
700 0 ▶ open $fh, $cmd || undef $fh;
Condition0%0/2
$cmd || undef $fh
l!l
00
MC/DC0%0/1
$cmd || undef $fh
$cmd
Truth table0%0/2
A
A= $cmd
Aresult
11
00
701 0 return $fh;
702 }
703  
704 0 ▶sub _get_upload_fh_win32 {
Subroutine _get_upload_fh_win32: not called CC=1 SCAR=6.9
705 0 my ($uri,$proto,$userpipe)=@_;
706 0 die "Can't save files using protocol $proto on Windows\n";
707 }
708  
709 0 ▶sub _get_upload_fh_posix {
Subroutine _get_upload_fh_posix: not called CC=16 SCAR=56.1
710 0 my ($uri,$proto,$userpipe)=@_;
711 0 ▶ print STDERR __PACKAGE__.": uploading file using protocol $proto ($uri)\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
712 0 ▶ return if $proto eq 'http' or $proto eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "http" or $proto eq "https" 0 0 0
Condition0%0/3
$proto eq "http" or $proto eq "https"
l!l&&r!l&&!r
000
MC/DC0%0/2
$proto eq "http" or $proto eq "https"
$proto eq "http" $proto eq "https"
Truth table0%0/3
A or B
A= $proto eq "http"B= $proto eq "https"
ABresult
1X1
011
000
713 0 ▶ if ($ssh and -x $ssh and $proto =~ /^(ssh|fish|sftp)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/) 0 0 0
Condition0%0/6
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
!ll&&!rl&&r
000
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh
!ll&&!rl&&r
000
MC/DC0%0/3
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
$Treex::PML::IO::ssh -x $Treex::PML::IO::ssh $proto =~ /^(ssh|fish|sftp)$/
Truth table0%0/4
A and B and C
A= $Treex::PML::IO::sshB= -x $Treex::PML::IO::sshC= $proto =~ /^(ssh|fish|sftp)$/
ABCresult
0XX0
10X0
1100
1111
714 0 ▶ print STDERR __PACKAGE__.": using plain ssh\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
715 0 ▶ if ($uri =~ m{^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$]) { } 0 0 0
716 0 my ($host,$file) = ($1,$2);
717 0 return _open_upload_pipe(_is_gzip($uri), $userpipe, "$ssh $ssh_opts ".
718 quote_filename($host)." /bin/cat \\> ".
719 quote_filename(quote_filename($file)));
720 } else {
721 0 die "failed to parse URI for ssh $uri\n";
722 }
723 }
724 0 ▶ if ($kioclient and -x $kioclient) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient) 0 0 0
Condition0%0/3
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
!ll&&!rl&&r
000
MC/DC0%0/2
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
$Treex::PML::IO::kioclient -x $Treex::PML::IO::kioclient
Truth table0%0/3
A and B
A= $Treex::PML::IO::kioclientB= -x $Treex::PML::IO::kioclient
ABresult
0X0
100
111
725 0 ▶ print STDERR __PACKAGE__.": using kioclient\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
726 # translate ssh protocol to fish protocol
727 0 ▶ if ($proto eq 'ssh') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "ssh") 0 0 0
728 0 $uri =~ s{^\s*ssh:(?://)?([/:]*)[:/]}{fish://$1/};
729 }
730 0 return _open_upload_pipe(_is_gzip($uri),$userpipe,
731 "$kioclient $kioclient_opts put ".quote_filename($uri));
732 }
733 0 ▶ if ($curl and -x $curl and $proto =~ /^(?:ftps?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::curl and -x $Treex::PML::IO::curl and $proto =~ /^(?:ftps?)$/) 0 0 0
Condition0%0/6
$Treex::PML::IO::curl and -x $Treex::PML::IO::curl and $proto =~ /^(?:ftps?)$/
!ll&&!rl&&r
000
$Treex::PML::IO::curl and -x $Treex::PML::IO::curl
!ll&&!rl&&r
000
MC/DC0%0/3
$Treex::PML::IO::curl and -x $Treex::PML::IO::curl and $proto =~ /^(?:ftps?)$/
$Treex::PML::IO::curl -x $Treex::PML::IO::curl $proto =~ /^(?:ftps?)$/
Truth table0%0/4
A and B and C
A= $Treex::PML::IO::curlB= -x $Treex::PML::IO::curlC= $proto =~ /^(?:ftps?)$/
ABCresult
0XX0
10X0
1100
1111
734 0 return _open_upload_pipe("$curl --upload-file - $curl_opts ".quote_filename($uri));
735 }
736 0 die "No handlers for protocol $proto\n";
737 }
738  
739 =item get_store_fh ($uri, $command?)
740  
741 If $command is provided, returns a writable filehandle for a pipe to a given
742 command whose output is redirected to an uploader to the given $URI
743 (for file $URIs this simply redirects the output of the command to the
744 given file (gzipping the data first if the $URI ends with the '.gz' or
745 '.gz~' suffix).
746  
747 If $command is not given, simly retuns a writable file handle to a
748 given file (possibly performing gzip if the file name ends with the
749 '.gz' or '.gz~' suffix).
750  
751 =cut
752  
753 35 ▶sub get_store_fh {
Subroutine get_store_fh: called CC=6 SCAR=27.3
Pod documented
754 35 my ($uri,$user_pipe) = @_;
755 35 my $proto = get_protocol($uri);
756 35 ▶ if ($proto eq 'file') {
Branch: 2 branches
BranchTrueFalseTotal
if ($proto eq 'file') { } 35 0 35
elsif ($proto eq 'ntred' or $proto =~ /$Treex::PML::IO::reject_proto/) { } 0 0 0
757 35 $uri = get_filename($uri);
758 35 ▶ if ($user_pipe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_pipe) { } 0 35 35
759 0 return open_pipe($uri,'w',$user_pipe);
760 } else {
761 35 return open_file($uri,'w');
762 }
763 ▶ } elsif ($proto eq 'ntred' or $proto =~ /$reject_proto/) {
Condition0%0/3
$proto eq "ntred" or $proto =~ /$Treex::PML::IO::reject_proto/
l!l&&r!l&&!r
000
MC/DC0%0/2
$proto eq "ntred" or $proto =~ /$Treex::PML::IO::reject_proto/
$proto eq "ntred" $proto =~ /$Treex::PML::IO::reject_proto/
Truth table0%0/3
A or B
A= $proto eq "ntred"B= $proto =~ /$Treex::PML::IO::reject_proto/
ABresult
1X1
011
000
764 0 return $uri;
765 } else {
766 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
767 0 return _get_upload_fh_win32($uri,$proto,$user_pipe);
768 } else {
769 0 return _get_upload_fh_posix($uri,$proto,$user_pipe);
770 }
771 }
772 }
773  
774 =item unlink_uri($URI)
775  
776 Delete the resource point to by a given URI (if supported by the
777 corresponding protocol handler).
778  
779 =cut
780  
781 0 ▶sub unlink_uri {
Subroutine unlink_uri: not called CC=2 SCAR=17.9
Pod documented
782 0 ▶ ($^O eq 'MSWin32') ? &_unlink_uri_win32 : &_unlink_uri_posix;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
783 }
784  
785 0 ▶sub _unlink_uri_win32 {
Subroutine _unlink_uri_win32: not called CC=2 SCAR=17.9
786 0 my ($uri) = @_;
787 0 my $proto = get_protocol($uri);
788 0 ▶ if ($proto eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'file') { } 0 0 0
789 0 unlink get_filename($uri);
790 } else {
791 0 die "Can't unlink file $uri\n";
792 }
793 }
794  
795 0 ▶sub _unlink_uri_posix {
Subroutine _unlink_uri_posix: not called CC=13 SCAR=52.0
796 0 my ($uri)=@_;
797 0 my $proto = get_protocol($uri);
798 0 ▶ if ($proto eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "file") 0 0 0
799 0 return unlink get_filename($uri);
800 }
801 0 ▶ print STDERR __PACKAGE__.": unlinking file $uri using protocol $proto\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
802 0 ▶ if ($ssh and -x $ssh and $proto =~ /^(ssh|fish|sftp)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/) 0 0 0
Condition0%0/6
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
!ll&&!rl&&r
000
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh
!ll&&!rl&&r
000
MC/DC0%0/3
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
$Treex::PML::IO::ssh -x $Treex::PML::IO::ssh $proto =~ /^(ssh|fish|sftp)$/
Truth table0%0/4
A and B and C
A= $Treex::PML::IO::sshB= -x $Treex::PML::IO::sshC= $proto =~ /^(ssh|fish|sftp)$/
ABCresult
0XX0
10X0
1100
1111
803 0 ▶ print STDERR __PACKAGE__.": using plain ssh\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
804 0 ▶ if ($uri =~ m{^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$]) { } 0 0 0
805 0 my ($host,$file) = ($1,$2);
806 0 ▶ return (system("$ssh $ssh_opts ".quote_filename($host)." /bin/rm ".
Branch: 1 branch
BranchTrueFalseTotal
system("$Treex::PML::IO::ssh $Treex::PML::IO::ssh_opts " . quote_filename($host) . ' /bin/rm ' . quote_filename(quote_filename($file))) == 0 ? : 0 0 0
807 quote_filename(quote_filename($file)))==0) ? 1 : 0;
808 } else {
809 0 die "failed to parse URI for ssh $uri\n";
810 }
811 }
812 0 ▶ if ($kioclient and -x $kioclient) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient) 0 0 0
Condition0%0/3
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
!ll&&!rl&&r
000
MC/DC0%0/2
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
$Treex::PML::IO::kioclient -x $Treex::PML::IO::kioclient
Truth table0%0/3
A and B
A= $Treex::PML::IO::kioclientB= -x $Treex::PML::IO::kioclient
ABresult
0X0
100
111
813 # translate ssh protocol to fish protocol
814 0 ▶ if ($proto eq 'ssh') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "ssh") 0 0 0
815 0 $uri =~ s{^\s*ssh:(?://)?([/:]*)[:/]}{fish://$1/};
816 }
817 0 ▶ return (system("$kioclient $kioclient_opts rm ".quote_filename($uri))==0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
system("$Treex::PML::IO::kioclient $Treex::PML::IO::kioclient_opts rm " . quote_filename($uri)) == 0 ? : 0 0 0
818 }
819 0 die "No handlers for protocol $proto\n";
820 }
821  
822 =item rename_uri($URI_1,$URI_2)
823  
824 Rename the resource point to by $URI_1 to $URI_2 (if supported by the
825 corresponding protocol handlers). The URIs must point to the same
826 physical storage.
827  
828 =cut
829  
830 24 ▶sub rename_uri {
Subroutine rename_uri: called CC=3 SCAR=12.0
Pod documented
831 24 ▶ print STDERR __PACKAGE__.": rename @_\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 24 24
832 24 ▶ ($^O eq 'MSWin32') ? &_rename_uri_win32 : &_rename_uri_posix;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 24 24
833 }
834  
835  
836 0 ▶sub _rename_uri_win32 {
Subroutine _rename_uri_win32: not called CC=4 SCAR=30.0
837 0 my ($uri1,$uri2) = @_;
838 0 my $proto1 = get_protocol($uri1);
839 0 my $proto2 = get_protocol($uri2);
840 0 ▶ if ($proto1 eq 'file' and $proto2 eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto1 eq 'file' and $proto2 eq 'file') { } 0 0 0
Condition0%0/3
$proto1 eq "file" and $proto2 eq "file"
!ll&&!rl&&r
000
MC/DC0%0/2
$proto1 eq "file" and $proto2 eq "file"
$proto1 eq "file" $proto2 eq "file"
Truth table0%0/3
A and B
A= $proto1 eq "file"B= $proto2 eq "file"
ABresult
0X0
100
111
841 0 my $uri1 = get_filename($uri1);
842 0 ▶ return unless -f $uri1;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $uri1 0 0 0
843 0 rename $uri1, get_filename($uri2);
844 } else {
845 0 die "Can't rename file $uri1 to $uri2\n";
846 }
847 }
848  
849 24 ▶sub _rename_uri_posix {
Subroutine _rename_uri_posix: called CC=17 SCAR=51.2
850 24 my ($uri1,$uri2) = @_;
851 24 my $proto = get_protocol($uri1);
852 24 my $proto2 = get_protocol($uri2);
853 24 ▶ if ($proto ne $proto2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto ne $proto2) 0 24 24
854 0 die "Can't rename file $uri1 to $uri2\n";
855 }
856 24 ▶ if ($proto eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "file") 24 0 24
857 24 my $uri1 = get_filename($uri1);
858 24 ▶ return unless -f $uri1;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $uri1 1 23 24
859 23 return rename $uri1, get_filename($uri2);
860 }
861 0 ▶ print STDERR __PACKAGE__.": rename file $uri1 to $uri2 using protocol $proto\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
862 0 ▶ if ($ssh and -x $ssh and $proto =~ /^(ssh|fish|sftp)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/) 0 0 0
Condition0%0/6
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
!ll&&!rl&&r
000
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh
!ll&&!rl&&r
000
MC/DC0%0/3
$Treex::PML::IO::ssh and -x $Treex::PML::IO::ssh and $proto =~ /^(ssh|fish|sftp)$/
$Treex::PML::IO::ssh -x $Treex::PML::IO::ssh $proto =~ /^(ssh|fish|sftp)$/
Truth table0%0/4
A and B and C
A= $Treex::PML::IO::sshB= -x $Treex::PML::IO::sshC= $proto =~ /^(ssh|fish|sftp)$/
ABCresult
0XX0
10X0
1100
1111
863 0 ▶ print STDERR __PACKAGE__.": using plain ssh\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 0 0
864 0 ▶ if ($uri1 =~ m{^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri1 =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$]) { } 0 0 0
865 0 my ($host,$file) = ($1,$2);
866 0 ▶ if ($uri2 =~ m{^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$} and $1 eq $host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri2 =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$] and $1 eq $host) { } 0 0 0
Condition0%0/3
$uri2 =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$] and $1 eq $host
!ll&&!rl&&r
000
MC/DC0%0/2
$uri2 =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$] and $1 eq $host
$uri2 =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$] $1 eq $host
Truth table0%0/3
A and B
A= $uri2 =~ m[^\s*(?:ssh|sftp|fish):(?://)?([^-/][^/]*)(/.*)$]B= $1 eq $host
ABresult
0X0
100
111
867 0 my $file2 = $2;
868 0 ▶ return (system("$ssh $ssh_opts ".quote_filename($host)." /bin/mv ".
Branch: 1 branch
BranchTrueFalseTotal
system("$Treex::PML::IO::ssh $Treex::PML::IO::ssh_opts " . quote_filename($host) . ' /bin/mv ' . quote_filename(quote_filename($file)) . ' ' . quote_filename(quote_filename($file2))) == 0 ? : 0 0 0
869 quote_filename(quote_filename($file))." ".
870 quote_filename(quote_filename($file2)))==0) ? 1 : 0;
871 } else {
872 0 die "failed to parse URI for ssh $uri2\n";
873 }
874 } else {
875 0 die "failed to parse URI for ssh $uri1\n";
876 }
877 }
878 0 ▶ if ($kioclient and -x $kioclient) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient) 0 0 0
Condition0%0/3
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
!ll&&!rl&&r
000
MC/DC0%0/2
$Treex::PML::IO::kioclient and -x $Treex::PML::IO::kioclient
$Treex::PML::IO::kioclient -x $Treex::PML::IO::kioclient
Truth table0%0/3
A and B
A= $Treex::PML::IO::kioclientB= -x $Treex::PML::IO::kioclient
ABresult
0X0
100
111
879 # translate ssh protocol to fish protocol
880 0 ▶ if ($proto eq 'ssh') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "ssh") 0 0 0
881 0 $uri1 =~ s{^\s*ssh:(?://)?([/:]*)[:/]}{fish://$1/};
882 0 $uri2 =~ s{^\s*ssh:(?://)?([/:]*)[:/]}{fish://$1/};
883 }
884 0 ▶ return (system("$kioclient $kioclient_opts mv ".quote_filename($uri1).
Branch: 1 branch
BranchTrueFalseTotal
system("$Treex::PML::IO::kioclient $Treex::PML::IO::kioclient_opts mv " . quote_filename($uri1) . ' ' . quote_filename($uri2)) == 0 ? : 0 0 0
885 " ".quote_filename($uri2))==0 ? 1 : 0);
886 }
887 0 die "No handlers for protocol $proto\n";
888 }
889  
890  
891  
892 =item open_backend (filename,mode,encoding?)
893  
894 Open given file for reading or writing (depending on mode which may be
895 one of "r" or "w"); Return the corresponding object based on
896 File::Handle class. Only files the filename of which ends with '.gz'
897 are considered to be gz-commpressed. All other files are opened using
898 IO::File.
899  
900 Optionally, in perl ver. >= 5.8, you may also specify file character
901 encoding.
902  
903 =cut
904  
905  
906 215 ▶sub open_backend {
Subroutine open_backend: called CC=5 SCAR=17.8
Pod documented
907 215 my ($filename, $rw,$encoding)=@_;
908 215 $filename =~ s/^\s*|\s*$//g;
909 215 ▶ if ($rw eq 'r') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rw eq 'r') { } 180 35 215
elsif ($rw eq 'w') { } 35 0 35
910 180 ▶ return set_encoding(open_file($filename,$rw)||undef,$encoding);
Condition50%1/2
open_file($filename, $rw) || undef
l!l
1800
MC/DC0%0/1
open_file($filename, $rw) || undef
open_file($filename, $rw)
Truth table50%1/2
A
A= open_file($filename, $rw)
Aresult
11
00
911 } elsif ($rw eq 'w') {
912 35 ▶ return set_encoding(get_store_fh($filename)||undef,$encoding);
Condition50%1/2
get_store_fh($filename) || undef
l!l
350
MC/DC0%0/1
get_store_fh($filename) || undef
get_store_fh($filename)
Truth table50%1/2
A
A= get_store_fh($filename)
Aresult
11
00
913 } else {
914 0 croak "2nd argument to open_backend must be 'r' or 'w'!";
915 }
916 0 return;
917 }
918  
919 =pod
920  
921 =item close_backend (filehandle)
922  
923 Close given filehandle opened by previous call to C<open_backend>
924  
925 =cut
926  
927 215 ▶sub close_backend {
Subroutine close_backend: called CC=9 SCAR=23.7
Pod documented
928 215 my ($fh)=@_;
929 # Win32 hack:
930 215 ▶ if (ref($fh) eq 'File::Temp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "File::Temp") 5 210 215
931 5 my $filename = ${*$fh}{'ZIPTOFILE'};
932 5 ▶ if ($filename ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename ne "") 5 0 5
933 5 ▶ print STDERR __PACKAGE__.": Doing the real save to $filename\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::IO::Debug 0 5 5
934 5 seek($fh,0,SEEK_SET);
935 5 require IO::Zlib;
936 5 my $tmp = new IO::Zlib();
937 5 ▶ $tmp->open($filename,"wb") || die "Cannot write to $filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmp->open($filename, "wb") or die "Cannot write to ${filename}: $!\n" 5 0 5
938 # probably bug in Perl 5.8.9? - using just :raw here is not enough
939 5 binmode $fh, ':raw:perlio:bytes';
940 5 local $/;
941 5 $tmp->print(<$fh>);
942 5 $tmp->close;
943 }
944 }
945 215 my $ret;
946 215 ▶ if ((blessed($fh) and $fh->isa('IO::Zlib'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($fh) and $fh->isa('IO::Zlib')) { } 0 215 215
Condition33%1/3
builtin::blessed($fh) and $fh->isa("IO::Zlib")
!ll&&!rl&&r
02150
MC/DC0%0/2
builtin::blessed($fh) and $fh->isa("IO::Zlib")
builtin::blessed($fh) $fh->isa("IO::Zlib")
Truth table33%1/3
A and B
A= builtin::blessed($fh)B= $fh->isa("IO::Zlib")
ABresult
0X0
100
111
947 0 $ret = 1;
948 } else {
949 215 ▶ $ret = ref($fh) && $fh->close();
Condition33%1/3
ref $fh && $fh->close
!ll&&!rl&&r
00215
MC/DC0%0/2
ref $fh && $fh->close
ref $fh $fh->close
Truth table33%1/3
A && B
A= ref $fhB= $fh->close
ABresult
0X0
100
111
950 }
951 215 my $unlink = delete $UNLINK_ON_CLOSE{ $fh };
952 215 ▶ if ($unlink) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unlink) 0 215 215
953 0 unlink $unlink;
954 }
955 215 return $ret;
956 }
957  
958  
959 =item open_uri (URI,encoding?)
960  
961 Open given URL for reading, returning an object based on File::Handle
962 class. Since for some types of URLs this function first copies the
963 data into a temporary file, use close_uri($fh) on the resulting
964 filehandle to close it and clean up the temporary file.
965  
966 Optionally, in perl ver. >= 5.8, you may also specify file character
967 encoding.
968  
969 =cut
970  
971 132 ▶sub open_uri {
Subroutine open_uri: called CC=5 SCAR=20.9
Pod documented
972 132 my ($uri,$encoding) = @_;
973 132 my ($local_file, $is_temporary) = fetch_file( $uri );
974 132 ▶ my $fh = open_backend($local_file,'r') || return;
Condition50%1/2
open_backend($local_file, 'r') || (return)
l!l
1320
MC/DC0%0/1
open_backend($local_file, 'r') || (return)
open_backend($local_file, 'r')
Truth table50%1/2
A
A= open_backend($local_file, 'r')
Aresult
11
00
975 132 ▶ if ($is_temporary and $local_file ne $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_temporary and $local_file ne $uri) 0 132 132
Condition33%1/3
$is_temporary and $local_file ne $uri
!ll&&!rl&&r
13200
MC/DC0%0/2
$is_temporary and $local_file ne $uri
$is_temporary $local_file ne $uri
Truth table33%1/3
A and B
A= $is_temporaryB= $local_file ne $uri
ABresult
0X0
100
111
976 0 ▶ if (!unlink($local_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $local_file) 0 0 0
977 0 $UNLINK_ON_CLOSE{ $fh } = $local_file;
978 }
979 }
980 132 return set_encoding($fh,$encoding);
981 }
982  
983 9 *close_uri = \&close_backend;
984  
985 =item close_uri (filehandle)
986  
987 Close given filehandle opened by previous call to C<open_uri>.
988  
989 =cut
990  
991  
992 =item copy_uri ($URI_1,$URI_2)
993  
994 Copy the resource pointed to by the URI $URI_1 to $URI_2. The type of
995 $URI_2 must be writable.
996  
997 =cut
998  
999 0 ▶sub copy_uri {
Subroutine copy_uri: not called CC=4 SCAR=30.0
Pod documented
1000 0 my ($src_uri,$target_uri)=@_;
1001 0 ▶ my $in = open_uri($src_uri)
Branch: 1 branch
BranchTrueFalseTotal
my $in = open_uri($src_uri) or die "Cannot open source ${src_uri}: $!\n" 0 0 0
1002 or die "Cannot open source $src_uri: $!\n";
1003 0 ▶ my $out = open_backend($target_uri,'w')
Branch: 1 branch
BranchTrueFalseTotal
my $out = open_backend($target_uri, "w") or die "Cannot open target ${target_uri}: $!\n" 0 0 0
1004 or die "Cannot open target $target_uri: $!\n";
1005 0 my $L=1024*100;
1006 0 my $buffer;
1007 0 ▶ while(read($in,$buffer,$L)>0) {
Branch: 1 branch
BranchTrueFalseTotal
if (read($in, $buffer, $L) > 0) 0 0 0
1008 0 print $out ($buffer);
1009 }
1010 0 close_backend($in);
1011 0 close_backend($out);
1012 }
1013  
1014 =back
1015  
1016 =head1 COPYRIGHT AND LICENSE
1017  
1018 Copyright (C) 2006-2010 by Petr Pajas, 2010-2024 Jan Stepanek
1019  
1020 This library is free software; you can redistribute it and/or modify
1021 it under the same terms as Perl itself, either Perl version 5.8.2 or,
1022 at your option, any later version of Perl 5 you may have available.
1023  
1024 =cut
« PML/Schema/Seq.pm ↑ Summary PML/Backend/TrXML.pm »