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