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";
14 # the smallest possible profile
15 # TODO: trim this, Plack pulls in Filesys::Notify::Simple,
16 # and we don't need that for mda-only installs
24 Email::MIME::ContentType
26 Filesys::Notify::Simple
31 # everything optional for normal use
39 Plack::Middleware::Deflater
40 Plack::Middleware::ReverseProxy
50 Test::HTTP::Server::Simple
57 # account for granularity differences between package systems and OSes
59 if ($^O eq 'freebsd') {
60 @precious = qw(perl curl Socket6 IO::Compress::Gzip);
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(
80 # package names which can't be mapped automatically:
82 'perl' => { pkg => 'perl5' },
84 deb => 'libtimedate-perl',
86 rpm => 'perl-TimeDate',
89 deb => 'perl', # libperl5.XX, but the XX varies
93 deb => 'perl', # libperl5.XX, but the XX varies
97 'IO::Compress::Gzip' => {
98 deb => 'perl', # perl-modules-5.xx
100 rpm => 'perl-PerlIO-gzip',
102 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
104 deb => 'liburi-perl',
109 deb => 'libhighlight-perl',
114 # we call xapian-compact(1) in public-inbox-compact(1)
115 'xapian-compact' => {
116 deb => 'xapian-tools',
117 pkg => 'xapian-core',
118 rpm => 'xapian-core', # ???
124 pkg => 'p5-IO-KQueue',
129 my (@pkg_install, @pkg_remove, %all);
130 for my $ary (values %$profiles) {
131 $all{$_} = \@pkg_remove for @$ary;
133 if ($^O eq 'freebsd') {
134 $all{'IO::KQueue'} = \@pkg_remove;
136 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
138 # parse the profile list from the command-line
139 for my $profile (@ARGV) {
140 if ($profile =~ s/-\z//) {
141 # like apt-get, trailing "-" means remove
142 profile2dst($profile, \@pkg_remove);
144 profile2dst($profile, \@pkg_install);
148 # fill in @pkg_install and @pkg_remove:
149 while (my ($pkg, $dst_pkg_list) = each %all) {
150 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
154 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
156 # OS-specific cleanups appreciated
158 if ($pkg_fmt eq 'deb') {
159 my @quiet = $ENV{V} ? () : ('-q');
160 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
162 # apt-get lets you suffix a package with "-" to
163 # remove it in an "install" sub-command:
164 map { "$_-" } @pkg_remove);
165 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
166 } elsif ($pkg_fmt eq 'pkg') {
167 my @quiet = $ENV{V} ? () : ('-q');
168 # FreeBSD, maybe other *BSDs are similar?
170 # don't remove stuff that isn't installed:
171 exclude_uninstalled(\@pkg_remove);
172 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
173 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
174 root(qw(pkg autoremove -y), @quiet);
175 # TODO: yum / rpm support
177 die "unsupported package format: $pkg_fmt\n";
182 # map a generic package name to an OS package name
184 my ($pkg, $fmt) = @_;
186 # check explicit overrides, first:
187 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
191 # check common Perl module name patterns:
192 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
196 return "lib$pkg-perl";
197 } elsif ($fmt eq 'rpm') {
200 } elsif ($fmt eq 'pkg') {
204 die "unsupported package format: $fmt for $pkg\n"
208 # use package name as-is (e.g. 'curl' or 'w3m')
212 # maps a install profile to a package list (@pkg_remove or @pkg_install)
214 my ($profile, $dst_pkg_list) = @_;
215 if (my $pkg_list = $profiles->{$profile}) {
216 $all{$_} = $dst_pkg_list for @$pkg_list;
217 } elsif ($all{$profile}) { # $profile is just a package name
218 $all{$profile} = $dst_pkg_list;
220 die "unrecognized profile or package: $profile\n";
224 sub exclude_uninstalled {
227 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
228 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
229 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
232 my $cb = $inst_check{$pkg_fmt} || die <<"";
233 don't know how to check install status for $pkg_fmt
236 for my $pkg (@$list) {
237 push @tmp, $pkg if $cb->($pkg);
243 print join(' ', @_), "\n";
244 return if $ENV{DRY_RUN};
245 return if system(@_) == 0;
246 warn 'command failed: ', join(' ', @_), "\n";
250 # ensure result can be pushed into an array:
253 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;