-
Notifications
You must be signed in to change notification settings - Fork 2
/
update-depdb.pl
executable file
·138 lines (113 loc) · 3.04 KB
/
update-depdb.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/perl
# This script requires vercmp from pacman in PATH
use strict;
use warnings;
use v5.10;
use autodie;
use File::Basename;
use Cwd;
use Getopt::Std;
use JSON qw( decode_json );
my $progname = basename($0);
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub HELPMESSAGE {
print "usage: $progname [options] DBFILE\n";
print <<EOF
options:
-q suppress some output
-d add --try to the pkgdepdb commandline
-r reinstall same-version packages
EOF
;
}
our ($opt_q, $opt_d, $opt_h, $opt_r);
getopts('qdhr');
if ($opt_h) {
HELPMESSAGE;
exit 0;
}
if (scalar(@ARGV) > 1) {
HELPMESSAGE;
exit 1;
}
my @depdb_params;
if (scalar(@ARGV) == 1) {
@depdb_params = ( 'pkgdepdb', '--db', $ARGV[0], '--json=query' );
} else {
@depdb_params = ( 'pkgdepdb', '--json=query' );
}
# Part 1: check for dups (this should have happened already)
my %packages;
sub vercmp($$) {
my $p;
open $p, '-|', 'vercmp', @_;
my $res= <$p>;
close($p);
die "vercmp failed with status $?" if ($?);
return $res;
}
for my $file (<*.pkg.tar.xz>) {
# Since we're nice, let's warn about missing sigs now, eh?
print "WARNING: missing signature for $file\n" unless $opt_q or -e "${file}.sig";
# get the name
#if (not($file =~ m@^(?<name>.*?(?=-\d))-(?<version>\d[^-]*-\d[^-]*)-(?:x86_64|i686|any)\.pkg\.tar\.xz$@)) {
if (not($file =~ m@^(?<name>.*)-(?<version>[^-]+-\d[^-]*)-(?:x86_64|i686|any)\.pkg\.tar\.xz$@)) {
print "WARNING: archive not recognized as a package: $file\n";
next;
}
my $name = $+{name};
my $ver = $+{version};
if (exists($packages{$name})) {
print "WARNING: duplicate package found: $name\n";
# use the newer one...
next if vercmp(${$packages{$name}}{ver}, $ver) > 0;
}
$packages{$name} = {name => $+{name}, ver => $+{version}, tar => $file};
}
# Part 2: read the pkgdepdb...
open my $pdb, '-|', @depdb_params, '--quiet', '-P';
my $installed_json = do { local $/; <$pdb> };
close $pdb;
die "pkgdepdb failed with status $?" if ($?);
my $pdb_installed = decode_json($installed_json);
my $installed = ${$pdb_installed}{packages};
for my $pkghash (@$installed) {
my $name = ${$pkghash}{name};
next unless exists($packages{$name});
# this package exists; check the version
my $existing = $packages{$name};
my $dbver = ${$pkghash}{version};
my $flver = ${$existing}{ver};
my $cmp = vercmp($dbver, $flver);
if ($cmp > 0) {
# existing is newer
print("A newer version of $name is already in place: $dbver (found version $flver)\n");
delete $packages{$name};
}
elsif ($cmp == 0) {
if ($opt_r) {
print("Reinstalling $name version $dbver\n") unless $opt_q;
next;
}
# without -r we don't reinstall
delete $packages{$name};
}
else {
# existing one is older, this is fine
print("Upgrading $name from $dbver to $flver\n") unless $opt_q;
}
}
my @tarlist;
keys %packages;
while (my ($name, $pkg) = each %packages) {
push @tarlist, ${$pkg}{tar};
}
if (scalar(keys %packages) == 0) {
print("No packages to upgrade\n") unless $opt_q;
exit 0;
}
if ($opt_d) {
exec @depdb_params, '--dry', '-i', @tarlist;
} else {
exec @depdb_params, '-i', @tarlist;
}