|
| 1 | +package MetaCPAN::External::Debian; |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use v5.36; |
| 6 | + |
| 7 | +use CPAN::DistnameInfo (); |
| 8 | +use DBI (); |
| 9 | + |
| 10 | +use MetaCPAN::ES; |
| 11 | + |
| 12 | +use Sub::Exporter -setup => { |
| 13 | + exports => [ qw< |
| 14 | + run_debian |
| 15 | + > ] |
| 16 | +}; |
| 17 | + |
| 18 | +sub run_debian () { |
| 19 | + my $ret = {}; |
| 20 | + |
| 21 | + my $host_regex = _get_host_regex(); |
| 22 | + |
| 23 | + # connect to the database |
| 24 | + my $dbh = DBI->connect( "dbi:Pg:host=udd-mirror.debian.net;dbname=udd", |
| 25 | + 'udd-mirror', 'udd-mirror' ); |
| 26 | + |
| 27 | + # special cases |
| 28 | + my %skip = ( 'libbssolv-perl' => 1 ); |
| 29 | + |
| 30 | + # multiple queries are needed |
| 31 | + my @sql = ( |
| 32 | + |
| 33 | + # packages with upstream identified as CPAN |
| 34 | + q{select u.source, u.upstream_url from upstream_metadata um join upstream u on um.source = u.source where um.key='Archive' and um.value='CPAN'}, |
| 35 | + |
| 36 | + # packages which upstream URL pointing to CPAN |
| 37 | + qq{select source, upstream_url from upstream where upstream_url ~ '${\$host_regex}'}, |
| 38 | + ); |
| 39 | + |
| 40 | + my @failures; |
| 41 | + |
| 42 | + for my $sql (@sql) { |
| 43 | + my $sth = $dbh->prepare($sql); |
| 44 | + $sth->execute(); |
| 45 | + |
| 46 | + # map Debian source package to CPAN distro |
| 47 | + while ( my ( $source, $url ) = $sth->fetchrow ) { |
| 48 | + next if $skip{$source}; |
| 49 | + if ( my $dist = dist_for_debian( $source, $url ) ) { |
| 50 | + $ret->{dist}{$dist} = $source; |
| 51 | + } |
| 52 | + else { |
| 53 | + push @failures => [ $source, $url ]; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (@failures) { |
| 59 | + my $ret->{errors_email_body} = join "\n" => |
| 60 | + map { sprintf "%s %s", $_->[0], $_->[1] // '<undef>' } @failures; |
| 61 | + } |
| 62 | + |
| 63 | + return $ret; |
| 64 | +} |
| 65 | + |
| 66 | +sub dist_for_debian ( $source, $url ) { |
| 67 | + my %alias = ( |
| 68 | + 'datapager' => 'data-pager', |
| 69 | + 'html-format' => 'html-formatter', |
| 70 | + ); |
| 71 | + |
| 72 | + my $dist = CPAN::DistnameInfo->new($url); |
| 73 | + if ( $dist->dist ) { |
| 74 | + return $dist->dist; |
| 75 | + } |
| 76 | + elsif ( $source =~ /^lib(.*)-perl$/ ) { |
| 77 | + my $es = MetaCPAN::ES->new( type => 'release' ); |
| 78 | + my $res = $es->scroll( |
| 79 | + body => { |
| 80 | + query => { |
| 81 | + term => { 'distribution.lowercase' => $alias{$1} // $1 } |
| 82 | + }, |
| 83 | + sort => [ { 'date' => 'desc' } ], |
| 84 | + } |
| 85 | + )->next; |
| 86 | + |
| 87 | + return $res->{_source}{distribution} |
| 88 | + if $res; |
| 89 | + } |
| 90 | + |
| 91 | + return; |
| 92 | +} |
| 93 | + |
| 94 | +sub _get_host_regex () { |
| 95 | + my @cpan_hosts = qw< |
| 96 | + backpan.cpan.org |
| 97 | + backpan.perl.org |
| 98 | + cpan.metacpan.org |
| 99 | + cpan.noris.de |
| 100 | + cpan.org |
| 101 | + cpan.perl.org |
| 102 | + search.cpan.org |
| 103 | + www.cpan.org |
| 104 | + www.perl.com |
| 105 | + >; |
| 106 | + |
| 107 | + return |
| 108 | + '^(https?|ftp)://(' |
| 109 | + . join( '|', map {s/\./\\./r} @cpan_hosts ) . ')/'; |
| 110 | +} |
| 111 | + |
| 112 | +1; |
0 commit comments