2 # Copyright (C) 2019-2021 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
39 Plack::Middleware::ReverseProxy
46 # optional developer stuff
51 Plack::Test::ExternalServer
55 # account for granularity differences between package systems and OSes
57 if ($^O eq 'freebsd') {
58 @precious = qw(perl curl Socket6 IO::Compress::Gzip);
59 } elsif ($pkg_fmt eq 'rpm') {
60 @precious = qw(perl curl);
64 my $re = join('|', map { quotemeta($_) } @precious);
65 for my $list (values %$profiles) {
66 @$list = grep(!/\A(?:$re)\z/, @$list);
68 push @{$profiles->{essential}}, @precious;
73 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
75 # package names which can't be mapped automatically:
77 'perl' => { pkg => 'perl5' },
79 deb => 'libtimedate-perl',
81 rpm => 'perl-TimeDate',
84 deb => 'perl', # libperl5.XX, but the XX varies
88 deb => 'perl', # libperl5.XX, but the XX varies
92 deb => 'perl', # libperl5.XX, but the XX varies
96 'ExtUtils::MakeMaker' => {
97 deb => 'perl', # perl-modules-5.xx
99 rpm => 'perl-ExtUtils-MakeMaker',
101 'IO::Compress::Gzip' => {
102 deb => 'perl', # perl-modules-5.xx
104 rpm => 'perl-IO-Compress',
106 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
108 deb => 'libplack-perl',
110 rpm => 'perl-Plack-Test',
113 deb => 'liburi-perl',
118 deb => 'perl', # perl-modules-5.XX, but the XX varies
120 rpm => 'perl-Test-Simple',
123 deb => 'libhighlight-perl',
128 # we call xapian-compact(1) in public-inbox-compact(1)
129 'xapian-compact' => {
130 deb => 'xapian-tools',
131 pkg => 'xapian-core',
132 rpm => 'xapian-core', # ???
138 pkg => 'p5-IO-KQueue',
143 my (@pkg_install, @pkg_remove, %all);
144 for my $ary (values %$profiles) {
145 $all{$_} = \@pkg_remove for @$ary;
147 if ($^O eq 'freebsd') {
148 $all{'IO::KQueue'} = \@pkg_remove;
150 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
152 # parse the profile list from the command-line
153 for my $profile (@ARGV) {
154 if ($profile =~ s/-\z//) {
155 # like apt-get, trailing "-" means remove
156 profile2dst($profile, \@pkg_remove);
158 profile2dst($profile, \@pkg_install);
162 # fill in @pkg_install and @pkg_remove:
163 while (my ($pkg, $dst_pkg_list) = each %all) {
164 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
168 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
170 # OS-specific cleanups appreciated
172 if ($pkg_fmt eq 'deb') {
173 my @quiet = $ENV{V} ? () : ('-q');
174 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
176 # apt-get lets you suffix a package with "-" to
177 # remove it in an "install" sub-command:
178 map { "$_-" } @pkg_remove);
179 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
180 } elsif ($pkg_fmt eq 'pkg') {
181 my @quiet = $ENV{V} ? () : ('-q');
182 # FreeBSD, maybe other *BSDs are similar?
184 # don't remove stuff that isn't installed:
185 exclude_uninstalled(\@pkg_remove);
186 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
187 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
188 root(qw(pkg autoremove -y), @quiet);
189 # TODO: yum / rpm support
190 } elsif ($pkg_fmt eq 'rpm') {
191 my @quiet = $ENV{V} ? () : ('-q');
192 exclude_uninstalled(\@pkg_remove);
193 root(qw(yum remove -y), @quiet, @pkg_remove) if @pkg_remove;
194 root(qw(yum install -y), @quiet, @pkg_install) if @pkg_install;
196 die "unsupported package format: $pkg_fmt\n";
201 # map a generic package name to an OS package name
203 my ($pkg, $fmt) = @_;
205 # check explicit overrides, first:
206 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
210 # check common Perl module name patterns:
211 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
215 return "lib$pkg-perl";
216 } elsif ($fmt eq 'rpm') {
219 } elsif ($fmt eq 'pkg') {
223 die "unsupported package format: $fmt for $pkg\n"
227 # use package name as-is (e.g. 'curl' or 'w3m')
231 # maps a install profile to a package list (@pkg_remove or @pkg_install)
233 my ($profile, $dst_pkg_list) = @_;
234 if (my $pkg_list = $profiles->{$profile}) {
235 $all{$_} = $dst_pkg_list for @$pkg_list;
236 } elsif ($all{$profile}) { # $profile is just a package name
237 $all{$profile} = $dst_pkg_list;
239 die "unrecognized profile or package: $profile\n";
243 sub exclude_uninstalled {
246 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
247 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
248 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
251 my $cb = $inst_check{$pkg_fmt} || die <<"";
252 don't know how to check install status for $pkg_fmt
255 for my $pkg (@$list) {
256 push @tmp, $pkg if $cb->($pkg);
262 print join(' ', @_), "\n";
263 return if $ENV{DRY_RUN};
264 return if system(@_) == 0;
265 warn 'command failed: ', join(' ', @_), "\n";
269 # ensure result can be pushed into an array:
272 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;