2 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Helper script for installing/uninstalling packages for CI use
5 # Intended for use on non-production chroots or VMs since it
6 # changes installed packages
8 my $usage = "$0 PKG_FMT PROFILE [PROFILE_MOD]";
10 @ARGV or die $usage, "\n";
12 my @test_essential = qw(Test::Simple); # we actually use Test::More
16 # the smallest possible profile for testing
17 # TODO: trim URI::Escape from this, maybe
29 # everything optional for normal use
35 Filesys::Notify::Simple
40 Plack::Middleware::Deflater
41 Plack::Middleware::ReverseProxy
48 # optional developer stuff
56 # account for granularity differences between package systems and OSes
58 if ($^O eq 'freebsd') {
59 @precious = qw(perl curl Socket6 IO::Compress::Gzip);
60 } elsif ($pkg_fmt eq 'rpm') {
61 @precious = qw(perl curl);
65 my $re = join('|', map { quotemeta($_) } @precious);
66 for my $list (values %$profiles) {
67 @$list = grep(!/\A(?:$re)\z/, @$list);
69 push @{$profiles->{essential}}, @precious;
74 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
76 # package names which can't be mapped automatically:
78 'perl' => { pkg => 'perl5' },
80 deb => 'libtimedate-perl',
82 rpm => 'perl-TimeDate',
85 deb => 'perl', # libperl5.XX, but the XX varies
89 deb => 'perl', # libperl5.XX, but the XX varies
93 deb => 'perl', # libperl5.XX, but the XX varies
97 'ExtUtils::MakeMaker' => {
98 deb => 'perl', # perl-modules-5.xx
100 rpm => 'perl-ExtUtils-MakeMaker',
102 'IO::Compress::Gzip' => {
103 deb => 'perl', # perl-modules-5.xx
105 rpm => 'perl-IO-Compress',
107 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
109 deb => 'libplack-perl',
111 rpm => 'perl-Plack-Test',
114 deb => 'liburi-perl',
119 deb => 'perl', # perl-modules-5.XX, but the XX varies
121 rpm => 'perl-Test-Simple',
124 deb => 'libhighlight-perl',
129 # we call xapian-compact(1) in public-inbox-compact(1)
130 'xapian-compact' => {
131 deb => 'xapian-tools',
132 pkg => 'xapian-core',
133 rpm => 'xapian-core', # ???
139 pkg => 'p5-IO-KQueue',
144 my (@pkg_install, @pkg_remove, %all);
145 for my $ary (values %$profiles) {
146 $all{$_} = \@pkg_remove for @$ary;
148 if ($^O eq 'freebsd') {
149 $all{'IO::KQueue'} = \@pkg_remove;
151 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
153 # parse the profile list from the command-line
154 for my $profile (@ARGV) {
155 if ($profile =~ s/-\z//) {
156 # like apt-get, trailing "-" means remove
157 profile2dst($profile, \@pkg_remove);
159 profile2dst($profile, \@pkg_install);
163 # fill in @pkg_install and @pkg_remove:
164 while (my ($pkg, $dst_pkg_list) = each %all) {
165 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
169 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
171 # OS-specific cleanups appreciated
173 if ($pkg_fmt eq 'deb') {
174 my @quiet = $ENV{V} ? () : ('-q');
175 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
177 # apt-get lets you suffix a package with "-" to
178 # remove it in an "install" sub-command:
179 map { "$_-" } @pkg_remove);
180 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
181 } elsif ($pkg_fmt eq 'pkg') {
182 my @quiet = $ENV{V} ? () : ('-q');
183 # FreeBSD, maybe other *BSDs are similar?
185 # don't remove stuff that isn't installed:
186 exclude_uninstalled(\@pkg_remove);
187 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
188 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
189 root(qw(pkg autoremove -y), @quiet);
190 # TODO: yum / rpm support
191 } elsif ($pkg_fmt eq 'rpm') {
192 my @quiet = $ENV{V} ? () : ('-q');
193 exclude_uninstalled(\@pkg_remove);
194 root(qw(yum remove -y), @quiet, @pkg_remove) if @pkg_remove;
195 root(qw(yum install -y), @quiet, @pkg_install) if @pkg_install;
197 die "unsupported package format: $pkg_fmt\n";
202 # map a generic package name to an OS package name
204 my ($pkg, $fmt) = @_;
206 # check explicit overrides, first:
207 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
211 # check common Perl module name patterns:
212 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
216 return "lib$pkg-perl";
217 } elsif ($fmt eq 'rpm') {
220 } elsif ($fmt eq 'pkg') {
224 die "unsupported package format: $fmt for $pkg\n"
228 # use package name as-is (e.g. 'curl' or 'w3m')
232 # maps a install profile to a package list (@pkg_remove or @pkg_install)
234 my ($profile, $dst_pkg_list) = @_;
235 if (my $pkg_list = $profiles->{$profile}) {
236 $all{$_} = $dst_pkg_list for @$pkg_list;
237 } elsif ($all{$profile}) { # $profile is just a package name
238 $all{$profile} = $dst_pkg_list;
240 die "unrecognized profile or package: $profile\n";
244 sub exclude_uninstalled {
247 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
248 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
249 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
252 my $cb = $inst_check{$pkg_fmt} || die <<"";
253 don't know how to check install status for $pkg_fmt
256 for my $pkg (@$list) {
257 push @tmp, $pkg if $cb->($pkg);
263 print join(' ', @_), "\n";
264 return if $ENV{DRY_RUN};
265 return if system(@_) == 0;
266 warn 'command failed: ', join(' ', @_), "\n";
270 # ensure result can be pushed into an array:
273 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;