Skip to content

Commit 7ac439c

Browse files
briandfoyGrinnz
andcommitted
Use the MetaCPAN URLs to fetch the Changes file, using core modules
though. This was reported as andk/cpanpm#117 Take out LWP and use HTTP::Tiny. Use the MetaCPAN API instead of old search.cpan.org links. Co-authored-by: Dan Book <[email protected]>
1 parent b2238e2 commit 7ac439c

File tree

2 files changed

+92
-45
lines changed

2 files changed

+92
-45
lines changed

Makefile.PL

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ my %WriteMakefile = (
6969
'PREREQ_PM' => {
7070
'Carp' => '0',
7171
'CPAN' => '2.36',
72+
'HTTP::Tiny' => '0',
73+
'JSON::PP' => '0',
7274
},
7375

7476
'META_MERGE' => {

lib/App/Cpan/Upgraded.pm

Lines changed: 90 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,18 @@ is C</usr/local/bin/git>.
292292
293293
=cut
294294

295-
use autouse Carp => qw(carp croak cluck);
296295
use CPAN 1.80 (); # needs no test
297296
use Config;
298-
use autouse Cwd => qw(cwd);
299-
use autouse 'Data::Dumper' => qw(Dumper);
300-
use File::Spec::Functions qw(catfile file_name_is_absolute rel2abs);
301-
use File::Basename;
297+
use Data::Dumper;
302298
use Getopt::Std;
303299

300+
use autouse 'Carp' => qw(carp croak cluck);
301+
use autouse 'Cwd' => qw(cwd);
302+
use autouse 'File::Basename' => qw(dirname);
303+
use autouse 'File::Spec::Functions' => qw(catfile file_name_is_absolute rel2abs);
304+
use autouse 'JSON::PP' => qw(decode_json);
305+
use autouse 'User::pwent' => qw(getpw);
306+
304307
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
305308
# Internal constants
306309
use constant TRUE => 1;
@@ -349,6 +352,8 @@ sub NO_ARGS () { 0 }
349352
sub ARGS () { 1 }
350353
sub GOOD_EXIT () { 0 }
351354

355+
sub dumper { Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(1)->Dump }
356+
352357
%Method_table = (
353358
# key => [ sub ref, takes args?, exit value, description ]
354359

@@ -506,7 +511,7 @@ sub run {
506511
$logger->debug( "Patched cargo culting" );
507512

508513
my $options = $class->_process_options;
509-
$logger->debug( "Options are @{[Dumper($options)]}" );
514+
$logger->debug( "Options are @{[dumper($options)]}" );
510515

511516
$class->_process_setup_options( $options );
512517

@@ -566,8 +571,17 @@ sub _safe_load_module {
566571
local @INC = @INC;
567572
pop @INC if $INC[-1] eq '.';
568573

569-
eval "require $name; 1";
570-
}
574+
my $rc = eval "require $name; 1";
575+
unless( $rc ) {
576+
$logger->error( "Could not load $name" );
577+
}
578+
579+
return $rc;
580+
}
581+
582+
sub _safe_load_modules {
583+
_safe_load_module($_) for @_;
584+
}
571585

572586
sub _init_logger {
573587
my $log4perl_loaded = _safe_load_module("Log::Log4perl");
@@ -900,9 +914,8 @@ sub _expand_filename {
900914
}
901915

902916
sub _home_of {
903-
require User::pwent;
904917
my( $user ) = @_;
905-
my $ent = User::pwent::getpw($user) or return;
918+
my $ent = getpw($user) or return;
906919
return $ent->dir;
907920
}
908921

@@ -1178,28 +1191,29 @@ sub _get_file {
11781191
# subroutine.
11791192
return { path => undef, success => 0 } unless defined $path;
11801193

1181-
my $loaded = _safe_load_module("LWP::Simple");
1182-
croak "You need LWP::Simple to use features that fetch files from CPAN\n"
1194+
my $loaded = _safe_load_module("HTTP::Tiny");
1195+
croak "You need HTTP::Tiny to use features that fetch files from CPAN\n"
11831196
unless $loaded;
11841197

11851198
my $file = substr $path, rindex( $path, '/' ) + 1;
11861199
my $store_path = catfile( cwd(), $file );
11871200
$logger->debug( "Store path is $store_path" );
11881201

1189-
my $status_code;
1190-
my $success = 0;
1202+
my $response;
11911203
foreach my $site ( @{ $CPAN::Config->{urllist} } ) {
11921204
my $fetch_path = join "/", $site, $path;
11931205
$logger->debug( "Trying $fetch_path" );
1194-
$status_code = LWP::Simple::getstore( $fetch_path, $store_path );
1195-
if( 200 <= $status_code and $status_code < 300 ) {
1196-
$success = 1;
1197-
last;
1198-
}
1199-
$logger->warn( "Could not get [$fetch_path]: Status code $status_code" );
1206+
$response = HTTP::Tiny->new->mirror( $fetch_path, $store_path, {} );
1207+
last if $response->{'success'};
1208+
$logger->warn( "Could not get [$fetch_path]: Status code " . $response->{'status'} );
12001209
}
12011210

1202-
return { path => $path, store_path => $store_path, status_code => $status_code, success => $success };
1211+
return {
1212+
path => $path,
1213+
store_path => $store_path,
1214+
status_code => $response->{'status'},
1215+
success => $response->{'success'}
1216+
};
12031217
}
12041218

12051219
sub _gitify {
@@ -1241,49 +1255,80 @@ sub _show_Changes {
12411255
my $args = shift;
12421256

12431257
foreach my $arg ( @$args ) {
1244-
$logger->info( "Checking $arg\n" );
1258+
$logger->info( "Checking Changes for $arg\n" );
12451259

12461260
my $module = _expand_module( $arg ) or next;
1247-
1261+
$logger->debug( dumper($module) );
12481262
my $out = _get_cpanpm_output();
1263+
next unless eval { $module->id };
12491264

1250-
next unless eval { $module->inst_file };
1251-
#next if $module->uptodate;
1252-
1253-
( my $id = $module->id() ) =~ s/::/\-/;
1254-
1255-
my $url = "http://search.cpan.org/~" . lc( $module->userid ) . "/" .
1256-
$id . "-" . $module->cpan_version() . "/";
1257-
1258-
#print "URL: $url\n";
1259-
_get_changes_file($url);
1265+
print _get_changes_file( $module->id );
12601266
}
12611267

12621268
return HEY_IT_WORKED;
12631269
}
12641270

12651271
sub _get_changes_file {
1266-
croak "Reading Changes files requires LWP::Simple and URI\n"
1267-
unless _safe_load_module("LWP::Simple") && _safe_load_module("URI");
1272+
my $r = _safe_load_modules(qw(HTTP::Tiny));
12681273

1269-
my $url = shift;
1274+
my $module = shift;
1275+
$logger->debug("getting Changes for <$module>");
12701276

1271-
my $content = LWP::Simple::get( $url );
1272-
$logger->info( "Got $url ..." ) if defined $content;
1273-
#print $content;
1277+
my $distribution = _get_distribution_name_from_module( $module );
1278+
$logger->debug("Distribution name is <$distribution>");
1279+
return unless defined $distribution;
12741280

1275-
my( $change_link ) = $content =~ m|<a href="(.*?)">Changes</a>|gi;
1281+
my $url = "https://fastapi.metacpan.org/v1/changes/$distribution";
1282+
$logger->debug( "Fetching $url" );
12761283

1277-
my $changes_url = URI->new_abs( $change_link, $url );
1278-
$logger->debug( "Change link is: $changes_url" );
1284+
my $response = HTTP::Tiny->new->get( $url );
1285+
unless( $response->{'success'} ) {
1286+
$logger->error("Could not fetch contents for $url");
1287+
return;
1288+
}
12791289

1280-
my $changes = LWP::Simple::get( $changes_url );
1290+
my $decoded = eval { decode_json($response->{'content'}) };
1291+
unless( $decoded ) {
1292+
$logger->error("Could not decode JSON for $url");
1293+
return;
1294+
}
12811295

1282-
print $changes;
1296+
return unless exists $decoded->{'content'};
1297+
my $changes = $decoded->{'content'};
12831298

1284-
return HEY_IT_WORKED;
1299+
return $changes;
12851300
}
12861301

1302+
sub _get_distribution_name_from_module {
1303+
my $r = _safe_load_modules( qw(HTTP::Tiny) );
1304+
1305+
my( $module ) = @_;
1306+
my $url = "https://fastapi.metacpan.org/v1/module/" . $module;
1307+
my $response = HTTP::Tiny->new->get( $url );
1308+
unless( $response->{'success'} ) {
1309+
$logger->error( "Could not fetch $url" );
1310+
return;
1311+
}
1312+
1313+
unless( $response->{'content'} ) {
1314+
$logger->error("No distribution name for $module");
1315+
return;
1316+
}
1317+
1318+
return do {
1319+
my $decoded = eval { decode_json($response->{'content'}) };
1320+
if( ! defined $decoded ) {
1321+
$logger->error( "Could not decode JSON from $url" ); ();
1322+
}
1323+
elsif( ! exists $decoded->{'distribution'} ) {
1324+
$logger->error( "Missing content in JSON from $url" ); ();
1325+
}
1326+
else {
1327+
$decoded->{'distribution'};
1328+
}
1329+
};
1330+
}
1331+
12871332
sub _show_Author {
12881333
my $args = shift;
12891334

0 commit comments

Comments
 (0)