script/getcpan.pl untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 158 CC 7 SCAR 18.1
CC
7
Cov
0%
create_pid
12.0
entry
6.0
remove_pid
6.0
CRAP 6.1
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use warnings;
5  
6  
7 use lib qw(../lib);
8  
9 my $job_entries = {};
10  
11 my $CPANFILE = './cpanfile.txt';
12  
13 my $PIDFILE = 'cpanfile.pid';
14  
15 my $url = 'http://www.cpan.org/modules/01modules.index.html';
16  
17 my $entries = {};
18  
19 create_pid($PIDFILE);
20  
21 #remove_file($CPANFILE);
22  
23 #fetch_file($url, $CPANFILE);
24  
25 my $config = {
26 'db_connect' => [
27 'dbi:SQLite:dbname=../wcpancover.db',
28 undef,
29 undef,
30 {
31 'sqlite_unicode' => 1
32 } ],
33 'db_schema' => 'Wcpancover::DB::Schema',
34 'secret' => 'app_secret'
35 };
36  
37 ########################################
38 my $schema_class = $config->{db_schema};
39 eval "require $schema_class"
40 or die "Could not load Schema Class ($schema_class), $@";
41 my $db_connect = $config->{db_connect}
42 or die "No DBI connection string provided";
43 my @db_connect = ref $db_connect ? @$db_connect : ($db_connect);
44  
45 my $schema = $schema_class->connect(@db_connect)
46 or die "Could not connect to $schema_class using $db_connect[0]";
47  
48 open (my $fh, '<', $CPANFILE) or die "couldn't open $CPANFILE, $@";
49  
50 my $found = 0;
51  
52 use CPAN::ReleaseHistory 0.10;
53 my $history = CPAN::ReleaseHistory->new();
54 my $iterator = $history->release_iterator();
55 while (my $release = $iterator->next_release) {
56 #last if ($found > 500);
57 $found++;
58 my $name = $release->distinfo->distvname;
59 my $dist = $release->distinfo->dist;
60 my $version = $release->distinfo->version // '';
61 my $author = $release->distinfo->cpanid;
62 my $date = $release->date;
63  
64 entry({
65 author => $author,
66 name => $name,
67 dist => $dist,
68 version => $version . '',
69 date => $date,
70 });
71 }
72  
73 print STDERR 'records processed: ',$found,"\n";
74  
75 if (0) {
76 my $rs = $schema->resultset('Package');
77 my $result =
78 $schema->resultset('Package')->search(undef, {order_by => {-asc => 'name'}});
79  
80  
81 while (my $record = $result->next) {
82 if (!exists $entries->{$record->name}) {
83 $record->delete;
84 }
85 }
86  
87 }
88  
89 remove_pid($PIDFILE);
90  
91 sub entry {
92 my ($entry) = @_;
93  
94 if (exists $entry->{name}) {
95 $entries->{$entry->{name}}++;
96  
97 my $rs = $schema->resultset('Package');
98 $rs->update_or_create($entry,{ key => 'name_UNIQUE' });
99 }
100 }
101  
102 sub create_pid {
103 my $pidfile = shift;
104 if (-f $pidfile) {
105 open(LF, "<$pidfile") or die "unable to open $pidfile: $!";
106 my $pid = <LF>;
107 chomp $pid;
108  
109 close(LF);
110 my @tmp = `ps --no-headers -p $pid | grep $pid`;
111  
112 if ($#tmp < 0) {
113 print STDERR
114 "PIDFILE exists, foreign pid not running $pid, removing PIDFILE", "\n";
115 unlink($PIDFILE);
116  
117 print STDERR "Creating PID " . $$ . " (" . $PIDFILE . ")", "\n";
118 open(L, ">$pidfile") or die "unable to create pidfile: $!";
119 print L $$;
120 close(L);
121  
122 return;
123 }
124  
125 print STDERR "Allready running ($pid) ... exit", "\n";
126 exit;
127 }
128  
129 open(L, ">$pidfile") or die "unable to create pidfile: $!";
130 print L $$;
131 close(L);
132 }
133  
134 sub remove_pid {
135 my $pidfile = shift;
136 print STDERR "Remove Pidfile (" . $pidfile . ")", "\n";
137 unless (-f $pidfile) {
138 return;
139 }
140 unlink($pidfile) or die "unable to delete pidfile: $!";
141 }
142  
143 sub remove_file {
144 my $xmlfile = shift;
145 print STDERR "Remove cpanfile (" . $xmlfile . ")", "\n";
146 unless (-f $xmlfile) {
147 return;
148 }
149 unlink($xmlfile) or die "unable to delete $xmlfile: $!";
150 }
151  
152 sub fetch_file {
153 my ($url, $file) = @_;
154  
155 #my $command = '/usr/bin/wget';
156 #my $outfile = "--output-document=$file";
157  
158 my $command = '/usr/bin/curl';
159 my $outfile = "> $file";
160  
161 my @command = ($command, $url, $outfile);
162  
163 #print STDERR join(' ',@command),"\n";
164  
165 my $command_string = join(' ', @command);
166 print STDERR $command_string, "\n";
167 system($command_string);
168  
169 if ($? == -1) {
170 #print STDERR "wget command failed: $!\n";
171 #return 0;
172 die "fetch file failed: $!";
173 }
174 }
175  
176 exit;
177  
178 __END__