| 1 | package CPAN::Maker::Bootstrapper::Role::DarkPANRequires; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp qw(croak); | ||
| 7 | use CLI::Simple::Constants qw(:booleans); | ||
| 8 | use CLI::Simple::Utils qw(slurp choose); | ||
| 9 | use Data::Dumper; | ||
| 10 | use English qw(-no_match_vars); | ||
| 11 | use JSON; | ||
| 12 | |||
| 13 | use Role::Tiny; | ||
| 14 | |||
| 15 | use Readonly; | ||
| 16 | |||
| 17 | Readonly::Scalar our $METACPAN_URL => 'https://fastapi.metacpan.org/v1/download_url/'; | ||
| 18 | Readonly::Scalar our $CPANFILE_DARKPAN => 'cpanfile.darkpan'; | ||
| 19 | Readonly::Scalar our $CPANM_DARKPAN => 'cpanm.darkpan'; | ||
| 20 | Readonly::Scalar our $REQUIRES_FILE => 'requires'; | ||
| 21 | |||
| 22 | ######################################################################## | ||
| 23 | sub cmd_create_darkpan_requires { | ||
| 24 | ######################################################################## | ||
| 25 | my ($self) = @_; | ||
| 26 | |||
| 27 | require HTTP::Tiny; | ||
| 28 | require IO::Uncompress::Gunzip; | ||
| 29 | |||
| 30 | my ($requires_file) = $self->get_args; | ||
| 31 | $requires_file //= $REQUIRES_FILE; | ||
| 32 | |||
| 33 | my $filter = choose { | ||
| 34 | my $filter_file = $self->get_filter; | ||
| 35 | |||
| 36 | return {} | ||
| 37 | if !$filter_file; | ||
| 38 | |||
| 39 | die "ERROR: $filter_file not found\n" | ||
| 40 | if !-f $filter_file; | ||
| 41 | |||
| 42 | return { map { $_ => 1 } split /\n/xsm, slurp($filter_file) }; | ||
| 43 | }; | ||
| 44 | |||
| 45 | open my $requires_fh, '<', $requires_file | ||
| 46 | or die sprintf 'ERROR: Could not open %s: %s', $requires_file, $OS_ERROR; | ||
| 47 | |||
| 48 | my $darkpan_index = $self->fetch_darkpan_index; | ||
| 49 | |||
| 50 | my %darkpan_requires; | ||
| 51 | |||
| 52 | while ( my $line = <$requires_fh> ) { | ||
| 53 | chomp $line; | ||
| 54 | |||
| 55 | next | ||
| 56 | if $line =~ /^\s*$/; | ||
| 57 | |||
| 58 | next | ||
| 59 | if $line =~ /^\s*#/; | ||
| 60 | |||
| 61 | my ( $module, $version ) = split /\s+/, $line, 2; | ||
| 62 | |||
| 63 | next | ||
| 64 | if !$module; | ||
| 65 | |||
| 66 | next | ||
| 67 | if !$self->is_on_darkpan( $module, $darkpan_index ); | ||
| 68 | |||
| 69 | my $distribution = $self->is_on_metacpan($module); | ||
| 70 | |||
| 71 | if ($distribution) { | ||
| 72 | $self->get_logger->warn( sprintf '%s is available from both CPAN and the configured DarkPAN; preferring DarkPAN', | ||
| 73 | $module ); | ||
| 74 | } | ||
| 75 | |||
| 76 | next if exists $filter->{$module}; # filter unwanted darkpan distributions | ||
| 77 | |||
| 78 | $version //= 0; | ||
| 79 | |||
| 80 | $darkpan_requires{$module} = $version; | ||
| 81 | } | ||
| 82 | |||
| 83 | close $requires_fh | ||
| 84 | or croak sprintf 'Could not close %s: %s', | ||
| 85 | $REQUIRES_FILE, | ||
| 86 | $OS_ERROR; | ||
| 87 | |||
| 88 | $self->write_cpanfile_darkpan( \%darkpan_requires ); | ||
| 89 | |||
| 90 | $self->write_cpanm_darkpan( \%darkpan_requires ); | ||
| 91 | |||
| 92 | return $SUCCESS; | ||
| 93 | } | ||
| 94 | |||
| 95 | ######################################################################## | ||
| 96 | sub write_cpanfile_darkpan { | ||
| 97 | ######################################################################## | ||
| 98 | my ( $self, $requires ) = @_; | ||
| 99 | |||
| 100 | open my $fh, '>', $CPANFILE_DARKPAN | ||
| 101 | or croak sprintf 'Could not write %s: %s', | ||
| 102 | $CPANFILE_DARKPAN, | ||
| 103 | $OS_ERROR; | ||
| 104 | |||
| 105 | for my $module ( sort keys %{$requires} ) { | ||
| 106 | my $version = $requires->{$module}; | ||
| 107 | |||
| 108 | printf {$fh} | ||
| 109 | "requires '%s', '%s';\n", | ||
| 110 | $module, | ||
| 111 | $version; | ||
| 112 | } | ||
| 113 | |||
| 114 | close $fh | ||
| 115 | or croak sprintf 'Could not close %s: %s', | ||
| 116 | $CPANFILE_DARKPAN, | ||
| 117 | $OS_ERROR; | ||
| 118 | |||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | ######################################################################## | ||
| 123 | sub write_cpanm_darkpan { | ||
| 124 | ######################################################################## | ||
| 125 | my ( $self, $requires ) = @_; | ||
| 126 | |||
| 127 | open my $fh, '>', $CPANM_DARKPAN | ||
| 128 | or croak sprintf 'Could not write %s: %s', | ||
| 129 | $CPANM_DARKPAN, | ||
| 130 | $OS_ERROR; | ||
| 131 | |||
| 132 | for my $module ( sort keys %{$requires} ) { | ||
| 133 | my $version = $requires->{$module}; | ||
| 134 | |||
| 135 | if ( !$version || $version eq '0' ) { | ||
| 136 | print {$fh} $module, "\n"; | ||
| 137 | next; | ||
| 138 | } | ||
| 139 | |||
| 140 | printf {$fh} | ||
| 141 | "%s~%s\n", | ||
| 142 | $module, | ||
| 143 | $version; | ||
| 144 | } | ||
| 145 | |||
| 146 | close $fh | ||
| 147 | or croak sprintf 'Could not close %s: %s', | ||
| 148 | $CPANM_DARKPAN, | ||
| 149 | $OS_ERROR; | ||
| 150 | |||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | ######################################################################## | ||
| 155 | sub fetch_darkpan_index { | ||
| 156 | ######################################################################## | ||
| 157 | my ($self) = @_; | ||
| 158 | |||
| 159 | my $darkpan_url = $ENV{DARKPAN_URL}; | ||
| 160 | |||
| 161 | croak 'DARKPAN_URL is not set' | ||
| 162 | if !$darkpan_url; | ||
| 163 | |||
| 164 | $darkpan_url =~ s{/\z}{}; | ||
| 165 | |||
| 166 | my $url = $darkpan_url . '/modules/02packages.details.txt.gz'; | ||
| 167 | |||
| 168 | my $listing = $self->fetch_02packages($url); | ||
| 169 | |||
| 170 | $listing =~ s/\A.*?\n\n//s; | ||
| 171 | |||
| 172 | my %modules; | ||
| 173 | |||
| 174 | for my $line ( split /\n/, $listing ) { | ||
| 175 | next | ||
| 176 | if !$line; | ||
| 177 | |||
| 178 | my ( $module, $version, $distribution ) = split /\s+/, $line, 3; | ||
| 179 | |||
| 180 | next | ||
| 181 | if !$module; | ||
| 182 | |||
| 183 | $modules{$module} = { | ||
| 184 | version => $version, | ||
| 185 | distribution => $distribution, | ||
| 186 | }; | ||
| 187 | } | ||
| 188 | |||
| 189 | return \%modules; | ||
| 190 | } | ||
| 191 | |||
| 192 | ######################################################################## | ||
| 193 | sub is_on_darkpan { | ||
| 194 | ######################################################################## | ||
| 195 | my ( $self, $module, $darkpan_index ) = @_; | ||
| 196 | |||
| 197 | $darkpan_index //= $self->fetch_darkpan_index; | ||
| 198 | |||
| 199 | return q{} | ||
| 200 | if !exists $darkpan_index->{$module}; | ||
| 201 | |||
| 202 | return $module; | ||
| 203 | } | ||
| 204 | |||
| 205 | ######################################################################## | ||
| 206 | sub is_on_metacpan { | ||
| 207 | ######################################################################## | ||
| 208 | my ( $self, $module ) = @_; | ||
| 209 | |||
| 210 | my $url = $METACPAN_URL . $module; | ||
| 211 | |||
| 212 | my $rsp = HTTP::Tiny->new->get($url); | ||
| 213 | |||
| 214 | return q{} | ||
| 215 | if $rsp->{status} == 404; | ||
| 216 | |||
| 217 | croak sprintf 'ERROR: could not fetch %s: %s', $url, $rsp->{reason} | ||
| 218 | if !$rsp->{success}; | ||
| 219 | |||
| 220 | my $payload = decode_json( $rsp->{content} ); | ||
| 221 | |||
| 222 | return $payload->{distribution} // q{}; | ||
| 223 | } | ||
| 224 | |||
| 225 | ######################################################################## | ||
| 226 | sub fetch_02packages { | ||
| 227 | ######################################################################## | ||
| 228 | my ( $self, $url ) = @_; | ||
| 229 | |||
| 230 | my $rsp = HTTP::Tiny->new->get($url); | ||
| 231 | |||
| 232 | croak sprintf 'ERROR: could not fetch %s: %s', $url, $rsp->{reason} | ||
| 233 | if !$rsp->{success}; | ||
| 234 | |||
| 235 | my $listing = q{}; | ||
| 236 | |||
| 237 | no warnings 'once'; | ||
| 238 | |||
| 239 | IO::Uncompress::Gunzip::gunzip( \$rsp->{content} => \$listing ) | ||
| 240 | or croak sprintf 'ERROR: could not decompress %s: %s', | ||
| 241 | $url, $IO::Uncompress::Gunzip::GunzipError; | ||
| 242 | |||
| 243 | return $listing; | ||
| 244 | } | ||
| 245 | |||
| 246 | 1; |