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);
61 } elsif ($pkg_fmt eq 'rpm') {
62 @precious = qw(perl curl);
66 my $re = join('|', map { quotemeta($_) } @precious);
67 for my $list (values %$profiles) {
68 @$list = grep(!/\A(?:$re)\z/, @$list);
70 push @{$profiles->{essential}}, @precious;
75 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
77 # package names which can't be mapped automatically:
79 'perl' => { pkg => 'perl5' },
81 deb => 'libtimedate-perl',
83 rpm => 'perl-TimeDate',
86 deb => 'perl', # libperl5.XX, but the XX varies
90 deb => 'perl', # libperl5.XX, but the XX varies
94 'IO::Compress::Gzip' => {
95 deb => 'perl', # perl-modules-5.xx
97 rpm => 'perl-PerlIO-gzip',
99 'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
101 deb => 'liburi-perl',
106 deb => 'libhighlight-perl',
111 # we call xapian-compact(1) in public-inbox-compact(1)
112 'xapian-compact' => {
113 deb => 'xapian-tools',
114 pkg => 'xapian-core',
115 rpm => 'xapian-core', # ???
121 pkg => 'p5-IO-KQueue',
126 my (@pkg_install, @pkg_remove, %all);
127 for my $ary (values %$profiles) {
128 $all{$_} = \@pkg_remove for @$ary;
130 if ($^O eq 'freebsd') {
131 $all{'IO::KQueue'} = \@pkg_remove;
133 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
135 # parse the profile list from the command-line
136 for my $profile (@ARGV) {
137 if ($profile =~ s/-\z//) {
138 # like apt-get, trailing "-" means remove
139 profile2dst($profile, \@pkg_remove);
141 profile2dst($profile, \@pkg_install);
145 # fill in @pkg_install and @pkg_remove:
146 while (my ($pkg, $dst_pkg_list) = each %all) {
147 push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
151 qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
153 # OS-specific cleanups appreciated
155 if ($pkg_fmt eq 'deb') {
156 my @quiet = $ENV{V} ? () : ('-q');
157 root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
159 # apt-get lets you suffix a package with "-" to
160 # remove it in an "install" sub-command:
161 map { "$_-" } @pkg_remove);
162 root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
163 } elsif ($pkg_fmt eq 'pkg') {
164 my @quiet = $ENV{V} ? () : ('-q');
165 # FreeBSD, maybe other *BSDs are similar?
167 # don't remove stuff that isn't installed:
168 exclude_uninstalled(\@pkg_remove);
169 root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
170 root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
171 root(qw(pkg autoremove -y), @quiet);
172 # TODO: yum / rpm support
173 } elsif ($pkg_fmt eq 'rpm') {
174 my @quiet = $ENV{V} ? () : ('-q');
175 exclude_uninstalled(\@pkg_remove);
176 root(qw(yum remove -y), @quiet, @pkg_remove) if @pkg_remove;
177 root(qw(yum install -y), @quiet, @pkg_install) if @pkg_install;
179 die "unsupported package format: $pkg_fmt\n";
184 # map a generic package name to an OS package name
186 my ($pkg, $fmt) = @_;
188 # check explicit overrides, first:
189 if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
193 # check common Perl module name patterns:
194 if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
198 return "lib$pkg-perl";
199 } elsif ($fmt eq 'rpm') {
202 } elsif ($fmt eq 'pkg') {
206 die "unsupported package format: $fmt for $pkg\n"
210 # use package name as-is (e.g. 'curl' or 'w3m')
214 # maps a install profile to a package list (@pkg_remove or @pkg_install)
216 my ($profile, $dst_pkg_list) = @_;
217 if (my $pkg_list = $profiles->{$profile}) {
218 $all{$_} = $dst_pkg_list for @$pkg_list;
219 } elsif ($all{$profile}) { # $profile is just a package name
220 $all{$profile} = $dst_pkg_list;
222 die "unrecognized profile or package: $profile\n";
226 sub exclude_uninstalled {
229 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
230 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
231 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
234 my $cb = $inst_check{$pkg_fmt} || die <<"";
235 don't know how to check install status for $pkg_fmt
238 for my $pkg (@$list) {
239 push @tmp, $pkg if $cb->($pkg);
245 print join(' ', @_), "\n";
246 return if $ENV{DRY_RUN};
247 return if system(@_) == 0;
248 warn 'command failed: ', join(' ', @_), "\n";
252 # ensure result can be pushed into an array:
255 ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;