aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNicolas Vigier <boklm@mageia.org>2013-06-13 15:21:47 +0000
committerNicolas Vigier <boklm@mageia.org>2013-06-13 15:21:47 +0000
commitfbc35fd95c230d9b7459db1cb88670938a16274d (patch)
tree98be78d206f2cec246fb9772e06beb9a578503d3 /lib
parentb84016d16a0d80b05177e2fbb233a0cc9ddcfcd3 (diff)
downloadmgaadvisories-fbc35fd95c230d9b7459db1cb88670938a16274d.tar
mgaadvisories-fbc35fd95c230d9b7459db1cb88670938a16274d.tar.gz
mgaadvisories-fbc35fd95c230d9b7459db1cb88670938a16274d.tar.bz2
mgaadvisories-fbc35fd95c230d9b7459db1cb88670938a16274d.tar.xz
mgaadvisories-fbc35fd95c230d9b7459db1cb88670938a16274d.zip
Add 'update' command to download new advisories db
Diffstat (limited to 'lib')
-rw-r--r--lib/MGA/Advisories.pm39
1 files changed, 36 insertions, 3 deletions
diff --git a/lib/MGA/Advisories.pm b/lib/MGA/Advisories.pm
index ec91efa..b89ad60 100644
--- a/lib/MGA/Advisories.pm
+++ b/lib/MGA/Advisories.pm
@@ -2,12 +2,14 @@ package MGA::Advisories;
use warnings;
use strict;
-use YAML qw(LoadFile DumpFile);
+use YAML qw(LoadFile DumpFile Load);
use Template;
use DateTime;
use Email::Sender::Simple qw(try_to_sendmail);
use Email::Simple;
use Email::Simple::Creator;
+use LWP::UserAgent;
+use File::Basename;
#use Data::Dump qw(dd);
my $config_file = '/usr/share/mga-advisories/config';
@@ -65,9 +67,13 @@ sub get_advisories_from_dir {
return \%advisories;
}
+sub advdb_dumpfile {
+ $config->{advdb_dumpfile} || $ENV{HOME} . '/.mga-advisories/advisories.yaml';
+}
+
sub get_advisories_from_dump {
- return LoadFile($_[0] || $config->{advdv_dumpfile}
- || $ENV{HOME} . '/.mga-advisories/advisories.yaml');
+ my $advfile = advdb_dumpfile;
+ return -f $advfile ? LoadFile($advfile) : {};
}
sub get_advisories {
@@ -75,6 +81,33 @@ sub get_advisories {
: get_advisories_from_dir;
}
+sub download_advisories {
+ my $oldadvisories = get_advisories_from_dump;
+ my $ua = LWP::UserAgent->new;
+ my $resp = $ua->get($config->{dump_url});
+ die "Error loading $config->{dump_url}" unless $resp->is_success;
+ my $newadvisories = Load($resp->decoded_content);
+ my @newadv = grep { ! $oldadvisories->{$_} } keys %$newadvisories;
+ #dd \@newadv;
+ if (@newadv) {
+ my %n;
+ my @v = @{$newadvisories}{@newadv};
+ @n{@newadv} = @v;
+ print "New advisories have been downloaded :\n";
+ listadv({advisories => \%n});
+ } else {
+ print "No new advisories available\n";
+ }
+ if (!-d dirname(advdb_dumpfile)) {
+ mkdir dirname(advdb_dumpfile)
+ || die "Error creating directory " . dirname(advdb_dumpfile);
+ }
+ open(my $fh, '>', advdb_dumpfile)
+ || die "Could not open " . advdb_dumpfile;
+ print $fh $resp->decoded_content;
+ close $fh;
+}
+
sub publish_advisories {
my ($advdb) = @_;
foreach my $adv (keys %{$advdb->{advisories}}) {