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(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 'IO::Compress::Gzip' => {
93 deb => 'perl', # perl-modules-5.xx
95 rpm => 'perl-PerlIO-gzip',
97 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
104 deb => 'libhighlight-perl',
109 # we call xapian-compact(1) in public-inbox-compact(1)
110 'xapian-compact' => {
111 deb => 'xapian-tools',
112 pkg => 'xapian-core',
113 rpm => 'xapian-core', # ???
119 pkg => 'p5-IO-KQueue',
124 my (@pkg_install, @pkg_remove, %all);
125 for my $ary (values %$profiles) {
126 $all{$_} = \@pkg_remove for @$ary;
128 if ($^O eq 'freebsd') {
129 $all{'IO::KQueue'} = \@pkg_remove;
131 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
133 # parse the profile list from the command-line
134 for my $profile (@ARGV) {
135 if ($profile =~ s/-\z//) {
136 # like apt-get, trailing "-" means remove
137 profile2dst($profile, \@pkg_remove);
139 profile2dst($profile, \@pkg_install);
143 # fill in @pkg_install and @pkg_remove:
144 while (my ($pkg, $dst_pkg_list) = each %all) {
145 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
149 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
151 # OS-specific cleanups appreciated
153 if ($pkg_fmt eq 'deb') {
154 my @quiet = $ENV{V} ? () : ('-q');
155 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
157 # apt-get lets you suffix a package with "-" to
158 # remove it in an "install" sub-command:
159 map { "$_-" } @pkg_remove);
160 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
161 } elsif ($pkg_fmt eq 'pkg') {
162 my @quiet = $ENV{V} ? () : ('-q');
163 # FreeBSD, maybe other *BSDs are similar?
165 # don't remove stuff that isn't installed:
166 exclude_uninstalled(\@pkg_remove);
167 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
168 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
169 root(qw(pkg autoremove -y), @quiet);
170 # TODO: yum / rpm support
172 die "unsupported package format: $pkg_fmt\n";
177 # map a generic package name to an OS package name
179 my ($pkg, $fmt) = @_;
181 # check explicit overrides, first:
182 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
186 # check common Perl module name patterns:
187 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
191 return "lib$pkg-perl";
192 } elsif ($fmt eq 'rpm') {
195 } elsif ($fmt eq 'pkg') {
199 die "unsupported package format: $fmt for $pkg\n"
203 # use package name as-is (e.g. 'curl' or 'w3m')
207 # maps a install profile to a package list (@pkg_remove or @pkg_install)
209 my ($profile, $dst_pkg_list) = @_;
210 if (my $pkg_list = $profiles->{$profile}) {
211 $all{$_} = $dst_pkg_list for @$pkg_list;
212 } elsif ($all{$profile}) { # $profile is just a package name
213 $all{$profile} = $dst_pkg_list;
215 die "unrecognized profile or package: $profile\n";
219 sub exclude_uninstalled {
222 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
223 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
224 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
227 my $cb = $inst_check{$pkg_fmt} || die <<"";
228 don't know how to check install status for $pkg_fmt
231 for my $pkg (@$list) {
232 push @tmp, $pkg if $cb->($pkg);
238 print join(' ', @_), "\n";
239 return if $ENV{DRY_RUN};
240 return if system(@_) == 0;
241 warn 'command failed: ', join(' ', @_), "\n";
245 # ensure result can be pushed into an array:
248 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;