blib/lib/CPAN/Digger.pm

filter: Statementstmt 69.8% 123 / 176 Branchbran 54.6% 35 / 64 Conditioncond 26.6% 4 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 5 totaltotal 62.1% 179 / 288 SCAR 48.5
CC
42
Cov
64%
analyze_github
90.0
collect
48.1
get_data
6.0
CRAP 127.6
↑ Summary blib/lib/CPAN/Digger/CLI.pm »
1 package CPAN::Digger;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $VERSION = '1.03';
6  
7 5 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use MetaCPAN::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 5 ▶use CPAN::Digger::DB qw(get_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶my $tempdir = tempdir( CLEANUP => ($ENV{KEEP_TEMPDIR} ? 0 : 1) );
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'KEEP_TEMPDIR'} ? : 0 5 5
21  
22 5 my %known_licenses = map {$_ => 1} qw(apache_2_0 artistic_2 bsd gpl_3 lgpl_2_1 lgpl_3_0 perl_5); # open_source, unknown
23  
24 sub new {
25 5 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
26 5 my $self = bless {}, $class;
27 5 for my $key (keys %args) {
28 50 $self->{$key} = $args{$key};
29 }
30 5 $self->{log} = uc $self->{log};
31 5 $self->{check_github} = delete $self->{github};
32  
33 5 $self->{db} = CPAN::Digger::DB->new(db => $self->{db});
34  
35 5 return $self;
36 }
37  
38 sub get_vcs {
39 68 ▶ my ($repository) = @_;
Subroutine get_vcs: called CC=5 SCAR=16.2
Pod undocumented
40 68 ▶ if ($repository) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repository) 68 0 68
41 # $html .= sprintf qq{<a href="%s">%s %s</a><br>\n}, $repository->{$k}, $k, $repository->{$k};
42 # Try to get the web link
43 68 my $url = $repository->{web};
44 68 ▶ if (not $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 26 42 68
45 26 $url = $repository->{url};
46 26 $url =~ s{^git://}{https://};
47 26 $url =~ s{\.git$}{};
48 }
49 68 my $name = "repository";
50 68 ▶ if ($url =~ m{^https?://github.com/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^https?://github.com/]) 66 2 68
51 66 $name = 'GitHub';
52 }
53 68 ▶ if ($url =~ m{^https?://gitlab.com/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^https?://gitlab.com/]) 0 68 68
54 0 $name = 'GitLab';
55 }
56 68 return $url, $name;
57 }
58 }
59  
60 sub get_data {
61 76 ▶ my ($self, $item) = @_;
Subroutine get_data: called CC=6 SCAR=17.9
Pod undocumented
62  
63 76 my $logger = Log::Log4perl->get_logger();
64 76 my %data = (
65 distribution => $item->distribution,
66 version => $item->version,
67 author => $item->author,
68 date => $item->date,
69 );
70 #die Dumper $item;
71  
72 76 $logger->debug('dist: ', $item->distribution);
73 76 $logger->debug(' ', $item->author);
74 76 my @licenses = @{ $item->license };
75 76 $data{licenses} = join ' ', @licenses;
76 76 $logger->debug(' ', $data{licenses});
77 76 for my $license (@licenses) {
78 75 ▶ if ($license eq 'unknown') {
Branch: 2 branches
BranchTrueFalseTotal
if ($license eq 'unknown') { } 4 71 75
elsif (not exists $known_licenses{$license}) { } 1 70 71
79 4 $logger->error("Unknown license '$license'");
80 } elsif (not exists $known_licenses{$license}) {
81 1 $logger->warn("Unknown license '$license'. Probably CPAN::Digger needs to be updated");
82 }
83 }
84 # if there are not licenses =>
85 # if there is a license called "unknonws"
86 # check against a known list of licenses (grow it later, or look it up somewhere?)
87 76 my %resources = %{ $item->resources };
88 #say ' ', join ' ', keys %resources;
89 76 ▶ if ($resources{repository}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resources{'repository'}) { } 68 8 76
90 68 my ($vcs_url, $vcs_name) = get_vcs($resources{repository});
91 68 ▶ if ($vcs_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vcs_url) 68 0 68
92 68 $data{vcs_url} = $vcs_url;
93 68 $data{vcs_name} = $vcs_name;
94 68 $logger->debug(" $vcs_name: $vcs_url");
95 }
96 } else {
97 8 $logger->error('No repository for ', $item->distribution);
98 }
99 76 return %data;
100 }
101  
102  
103 sub analyze_github {
104 0 ▶ my ($data) = @_;
Subroutine analyze_github: not called CC=9 SCAR=45.0
Pod undocumented
105 0 my $logger = Log::Log4perl->get_logger();
106  
107 0 my $vcs_url = $data->{vcs_url};
108 0 my $repo_name = (split '\/', $vcs_url)[-1];
109 0 $logger->info("Analyze GitHub repo '$vcs_url' in directory $repo_name");
110  
111 0 my $ua = LWP::UserAgent->new(timeout => 5);
112 0 my $response = $ua->get($vcs_url);
113 0 my $status_line = $response->status_line;
114 0 ▶ if ($status_line eq '404 Not Found') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status_line eq "404 Not Found") 0 0 0
115 0 $logger->error("Repository '$vcs_url' Received 404 Not Found. Please update the link in the META file");
116 0 return;
117 }
118 0 ▶ if ($response->code != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 200) 0 0 0
119 0 $logger->error("Repository '$vcs_url' got a response of '$status_line'. Please report this to the maintainer of CPAN::Digger.");
120 0 return;
121 }
122 0 ▶ if ($response->redirects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->redirects) 0 0 0
123 0 $logger->error("Repository '$vcs_url' is being redirected. Please update the link in the META file");
124 }
125  
126 0 my $git = 'git';
127  
128 0 my @cmd = ($git, "clone", "--depth", "1", $data->{vcs_url});
129 0 my $cwd = getcwd();
130 0 chdir($tempdir);
131 my ($out, $err, $exit_code) = capture {
132 0 ▶ system(@cmd);
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 };
134 0 chdir($cwd);
135 0 my $repo = "$tempdir/$repo_name";
136 0 $logger->debug("REPO path '$repo'");
137  
138 0 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 0 0
139 # TODO capture stderr and include in the log
140 0 $logger->error("Failed to clone $vcs_url");
141 0 return;
142 }
143  
144 0 $data->{travis} = -e "$repo/.travis.yml";
145 0 my @ga = glob("$repo/.github/workflows/*");
146 0 ▶ $data->{github_actions} = (scalar(@ga) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
scalar @ga ? : 0 0 0
147 0 $data->{circleci} = -e "$repo/.circleci";
148 0 ▶ $data->{appveyor} = (-e "$repo/.appveyor.yml") || (-e "$repo/appveyor.yml");
Condition0%0/3
-e "$repo/.appveyor.yml" || -e "$repo/appveyor.yml"
l!l&&r!l&&!r
000
MC/DC0%0/2
-e "$repo/.appveyor.yml" || -e "$repo/appveyor.yml"
-e "$repo/.appveyor.yml" -e "$repo/appveyor.yml"
Truth table0%0/3
A || B
A= -e "$repo/.appveyor.yml"B= -e "$repo/appveyor.yml"
ABresult
1X1
011
000
149 0 $data->{azure_pipelines} = -e "$repo/azure-pipelines.yml";
150  
151 0 for my $ci (qw(travis github_actions circleci appveyor)) {
152 0 $logger->debug("Is CI '$ci'?");
153 0 ▶ if ($data->{$ci}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$ci}) 0 0 0
154 0 $logger->debug("CI '$ci' found!");
155 0 $data->{has_ci} = 1;
156 }
157 }
158 }
159  
160 sub collect {
161 5 ▶ my ($self) = @_;
Subroutine collect: called CC=24 SCAR=38.7
Pod undocumented
162  
163 5 my @all_the_distributions;
164  
165 5 my $log_level = $self->{log}; # TODO: shall we validate?
166 5 Log::Log4perl->easy_init({
167 level => $log_level,
168 layout => '%d{yyyy-MM-dd HH:mm:ss} - %p - %m%n',
169 });
170  
171 5 my $logger = Log::Log4perl->get_logger();
172 5 $logger->info('Starting');
173 5 $logger->info("Tempdir: $tempdir");
174 5 ▶ $logger->info("Recent: $self->{recent}") if $self->{recent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"recent"} 2 3 5
175 5 ▶ $logger->info("Author: $self->{author}") if $self->{author};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"author"} 3 2 5
176  
177 5 my $mcpan = MetaCPAN::Client->new();
178 5 my $rset;
179 5 ▶ if ($self->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'author'}) { } 3 2 5
180 3 my $author = $mcpan->author($self->{author});
181 #print $author;
182 3 $rset = $author->releases;
183 } else {
184 2 $rset = $mcpan->recent($self->{recent});
185 }
186 5 $logger->info("MetaCPAN::Client::ResultSet received with a total of $rset->{total} items");
187 5 my %distros;
188 5 my @fields = get_fields();
189 5 ▶ while ( my $item = $rset->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $rset->next) 112 5 117
190 112 ▶ next if $distros{ $item->distribution }; # We have already deal with this in this session
Branch: 1 branch
BranchTrueFalseTotal
if $distros{$item->distribution} 0 112 112
191 112 $distros{ $item->distribution } = 1;
192  
193 112 my $row = $self->{db}->db_get_distro($item->distribution);
194 112 ▶ next if $row and $row->{version} eq $item->version; # we already have this in the database (shall we call last?)
Branch: 1 branch
BranchTrueFalseTotal
if $row and $row->{"version"} eq $item->version 36 76 112
Condition66%2/3
$row and $row->{"version"} eq $item->version
!ll&&!rl&&r
76036
MC/DC50%1/2
$row and $row->{"version"} eq $item->version
$row $row->{"version"} eq $item->version
Truth table66%2/3
A and B
A= $rowB= $row->{"version"} eq $item->version
ABresult
0X0
100
111
195 76 my %data = $self->get_data($item);
196 #die Dumper \%data;
197 76 $self->{db}->db_insert_into(@data{@fields});
198 76 push @all_the_distributions, \%data;
199 }
200  
201 5 ▶ if ($self->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"author"}) 3 2 5
202 3 @all_the_distributions = reverse sort {$a->{date} cmp $b->{date}} @all_the_distributions;
203 3 ▶ if ($self->{limit} and @all_the_distributions > $self->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"limit"} and @all_the_distributions > $self->{"limit"}) 0 3 3
Condition33%1/3
$self->{"limit"} and @all_the_distributions > $self->{"limit"}
!ll&&!rl&&r
300
MC/DC0%0/2
$self->{"limit"} and @all_the_distributions > $self->{"limit"}
$self->{"limit"} @all_the_distributions > $self->{"limit"}
Truth table33%1/3
A and B
A= $self->{"limit"}B= @all_the_distributions > $self->{"limit"}
ABresult
0X0
100
111
204 0 @all_the_distributions = @all_the_distributions[0 .. $self->{limit}-1];
205 }
206 }
207  
208 # Check on the VCS
209 5 ▶ if ($self->{check_github}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"check_github"}) 0 5 5
210 0 $logger->info("Starting to check GitHub");
211 0 for my $data (@all_the_distributions) {
212 0 my $distribution = $data->{distribution};
213 0 my $data_ref = $self->{db}->db_get_distro($distribution);
214 0 ▶ next if not $data_ref->{vcs_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $data_ref->{"vcs_name"} 0 0 0
215  
216 0 ▶ if ($self->{check_github} and $data_ref->{vcs_name} eq 'GitHub') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"check_github"} and $data_ref->{"vcs_name"} eq "GitHub") 0 0 0
Condition0%0/3
$self->{"check_github"} and $data_ref->{"vcs_name"} eq "GitHub"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"check_github"} and $data_ref->{"vcs_name"} eq "GitHub"
$self->{"check_github"} $data_ref->{"vcs_name"} eq "GitHub"
Truth table0%0/3
A and B
A= $self->{"check_github"}B= $data_ref->{"vcs_name"} eq "GitHub"
ABresult
0X0
100
111
217 0 analyze_github($data_ref);
218 }
219 0 my %data = %$data_ref;
220 0 $self->{db}->db_update($distribution, @data{@fields});
221 0 ▶ sleep $self->{sleep} if $self->{sleep};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sleep"} 0 0 0
222 }
223 }
224  
225  
226 5 ▶ if ($self->{report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report"}) 1 4 5
227 #print "Text report\n";
228 1 my @distros = @{ $self->{db}->db_get_every_distro() };
229 1 ▶ if ($self->{limit} and @distros > $self->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"limit"} and @distros > $self->{"limit"}) 0 1 1
Condition33%1/3
$self->{"limit"} and @distros > $self->{"limit"}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"limit"} and @distros > $self->{"limit"}
$self->{"limit"} @distros > $self->{"limit"}
Truth table33%1/3
A and B
A= $self->{"limit"}B= @distros > $self->{"limit"}
ABresult
0X0
100
111
230 0 @distros = @distros[0 .. $self->{limit}-1];
231 }
232 1 for my $distro (@distros) {
233 #die Dumper $distro;
234 2 ▶ printf "%s %-40s %-7s", $distro->{date}, $distro->{distribution}, ($distro->{vcs_url} ? '' : 'NO VCS');
Branch: 1 branch
BranchTrueFalseTotal
$distro->{'vcs_url'} ? : 1 1 2
235 2 ▶ if ($self->{check_github}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"check_github"}) 0 2 2
236 0 ▶ printf "%-7s", ($distro->{has_ci} ? '' : 'NO CI');
Branch: 1 branch
BranchTrueFalseTotal
$distro->{'has_ci'} ? : 0 0 0
237 }
238 2 print "\n";
239 }
240 }
241 }
242  
243  
244 5 42;
245  
246  
247 =head1 NAME
248  
249 CPAN::Digger - To dig CPAN
250  
251 =head1 SYNOPSIS
252  
253 cpan-digger
254  
255 =head1 DESCRIPTION
256  
257 This is a command line program to collect some meta information about CPAN modules.
258  
259  
260 =head1 COPYRIGHT AND LICENSE
261  
262 Copyright (C) 2020 by L<Gabor Szabo|https://szabgab.com/>
263  
264 This library is free software; you can redistribute it and/or modify
265 it under the same terms as Perl itself, either Perl version 5.26.1 or,
266 at your option, any later version of Perl 5 you may have available.
267  
268 =cut
269  
↑ Summary blib/lib/CPAN/Digger/CLI.pm »