summaryrefslogtreecommitdiffstats
path: root/lib/App/ArchLinux/PackagerTools/CPAN/PackagesDetailsFetcher.pm
blob: 79b149ebc8c602275eafd2f58201276c407493a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package App::ArchLinux::PackagerTools::CPAN::PackagesDetailsFetcher;
use strictures;

use autodie;
use Function::Parameters;
use Log::Any qw($log);
use IPC::Run qw(run);

use App::ArchLinux::PackagerTools::Cache;

=head1 NAME

App::ArchLinux::PackagerTools::CPAN::PackagesDetailsFetcher - Provide access to the 02packages.details CPAN file

=head1 SYNOPSIS

 use App::ArchLinux::PackagerTools::CPAN::PackagesDetailsFetcher;

=head1 DESCRIPTION

Internal class that abstracts access to the 02packages file from CPAN.

=head1 METHODS

=head2 new

 my $fetcher = App::ArchLinux::PackagerTools::CPAN::PackagesDetailsFetcher->new();

Returns a new instance.

=cut

method new($class: $context, $deps = {}) {
	$deps->{config} //= App::ArchLinux::PackagerTools::Config->new($context);
	$deps->{cache} //= App::ArchLinux::PackagerTools::Cache->new($context);
	return $class->new_no_defaults($context, $deps);
}

method new_no_defaults($class: $context, $deps = {}) {
	my $self = {};
	bless $self, $class;
	$self->{deps} = $deps;
	return $self;
}

=head2 Public Methods
=cut

=head3 get_packages_data

 my $data = $fetcher->get_packages_data();

Return the uncompressed data of the 02packages.details.txt.gz file.

=cut

method get_packages_data() {
	my $conf = $self->{deps}->{config}->get_config();
	my $cache_timeout = $conf->{cpan}->{cache_timeout};
	my $mirror_url = $conf->{cpan}->{mirror_url};
	return $self->{deps}->{cache}->compute('02packages', $cache_timeout, sub {
		my $data;
		$log->debugf("Fetching 02packages file from mirror: %s", $mirror_url);
		run [qw(curl -s ), "$mirror_url/modules/02packages.details.txt.gz"], '|', ['zcat'], \$data or die $log->error("Failed to fetch and uncompress CPAN module index: $?")."\n";
		return $data;
	});
}

1;

__END__