blib/lib/Acme/AutoLoad.pm

filter: Statementstmt 43.6% 58 / 133 Branchbran 21.2% 17 / 80 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 72.7% 8 / 11 Podpod 0.0% 0 / 7 totaltotal 34.4% 86 / 250 SCAR 63.7
CC
44
Cov
35%
inc
233.6
fetch
28.5
full
20.0
CRAP 583.9
↑ Summary
1 package Acme::AutoLoad;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.08';
8  
9 1 our $last_fetched = "";
10 1 our $lib = "lib";
11 1 our $hook = \&inc;
12  
13 ▶sub ignore {}
Subroutine ignore: not called CC=1 SCAR=0.0
Pod undocumented
14 sub import {
15 1 ▶ warn "DEBUG: Congratulations! Acme::AutoLoad has been loaded.\n" if $ENV{AUTOLOAD_DEBUG};
Subroutine import: called CC=5 SCAR=16.7
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 1 1
16 1 ▶ $lib = $ENV{AUTOLOAD_LIB} if $ENV{AUTOLOAD_LIB};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_LIB"} 0 1 1
17 1 ▶ if ($lib =~ m{^[^/]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Acme::AutoLoad::lib =~ m[^[^/]]) 1 0 1
18 1 eval {
19 1 require Cwd;
20 1 $lib = Cwd::abs_path($lib);
21 };
22 }
23 1 ▶ push @INC, $lib, $hook if $hook;
Branch: 1 branch
BranchTrueFalseTotal
if $Acme::AutoLoad::hook 1 0 1
24 1 $hook = undef;
25 1 return \&ignore;
26 }
27  
28 sub mkbase {
29 4 ▶ my $path = shift;
Subroutine mkbase: called CC=6 SCAR=19.1
Pod undocumented
30 4 ▶ if ($path =~ s{/+[^/]*$ }{}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[/+[^/]*$ ][]x) 4 0 4
31 4 ▶ return 1 if -d $path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 2 2 4
32 }
33 2 ▶ die "$path: Not a directory\n" if lstat $path;
Branch: 1 branch
BranchTrueFalseTotal
if lstat $path 0 2 2
34 2 ▶ if (mkbase($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkbase($path)) 2 0 2
35 2 ▶ warn "DEBUG: mkbase: Creating [$path] ...\n" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 2 2
36 2 return mkdir $path, 0755;
37 }
38 0 return 0;
39 }
40  
41 sub fetch {
42 2 ▶ my $url = shift;
Subroutine fetch: called CC=7 SCAR=33.5
Pod undocumented
43 2 ▶ my $recurse = shift || {};
Condition50%1/2
shift() || {}
l!l
02
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
44 2 ▶ $url = full($url) unless $url =~ m{^\w+://};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^\w+://] 0 2 2
45 2 my $contents = get($url);
46 0 $last_fetched = $url;
47 0 ▶ if ($contents =~ m{The document has moved <a href="([^<>]+)">}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /The document has moved <a href="([^<>]+)">/) 0 0 0
48 0 my $bounce = $1;
49 0 ▶ if ($recurse->{$bounce} && $recurse->{$bounce} > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recurse->{$bounce} and $recurse->{$bounce} > 2) 0 0 0
Condition0%0/3
$recurse->{$bounce} and $recurse->{$bounce} > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$recurse->{$bounce} and $recurse->{$bounce} > 2
$recurse->{$bounce} $recurse->{$bounce} > 2
Truth table0%0/3
A and B
A= $recurse->{$bounce}B= $recurse->{$bounce} > 2
ABresult
0X0
100
111
50 0 return $contents;
51 }
52 0 $recurse->{$bounce}++;
53 0 ▶ return fetch($bounce, $recurse) if $recurse->{total}++<20;
Branch: 1 branch
BranchTrueFalseTotal
if $recurse->{"total"}++ < 20 0 0 0
54 }
55 0 return $contents;
56 }
57  
58 # full
59 # Turn a relative URL into a full URL
60 sub full {
61 0 ▶ my $rel = shift;
Subroutine full: not called CC=4 SCAR=30.0
Pod undocumented
62 0 ▶ if ($rel =~ m{http://} || $last_fetched !~ m{^(http://[^/]+)(/?.*)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel =~ m[http://] or not $Acme::AutoLoad::last_fetched =~ m[^(http://[^/]+)(/?.*)]) 0 0 0
Condition0%0/3
$rel =~ m[http://] or not $Acme::AutoLoad::last_fetched =~ m[^(http://[^/]+)(/?.*)]
l!l&&r!l&&!r
000
MC/DC0%0/2
$rel =~ m[http://] or not $Acme::AutoLoad::last_fetched =~ m[^(http://[^/]+)(/?.*)]
$rel =~ m[http://] not $Acme::AutoLoad::last_fetched =~ m[^(http://[^/]+)(/?.*)]
Truth table0%0/3
A or not(B)
A= $rel =~ m[http://]B= not $Acme::AutoLoad::last_fetched =~ m[^(http://[^/]+)(/?.*)]
ABresult
1X1
011
000
63 0 return $rel;
64 }
65 0 my $h = $1;
66 0 my $p = $2;
67 0 ▶ if ($rel =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel =~ m[^/]) 0 0 0
68 0 return "$h$rel";
69 }
70 0 $p =~ s{[^/]*$ }{}x;
71 0 return "$h$p$rel";
72 }
73  
74 # fly
75 # Create a stub module to load the real file on-the-fly if needed.
76 sub fly {
77 0 ▶ my $inc = shift;
Subroutine fly: not called CC=4 SCAR=30.0
Pod undocumented
78 0 my $url = shift;
79 0 my $write = shift;
80 0 ▶ warn "DEBUG: Creating stub for [$inc] in order to download [$url] later if needed.\n" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 0 0
81 0 my $contents = q{
82 my $url = q{$URL};
83 my $myself = $INC{"$inc"} || __FILE__;
84 warn "DEBUG: Downloading [$url] right now ...\n" if $ENV{AUTOLOAD_DEBUG};
85 my $m = Acme::AutoLoad::fetch($url);
86 if ($m =~ /package/) {
87 warn "DEBUG: Contents appear fine. Commencing BRICK OVER ...\n" if $ENV{AUTOLOAD_DEBUG};
88 if (open my $fh, ">", $myself) {
89 print $fh $m;
90 close $fh;
91 }
92 else {
93 warn "$myself: WARNING: Unable to repair! $!\n";
94 }
95 warn "DEBUG: Forcing re-evaluation of fresh module contents ...\n" if $ENV{AUTOLOAD_DEBUG};
96 my $e = eval $m;
97 if ($e) {
98 $INC{"$inc"} = $url;
99 $e;
100 }
101 else {
102 die "$url: $@\n";
103 }
104 }
105 else {
106 die "$url: STANKY! $m\n";
107 }
108 };
109 0 $contents =~ s/\s+/ /g;
110 0 $contents =~ s/([\;\{]+)\s+/$1\n/g;
111 0 $contents =~ s/^\s+//;
112 0 $contents =~ s/\s*$/\n/;
113 # Fake interpolation
114 0 $contents =~ s/\$URL/$url/g;
115 0 $contents =~ s/\$inc/$inc/g;
116 0 ▶ if ($write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write) 0 0 0
117 0 mkbase($write);
118 0 $contents =~ s/(\$myself)\s*=.*?;/$1 = "$write";/;
119 0 ▶ open my $fh, ">", $write or die "$write: open: OUCH! $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $write or die "${write}: open: OUCH! $!" 0 0 0
120 0 print $fh $contents;
121 0 close $fh;
122 }
123 0 return $contents;
124 }
125  
126 sub inc {
127 2 ▶ my $i = shift;
Subroutine inc: called CC=21 SCAR=54.5
Pod undocumented
128 2 my $f = shift;
129 2 my $cache_file = "$lib/$f";
130 2 ▶ if (-f $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cache_file) 0 2 2
131 0 warn "$cache_file: Broken module. Can't continue.\n";
132 0 return ();
133 }
134 2 ▶ mkbase($cache_file) or die "$cache_file: Unable to create! $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkbase($cache_file) or die "${cache_file}: Unable to create! $!\n" 0 2 2
135 2 ▶ shift @INC if $INC[0] eq \&ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $INC[0] eq \&Acme::AutoLoad::ignore 0 2 2
136  
137 2 ▶ if ($f =~ m{^([\w/]+)\.pm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ m[^([\w/]+)\.pm]) 2 0 2
138 2 my $dist = $1;
139 2 my $mod = $1;
140 2 $f = "$1.pm";
141 2 $dist =~ s{/+}{-}g;
142 2 $mod =~ s{/+}{::}g;
143  
144 2 ▶ my $mapper = $ENV{AUTOLOAD_SRC} || "http://fastapi.metacpan.org/v1/release";
Condition50%1/2
$ENV{'AUTOLOAD_SRC'} || 'http://fastapi.metacpan.org/v1/release'
l!l
02
MC/DC0%0/1
$ENV{'AUTOLOAD_SRC'} || 'http://fastapi.metacpan.org/v1/release'
$ENV{'AUTOLOAD_SRC'}
Truth table50%1/2
A
A= $ENV{'AUTOLOAD_SRC'}
Aresult
11
00
145 2 my $search = fetch("$mapper/$dist/");
146 0 ▶ warn "DEBUG: Probed: $last_fetched\n" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 0 0
147 0 ▶ if ($search =~ m{download_url.*?(\w+/[\w\d\-\.]+)\.tar.gz}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search =~ m[download_url.*?(\w+/[\w\d\-\.]+)\.tar.gz]) 0 0 0
148 0 my $src = full("/source/$1/");
149 0 ▶ if (my $MANIFEST = fetch "$src/MANIFEST") {
Branch: 1 branch
BranchTrueFalseTotal
if (my $MANIFEST = fetch("$src/MANIFEST")) { } 0 0 0
150 0 ▶ $src = $1 if $last_fetched =~ m{^(.*?)/+MANIFEST};
Branch: 1 branch
BranchTrueFalseTotal
if $Acme::AutoLoad::last_fetched =~ m[^(.*?)/+MANIFEST] 0 0 0
151 0 ▶ if ($MANIFEST =~ m{^lib/}m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MANIFEST =~ m[^lib/]m) { } 0 0 0
152 0 ▶ warn "DEBUG: YEY! Found a lib/ somewhere!\n" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 0 0
153 0 ▶ while ($MANIFEST =~ s{^lib/(\S+\.pm)}{ }m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MANIFEST =~ s[^lib/(\S+\.pm)][ ]m) 0 0 0
154 0 my $remote = $1;
155 0 ▶ warn "DEBUG: MATCH [lib/$remote] RIPPED OUT\n" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 0 0
156 0 $last_fetched = "$src/MANIFEST";
157 0 my $cache = "$lib/$remote";
158 0 ▶ if (!-f $cache) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $cache) 0 0 0
159 0 my $full = full("lib/$remote");
160 0 fly($remote,$full,$cache);
161 }
162 }
163 }
164 else {
165 0 ▶ warn "DEBUG: Oh, too bad there is no magic lib folder in the MANIFEST [$MANIFEST]\n" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 0 0
166 }
167 0 ▶ if (!-f $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $cache_file) 0 0 0
168 # Old versions of h2xs used to toss the end module right into the base folder?
169 0 ▶ if ($f =~ m{(\w+\.pm)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /(\w+\.pm)/) { } 0 0 0
170 0 my $stub = $1;
171 0 ▶ if ($MANIFEST =~ /^(.*$stub)$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MANIFEST =~ /^(.*$stub)$/m) { } 0 0 0
172 0 my $stab = $1;
173 0 $last_fetched = "$src/MANIFEST";
174 0 $stab = full($stab);
175 0 fly($f, $stab, $cache_file);
176 }
177 else {
178 0 ▶ warn "WARNING: No [$stub] in $src/MANIFEST? [$MANIFEST]" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 0 0
179 0 die "No [$stub] in $src/MANIFEST";
180 }
181 }
182 else {
183 0 warn "WARNING: Unable to extract stub from file [$f] ??\n";
184 }
185 }
186 }
187 else {
188 0 warn "$src: Incomplete distribution! Broken MANIFEST file?\n";
189 }
190 }
191 }
192  
193 0 ▶ if (open my $fh, "<", $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $cache_file) 0 0 0
194 0 $INC{$f} = $cache_file;
195 0 return $fh;
196 }
197  
198 0 return ();
199 }
200  
201 sub get {
202 2 ▶ local $_ = shift;
Subroutine get: called CC=3 SCAR=13.7
Pod undocumented
203 2 s{^http(s|)://}{}i;
204 2 s{^([\w\-\.\:]+)$}{$1/};
205 2 s{^([\w\-\.]+)/}{$1:80/};
206 2 ▶ if (m{^([\w\-\.]+:\d+)(/.*)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[^([\w\-\.]+:\d+)(/.*)]) 2 0 2
207 2 my $host = $1;
208 2 my $path = $2;
209 2 ▶ my $r = new IO::Socket::INET $host or return warn "$host$!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $r = "IO::Socket::INET"->new($host) or return warn("$host$!\n") 0 0 0
210 0 $host =~ s/:\d+$//;
211 0 print $r "GET $path HTTP/1.0\r\nUser-Agent: Acme::AutoLoad/url::get\r\nHost: $host\r\n\r\n";
212 0 local $/;
213 0 return [split/[\r\n]{3,}/,<$r>,2]->[1];
214 }
215 0 return "";
216 }
217  
218 1 ▶$INC{"Acme/AutoLoad.pm"} ||= __FILE__;
Condition50%1/2
$INC{"Acme/AutoLoad.pm"} ||= __FILE__
l!l
10
MC/DC0%0/1
$INC{"Acme/AutoLoad.pm"} ||= __FILE__
$INC{"Acme/AutoLoad.pm"}
Truth table50%1/2
A
A= $INC{"Acme/AutoLoad.pm"}
Aresult
11
00
219  
220 1 ▶warn "DEBUG: Congratulations! Acme::AutoLoad was compiled fine.\n" if $ENV{AUTOLOAD_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"AUTOLOAD_DEBUG"} 0 1 1
221  
222 1 1;
223 __END__
↑ Summary