sbocheck (3826B)
1 #!/usr/bin/perl 2 # 3 # vim: ts=4:noet 4 # 5 # sbocheck 6 # script to update the local sbo tree and check for updates 7 # 8 # authors: Jacob Pipkin <j@dawnrazor.net> 9 # Luke Williams <xocel@iquidus.org> 10 # Andreas Guldstrand <andreas.guldstrand@gmail.com> 11 # maintainer: Slack Coder <slackcoder@server.ky> 12 13 use 5.16.0; 14 use strict; 15 use warnings FATAL => 'all'; 16 use SBO::Lib qw/ _ERR_USAGE update_tree get_available_updates script_error open_fh is_local show_version get_local_outdated_versions /; 17 use Getopt::Long; 18 use File::Basename; 19 use List::Util 'max'; 20 use Data::Dumper; 21 22 my $self = basename($0); 23 24 sub show_usage { 25 print <<"EOF"; 26 Usage: $self 27 28 Options: 29 -h|--help: 30 this screen. 31 -v|--version: 32 version information. 33 34 EOF 35 return 1; 36 } 37 38 my ($help, $vers); 39 40 if (! GetOptions('help|h' => \$help, 'version|v' => \$vers)) { 41 show_usage(); 42 exit 1; 43 } 44 45 if ($help) { show_usage(); exit 0 } 46 if ($vers) { show_version(); exit 0 } 47 48 unless ($< == 0) { 49 warn "This script requires root privileges.\n"; 50 exit 51 } 52 53 update_tree(); 54 55 # retrieve and format list of available updates 56 sub get_update_list { 57 print "Checking for updated SlackBuilds...\n"; 58 my @updates = @{ get_available_updates() }; 59 my @outdated = get_local_outdated_versions(); 60 return() unless @outdated + @updates; 61 62 my %updates; 63 for my $update (@updates) { 64 $updates{$update->{name}} = { 65 installed => $update->{installed}, 66 available => $update->{update}, 67 local => is_local($update->{name}) 68 }; 69 } 70 for my $update (@outdated) { 71 my $name = $update->{name}; 72 $updates{$name}{installed} = $update->{version}; 73 $updates{$name}{sbo} = $update->{orig}; 74 $updates{$name}{local} = 1; 75 } 76 77 # Output should look like this where the < is aligned to the longest sboname 1.0 string (excepting ones that would then wrap): 78 # sboname 1.0 < needs updating (1.1 from overrides) 79 # sboname 1.0 < needs updating (1.1 from SBo) 80 # sboname 1.0 < needs updating (1.1 from overrides, 1.2 from SBo) 81 # sboname 1.0 < override outdated (1.1 from SBo) 82 83 my $max = 0; 84 foreach my $sbo (keys %updates) { 85 my $info = $updates{$sbo}; 86 my $current = sprintf "%s %s", $sbo, $info->{installed}; 87 88 my $available = ''; 89 if (defined $info->{available} and defined $info->{sbo}) { 90 $available = sprintf "needs updating (%s from overrides, %s from SBo)", $info->{available}, $info->{sbo}; 91 } 92 elsif ($info->{available}) { 93 $available = sprintf "needs updating (%s from %s)", $info->{available}, $info->{local} ? "overrides" : "SBo"; 94 } 95 else { 96 $available = sprintf "override outdated (%s from SBo)", $info->{sbo}; 97 } 98 $info->{name_str} = $current; 99 $info->{upd_str} = $available; 100 101 my $str = sprintf "%s < %s", $current, $available; 102 if (length($str) <= 80) { 103 $max = length($current) if length($current) > $max; 104 } 105 } 106 107 my @listing; 108 foreach my $sbo (sort keys %updates) { 109 my $info = $updates{$sbo}; 110 111 my $str = sprintf "%s < %s", $info->{name_str}, $info->{upd_str}; 112 if (length($str) <= 80) { 113 $str = sprintf "%-*s < %s", $max, $info->{name_str}, $info->{upd_str}; 114 my $adjust = 1; 115 while (length($str) > 80) { 116 $str = sprintf "%-*s < %s", $max-$adjust++, $info->{name_str}, $info->{upd_str}; 117 } 118 } 119 push @listing, $str; 120 } 121 return @listing; 122 } 123 124 # print list of updates 125 sub print_output { 126 my @listing = @_; 127 if (@listing) { 128 print "\n"; 129 say $_ for @listing; 130 print "\n"; 131 # save a log of available updates 132 my $logfile = '/var/log/sbocheck.log'; 133 unlink $logfile if -f $logfile; 134 my ($log_fh, $exit) = open_fh($logfile, '>'); 135 # non-fatal 136 if ($exit) { 137 warn $log_fh; 138 } else { 139 say {$log_fh} $_ for @listing; 140 close $log_fh; 141 say "A copy of the above result is kept in $logfile\n"; 142 } 143 } else { 144 say "\nNo updates available."; 145 } 146 return 1; 147 } 148 149 my @listing = get_update_list(); 150 print_output(@listing); 151 152 exit 0;