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
39 Plack::Middleware::ReverseProxy
46 # optional developer stuff
54 # account for granularity differences between package systems and OSes
56 if ($^O eq 'freebsd') {
57 @precious = qw(perl curl Socket6 IO::Compress::Gzip);
58 } elsif ($pkg_fmt eq 'rpm') {
59 @precious = qw(perl curl);
63 my $re = join('|', map { quotemeta($_) } @precious);
64 for my $list (values %$profiles) {
65 @$list = grep(!/\A(?:$re)\z/, @$list);
67 push @{$profiles->{essential}}, @precious;
72 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
74 # package names which can't be mapped automatically:
76 'perl' => { pkg => 'perl5' },
78 deb => 'libtimedate-perl',
80 rpm => 'perl-TimeDate',
83 deb => 'perl', # libperl5.XX, but the XX varies
87 deb => 'perl', # libperl5.XX, but the XX varies
91 deb => 'perl', # libperl5.XX, but the XX varies
95 'ExtUtils::MakeMaker' => {
96 deb => 'perl', # perl-modules-5.xx
98 rpm => 'perl-ExtUtils-MakeMaker',
100 'IO::Compress::Gzip' => {
101 deb => 'perl', # perl-modules-5.xx
103 rpm => 'perl-IO-Compress',
105 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
107 deb => 'libplack-perl',
109 rpm => 'perl-Plack-Test',
112 deb => 'liburi-perl',
117 deb => 'perl', # perl-modules-5.XX, but the XX varies
119 rpm => 'perl-Test-Simple',
122 deb => 'libhighlight-perl',
127 # we call xapian-compact(1) in public-inbox-compact(1)
128 'xapian-compact' => {
129 deb => 'xapian-tools',
130 pkg => 'xapian-core',
131 rpm => 'xapian-core', # ???
137 pkg => 'p5-IO-KQueue',
142 my (@pkg_install, @pkg_remove, %all);
143 for my $ary (values %$profiles) {
144 $all{$_} = \@pkg_remove for @$ary;
146 if ($^O eq 'freebsd') {
147 $all{'IO::KQueue'} = \@pkg_remove;
149 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
151 # parse the profile list from the command-line
152 for my $profile (@ARGV) {
153 if ($profile =~ s/-\z//) {
154 # like apt-get, trailing "-" means remove
155 profile2dst($profile, \@pkg_remove);
157 profile2dst($profile, \@pkg_install);
161 # fill in @pkg_install and @pkg_remove:
162 while (my ($pkg, $dst_pkg_list) = each %all) {
163 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
167 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
169 # OS-specific cleanups appreciated
171 if ($pkg_fmt eq 'deb') {
172 my @quiet = $ENV{V} ? () : ('-q');
173 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
175 # apt-get lets you suffix a package with "-" to
176 # remove it in an "install" sub-command:
177 map { "$_-" } @pkg_remove);
178 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
179 } elsif ($pkg_fmt eq 'pkg') {
180 my @quiet = $ENV{V} ? () : ('-q');
181 # FreeBSD, maybe other *BSDs are similar?
183 # don't remove stuff that isn't installed:
184 exclude_uninstalled(\@pkg_remove);
185 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
186 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
187 root(qw(pkg autoremove -y), @quiet);
188 # TODO: yum / rpm support
189 } elsif ($pkg_fmt eq 'rpm') {
190 my @quiet = $ENV{V} ? () : ('-q');
191 exclude_uninstalled(\@pkg_remove);
192 root(qw(yum remove -y), @quiet, @pkg_remove) if @pkg_remove;
193 root(qw(yum install -y), @quiet, @pkg_install) if @pkg_install;
195 die "unsupported package format: $pkg_fmt\n";
200 # map a generic package name to an OS package name
202 my ($pkg, $fmt) = @_;
204 # check explicit overrides, first:
205 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
209 # check common Perl module name patterns:
210 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
214 return "lib$pkg-perl";
215 } elsif ($fmt eq 'rpm') {
218 } elsif ($fmt eq 'pkg') {
222 die "unsupported package format: $fmt for $pkg\n"
226 # use package name as-is (e.g. 'curl' or 'w3m')
230 # maps a install profile to a package list (@pkg_remove or @pkg_install)
232 my ($profile, $dst_pkg_list) = @_;
233 if (my $pkg_list = $profiles->{$profile}) {
234 $all{$_} = $dst_pkg_list for @$pkg_list;
235 } elsif ($all{$profile}) { # $profile is just a package name
236 $all{$profile} = $dst_pkg_list;
238 die "unrecognized profile or package: $profile\n";
242 sub exclude_uninstalled {
245 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
246 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
247 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
250 my $cb = $inst_check{$pkg_fmt} || die <<"";
251 don't know how to check install status for $pkg_fmt
254 for my $pkg (@$list) {
255 push @tmp, $pkg if $cb->($pkg);
261 print join(' ', @_), "\n";
262 return if $ENV{DRY_RUN};
263 return if system(@_) == 0;
264 warn 'command failed: ', join(' ', @_), "\n";
268 # ensure result can be pushed into an array:
271 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;