| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use lib qw(../lib); | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | my $PIDFILE = 'coverfile.pid'; | ||
| 10 | my $development = 1; | ||
| 11 | my $debug = 1; | ||
| 12 | |||
| 13 | my $entries = {}; | ||
| 14 | |||
| 15 | create_pid($PIDFILE) unless $development; | ||
| 16 | |||
| 17 | my $config = { | ||
| 18 | 'db_connect' => [ | ||
| 19 | 'dbi:SQLite:dbname=../wcpancover.db', | ||
| 20 | undef, | ||
| 21 | undef, | ||
| 22 | { | ||
| 23 | 'sqlite_unicode' => 1 | ||
| 24 | } ], | ||
| 25 | 'db_schema' => 'Wcpancover::DB::Schema', | ||
| 26 | 'secret' => 'app_secret' | ||
| 27 | }; | ||
| 28 | |||
| 29 | ######################################## | ||
| 30 | my $schema_class = $config->{db_schema}; | ||
| 31 | eval "require $schema_class" | ||
| 32 | or die "Could not load Schema Class ($schema_class), $@"; | ||
| 33 | my $db_connect = $config->{db_connect} | ||
| 34 | or die "No DBI connection string provided"; | ||
| 35 | my @db_connect = ref $db_connect ? @$db_connect : ($db_connect); | ||
| 36 | |||
| 37 | my $schema = $schema_class->connect(@db_connect) | ||
| 38 | or die "Could not connect to $schema_class using $db_connect[0]"; | ||
| 39 | |||
| 40 | use CPAN::Cover::Results; | ||
| 41 | use String::Similarity; | ||
| 42 | |||
| 43 | my $iterator = CPAN::Cover::Results->new()->release_iterator(); | ||
| 44 | |||
| 45 | my $found = 0; | ||
| 46 | my $missing = 0; | ||
| 47 | while (my $release = $iterator->next) { | ||
| 48 | $found++; | ||
| 49 | #last if ($found > 500); | ||
| 50 | my $distname = $release->distname; | ||
| 51 | my $version = $release->version; | ||
| 52 | my $name = $release->distname . '-' . $release->version; | ||
| 53 | my $coverage = $release->total; | ||
| 54 | my $author = '?'; | ||
| 55 | |||
| 56 | if (0) { | ||
| 57 | next if ($distname !~ m/^AproJo/); | ||
| 58 | print STDERR 'distname: ', $distname, ' name: ', $name, ' author: ',$author, ' coverage: ',$coverage,"\n"; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | if (0) { | ||
| 63 | if ($version =~ m/-/) { | ||
| 64 | print STDERR 'distname: ', $distname, | ||
| 65 | ' version: ', $version, | ||
| 66 | ' name: ', $name, | ||
| 67 | ' author: ',$author, | ||
| 68 | ' coverage: ',$coverage,"\n" if $debug; | ||
| 69 | next; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | my $rs = $schema->resultset('Package'); | ||
| 75 | my $result = | ||
| 76 | $schema->resultset('Package')->single({name => $name}); | ||
| 77 | if (!$result) { | ||
| 78 | #print STDERR 'author not found for package ',$name,' trying distname ',$distname,"\n"; | ||
| 79 | my $maybe = most_similar($distname,$name,$schema); | ||
| 80 | #print STDERR 'found a maybe package ',$maybe,"\n"; | ||
| 81 | if ($maybe) { | ||
| 82 | $author = $schema->resultset('Package')->search( { name => { -like => $maybe . '%' }})->first->author() | ||
| 83 | || '?'; | ||
| 84 | } | ||
| 85 | $missing++; | ||
| 86 | } | ||
| 87 | if ($result) { | ||
| 88 | $author = $result->author; | ||
| 89 | } | ||
| 90 | #print STDERR 'name: ', $name, ' author: ',$author, ' coverage: ',$coverage,"\n"; | ||
| 91 | entry({ | ||
| 92 | author => $author, | ||
| 93 | name => $name, | ||
| 94 | coverage => $coverage, | ||
| 95 | dist => $distname, | ||
| 96 | version => $version, | ||
| 97 | }) if (1); | ||
| 98 | } | ||
| 99 | |||
| 100 | print STDERR | ||
| 101 | 'found: ',$found,' missing: ',$missing,"\n"; | ||
| 102 | |||
| 103 | if (0) { | ||
| 104 | my $rs = $schema->resultset('Cover'); | ||
| 105 | my $result = | ||
| 106 | $schema->resultset('Cover')->search(undef, {order_by => {-asc => 'name'}}); | ||
| 107 | |||
| 108 | |||
| 109 | while (my $record = $result->next) { | ||
| 110 | if (!exists $entries->{$record->name}) { | ||
| 111 | $record->delete; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | remove_pid($PIDFILE) unless $development; | ||
| 118 | |||
| 119 | sub most_similar { | ||
| 120 | my ($query,$name1, $schema) = @_; | ||
| 121 | |||
| 122 | my $rs = $schema->resultset('Package'); | ||
| 123 | my $result = | ||
| 124 | $schema->resultset('Package')->search( { name => { -like => $query . '%' }}); | ||
| 125 | |||
| 126 | if (!$result) { | ||
| 127 | $query =~ s/(-[vV]?\d[\d._]*).*$/$1/; | ||
| 128 | $result = | ||
| 129 | $schema->resultset('Package')->search( { name => { -like => $query . '%' }}); | ||
| 130 | |||
| 131 | return '' unless ($result); | ||
| 132 | } | ||
| 133 | |||
| 134 | my $similars = {}; | ||
| 135 | |||
| 136 | while (my $record = $result->next) { | ||
| 137 | my $name2 = $record->name; | ||
| 138 | |||
| 139 | $name2 =~ s/-TRIAL//; | ||
| 140 | |||
| 141 | my $similarity = similarity($name1, $name2); | ||
| 142 | $similars->{$name2} = $similarity; | ||
| 143 | } | ||
| 144 | my @keys = sort { $similars->{$b} <=> $similars->{$a} } keys(%$similars); | ||
| 145 | #print STDERR 'similars: ',Dumper($similars) if $debug; | ||
| 146 | return $keys[0]; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | sub entry { | ||
| 151 | my ($entry) = @_; | ||
| 152 | |||
| 153 | if (exists $entry->{name}) { | ||
| 154 | $entries->{$entry->{name}}++; | ||
| 155 | |||
| 156 | my $rs = $schema->resultset('Cover'); | ||
| 157 | $rs->update_or_create($entry,{ key => 'name_UNIQUE' }); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | sub create_pid { | ||
| 164 | my $pidfile = shift; | ||
| 165 | if (-f $pidfile) { | ||
| 166 | open(LF, "<$pidfile") or die "unable to open $pidfile: $!"; | ||
| 167 | my $pid = <LF>; | ||
| 168 | chomp $pid; | ||
| 169 | |||
| 170 | close(LF); | ||
| 171 | my @tmp = `ps --no-headers -p $pid | grep $pid`; | ||
| 172 | |||
| 173 | if ($#tmp < 0) { | ||
| 174 | print STDERR | ||
| 175 | "PIDFILE exists, foreign pid not running $pid, removing PIDFILE", "\n"; | ||
| 176 | unlink($PIDFILE); | ||
| 177 | |||
| 178 | print STDERR "Creating PID " . $$ . " (" . $PIDFILE . ")", "\n"; | ||
| 179 | open(L, ">$pidfile") or die "unable to create pidfile: $!"; | ||
| 180 | print L $$; | ||
| 181 | close(L); | ||
| 182 | |||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | print STDERR "Allready running ($pid) ... exit", "\n"; | ||
| 187 | exit; | ||
| 188 | } | ||
| 189 | |||
| 190 | open(L, ">$pidfile") or die "unable to create pidfile: $!"; | ||
| 191 | print L $$; | ||
| 192 | close(L); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub remove_pid { | ||
| 196 | my $pidfile = shift; | ||
| 197 | print STDERR "Remove Pidfile (" . $pidfile . ")", "\n"; | ||
| 198 | unless (-f $pidfile) { | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | unlink($pidfile) or die "unable to delete pidfile: $!"; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub remove_file { | ||
| 205 | my $file = shift; | ||
| 206 | print STDERR "Remove file (" . $file . ")", "\n"; | ||
| 207 | unless (-f $file) { | ||
| 208 | return; | ||
| 209 | } | ||
| 210 | unlink($file) or die "unable to delete $file: $!"; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub fetch_file { | ||
| 214 | my ($url, $file) = @_; | ||
| 215 | |||
| 216 | #my $command = '/usr/bin/wget'; | ||
| 217 | #my $outfile = "--output-document=$file"; | ||
| 218 | |||
| 219 | my $command = '/usr/bin/curl'; | ||
| 220 | my $outfile = "> $file"; | ||
| 221 | |||
| 222 | my @command = ($command, $url, $outfile); | ||
| 223 | |||
| 224 | #print STDERR join(' ',@command),"\n"; | ||
| 225 | |||
| 226 | my $command_string = join(' ', @command); | ||
| 227 | print STDERR $command_string, "\n"; | ||
| 228 | system($command_string); | ||
| 229 | |||
| 230 | if ($? == -1) { | ||
| 231 | #print STDERR "wget command failed: $!\n"; | ||
| 232 | #return 0; | ||
| 233 | die "fetch file failed: $!"; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | exit; | ||
| 238 | |||
| 239 | __END__ |