Distlinks/URInFiles.pm

filter: Statementstmt 25.7% 45 / 175 Branchbran 4.3% 2 / 46 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 50.0% 11 / 22 Podpod 0.0% 0 / 9 totaltotal 20.3% 58 / 285 CC 32 SCAR 44.1
CC
32
Cov
20%
file_text
244.9
next
42.0
unzip
20.0
CRAP 82.6
« Distlinks.pm ↑ Summary Distlinks/FileFind.pm »
1 # Copyright 2010, 2011, 2012, 2013 Kevin Ryde
2  
3 # This file is part of Distlinks.
4 #
5 # Distlinks is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Distlinks is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Distlinks. If not, see <http://www.gnu.org/licenses/>.
17  
18 package App::Distlinks::URInFiles;
19 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Perl6::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Locale::TextDomain ('App-Distlinks');
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 1 our $VERSION = 11;
32  
33 # my %exclude_dirs = (# 'b' => 1,
34 # 'blib' => 1,
35 # 'CVS' => 1,
36 # '.svn' => 1,
37 # );
38  
39 # emacs backup foo~
40 # emacs lock .#foo or autosave #foo#
41 1 my $exclude_files_re = qr/~$|^\.?#/;
42  
43 1 my $exclude_dirs_re = qr/^(blib|CVS|\.svn)$/;
44  
45  
46 #------------------------------------------------------------------------------
47 # generic
48  
49 3 ▶sub join_backslashed_newlines {
Subroutine join_backslashed_newlines: called CC=2 SCAR=6.9
Pod undocumented
50 3 my ($str) = @_;
51 3 ▶ while ((my $eol = index ($str, "\\\n")) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $eol = index($str, "\\\n")) > 0) 3 3 6
52 3 my $bol = max (rindex ($str, "\n", $eol), 0);
53 3 substr ($str, $eol,2, ''); # delete \ and newline
54 3 substr ($str, $bol,0, "\n"); # insert newline
55 }
56 3 return $str;
57 }
58  
59  
60 #------------------------------------------------------------------------------
61 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
62 0 my ($class, @inputs) = @_;
63 0 return bless { finders => [ $class->_make_finder(@inputs) ],
64 verbose => 0,
65 }, $class;
66 }
67  
68 0 ▶sub _make_finder {
Subroutine _make_finder: not called CC=1 SCAR=6.9
69 0 my ($class, @inputs) = @_;
70 0 require File::Find::Iterator;
71 0 File::Find::Iterator->create (dir => \@inputs,
72 filter => \&_is_not_excluded);
73 }
74 0 ▶sub _is_not_excluded {
Subroutine _is_not_excluded: not called CC=2 SCAR=17.9
75 return (! -d
76 0 ▶ && ! m{~$
Condition0%0/3
-d $_ || m[~$ |(^|/) (\.?\# |(blib|CVS|\.svn)($|/) ) ]x
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $_ || m[~$ |(^|/) (\.?\# |(blib|CVS|\.svn)($|/) ) ]x
-d $_ m[~$ |(^|/) (\.?\# |(blib|CVS|\.svn)($|/) ) ]x
Truth table0%0/3
A || B
A= -d $_B= m[~$ |(^|/) (\.?\# |(blib|CVS|\.svn)($|/) ) ]x
ABresult
1X1
011
000
77 |(^|/)
78 (\.?\#
79 |(blib|CVS|\.svn)($|/)
80 )
81 }x);
82 }
83  
84 0 ▶sub next {
Subroutine next: not called CC=6 SCAR=37.4
Pod undocumented
85 0 my ($self) = @_;
86 ### URInFiles next() ...
87  
88 0 for (;;) {
89 0 ▶ if (my $urit = $self->{'urit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $urit = $self->{"urit"}) 0 0 0
90 0 ▶ if (defined (my $ufound = $urit->next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ufound = $urit->next)) 0 0 0
91 0 return $ufound;
92 }
93 }
94 0 my $finder = $self->{'finders'}->[0];
95 0 ▶ my $filename = $finder->next // do {
Condition0%0/3
$finder->next // do { shift @{$$self{"finders"};}; if (@{$self->{"finders"};}) { next; } else { return; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$finder->next // do { shift @{$$self{"finders"};}; if (@{$self->{"finders"};}) { next; } else { return; } }
$finder->next do { shift @{$$self{"finders"};}; if (@{$self->{"finders"};}) { next; } else { return; } }
Truth table0%0/3
A // B
A= $finder->nextB= do { shift @{$$self{"finders"};}; if (@{$self->{"finders"};}) { next; } else { return; } }
ABresult
1X1
011
000
96 0 shift @{$self->{'finders'}};
97 0 ▶ if (@{$self->{'finders'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'finders'};}) { } 0 0 0
98 0 next;
99 } else {
100 0 return;
101 }
102 };
103 ### URInFiles filename: $filename
104  
105 my $content = $self->file_text ($filename)
106 0 ▶ // do {
Condition0%0/3
$self->file_text($filename) // do { next }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->file_text($filename) // do { next }
$self->file_text($filename) do { next }
Truth table0%0/3
A // B
A= $self->file_text($filename)B= do { next }
ABresult
1X1
011
000
107 ### no content for: $filename
108 0 next;
109 };
110  
111 0 require App::Distlinks::URIterator;
112 0 $self->{'urit'} = App::Distlinks::URIterator->new
113 (filename => $filename,
114 content => $content,
115 base => URI::file->new_abs($filename));
116 }
117 }
118  
119 1 ▶use constant::defer _FILETYPER => sub {
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 require File::Type;
121 0 return File::Type->new;
122 1 };
123  
124 1 my %bom_coding = ("\x{EF}\x{BB}\x{BF}" => 'UTF-8',
125 "\x{FF}\x{FE}" => 'UTF-16le',
126 "\x{FE}\x{FF}" => 'UTF-16be',
127 "\x{FF}\x{FE}\x{00}\x{00}" => 'UTF-32le',
128 "\x{00}\x{00}\x{FE}\x{FF}" => 'UTF-32be');
129 1 my %executable_types = ('application/x-executable-file' => 1,
130 'application/x-ms-dos-executable' => 1);
131 1 my %archive_types = ('application/x-gtar' => 'untar',
132 'application/x-tar' => 'untar',
133 'application/zip' => 'unzip');
134 1 my %compress_types =
135 ('application/x-gzip' => [ 'IO::Uncompress::Gunzip',
136 'IO::Uncompress::Gunzip::gunzip' ],
137 'application/x-bzip2' => [ 'IO::Uncompress::Bunzip2',
138 'IO::Uncompress::Bunzip2::bunzip2' ]);
139  
140 1 my $exif;
141 1 my %exif_exclude = (HTML => 1,
142 ZIP => 1);
143  
144 0 ▶sub file_text {
Subroutine file_text: not called CC=16 SCAR=55.0
Pod undocumented
145 0 my ($self, $filename) = @_;
146 ### URInFiles file_text(): $filename
147  
148 0 my $content = eval { Perl6::Slurp::slurp($filename) }
149 0 ▶ // do {
Condition0%0/3
eval { do { Perl6::Slurp::slurp($filename) } } // do { print $@; return undef }
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { Perl6::Slurp::slurp($filename) } } // do { print $@; return undef }
eval { do { Perl6::Slurp::slurp($filename) } } do { print $@; return undef }
Truth table0%0/3
A // B
A= eval { do { Perl6::Slurp::slurp($filename) } }B= do { print $@; return undef }
ABresult
1X1
011
000
150 0 print $@;
151 0 return undef;
152 };
153 ### $content
154  
155 0 for (;;) {
156 0 foreach my $bom (keys %bom_coding) {
157 0 ▶ if ($content =~ /^\Q$bom/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\Q$bom\E/) 0 0 0
158 0 require Encode;
159 0 return Encode::decode ($bom_coding{$bom}, $content);
160 }
161 }
162  
163 0 ▶ if ($content =~ /^(\x{DE}\x{12}\x{04}\x{95}|\x{95}\x{04}\x{12}\x{DE})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^(\x{DE}\x{12}\x{04}\x{95}|\x{95}\x{04}\x{12}\x{DE})/) 0 0 0
164 0 $content = $self->msgunfmt($filename, $content);
165 }
166  
167 0 my $type = _FILETYPER()->checktype_contents($content);
168 0 ▶ if ($self->{'verbose'} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
169 0 print __x("filetype {type}\n", type => $type);
170 }
171 0 ▶ if ($executable_types{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($executable_types{$type}) 0 0 0
172 0 return $content;
173 }
174  
175 0 ▶ if (my $method = $archive_types{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $archive_types{$type}) 0 0 0
176 0 ▶ my $tempdir = $self->$method($filename, $content) // return;
Condition0%0/2
$self->$method($filename, $content) // (return)
l!l
00
MC/DC0%0/1
$self->$method($filename, $content) // (return)
$self->$method($filename, $content)
Truth table0%0/2
A
A= $self->$method($filename, $content)
Aresult
11
00
177 ### URInFiles also tempdir: $tempdir
178 0 unshift @{$self->{'finders'}}, $self->_make_finder($tempdir);
179 0 return;
180 }
181  
182 0 ▶ if (my $info = $compress_types{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $compress_types{$type}) 0 0 0
183 0 my ($class, $func) = @$info;
184 ### URInFiles class: $class
185 0 require Module::Load;
186 0 Module::Load::load ($class);
187 0 my $input = $content;
188 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 0 my $status = &$func (\$input, \$content);
190 0 next;
191 }
192  
193 0 ▶ $exif ||= do {
Condition0%0/3
$exif ||= do { require Image::ExifTool; my $e = "Image::ExifTool"->new; $e->Options("List", 0, "Binary", 1, "Unknown", 1); $e }
l!l&&r!l&&!r
000
MC/DC0%0/2
$exif ||= do { require Image::ExifTool; my $e = "Image::ExifTool"->new; $e->Options("List", 0, "Binary", 1, "Unknown", 1); $e }
$exif do { require Image::ExifTool; my $e = "Image::ExifTool"->new; $e->Options("List", 0, "Binary", 1, "Unknown", 1); $e }
Truth table0%0/3
A ||= B
A= $exifB= do { require Image::ExifTool; my $e = "Image::ExifTool"->new; $e->Options("List", 0, "Binary", 1, "Unknown", 1); $e }
ABresult
1X1
011
000
194 0 require Image::ExifTool;
195 0 my $e = Image::ExifTool->new;
196 0 $e->Options (List => 0, # give list values as comma separated
197 Binary => 1,
198 Unknown => 1);
199 0 $e };
200 0 ▶ if ($exif->ExtractInfo (\$content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exif->ExtractInfo(\$content)) 0 0 0
201 0 my $info = $exif->GetInfo;
202 0 ▶ if ($self->{'verbose'} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
203 0 require Data::Dumper;
204 0 print " ExifTool info: ",
205 Data::Dumper->new([$info],['info'])->Sortkeys(1)->Dump;
206 }
207 0 ▶ if ($exif_exclude{$info->{'FileType'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exif_exclude{$info->{"FileType"}}) 0 0 0
208 0 last;
209 }
210 0 $content = '';
211 0 ▶ while (my ($key, $value) = each %$info) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$info) 0 0 0
212 0 $content .= "$key -- $value\n";
213 }
214 0 ▶ if ($self->{'verbose'} >= 2) { print __("image text:\n"),$content; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
215 0 return $content;
216 }
217  
218 0 last;
219 }
220 ### URInFiles file_text() content: $content
221 0 return $content;
222 }
223  
224 # $filename is a .tar or .tar.gz file
225 # untar it into a temporary directory and return the name of that directory
226 #
227 0 ▶sub untar {
Subroutine untar: not called CC=2 SCAR=17.9
Pod undocumented
228 0 my ($self, $filename, $content) = @_;
229 0 require File::Temp;
230 0 my $tempdir = File::Temp->newdir ('distlinks-XXXXXX',
231 TMPDIR => 1,
232 CLEANUP => 0);
233 0 ▶ if ($self->{'verbose'}) { print __x("untar to dir: {dir}\n",
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
234 dir => $tempdir); }
235  
236 0 require Archive::Tar;
237 0 require IO::String;
238 0 my $io = IO::String->new ($content);
239 0 my $tar = Archive::Tar->new;
240 0 $tar->read ($io);
241 0 { require File::chdir;
242 0 local $File::chdir::CWD = $tempdir;
243 0 $tar->extract;
244 }
245 0 $self->chmod_tree_readonly ($tempdir);
246 0 return $tempdir;
247 }
248  
249 0 ▶sub unzip {
Subroutine unzip: not called CC=4 SCAR=30.0
Pod undocumented
250 0 my ($self, $filename, $content) = @_;
251 0 require File::Temp;
252 0 my $tempdir = File::Temp->newdir ('distlinks-XXXXXX',
253 TMPDIR => 1,
254 CLEANUP => 0);
255 0 ▶ if ($self->{'verbose'}) { print __x("unzip to dir: {dir}\n",
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
256 dir => $tempdir); }
257  
258 0 require Archive::Zip;
259 0 require IO::String;
260 0 my $zip = Archive::Zip->new;
261 0 my $io = IO::String->new ($content);
262 0 ▶ if ((my $ret = $zip->readFromFileHandle ($io))
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ret = $zip->readFromFileHandle($io)) != Archive::Zip::AZ_OK()) 0 0 0
263 != Archive::Zip::AZ_OK()) {
264 0 print __x("Cannot parse {filename}: {error}\n",
265 filename => $filename,
266 error => $ret);
267 0 return;
268 }
269 ### URInFiles zip members: $zip->memberNames
270  
271 0 ▶ if ((my $ret = $zip->extractTree (undef, "$tempdir/"))
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ret = $zip->extractTree(undef, "$tempdir/")) != Archive::Zip::AZ_OK()) 0 0 0
272 != Archive::Zip::AZ_OK()) {
273 0 print __x("Cannot extract {filename}: {error}\n",
274 filename => $filename,
275 error => $ret);
276 0 return;
277 }
278 0 $self->chmod_tree_readonly ($tempdir);
279 0 return $tempdir;
280 }
281  
282 0 ▶sub chmod_tree_readonly {
Subroutine chmod_tree_readonly: not called CC=3 SCAR=24.8
Pod undocumented
283 0 my ($self, $tempdir) = @_;
284 0 require File::Find::Iterator;
285 0 my $find = File::Find::Iterator->create (dir => [$tempdir]);
286 0 ▶ while (my $filename = $find->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filename = $find->next) 0 0 0
287 0 my $mode = (stat $filename)[2] & ~0222; # no write perm
288 0 ▶ if (chmod($mode, $filename) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (chmod($mode, $filename) != 1) 0 0 0
289 0 print __x("Oops, cannot set readonly on {filename}: {error}\n",
290 filename => $filename,
291 error => $!);
292 }
293 }
294 }
295  
296  
297 0 ▶sub msgunfmt {
Subroutine msgunfmt: not called CC=3 SCAR=24.8
Pod undocumented
298 0 my ($self, $filename, $content) = @_;
299 0 ▶ if ($self->{'verbose'} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
300 0 print __x("msgunfmt {filename}\n", filename => $filename);
301 }
302 0 require File::Temp;
303 0 my $fh = File::Temp->new (TEMPLATE => 'distlinks-XXXXXX',
304 SUFFIX => '.mo',
305 TMPDIR => 1);
306 0 $fh->unlink_on_destroy(0);
307  
308 0 print $fh $content;
309 0 close $fh;
310 0 my $tempfile = $fh->filename;
311  
312 ### URInFiles msgunfmt: $tempfile
313 0 $content = qx{msgunfmt $tempfile};
314  
315 0 ▶ if (my $charset = po_charset ($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $charset = po_charset($content)) 0 0 0
316 ### URInFiles charset: $charset
317 0 require Encode;
318 0 $content = Encode::decode ($charset, $content);
319 }
320 0 return $content;
321 }
322  
323 0 ▶sub po_charset {
Subroutine po_charset: not called CC=2 SCAR=17.9
Pod undocumented
324 0 my ($content) = @_;
325 0 ▶ return ($content =~ m{Content-Type: text/plain; charset=([^\\\n\"]+)}
Condition0%0/3
$content =~ m[Content-Type: text/plain; charset=([^\\\n\"]+)] && $1
!ll&&!rl&&r
000
MC/DC0%0/2
$content =~ m[Content-Type: text/plain; charset=([^\\\n\"]+)] && $1
$content =~ m[Content-Type: text/plain; charset=([^\\\n\"]+)] $1
Truth table0%0/3
A && B
A= $content =~ m[Content-Type: text/plain; charset=([^\\\n\"]+)]B= $1
ABresult
0X0
100
111
326 && $1);
327 }
328  
329 1 1;
330 __END__
« Distlinks.pm ↑ Summary Distlinks/FileFind.pm »