]> Sergey Matveev's repositories - public-inbox.git/blob - ci/deps.perl
remove most internal Email::MIME usage
[public-inbox.git] / ci / deps.perl
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019-2020 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 my @test_essential = qw(Test::Simple); # we actually use Test::More
13
14 # package profiles
15 my $profiles = {
16         # the smallest possible profile for testing
17         # TODO: trim URI::Escape from this, maybe
18         essential => [ qw(
19                 git
20                 perl
21                 Devel::Peek
22                 Digest::SHA
23                 Encode
24                 ExtUtils::MakeMaker
25                 IO::Compress::Gzip
26                 URI::Escape
27                 ), @test_essential ],
28
29         # everything optional for normal use
30         optional => [ qw(
31                 Date::Parse
32                 BSD::Resource
33                 DBD::SQLite
34                 DBI
35                 Filesys::Notify::Simple
36                 Inline::C
37                 Net::Server
38                 Plack
39                 Plack::Test
40                 Plack::Middleware::Deflater
41                 Plack::Middleware::ReverseProxy
42                 Search::Xapian
43                 Socket6
44                 highlight.pm
45                 xapian-compact
46                 ) ],
47
48         # optional developer stuff
49         devtest => [ qw(
50                 XML::TreePP
51                 curl
52                 w3m
53                 ) ],
54 };
55
56 # account for granularity differences between package systems and OSes
57 my @precious;
58 if ($^O eq 'freebsd') {
59         @precious = qw(perl curl Socket6 IO::Compress::Gzip);
60 } elsif ($pkg_fmt eq 'rpm') {
61         @precious = qw(perl curl);
62 }
63
64 if (@precious) {
65         my $re = join('|', map { quotemeta($_) } @precious);
66         for my $list (values %$profiles) {
67                 @$list = grep(!/\A(?:$re)\z/, @$list);
68         }
69         push @{$profiles->{essential}}, @precious;
70 }
71
72
73 # bare minimum for v2
74 $profiles->{v2essential} = [ @{$profiles->{essential}}, qw(DBD::SQLite DBI) ];
75
76 # package names which can't be mapped automatically:
77 my $non_auto = {
78         'perl' => { pkg => 'perl5' },
79         'Date::Parse' => {
80                 deb => 'libtimedate-perl',
81                 pkg => 'p5-TimeDate',
82                 rpm => 'perl-TimeDate',
83         },
84         'Devel::Peek' => {
85                 deb => 'perl', # libperl5.XX, but the XX varies
86                 pkg => 'perl5',
87         },
88         'Digest::SHA' => {
89                 deb => 'perl', # libperl5.XX, but the XX varies
90                 pkg => 'perl5',
91         },
92         'Encode' => {
93                 deb => 'perl', # libperl5.XX, but the XX varies
94                 pkg => 'perl5',
95                 rpm => 'perl-Encode',
96         },
97         'ExtUtils::MakeMaker' => {
98                 deb => 'perl', # perl-modules-5.xx
99                 pkg => 'perl5',
100                 rpm => 'perl-ExtUtils-MakeMaker',
101         },
102         'IO::Compress::Gzip' => {
103                 deb => 'perl', # perl-modules-5.xx
104                 pkg => 'perl5',
105                 rpm => 'perl-IO-Compress',
106         },
107         'DBD::SQLite' => { deb => 'libdbd-sqlite3-perl' },
108         'Plack::Test' => {
109                 deb => 'libplack-perl',
110                 pkg => 'p5-Plack',
111                 rpm => 'perl-Plack-Test',
112         },
113         'URI::Escape' => {
114                 deb => 'liburi-perl',
115                 pkg => 'p5-URI',
116                 rpm => 'perl-URI',
117         },
118         'Test::Simple' => {
119                 deb => 'perl', # perl-modules-5.XX, but the XX varies
120                 pkg => 'perl5',
121                 rpm => 'perl-Test-Simple',
122         },
123         'highlight.pm' => {
124                 deb => 'libhighlight-perl',
125                 pkg => [],
126                 rpm => [],
127         },
128
129         # we call xapian-compact(1) in public-inbox-compact(1)
130         'xapian-compact' => {
131                 deb => 'xapian-tools',
132                 pkg => 'xapian-core',
133                 rpm => 'xapian-core', # ???
134         },
135
136         # OS-specific
137         'IO::KQueue' => {
138                 deb => [],
139                 pkg => 'p5-IO-KQueue',
140                 rpm => [],
141         },
142 };
143
144 my (@pkg_install, @pkg_remove, %all);
145 for my $ary (values %$profiles) {
146         $all{$_} = \@pkg_remove for @$ary;
147 }
148 if ($^O eq 'freebsd') {
149         $all{'IO::KQueue'} = \@pkg_remove;
150 }
151 $profiles->{all} = [ keys %all ]; # pseudo-profile for all packages
152
153 # parse the profile list from the command-line
154 for my $profile (@ARGV) {
155         if ($profile =~ s/-\z//) {
156                 # like apt-get, trailing "-" means remove
157                 profile2dst($profile, \@pkg_remove);
158         } else {
159                 profile2dst($profile, \@pkg_install);
160         }
161 }
162
163 # fill in @pkg_install and @pkg_remove:
164 while (my ($pkg, $dst_pkg_list) = each %all) {
165         push @$dst_pkg_list, list(pkg2ospkg($pkg, $pkg_fmt));
166 }
167
168 my @apt_opts =
169         qw(-o APT::Install-Recommends=false -o APT::Install-Suggests=false);
170
171 # OS-specific cleanups appreciated
172
173 if ($pkg_fmt eq 'deb') {
174         my @quiet = $ENV{V} ? () : ('-q');
175         root('apt-get', @apt_opts, qw(install --purge -y), @quiet,
176                 @pkg_install,
177                 # apt-get lets you suffix a package with "-" to
178                 # remove it in an "install" sub-command:
179                 map { "$_-" } @pkg_remove);
180         root('apt-get', @apt_opts, qw(autoremove --purge -y), @quiet);
181 } elsif ($pkg_fmt eq 'pkg') {
182         my @quiet = $ENV{V} ? () : ('-q');
183         # FreeBSD, maybe other *BSDs are similar?
184
185         # don't remove stuff that isn't installed:
186         exclude_uninstalled(\@pkg_remove);
187         root(qw(pkg remove -y), @quiet, @pkg_remove) if @pkg_remove;
188         root(qw(pkg install -y), @quiet, @pkg_install) if @pkg_install;
189         root(qw(pkg autoremove -y), @quiet);
190 # TODO: yum / rpm support
191 } elsif ($pkg_fmt eq 'rpm') {
192         my @quiet = $ENV{V} ? () : ('-q');
193         exclude_uninstalled(\@pkg_remove);
194         root(qw(yum remove -y), @quiet, @pkg_remove) if @pkg_remove;
195         root(qw(yum install -y), @quiet, @pkg_install) if @pkg_install;
196 } else {
197         die "unsupported package format: $pkg_fmt\n";
198 }
199 exit 0;
200
201
202 # map a generic package name to an OS package name
203 sub pkg2ospkg {
204         my ($pkg, $fmt) = @_;
205
206         # check explicit overrides, first:
207         if (my $ospkg = $non_auto->{$pkg}->{$fmt}) {
208                 return $ospkg;
209         }
210
211         # check common Perl module name patterns:
212         if ($pkg =~ /::/ || $pkg =~ /\A[A-Z]/) {
213                 if ($fmt eq 'deb') {
214                         $pkg =~ s/::/-/g;
215                         $pkg =~ tr/A-Z/a-z/;
216                         return "lib$pkg-perl";
217                 } elsif ($fmt eq 'rpm') {
218                         $pkg =~ s/::/-/g;
219                         return "perl-$pkg"
220                 } elsif ($fmt eq 'pkg') {
221                         $pkg =~ s/::/-/g;
222                         return "p5-$pkg"
223                 } else {
224                         die "unsupported package format: $fmt for $pkg\n"
225                 }
226         }
227
228         # use package name as-is (e.g. 'curl' or 'w3m')
229         $pkg;
230 }
231
232 # maps a install profile to a package list (@pkg_remove or @pkg_install)
233 sub profile2dst {
234         my ($profile, $dst_pkg_list) = @_;
235         if (my $pkg_list = $profiles->{$profile}) {
236                 $all{$_} = $dst_pkg_list for @$pkg_list;
237         } elsif ($all{$profile}) { # $profile is just a package name
238                 $all{$profile} = $dst_pkg_list;
239         } else {
240                 die "unrecognized profile or package: $profile\n";
241         }
242 }
243
244 sub exclude_uninstalled {
245         my ($list) = @_;
246         my %inst_check = (
247                 pkg => sub { system(qw(pkg info -q), $_[0]) == 0 },
248                 deb => sub { system("dpkg -s $_[0] >/dev/null 2>&1") == 0 },
249                 rpm => sub { system("rpm -qs $_[0] >/dev/null 2>&1") == 0 },
250         );
251
252         my $cb = $inst_check{$pkg_fmt} || die <<"";
253 don't know how to check install status for $pkg_fmt
254
255         my @tmp;
256         for my $pkg (@$list) {
257                 push @tmp, $pkg if $cb->($pkg);
258         }
259         @$list = @tmp;
260 }
261
262 sub root {
263         print join(' ', @_), "\n";
264         return if $ENV{DRY_RUN};
265         return if system(@_) == 0;
266         warn 'command failed: ', join(' ', @_), "\n";
267         exit($? >> 8);
268 }
269
270 # ensure result can be pushed into an array:
271 sub list {
272         my ($pkg) = @_;
273         ref($pkg) eq 'ARRAY' ? @$pkg : $pkg;
274 }