aboutsummaryrefslogtreecommitdiffstats
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
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
-rw-r--r--NEWS1
-rw-r--r--config_default1
-rw-r--r--lib/MGA/Advisories.pm39
-rwxr-xr-xmgaadv14
4 files changed, 52 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 85e7f1f..153b845 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@
- add 'show' command to show an advisory
- load config from home directory if file exists
- add option to load advisories db from a file dump instead of a directory
+- add 'update' command to download new advisories db
Version 0.4
diff --git a/config_default b/config_default
index 3b871a3..7f7cb05 100644
--- a/config_default
+++ b/config_default
@@ -1,4 +1,5 @@
mode: dump
+dump_url: http://advisories.mageia.org/advisories.yaml
advisories_dir: /var/lib/mga-advisories/advisories
status_dir: /var/lib/mga-advisories/status
tmpl_dir: /usr/share/mga-advisories/tmpl
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}}) {
diff --git a/mgaadv b/mgaadv
index a2ad4de..216d052 100755
--- a/mgaadv
+++ b/mgaadv
@@ -42,6 +42,15 @@ $0 show [ID]
Show an advisory.
END
},
+ update => {
+ run => \&updateadv,
+ descr => 'Update the advisories database',
+ usage => <<END,
+$0 update
+
+Update the advisories database.
+END
+ },
usage => {
run => \&usage,
descr => 'Show usage informations for an action',
@@ -104,6 +113,11 @@ sub showadv {
MGA::Advisories::showadv(\%advdb, $adv);
}
+sub updateadv {
+ usageexit('usage', $_[0]) unless @_ == 1;
+ MGA::Advisories::download_advisories;
+}
+
if (@ARGV == 0 || !$actions{$ARGV[0]}) {
usageexit();
}