File Coverage

blib/lib/Dist/Util/Debian.pm
Criterion Covered Total %
statement 13 70 18.5
branch 0 32 0.0
condition 0 3 0.0
subroutine 5 10 50.0
pod 5 5 100.0
total 23 120 19.1


line stmt bran cond sub pod time code
1             package Dist::Util::Debian;
2              
3             our $DATE = '2018-10-23'; # DATE
4             our $VERSION = '0.007'; # VERSION
5              
6 1     1   64882 use 5.010001;
  1         12  
7 1     1   6 use strict;
  1         1  
  1         22  
8 1     1   5 use warnings;
  1         1  
  1         31  
9 1     1   1439 use Log::ger;
  1         48  
  1         5  
10              
11             require Exporter;
12             our @ISA = qw(Exporter);
13             our @EXPORT_OK = qw(
14             dist2deb
15             deb_exists
16             dist_has_deb
17             deb_ver
18             dist_deb_ver
19             );
20              
21             sub dist2deb {
22 1     1 1 88 my $dist = shift;
23 1         9 return "lib" . lc($dist) . "-perl";
24             }
25              
26             sub _deb_exists_or_deb_ver {
27 0     0     my $which = shift;
28 0 0         my $opts = ref($_[0]) eq 'HASH' ? shift : {};
29              
30 0 0         if ($opts->{use_allpackages}) {
31 0           require File::Slurper::Temp;
32 0           require File::Util::Tempdir;
33 0           require HTTP::Tiny;
34 0           require IO::Uncompress::Gunzip;
35 0           my $url = "https://packages.debian.org/unstable/allpackages?format=txt.gz";
36 0           my $path = File::Util::Tempdir::get_tempdir() . "/allpackages.txt";
37 0           my @stat = stat($path);
38 0 0 0       unless (@stat && $stat[9] > time() - 86400) {
39 0           log_trace "Downloading $url ...";
40 0           my $res = HTTP::Tiny->new->get($url);
41 0 0         unless ($res->{success}) {
42 0           warn "Can't download $url: $res->{status} - $res->{reason}";
43 0           return undef;
44             }
45 0           my $uncompressed = "";
46 0 0         IO::Uncompress::Gunzip::gunzip(\($res->{content}), \$uncompressed)
47             or die "gunzip failed: $IO::Uncompress::Gunzip::GunzipError";
48 0           File::Slurper::Temp::write_text($path, $uncompressed);
49             }
50 0           my %versions;
51 0           my $re = join("|", map { quotemeta($_) } @_); $re = qr/^($re) \(([^\)]+?)(?:\)|\s)/;
  0            
  0            
52 0           log_trace "Reading $path ...";
53 0 0         open my($fh), "<", $path or die "Can't open $path: $!";
54 0           while (defined(my $line = <$fh>)) {
55 0 0         if ($line =~ $re) {
56 0           $versions{$1} = $2;
57             }
58             }
59 0 0         return map { $which eq 'deb_exists' ? (defined $versions{$_} ? 1:0) : $versions{$_} } @_;
  0 0          
60             } else {
61 0           require HTTP::Tiny;
62 0           my @res;
63 0           for my $deb (@_) {
64 0           my $url = "https://packages.debian.org/sid/$deb";
65 0           log_trace "Checking package $deb from $url ...";
66 0           my $res = HTTP::Tiny->new->get($url);
67 0 0         unless ($res->{success}) {
68 0           warn "Can't check $url: $res->{status} - $res->{reason}";
69 0           push @res, undef;
70 0           next;
71             }
72 0 0         if ($res->{content} =~ /No such package/) {
    0          
73 0 0         push @res, $which eq 'deb_exists' ? 0 : undef;
74 0           next;
75             } elsif ($res->{content} =~ /Package: \Q$deb\E \(([^\)]+?)(?:\)|\s)/) {
76 0 0         push @res, $which eq 'deb_exists' ? 1 : $1;
77 0           next;
78             } else {
79 0           warn "Can't understand the content of $url, no indication of ".
80             "package exists or doesn't exist";
81 0           push @res, undef;
82 0           next;
83             }
84             }
85 0           return @res;
86             }
87             }
88              
89             sub deb_exists {
90 0     0 1   _deb_exists_or_deb_ver('deb_exists', @_);
91             }
92              
93             sub deb_ver {
94 0     0 1   _deb_exists_or_deb_ver('deb_ver', @_);
95             }
96              
97             sub dist_has_deb {
98 0 0   0 1   my $opts = ref($_[0]) eq 'HASH' ? shift : {};
99              
100 0           deb_exists($opts, map { dist2deb($_) } @_);
  0            
101             }
102              
103             sub dist_deb_ver {
104 0 0   0 1   my $opts = ref($_[0]) eq 'HASH' ? shift : {};
105              
106 0           deb_ver($opts, map { dist2deb($_) } @_);
  0            
107             }
108              
109             1;
110             # ABSTRACT: Utilities related to Perl distribution and Debian
111              
112             __END__