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
27 # everything optional for normal use
37 Plack::Middleware::ReverseProxy
44 # optional developer stuff
49 Plack::Test::ExternalServer
53 # account for granularity differences between package systems and OSes
55 if ($^O eq 'freebsd') {
56 @precious = qw(perl curl Socket6 IO::Compress::Gzip);
57 } elsif ($pkg_fmt eq 'rpm') {
58 @precious = qw(perl curl);
62 my $re = join('|', map { quotemeta($_) } @precious);
63 for my $list (values %$profiles) {
64 @$list = grep(!/\A(?:$re)\z/, @$list);
66 push @{$profiles->{essential}}, @precious;
71 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
73 # package names which can't be mapped automatically:
75 'perl' => { pkg => 'perl5' },
77 deb => 'libtimedate-perl',
79 rpm => 'perl-TimeDate',
82 deb => 'perl', # libperl5.XX, but the XX varies
86 deb => 'perl', # libperl5.XX, but the XX varies
90 'ExtUtils::MakeMaker' => {
91 deb => 'perl', # perl-modules-5.xx
93 rpm => 'perl-ExtUtils-MakeMaker',
95 'IO::Compress::Gzip' => {
96 deb => 'perl', # perl-modules-5.xx
98 rpm => 'perl-IO-Compress',
100 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
102 deb => 'libplack-perl',
104 rpm => 'perl-Plack-Test',
107 deb => 'liburi-perl',
112 deb => 'perl', # perl-modules-5.XX, but the XX varies
114 rpm => 'perl-Test-Simple',
117 deb => 'libhighlight-perl',
122 # we call xapian-compact(1) in public-inbox-compact(1)
123 'xapian-compact' => {
124 deb => 'xapian-tools',
125 pkg => 'xapian-core',
126 rpm => 'xapian-core', # ???
132 pkg => 'p5-IO-KQueue',
137 my (@pkg_install, @pkg_remove, %all);
138 for my $ary (values %$profiles) {
139 $all{$_} = \@pkg_remove for @$ary;
141 if ($^O eq 'freebsd') {
142 $all{'IO::KQueue'} = \@pkg_remove;
144 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
146 # parse the profile list from the command-line
147 for my $profile (@ARGV) {
148 if ($profile =~ s/-\z//) {
149 # like apt-get, trailing "-" means remove
150 profile2dst($profile, \@pkg_remove);
152 profile2dst($profile, \@pkg_install);
156 # fill in @pkg_install and @pkg_remove:
157 while (my ($pkg, $dst_pkg_list) = each %all) {
158 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
162 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
164 # OS-specific cleanups appreciated
166 if ($pkg_fmt eq 'deb') {
167 my @quiet = $ENV{V} ? () : ('-q');
168 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
170 # apt-get lets you suffix a package with "-" to
171 # remove it in an "install" sub-command:
172 map { "$_-" } @pkg_remove);
173 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
174 } elsif ($pkg_fmt eq 'pkg') {
175 my @quiet = $ENV{V} ? () : ('-q');
176 # FreeBSD, maybe other *BSDs are similar?
178 # don't remove stuff that isn't installed:
179 exclude_uninstalled(\@pkg_remove);
180 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
181 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
182 root(qw(pkg autoremove -y), @quiet);
183 # TODO: yum / rpm support
184 } elsif ($pkg_fmt eq 'rpm') {
185 my @quiet = $ENV{V} ? () : ('-q');
186 exclude_uninstalled(\@pkg_remove);
187 root(qw(yum remove -y), @quiet, @pkg_remove) if @pkg_remove;
188 root(qw(yum install -y), @quiet, @pkg_install) if @pkg_install;
190 die "unsupported package format: $pkg_fmt\n";
195 # map a generic package name to an OS package name
197 my ($pkg, $fmt) = @_;
199 # check explicit overrides, first:
200 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
204 # check common Perl module name patterns:
205 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
209 return "lib$pkg-perl";
210 } elsif ($fmt eq 'rpm') {
213 } elsif ($fmt eq 'pkg') {
217 die "unsupported package format: $fmt for $pkg\n"
221 # use package name as-is (e.g. 'curl' or 'w3m')
225 # maps a install profile to a package list (@pkg_remove or @pkg_install)
227 my ($profile, $dst_pkg_list) = @_;
228 if (my $pkg_list = $profiles->{$profile}) {
229 $all{$_} = $dst_pkg_list for @$pkg_list;
230 } elsif ($all{$profile}) { # $profile is just a package name
231 $all{$profile} = $dst_pkg_list;
233 die "unrecognized profile or package: $profile\n";
237 sub exclude_uninstalled {
240 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
241 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
242 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
245 my $cb = $inst_check{$pkg_fmt} || die <<"";
246 don't know how to check install status for $pkg_fmt
249 for my $pkg (@$list) {
250 push @tmp, $pkg if $cb->($pkg);
256 print join(' ', @_), "\n";
257 return if $ENV{DRY_RUN};
258 return if system(@_) == 0;
259 warn 'command failed: ', join(' ', @_), "\n";
263 # ensure result can be pushed into an array:
266 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;