]> Sergey Matveev's repositories - public-inbox.git/blob - ci/deps.perl
lazy load Xapian and make it optional for v2
[public-inbox.git] / ci / deps.perl
1 #!/usr/bin/perl -w
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
7 use strict;
8 my $usage = "$0 PKG_FMT PROFILE [PROFILE_MOD]";
9 my $pkg_fmt = shift;
10 @ARGV or die $usage, "\n";
11
12 # package profiles
13 my $profiles = {
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
17         essential => [ qw(
18                 git
19                 perl
20                 Date::Parse
21                 Devel::Peek
22                 Email::Simple
23                 Email::MIME
24                 Email::MIME::ContentType
25                 Encode
26                 Filesys::Notify::Simple
27                 Plack
28                 URI::Escape
29                 ) ],
30
31         # everything optional for normal use
32         optional => [ qw(
33                 BSD::Resource
34                 DBD::SQLite
35                 DBI
36                 IO::Compress::Gzip
37                 Inline::C
38                 Net::Server
39                 Plack::Middleware::Deflater
40                 Plack::Middleware::ReverseProxy
41                 Search::Xapian
42                 Socket6
43                 highlight.pm
44                 xapian-compact
45                 ) ],
46
47         # developer stuff
48         devtest => [ qw(
49                 IPC::Run
50                 Test::HTTP::Server::Simple
51                 XML::Feed
52                 curl
53                 w3m
54                 ) ],
55 };
56
57 # account for granularity differences between package systems and OSes
58 my @precious;
59 if ($^O eq 'freebsd') {
60         @precious = qw(perl curl Socket6 IO::Compress::Gzip);
61 }
62
63 if (@precious) {
64         my $re = join('|', map { quotemeta($_) } @precious);
65         for my $list (values %$profiles) {
66                 @$list = grep(!/\A(?:$re)\z/, @$list);
67         }
68         push @{$profiles->{essential}}, @precious;
69 }
70
71
72 # bare minimum for v2
73 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
74
75 # package names which can't be mapped automatically:
76 my $non_auto = {
77         'perl' => { pkg => 'perl5' },
78         'Date::Parse' => {
79                 deb => 'libtimedate-perl',
80                 pkg => 'p5-TimeDate',
81                 rpm => 'perl-TimeDate',
82         },
83         'Devel::Peek' => {
84                 deb => 'perl', # libperl5.XX, but the XX varies
85                 pkg => 'perl5',
86         },
87         'Encode' => {
88                 deb => 'perl', # libperl5.XX, but the XX varies
89                 pkg => 'perl5',
90                 rpm => 'perl-Encode',
91         },
92         'IO::Compress::Gzip' => {
93                 deb => 'perl', # perl-modules-5.xx
94                 pkg => 'perl5',
95                 rpm => 'perl-PerlIO-gzip',
96         },
97         'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
98         'URI::Escape' => {
99                 deb => 'liburi-perl',
100                 pkg => 'p5-URI',
101                 rpm => 'perl-URI',
102         },
103         'highlight.pm' => {
104                 deb => 'libhighlight-perl',
105                 pkg => [],
106                 rpm => [],
107         },
108
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', # ???
114         },
115
116         # OS-specific
117         'IO::KQueue' => {
118                 deb => [],
119                 pkg => 'p5-IO-KQueue',
120                 rpm => [],
121         },
122 };
123
124 my (@pkg_install, @pkg_remove, %all);
125 for my $ary (values %$profiles) {
126         $all{$_} = \@pkg_remove for @$ary;
127 }
128 if ($^O eq 'freebsd') {
129         $all{'IO::KQueue'} = \@pkg_remove;
130 }
131 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
132
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);
138         } else {
139                 profile2dst($profile, \@pkg_install);
140         }
141 }
142
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));
146 }
147
148 my @apt_opts =
149         qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
150
151 # OS-specific cleanups appreciated
152
153 if ($pkg_fmt eq 'deb') {
154         my @quiet = $ENV{V} ? () : ('-q');
155         root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
156                 @pkg_install,
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?
164
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
171 } else {
172         die "unsupported package format: $pkg_fmt\n";
173 }
174 exit 0;
175
176
177 # map a generic package name to an OS package name
178 sub pkg2ospkg {
179         my ($pkg, $fmt) = @_;
180
181         # check explicit overrides, first:
182         if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
183                 return $ospkg;
184         }
185
186         # check common Perl module name patterns:
187         if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
188                 if ($fmt eq 'deb') {
189                         $pkg =~ s/::/-/g;
190                         $pkg =~ tr/A-Z/a-z/;
191                         return "lib$pkg-perl";
192                 } elsif ($fmt eq 'rpm') {
193                         $pkg =~ s/::/-/g;
194                         return "perl-$pkg"
195                 } elsif ($fmt eq 'pkg') {
196                         $pkg =~ s/::/-/g;
197                         return "p5-$pkg"
198                 } else {
199                         die "unsupported package format: $fmt for $pkg\n"
200                 }
201         }
202
203         # use package name as-is (e.g. 'curl' or 'w3m')
204         $pkg;
205 }
206
207 # maps a install profile to a package list (@pkg_remove or @pkg_install)
208 sub profile2dst {
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;
214         } else {
215                 die "unrecognized profile or package: $profile\n";
216         }
217 }
218
219 sub exclude_uninstalled {
220         my ($list) = @_;
221         my %inst_check = (
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 },
225         );
226
227         my $cb = $inst_check{$pkg_fmt} || die <<"";
228 don't know how to check install status for $pkg_fmt
229
230         my @tmp;
231         for my $pkg (@$list) {
232                 push @tmp, $pkg if $cb->($pkg);
233         }
234         @$list = @tmp;
235 }
236
237 sub root {
238         print join(' ', @_), "\n";
239         return if $ENV{DRY_RUN};
240         return if system(@_) == 0;
241         warn 'command failed: ', join(' ', @_), "\n";
242         exit($? >> 8);
243 }
244
245 # ensure result can be pushed into an array:
246 sub list {
247         my ($pkg) = @_;
248         ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;
249 }