2 # Copyright (C) 2019 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 Plack::Test);
16 # the smallest possible profile for testing
17 # TODO: trim this, Plack pulls in Filesys::Notify::Simple,
18 # and we don't need that for mda-only installs
26 Email::MIME::ContentType
29 Filesys::Notify::Simple
34 # everything optional for normal use
43 Plack::Middleware::Deflater
44 Plack::Middleware::ReverseProxy
51 # optional developer stuff
59 # account for granularity differences between package systems and OSes
61 if ($^O eq 'freebsd') {
62 @precious = qw(perl curl Socket6 IO::Compress::Gzip);
63 } elsif ($pkg_fmt eq 'rpm') {
64 @precious = qw(perl curl);
68 my $re = join('|', map { quotemeta($_) } @precious);
69 for my $list (values %$profiles) {
70 @$list = grep(!/\A(?:$re)\z/, @$list);
72 push @{$profiles->{essential}}, @precious;
77 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
79 # package names which can't be mapped automatically:
81 'perl' => { pkg => 'perl5' },
83 deb => 'libtimedate-perl',
85 rpm => 'perl-TimeDate',
88 deb => 'perl', # libperl5.XX, but the XX varies
92 deb => 'perl', # libperl5.XX, but the XX varies
96 deb => 'perl', # libperl5.XX, but the XX varies
100 'ExtUtils::MakeMaker' => {
101 deb => 'perl', # perl-modules-5.xx
103 rpm => 'perl-ExtUtils-MakeMaker',
105 'IO::Compress::Gzip' => {
106 deb => 'perl', # perl-modules-5.xx
108 rpm => 'perl-IO-Compress',
110 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
112 deb => 'libplack-perl',
114 rpm => 'perl-Plack-Test',
117 deb => 'liburi-perl',
122 deb => 'perl', # perl-modules-5.XX, but the XX varies
124 rpm => 'perl-Test-Simple',
127 deb => 'libhighlight-perl',
132 # we call xapian-compact(1) in public-inbox-compact(1)
133 'xapian-compact' => {
134 deb => 'xapian-tools',
135 pkg => 'xapian-core',
136 rpm => 'xapian-core', # ???
142 pkg => 'p5-IO-KQueue',
147 my (@pkg_install, @pkg_remove, %all);
148 for my $ary (values %$profiles) {
149 $all{$_} = \@pkg_remove for @$ary;
151 if ($^O eq 'freebsd') {
152 $all{'IO::KQueue'} = \@pkg_remove;
154 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
156 # parse the profile list from the command-line
157 for my $profile (@ARGV) {
158 if ($profile =~ s/-\z//) {
159 # like apt-get, trailing "-" means remove
160 profile2dst($profile, \@pkg_remove);
162 profile2dst($profile, \@pkg_install);
166 # fill in @pkg_install and @pkg_remove:
167 while (my ($pkg, $dst_pkg_list) = each %all) {
168 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
172 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
174 # OS-specific cleanups appreciated
176 if ($pkg_fmt eq 'deb') {
177 my @quiet = $ENV{V} ? () : ('-q');
178 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
180 # apt-get lets you suffix a package with "-" to
181 # remove it in an "install" sub-command:
182 map { "$_-" } @pkg_remove);
183 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
184 } elsif ($pkg_fmt eq 'pkg') {
185 my @quiet = $ENV{V} ? () : ('-q');
186 # FreeBSD, maybe other *BSDs are similar?
188 # don't remove stuff that isn't installed:
189 exclude_uninstalled(\@pkg_remove);
190 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
191 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
192 root(qw(pkg autoremove -y), @quiet);
193 # TODO: yum / rpm support
194 } elsif ($pkg_fmt eq 'rpm') {
195 my @quiet = $ENV{V} ? () : ('-q');
196 exclude_uninstalled(\@pkg_remove);
197 root(qw(yum remove -y), @quiet, @pkg_remove) if @pkg_remove;
198 root(qw(yum install -y), @quiet, @pkg_install) if @pkg_install;
200 die "unsupported package format: $pkg_fmt\n";
205 # map a generic package name to an OS package name
207 my ($pkg, $fmt) = @_;
209 # check explicit overrides, first:
210 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
214 # check common Perl module name patterns:
215 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
219 return "lib$pkg-perl";
220 } elsif ($fmt eq 'rpm') {
223 } elsif ($fmt eq 'pkg') {
227 die "unsupported package format: $fmt for $pkg\n"
231 # use package name as-is (e.g. 'curl' or 'w3m')
235 # maps a install profile to a package list (@pkg_remove or @pkg_install)
237 my ($profile, $dst_pkg_list) = @_;
238 if (my $pkg_list = $profiles->{$profile}) {
239 $all{$_} = $dst_pkg_list for @$pkg_list;
240 } elsif ($all{$profile}) { # $profile is just a package name
241 $all{$profile} = $dst_pkg_list;
243 die "unrecognized profile or package: $profile\n";
247 sub exclude_uninstalled {
250 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
251 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
252 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
255 my $cb = $inst_check{$pkg_fmt} || die <<"";
256 don't know how to check install status for $pkg_fmt
259 for my $pkg (@$list) {
260 push @tmp, $pkg if $cb->($pkg);
266 print join(' ', @_), "\n";
267 return if $ENV{DRY_RUN};
268 return if system(@_) == 0;
269 warn 'command failed: ', join(' ', @_), "\n";
273 # ensure result can be pushed into an array:
276 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;