Using threads, because it's fun:)
This commit is contained in:
122
pcurse
122
pcurse
@@ -1,44 +1,114 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
no warnings 'all';
|
||||
use lib '.';
|
||||
use pcurse;
|
||||
use feature ':5.10';
|
||||
|
||||
select(STDOUT);
|
||||
$| = 1;
|
||||
|
||||
my $ret;
|
||||
my $msg;
|
||||
my %opts = pcurse::parse_arguments;
|
||||
$opts{'config'} = $ENV{'HOME'}.'/.pcurse/config.json' unless($opts{'config'});
|
||||
my $conf = pcurse::load_config($opts{'config'});
|
||||
my @toupd;
|
||||
my %jobs;
|
||||
my $workers;
|
||||
my $opts = pcurse::parse_arguments;
|
||||
$opts->{'config'} = $ENV{'HOME'}.'/.pcurse/config.json' unless(defined($opts->{'config'}));
|
||||
my $conf = pcurse::load_config($opts->{'config'});
|
||||
$conf = pcurse::check_config($conf);
|
||||
($ret,$msg) = pcurse::save_config($opts{'config'},$conf);
|
||||
print $msg."\n" unless($ret);
|
||||
($ret,$msg) = pcurse::save_config($conf->{'config'},$conf);
|
||||
unless($ret) {
|
||||
print $msg."\n";
|
||||
exit 0;
|
||||
}
|
||||
$conf = pcurse::merge_opts($opts,$conf);
|
||||
my $addons = pcurse::load_addons($conf->{'addons'});
|
||||
say 'Loaded '.scalar(@{$addons}).' addons';
|
||||
my $pool = pcurse::init_pool($conf->{'workers'});
|
||||
my $worki = 0;
|
||||
print 'Checking for updates ';
|
||||
foreach my $addon(@{$addons}) {
|
||||
print 'Found '.$addon->{'name'}.'('.$addon->{'version'}.')';
|
||||
if(exists($addon->{'uri'})) {
|
||||
my $html = pcurse::html_get($conf->{'baseuri'}.$addon->{'uri'});
|
||||
my $fileid = pcurse::get_latest_file_id($html,$addon->{'uri'});
|
||||
if($fileid) {
|
||||
my $version = pcurse::get_product_version($html,$addon->{'uri'},$fileid);
|
||||
print ' - latest version is '.$version;
|
||||
if($version && ($version ne $addon->{'version'})) {
|
||||
unless($opts{'test'}) {
|
||||
print ' - updating';
|
||||
my $ret = pcurse::update($conf->{'baseuri'}.$addon->{'uri'},$fileid,$conf->{'wowpath'});
|
||||
if($ret) {
|
||||
print ' - done';
|
||||
$addon->{'version'} = $version;
|
||||
} else {
|
||||
print ' - failed';
|
||||
}
|
||||
}
|
||||
my $workid = $pool->job('check',$addon,$conf);
|
||||
$jobs{$workid}{'todo'} = 'check';
|
||||
$jobs{$workid}{'job'} = $addon;
|
||||
$jobs{$workid}{'i'} = $worki;
|
||||
$worki++;
|
||||
}
|
||||
while(scalar(keys %jobs)) {
|
||||
my $jobid;
|
||||
if(my @jres = $pool->result_any(\$jobid)) {
|
||||
if($jres[0] == 1) {
|
||||
my $a = $jres[1]; #Addon data structure
|
||||
my $i = $jobs{$jobid}{'i'}; #ID from @{$addons}
|
||||
my $v = $jres[1]->{'targetversion'}; #Version we're trying to fetch
|
||||
die "Not enough params: a: $a, i: $i, v: $v" unless(defined($a) && defined($i) && defined($v));
|
||||
my $nextjob = [ $i, $a, $v ];
|
||||
push(@toupd,$nextjob);
|
||||
} elsif($jres[1] eq 'No need to update') {
|
||||
} else {
|
||||
say 'Error during check: '.$jres[1];
|
||||
}
|
||||
delete $jobs{$jobid};
|
||||
print '.';
|
||||
}
|
||||
}
|
||||
|
||||
print "\n";
|
||||
|
||||
print 'Downloading updates ' if(scalar(@toupd));
|
||||
foreach my $a(@toupd) {
|
||||
my $id = $a->[0];
|
||||
my $addon = $a->[1];
|
||||
my $version = $a->[2];
|
||||
my $workid = $pool->job('download',$conf->{'baseuri'}.$addon->{'uri'},$addon->{'fileid'});
|
||||
$jobs{$workid}{'todo'} = 'download';
|
||||
$jobs{$workid}{'job'} = [ $conf->{'baseuri'}.$addon->{'uri'},$addon->{'fileid'} ];
|
||||
$jobs{$workid}{'id'} = $id;
|
||||
$jobs{$workid}{'tv'} = $version;
|
||||
}
|
||||
|
||||
my %tounpack;
|
||||
while(scalar(keys %jobs)) {
|
||||
my $jobid;
|
||||
if(my @jres = $pool->result_any(\$jobid)) {
|
||||
if($jres[0]{'retval'} == 1) {
|
||||
my $ret = $jres[0]{'retval'};
|
||||
my $filename = $jres[0]{'filename'};
|
||||
my $file = $jres[0]{'filecontent'};
|
||||
my $version = $jobs{$jobid}{'tv'};
|
||||
if(defined($filename)) {
|
||||
#say 'Going to unpack file '.$filename.' containing version '.$version;
|
||||
$tounpack{$filename} = [ $jobs{$jobid}{'id'},$file,$version ];
|
||||
} else {
|
||||
#print Dumper @jres;
|
||||
my $ai = $jobs{$jobid}{'id'};
|
||||
my $an = $addons->[$ai]->{'name'};
|
||||
say 'Passed an empty filename? in update';
|
||||
}
|
||||
} else {
|
||||
print 'Could not find file id for '.$addon->{'name'};
|
||||
my $uri = shift;
|
||||
say 'Download failed for '.$uri;
|
||||
}
|
||||
delete $jobs{$jobid};
|
||||
print '.';
|
||||
}
|
||||
print "\n";
|
||||
}
|
||||
$pool->shutdown;
|
||||
print "\n" if(scalar(@toupd));
|
||||
|
||||
say 'Unpacking updates ' if(scalar(keys %tounpack));
|
||||
foreach my $unpacking(keys %tounpack) {
|
||||
my $id = $tounpack{$unpacking}->[0];
|
||||
my $file = $tounpack{$unpacking}->[1];
|
||||
my $version = $tounpack{$unpacking}->[2];
|
||||
if(pcurse::update($unpacking,$file,$conf->{'wowpath'})) {
|
||||
say 'Updated '.$addons->[$id]->{'name'}.' from version '.$addons->[$id]->{'version'}.' to '.$version;
|
||||
$addons->[$id]->{'version'} = $version;
|
||||
} else {
|
||||
say 'Unpacking failed for '.$unpacking;
|
||||
}
|
||||
}
|
||||
|
||||
($ret,$msg) = pcurse::save_config($conf->{'addons'},$addons);
|
||||
print $msg."\n" unless($ret);
|
||||
|
||||
Reference in New Issue
Block a user