| 1 | package Dist::Surveyor::Inquiry; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Memoize; # core | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Fcntl qw(:DEFAULT :flock); # core | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Dist::Surveyor::DB_File; # internal | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use JSON::MaybeXS qw(JSON decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw(looks_like_number); # core | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | our $VERSION = '0.023'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Dist::Surveyor::Inquiry - Handling the meta-cpan API access for Dist::Surveyor | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | There are a few things that needed to be known in this module: | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =over | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =item * | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | $metacpan_size - internally defined global to limit the maximum size of | ||||||||||||||||||||||||||||
| 30 | every API call | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =item * | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | $metacpan_calls - internally defined global counting how many API call happen. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item * | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | This module checks $::DEBUG and $::VERBOSE for obvious proposes. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item * | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | For initating cache-on-disk, call Dist::Surveyor::Inquiry->perma_cache() | ||||||||||||||||||||||||||||
| 43 | (this should be usually done, except in testing environment) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =back | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # We have to limit the number of results when using MetaCPAN::API. | ||||||||||||||||||||||||||||
| 50 | # We can'r make it too large as it hurts the server (it preallocates) | ||||||||||||||||||||||||||||
| 51 | # but need to make it large enough for worst case distros (eg eBay-API). | ||||||||||||||||||||||||||||
| 52 | # TODO: switching to the ElasticSearch module, with cursor support, will | ||||||||||||||||||||||||||||
| 53 | # probably avoid the need for this. Else we could dynamically adjust. | ||||||||||||||||||||||||||||
| 54 | 3 | our $metacpan_size = 2500; | |||||||||||||||||||||||||||
| 55 | 3 | our $metacpan_calls = 0; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | our ($DEBUG, $VERBOSE); | |||||||||||||||||||||||||||
| 58 | 3 | *DEBUG = \$::DEBUG; | |||||||||||||||||||||||||||
| 59 | 3 | *VERBOSE = \$::VERBOSE; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 62 | 3 | our @ISA = qw{Exporter}; | |||||||||||||||||||||||||||
| 63 | 3 | our @EXPORT = qw{ | |||||||||||||||||||||||||||
| 64 | get_candidate_cpan_dist_releases | ||||||||||||||||||||||||||||
| 65 | get_candidate_cpan_dist_releases_fallback | ||||||||||||||||||||||||||||
| 66 | get_module_versions_in_release | ||||||||||||||||||||||||||||
| 67 | get_release_info | ||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | my $agent_string = "dist_surveyor/$VERSION"; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | my ($ua, $wget, $curl); | |||||||||||||||||||||||||||
| 73 | 3 | ▶ | if (HTTP::Tiny->can_ssl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 3 | $ua = HTTP::Tiny->new( | |||||||||||||||||||||||||||
| 75 | agent => $agent_string, | ||||||||||||||||||||||||||||
| 76 | timeout => 10, | ||||||||||||||||||||||||||||
| 77 | keep_alive => 1, | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | } else { # for fatpacking support | ||||||||||||||||||||||||||||
| 80 | 0 | require File::Which; | |||||||||||||||||||||||||||
| 81 | 0 | require IPC::System::Simple; | |||||||||||||||||||||||||||
| 82 | 0 | $wget = File::Which::which('wget'); | |||||||||||||||||||||||||||
| 83 | 0 | $curl = File::Which::which('curl'); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 60 | ▶ | sub _https_request { | ||||||||||||||||||||||||||
|
Subroutine
_https_request: called CC=14 SCAR=38.7
| |||||||||||||||||||||||||||||
| 87 | 60 | my ($method, $url, $headers, $content) = @_; | |||||||||||||||||||||||||||
| 88 | 60 | ▶ | $headers ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$headers ||= {}
MC/DC100%1/1
$headers ||= {}
$headers
Truth table100%2/2
A
A= $headers
| |||||||||||||||||||||||||||||
| 89 | 60 | ▶ | $method = uc($method || 'GET'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$method || 'GET'
MC/DC0%0/1
$method || 'GET'
$method
Truth table50%1/2
A
A= $method
| |||||||||||||||||||||||||||||
| 90 | 60 | ▶ | if (defined $ua) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 91 | 60 | my %options; | |||||||||||||||||||||||||||
| 92 | 60 | ▶ | $options{headers} = $headers if %$headers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 60 | ▶ | $options{content} = $content if defined $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 60 | my $response = $ua->request($method, $url, \%options); | |||||||||||||||||||||||||||
| 95 | 60 | ▶ | unless ($response->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | die "Transport error: $response->{content}\n" if $response->{status} == 599; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | die "HTTP error: $response->{status} $response->{reason}\n"; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 60 | return $response->{content}; | |||||||||||||||||||||||||||
| 100 | } elsif (defined $wget) { | ||||||||||||||||||||||||||||
| 101 | 0 | my @args = ('-q', '-O', '-', '-U', $agent_string, '-T', 10, '--method', $method); | |||||||||||||||||||||||||||
| 102 | 0 | push @args, '--header', "$_: $headers->{$_}" for keys %$headers; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | push @args, '--body-data', $content if defined $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | return IPC::System::Simple::capturex($wget, @args, $url); | |||||||||||||||||||||||||||
| 105 | } elsif (defined $curl) { | ||||||||||||||||||||||||||||
| 106 | 0 | my @args = ('-s', '-S', '-L', '-A', $agent_string, '--connect-timeout', 10, '-X', $method); | |||||||||||||||||||||||||||
| 107 | 0 | push @args, '-H', "$_: $headers->{$_}" for keys %$headers; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | push @args, '--data-raw', $content if defined $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | return IPC::System::Simple::capturex($curl, @args, $url); | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 0 | die "None of IO::Socket::SSL, wget, or curl are available; cannot make HTTPS requests."; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # caching via persistent memoize | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 3 | my %memoize_cache; | |||||||||||||||||||||||||||
| 118 | my $locking_file; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 Dist::Surveyor::Inquiry->perma_cache() | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Enable caching to disk of all the MetaCPAN API requests. | ||||||||||||||||||||||||||||
| 125 | This cache can grew to be quite big - 40MB is one case, but it worth it, | ||||||||||||||||||||||||||||
| 126 | as if you will need to run this program again, it will run much faster. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub perma_cache { | ||||||||||||||||||||||||||
|
Subroutine
perma_cache: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 132 | 0 | my $db_generation = 3; # XXX increment on incompatible change | |||||||||||||||||||||||||||
| 133 | 0 | my $pname = $FindBin::Script; | |||||||||||||||||||||||||||
| 134 | 0 | $pname =~ s/\..*$//; | |||||||||||||||||||||||||||
| 135 | 0 | my $memoize_file = "$pname-$db_generation.db"; | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | open $locking_file, ">", "$memoize_file.lock" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | or die "Unable to open lock file: $!"; | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | flock ($locking_file, LOCK_EX) || die "flock: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | tie %memoize_cache => 'Dist::Surveyor::DB_File', $memoize_file, O_CREAT|O_RDWR, 0640 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | or die "Unable to use persistent cache: $!"; | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | my @memoize_subs = qw( | |||||||||||||||||||||||||||
| 144 | get_candidate_cpan_dist_releases | ||||||||||||||||||||||||||||
| 145 | get_candidate_cpan_dist_releases_fallback | ||||||||||||||||||||||||||||
| 146 | get_module_versions_in_release | ||||||||||||||||||||||||||||
| 147 | get_release_info | ||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||
| 149 | 3 | for my $subname (@memoize_subs) { | |||||||||||||||||||||||||||
| 150 | my %memoize_args = ( | ||||||||||||||||||||||||||||
| 151 | SCALAR_CACHE => [ HASH => \%memoize_cache ], | ||||||||||||||||||||||||||||
| 152 | LIST_CACHE => 'FAULT', | ||||||||||||||||||||||||||||
| 153 | 62 | ▶ | NORMALIZER => sub { return join("\034", $subname, @_) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 154 | 12 | ); | |||||||||||||||||||||||||||
| 155 | 12 | memoize($subname, %memoize_args); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 get_release_info($author, $release) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Receive release info, such as: | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | get_release_info('SEMUELF', 'Dist-Surveyor-0.009') | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Returns a hashref containing all that release meta information, returned by | ||||||||||||||||||||||||||||
| 167 | C<https://fastapi.metacpan.org/v1/release/$author/$release> | ||||||||||||||||||||||||||||
| 168 | (but not information on the files inside the module) | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Dies on HTTP error, and warns on empty response. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 4 | ▶ | sub get_release_info { | ||||||||||||||||||||||||||
|
Subroutine
get_release_info: called CC=3 SCAR=12.6
| |||||||||||||||||||||||||||||
| 175 | 4 | my ($author, $release) = @_; | |||||||||||||||||||||||||||
| 176 | 4 | $metacpan_calls++; | |||||||||||||||||||||||||||
| 177 | 4 | my $response = _https_request(GET => "https://fastapi.metacpan.org/v1/release/$author/$release"); | |||||||||||||||||||||||||||
| 178 | 4 | my $release_data = decode_json $response; | |||||||||||||||||||||||||||
| 179 | 4 | ▶ | if (!$release_data or !$release_data->{release}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$release_data and $release_data->{"release"}
MC/DC0%0/2
$release_data and $release_data->{"release"}
$release_data
$release_data->{"release"}
Truth table33%1/3
A and B
A= $release_dataB= $release_data->{"release"}
| |||||||||||||||||||||||||||||
| 180 | 0 | warn "Can't find release details for $author/$release - SKIPPED!\n"; | |||||||||||||||||||||||||||
| 181 | 0 | return; # XXX could fake some of $release_data instead | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 4 | return $release_data->{release}; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 get_candidate_cpan_dist_releases($module, $version, $file_size) | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Return a hashref containing all the releases that contain this module | ||||||||||||||||||||||||||||
| 189 | (with the specific version and file size combination) | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | The keys are the release name (i.e. 'Dist-Surveyor-0.009') and the value | ||||||||||||||||||||||||||||
| 192 | is a hashref containing release information and file information: | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 'Dist-Surveyor-0.009' => { | ||||||||||||||||||||||||||||
| 195 | # release information | ||||||||||||||||||||||||||||
| 196 | 'date' => '2013-02-20T06:48:35.000Z', | ||||||||||||||||||||||||||||
| 197 | 'version' => '0.009', | ||||||||||||||||||||||||||||
| 198 | 'author' => 'SEMUELF', | ||||||||||||||||||||||||||||
| 199 | 'version_numified' => '0.009', | ||||||||||||||||||||||||||||
| 200 | 'release' => 'Dist-Surveyor-0.009', | ||||||||||||||||||||||||||||
| 201 | 'distribution' => 'Dist-Surveyor', | ||||||||||||||||||||||||||||
| 202 | 'version_obj' => <version object 0.009>, | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # File information | ||||||||||||||||||||||||||||
| 205 | 'path' => 'lib/Dist/Surveyor/DB_File.pm', | ||||||||||||||||||||||||||||
| 206 | 'stat.mtime' => 1361342736, | ||||||||||||||||||||||||||||
| 207 | 'module.version' => '0.009' | ||||||||||||||||||||||||||||
| 208 | 'module.version_numified' => '0.009', | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 7 | ▶ | sub get_candidate_cpan_dist_releases { | ||||||||||||||||||||||||||
|
Subroutine
get_candidate_cpan_dist_releases: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 214 | 7 | my ($module, $version, $file_size) = @_; | |||||||||||||||||||||||||||
| 215 | 7 | my $funcstr = "get_candidate_cpan_dist_releases($module, $version, $file_size)"; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 7 | my $version_qual = _prepare_version_query(0, $version); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 7 | ▶ | my @and_quals = ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | {"term" => {"module.name" => $module }}, | ||||||||||||||||||||||||||||
| 221 | (@$version_qual > 1 ? { "bool" => { "should" => $version_qual } } : $version_qual->[0]), | ||||||||||||||||||||||||||||
| 222 | ); | ||||||||||||||||||||||||||||
| 223 | 7 | ▶ | push @and_quals, {"term" => {"stat.size" => $file_size }} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | if $file_size; | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # XXX doesn't cope with odd cases like | ||||||||||||||||||||||||||||
| 227 | # http://explorer.metacpan.org/?url=/module/MLEHMANN/common-sense-3.4/sense.pm.PL | ||||||||||||||||||||||||||||
| 228 | 7 | $metacpan_calls++; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 7 | my $query = { | |||||||||||||||||||||||||||
| 231 | "size" => $metacpan_size, | ||||||||||||||||||||||||||||
| 232 | "query" => { "bool" => { | ||||||||||||||||||||||||||||
| 233 | "filter" => \@and_quals, | ||||||||||||||||||||||||||||
| 234 | }}, | ||||||||||||||||||||||||||||
| 235 | "fields" => [qw( | ||||||||||||||||||||||||||||
| 236 | release _parent author version version_numified module.version | ||||||||||||||||||||||||||||
| 237 | module.version_numified date stat.mtime distribution path | ||||||||||||||||||||||||||||
| 238 | )] | ||||||||||||||||||||||||||||
| 239 | }; | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 7 | my $response = _https_request(POST => 'https://fastapi.metacpan.org/v1/file', | |||||||||||||||||||||||||||
| 242 | { 'Content-Type' => 'application/json;charset=UTF-8' }, | ||||||||||||||||||||||||||||
| 243 | JSON->new->utf8->canonical->encode($query), | ||||||||||||||||||||||||||||
| 244 | ); | ||||||||||||||||||||||||||||
| 245 | 7 | return _process_response($funcstr, $response); | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head2 get_candidate_cpan_dist_releases_fallback($module, $version) | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Similar to get_candidate_cpan_dist_releases, but getting called when | ||||||||||||||||||||||||||||
| 251 | get_candidate_cpan_dist_releases fails for find matching file and release. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Maybe the file was tempared somehow, so the file size does not match anymore. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | sub get_candidate_cpan_dist_releases_fallback { | ||||||||||||||||||||||||||
|
Subroutine
get_candidate_cpan_dist_releases_fallback: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 258 | 0 | my ($module, $version) = @_; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # fallback to look for distro of the same name as the module | ||||||||||||||||||||||||||||
| 261 | # for odd cases like | ||||||||||||||||||||||||||||
| 262 | # http://explorer.metacpan.org/?url=/module/MLEHMANN/common-sense-3.4/sense.pm.PL | ||||||||||||||||||||||||||||
| 263 | 0 | (my $distname = $module) =~ s/::/-/g; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | my $version_qual = _prepare_version_query(1, $version); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | ▶ | my @and_quals = ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | {"term" => {"distribution" => $distname }}, | ||||||||||||||||||||||||||||
| 269 | (@$version_qual > 1 ? { "bool" => { "should" => $version_qual } } : $version_qual->[0]), | ||||||||||||||||||||||||||||
| 270 | ); | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | # XXX doesn't cope with odd cases like | ||||||||||||||||||||||||||||
| 273 | 0 | $metacpan_calls++; | |||||||||||||||||||||||||||
| 274 | 0 | my $query = { | |||||||||||||||||||||||||||
| 275 | "size" => $metacpan_size, | ||||||||||||||||||||||||||||
| 276 | "query" => { "bool" => { | ||||||||||||||||||||||||||||
| 277 | "filter" => \@and_quals, | ||||||||||||||||||||||||||||
| 278 | }}, | ||||||||||||||||||||||||||||
| 279 | "fields" => [qw( | ||||||||||||||||||||||||||||
| 280 | release _parent author version version_numified module.version | ||||||||||||||||||||||||||||
| 281 | module.version_numified date stat.mtime distribution path)] | ||||||||||||||||||||||||||||
| 282 | }; | ||||||||||||||||||||||||||||
| 283 | 0 | my $response = _https_request(POST => 'https://fastapi.metacpan.org/v1/file', | |||||||||||||||||||||||||||
| 284 | { 'Content-Type' => 'application/json;charset=UTF-8' }, | ||||||||||||||||||||||||||||
| 285 | JSON->new->utf8->canonical->encode($query), | ||||||||||||||||||||||||||||
| 286 | ); | ||||||||||||||||||||||||||||
| 287 | 0 | return _process_response("get_candidate_cpan_dist_releases_fallback($module, $version)", $response); | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 7 | ▶ | sub _prepare_version_query { | ||||||||||||||||||||||||||
|
Subroutine
_prepare_version_query: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 291 | 7 | my ($is_fallback, $version) = @_; | |||||||||||||||||||||||||||
| 292 | 7 | ▶ | $version = 0 if not defined $version; # XXX | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 7 | ▶ | my ($v_key, $num_key) = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | $is_fallback | ||||||||||||||||||||||||||||
| 295 | ? qw{ version version_numified } | ||||||||||||||||||||||||||||
| 296 | : qw{ module.version module.version_numified }; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # timbunce: So, the current situation is that: version_numified is a float | ||||||||||||||||||||||||||||
| 299 | # holding version->parse($raw_version)->numify, and version is a string | ||||||||||||||||||||||||||||
| 300 | # also holding version->parse($raw_version)->numify at the moment, and | ||||||||||||||||||||||||||||
| 301 | # that'll change to ->stringify at some point. Is that right now? | ||||||||||||||||||||||||||||
| 302 | # mo: yes, I already patched the indexer, so new releases are already | ||||||||||||||||||||||||||||
| 303 | # indexed ok, but for older ones I need to reindex cpan | ||||||||||||||||||||||||||||
| 304 | 7 | ▶ | my $v = (ref $version && $version->isa('version')) ? $version : version->parse($version); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $version and $version->isa("version")
MC/DC50%1/2
ref $version and $version->isa("version")
ref $version
$version->isa("version")
Truth table66%2/3
A and B
A= ref $versionB= $version->isa("version")
| |||||||||||||||||||||||||||||
| 305 | 7 | my %v = map { $_ => 1 } "$version", $v->stringify, $v->numify; | |||||||||||||||||||||||||||
| 306 | 7 | my @version_qual; | |||||||||||||||||||||||||||
| 307 | push @version_qual, { term => { $v_key => $_ } } | ||||||||||||||||||||||||||||
| 308 | 7 | for keys %v; | |||||||||||||||||||||||||||
| 309 | push @version_qual, { term => { $num_key => $_ }} | ||||||||||||||||||||||||||||
| 310 | 7 | for grep { looks_like_number($_) } keys %v; | |||||||||||||||||||||||||||
| 311 | 7 | return \@version_qual; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 7 | ▶ | sub _process_response { | ||||||||||||||||||||||||||
|
Subroutine
_process_response: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 315 | 7 | my ($funcname, $response) = @_; | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 7 | my $results = decode_json $response; | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 7 | my $hits = $results->{hits}{hits}; | |||||||||||||||||||||||||||
| 320 | 7 | ▶ | die "$funcname: too many results (>$metacpan_size)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | if @$hits >= $metacpan_size; | ||||||||||||||||||||||||||||
| 322 | warn "$funcname: ".Dumper($results) | ||||||||||||||||||||||||||||
| 323 | 7 | ▶ | if grep { not $_->{fields}{release} } @$hits; # XXX temp, seen once but not since | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | # filter out perl-like releases | ||||||||||||||||||||||||||||
| 326 | @$hits = | ||||||||||||||||||||||||||||
| 327 | 52 | grep { $_->{fields}{path} !~ m!^(?:t|xt|tests?|inc|samples?|ex|examples?|bak|local-lib)\b! } | |||||||||||||||||||||||||||
| 328 | 7 | grep { $_->{fields}{release} !~ /^(perl|ponie|parrot|kurila|SiePerl-)/ } | |||||||||||||||||||||||||||
| 329 | @$hits; | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 7 | for my $hit (@$hits) { | |||||||||||||||||||||||||||
| 332 | 51 | $hit->{release_id} = delete $hit->{_parent}; | |||||||||||||||||||||||||||
| 333 | # add version_obj for convenience (will fail and be undef for releases like "0.08124-TRIAL") | ||||||||||||||||||||||||||||
| 334 | 51 | $hit->{fields}{version_obj} = eval { version->parse($hit->{fields}{version}) }; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # we'll return { "Dist-Name-Version" => { details }, ... } | ||||||||||||||||||||||||||||
| 338 | 7 | my %dists = map { $_->{fields}{release} => $_->{fields} } @$hits; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 0 | ▶ | warn "$funcname: @{[ sort keys %dists ]}\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | if $VERBOSE; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 7 | return \%dists; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =head2 get_module_versions_in_release($author, $release) | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | Receive release info, such as: | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | get_module_versions_in_release('SEMUELF', 'Dist-Surveyor-0.009') | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | And returns a hashref, that contains one entry for each module that exists | ||||||||||||||||||||||||||||
| 353 | in the release. module information is the format: | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 'Dist::Surveyor' => { | ||||||||||||||||||||||||||||
| 356 | 'version' => '0.009', | ||||||||||||||||||||||||||||
| 357 | 'name' => 'Dist::Surveyor', | ||||||||||||||||||||||||||||
| 358 | 'path' => 'lib/Dist/Surveyor.pm', | ||||||||||||||||||||||||||||
| 359 | 'size' => 43879 | ||||||||||||||||||||||||||||
| 360 | }, | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | this function can be called for all sorts of releases that are only vague | ||||||||||||||||||||||||||||
| 363 | possibilities and aren't actually installed, so generally it's quiet | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =cut | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 49 | ▶ | sub get_module_versions_in_release { | ||||||||||||||||||||||||||
|
Subroutine
get_module_versions_in_release: called CC=17 SCAR=35.8
| |||||||||||||||||||||||||||||
| 368 | 49 | my ($author, $release) = @_; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 49 | $metacpan_calls++; | |||||||||||||||||||||||||||
| 371 | 49 | my $results = eval { | |||||||||||||||||||||||||||
| 372 | 49 | my $query = { | |||||||||||||||||||||||||||
| 373 | "size" => $metacpan_size, | ||||||||||||||||||||||||||||
| 374 | "query" => { "bool" => { | ||||||||||||||||||||||||||||
| 375 | "filter" => [ | ||||||||||||||||||||||||||||
| 376 | {"term" => {"release" => $release }}, | ||||||||||||||||||||||||||||
| 377 | {"term" => {"author" => $author }}, | ||||||||||||||||||||||||||||
| 378 | {"term" => {"mime" => "text/x-script.perl-module"}}, | ||||||||||||||||||||||||||||
| 379 | ], | ||||||||||||||||||||||||||||
| 380 | }}, | ||||||||||||||||||||||||||||
| 381 | "fields" => ["path","name","stat.size"], | ||||||||||||||||||||||||||||
| 382 | }; | ||||||||||||||||||||||||||||
| 383 | 49 | my $response = _https_request(POST => 'https://fastapi.metacpan.org/v1/file', | |||||||||||||||||||||||||||
| 384 | { 'Content-Type' => 'application/json;charset=UTF-8' }, | ||||||||||||||||||||||||||||
| 385 | JSON->new->utf8->canonical->encode($query), | ||||||||||||||||||||||||||||
| 386 | ); | ||||||||||||||||||||||||||||
| 387 | 49 | decode_json $response; | |||||||||||||||||||||||||||
| 388 | }; | ||||||||||||||||||||||||||||
| 389 | 49 | ▶ | if (not $results) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 0 | warn "Failed get_module_versions_in_release for $author/$release: $@"; | |||||||||||||||||||||||||||
| 391 | 0 | return {}; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | 49 | my $hits = $results->{hits}{hits}; | |||||||||||||||||||||||||||
| 394 | 49 | ▶ | die "get_module_versions_in_release($author, $release): too many results" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | if @$hits >= $metacpan_size; | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 49 | my %modules_in_release; | |||||||||||||||||||||||||||
| 398 | 49 | for my $hit (@$hits) { | |||||||||||||||||||||||||||
| 399 | 1926 | my $path = $hit->{fields}{path}; | |||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # XXX try to ignore files that won't get installed | ||||||||||||||||||||||||||||
| 402 | # XXX should use META noindex! | ||||||||||||||||||||||||||||
| 403 | 1926 | ▶ | if ($path =~ m!^(?:t|xt|tests?|inc|samples?|ex|examples?|bak|local-lib)\b!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | 137 | ▶ | warn "$author/$release: ignored non-installed module $path\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | if $DEBUG; | ||||||||||||||||||||||||||||
| 406 | 137 | next; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 1789 | my $size = $hit->{fields}{"stat.size"}; | |||||||||||||||||||||||||||
| 410 | # files can contain more than one package ('module') | ||||||||||||||||||||||||||||
| 411 | 1789 | ▶ | my $rel_mods = $hit->{_source}{module} || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$hit->{'_source'}{'module'} || []
MC/DC0%0/1
$hit->{'_source'}{'module'} || []
$hit->{'_source'}{'module'}
Truth table50%1/2
A
A= $hit->{'_source'}{'module'}
| |||||||||||||||||||||||||||||
| 412 | 1789 | for my $mod (@$rel_mods) { # actually packages in the file | |||||||||||||||||||||||||||
| 413 | # Some files may contain multiple packages. We want to ignore | ||||||||||||||||||||||||||||
| 414 | # all except the one that matches the name of the file. | ||||||||||||||||||||||||||||
| 415 | # We use a fairly loose (but still very effective) test because we | ||||||||||||||||||||||||||||
| 416 | # can't rely on $path including the full package name. | ||||||||||||||||||||||||||||
| 417 | 2126 | (my $filebasename = $hit->{fields}{name}) =~ s/\.pm$//; | |||||||||||||||||||||||||||
| 418 | 2126 | ▶ | if ($mod->{name} !~ m/\b$filebasename$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 371 | ▶ | warn "$author/$release: ignored $mod->{name} in $path\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | if $DEBUG; | ||||||||||||||||||||||||||||
| 421 | 371 | next; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | # warn if package previously seen in this release | ||||||||||||||||||||||||||||
| 425 | # with a different version or file size | ||||||||||||||||||||||||||||
| 426 | 1755 | ▶ | my $mod_version = $mod->{version} || $mod->{version_numified}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$mod->{'version'} || $mod->{'version_numified'}
MC/DC50%1/2
$mod->{'version'} || $mod->{'version_numified'}
$mod->{'version'}
$mod->{'version_numified'}
Truth table66%2/3
A || B
A= $mod->{'version'}B= $mod->{'version_numified'}
| |||||||||||||||||||||||||||||
| 427 | 1755 | ▶ | if (my $prev = $modules_in_release{$mod->{name}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 428 | 0 | my $version_obj = eval { version->parse($mod_version) }; | |||||||||||||||||||||||||||
| 429 | 0 | ▶ | die "$author/$release: $mod->{name} $mod_version: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 0 | ▶ | if ($VERBOSE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | # XXX could add a show-only-once cache here | ||||||||||||||||||||||||||||
| 433 | 0 | my $msg = "$mod->{name} $mod_version ($size) seen in $path after $prev->{path} $prev->{version} ($prev->{size})"; | |||||||||||||||||||||||||||
| 434 | warn "$release: $msg\n" | ||||||||||||||||||||||||||||
| 435 | 0 | ▶ | if ($version_obj != version->parse($prev->{version}) or $size != $prev->{size}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$version_obj != "version"->parse($prev->{"version"}) or $size != $prev->{"size"}
MC/DC0%0/2
$version_obj != "version"->parse($prev->{"version"}) or $size != $prev->{"size"}
$version_obj != "version"->parse($prev->{"version"})
$size != $prev->{"size"}
Truth table0%0/3
A or B
A= $version_obj != "version"->parse($prev->{"version"})B= $size != $prev->{"size"}
| |||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # keep result small as Storable thawing this is major runtime cost | ||||||||||||||||||||||||||||
| 440 | # (specifically we avoid storing a version_obj here) | ||||||||||||||||||||||||||||
| 441 | $modules_in_release{$mod->{name}} = { | ||||||||||||||||||||||||||||
| 442 | name => $mod->{name}, | ||||||||||||||||||||||||||||
| 443 | 1755 | path => $path, | |||||||||||||||||||||||||||
| 444 | version => $mod_version, | ||||||||||||||||||||||||||||
| 445 | size => $size, | ||||||||||||||||||||||||||||
| 446 | }; | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 0 | ▶ | warn "\n$author/$release contains: @{[ map { qq($_->{name} $_->{version}) } values %modules_in_release ]}\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | if $DEBUG; | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 49 | return \%modules_in_release; | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =head1 License, Copyright | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | Please see L<Dist::Surveyor> for details | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =cut | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 3 | 1; | |||||||||||||||||||||||||||