sphinx-pre-install: use a requirements file
[linux-block.git] / scripts / sphinx-pre-install
CommitLineData
24071ac1
MCC
1#!/usr/bin/perl
2use strict;
3
4# Copyright (c) 2017 Mauro Carvalho Chehab <mchehab@kernel.org>
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15
5be33182 16my $virtenv_dir = "sphinx_1.4";
fb947f3f 17my $requirement_file = "Documentation/sphinx/requirements.txt";
5be33182 18
24071ac1
MCC
19#
20# Static vars
21#
22
23my %missing;
24my $system_release;
25my $need = 0;
26my $optional = 0;
27my $need_symlink = 0;
28my $need_sphinx = 0;
29my $install = "";
30
31#
32# Command line arguments
33#
34
35my $pdf = 1;
36my $virtualenv = 1;
37
38#
39# List of required texlive packages on Fedora and OpenSuse
40#
41
42my %texlive = (
43 'adjustbox.sty' => 'texlive-adjustbox',
44 'amsfonts.sty' => 'texlive-amsfonts',
45 'amsmath.sty' => 'texlive-amsmath',
46 'amssymb.sty' => 'texlive-amsfonts',
47 'amsthm.sty' => 'texlive-amscls',
48 'anyfontsize.sty' => 'texlive-anyfontsize',
49 'atbegshi.sty' => 'texlive-oberdiek',
50 'bm.sty' => 'texlive-tools',
51 'capt-of.sty' => 'texlive-capt-of',
52 'cmap.sty' => 'texlive-cmap',
53 'ecrm1000.tfm' => 'texlive-ec',
54 'eqparbox.sty' => 'texlive-eqparbox',
55 'eu1enc.def' => 'texlive-euenc',
56 'fancybox.sty' => 'texlive-fancybox',
57 'fancyvrb.sty' => 'texlive-fancyvrb',
58 'float.sty' => 'texlive-float',
59 'fncychap.sty' => 'texlive-fncychap',
60 'footnote.sty' => 'texlive-mdwtools',
61 'framed.sty' => 'texlive-framed',
62 'luatex85.sty' => 'texlive-luatex85',
63 'multirow.sty' => 'texlive-multirow',
64 'needspace.sty' => 'texlive-needspace',
65 'palatino.sty' => 'texlive-psnfss',
66 'parskip.sty' => 'texlive-parskip',
67 'polyglossia.sty' => 'texlive-polyglossia',
68 'tabulary.sty' => 'texlive-tabulary',
69 'threeparttable.sty' => 'texlive-threeparttable',
70 'titlesec.sty' => 'texlive-titlesec',
71 'ucs.sty' => 'texlive-ucs',
72 'upquote.sty' => 'texlive-upquote',
73 'wrapfig.sty' => 'texlive-wrapfig',
74);
75
76#
77# Subroutines that checks if a feature exists
78#
79
80sub check_missing(%)
81{
82 my %map = %{$_[0]};
83
84 foreach my $prog (sort keys %missing) {
85 my $is_optional = $missing{$prog};
86
87 if ($is_optional) {
88 print "Warning: better to also install \"$prog\".\n";
89 } else {
90 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
91 }
92 if (defined($map{$prog})) {
93 $install .= " " . $map{$prog};
94 } else {
95 $install .= " " . $prog;
96 }
97 }
98
99 $install =~ s/^\s//;
100}
101
102sub add_package($$)
103{
104 my $package = shift;
105 my $is_optional = shift;
106
107 $missing{$package} = $is_optional;
108 if ($is_optional) {
109 $optional++;
110 } else {
111 $need++;
112 }
113}
114
115sub check_missing_file($$$)
116{
117 my $file = shift;
118 my $package = shift;
119 my $is_optional = shift;
120
121 return if(-e $file);
122
123 add_package($package, $is_optional);
124}
125
126sub findprog($)
127{
128 foreach(split(/:/, $ENV{PATH})) {
129 return "$_/$_[0]" if(-x "$_/$_[0]");
130 }
131}
132
133sub check_program($$)
134{
135 my $prog = shift;
136 my $is_optional = shift;
137
138 return if findprog($prog);
139
140 add_package($prog, $is_optional);
141}
142
143sub check_perl_module($$)
144{
145 my $prog = shift;
146 my $is_optional = shift;
147
148 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
149 return if ($err == 0);
150
151 add_package($prog, $is_optional);
152}
153
154sub check_python_module($$)
155{
156 my $prog = shift;
157 my $is_optional = shift;
158
159 my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
160 return if ($err == 0);
161 my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
162 return if ($err == 0);
163
164 add_package($prog, $is_optional);
165}
166
167sub check_rpm_missing($$)
168{
169 my @pkgs = @{$_[0]};
170 my $is_optional = $_[1];
171
172 foreach my $prog(@pkgs) {
173 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
174 add_package($prog, $is_optional) if ($err);
175 }
176}
177
178sub check_pacman_missing($$)
179{
180 my @pkgs = @{$_[0]};
181 my $is_optional = $_[1];
182
183 foreach my $prog(@pkgs) {
184 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
185 add_package($prog, $is_optional) if ($err);
186 }
187}
188
189sub check_missing_tex($)
190{
191 my $is_optional = shift;
192 my $kpsewhich = findprog("kpsewhich");
193
194 foreach my $prog(keys %texlive) {
195 my $package = $texlive{$prog};
196 if (!$kpsewhich) {
197 add_package($package, $is_optional);
198 next;
199 }
200 my $file = qx($kpsewhich $prog);
201 add_package($package, $is_optional) if ($file =~ /^\s*$/);
202 }
203}
204
205sub check_sphinx()
206{
207 return if findprog("sphinx-build");
208
209 if (findprog("sphinx-build-3")) {
210 $need_symlink = 1;
211 return;
212 }
213
214 if ($virtualenv) {
215 check_program("virtualenv", 0) if (!findprog("virtualenv-3"));
216 check_program("pip", 0) if (!findprog("pip3"));
217 $need_sphinx = 1;
218 } else {
219 add_package("python-sphinx", 0);
220 }
221}
222
223#
224# Ancillary subroutines
225#
226
227sub catcheck($)
228{
229 my $res = "";
230 $res = qx(cat $_[0]) if (-r $_[0]);
231 return $res;
232}
233
234sub which($)
235{
236 my $file = shift;
237 my @path = split ":", $ENV{PATH};
238
239 foreach my $dir(@path) {
240 my $name = $dir.'/'.$file;
241 return $name if (-x $name );
242 }
243 return undef;
244}
245
246#
247# Subroutines that check distro-specific hints
248#
249
250sub give_debian_hints()
251{
252 my %map = (
253 "python-sphinx" => "python3-sphinx",
254 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
255 "virtualenv" => "virtualenv",
256 "pip" => "python3-pip",
257 "dot" => "graphviz",
258 "convert" => "imagemagick",
259 "Pod::Usage" => "perl-modules",
260 "xelatex" => "texlive-xetex",
261 );
262
263 if ($pdf) {
264 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
265 "fonts-dejavu", 1);
266 }
267
268 check_program("dvipng", 1) if ($pdf);
269 check_missing(\%map);
270
271 return if (!$need && !$optional);
272 printf("You should run:\n\n\tsudo apt-get install $install\n");
273}
274
275sub give_redhat_hints()
276{
277 my %map = (
278 "python-sphinx" => "python3-sphinx",
279 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
280 "virtualenv" => "python3-virtualenv",
281 "pip" => "python3-pip",
282 "dot" => "graphviz",
283 "convert" => "ImageMagick",
284 "Pod::Usage" => "perl-Pod-Usage",
285 "xelatex" => "texlive-xetex-bin",
286 );
287
288 my @fedora_tex_pkgs = (
289 "texlive-collection-fontsrecommended",
290 "texlive-collection-latex",
291 "dejavu-sans-fonts",
292 "dejavu-serif-fonts",
293 "dejavu-sans-mono-fonts",
294 );
295
296 check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
297 check_missing_tex(1) if ($pdf);
298 check_missing(\%map);
299
300 return if (!$need && !$optional);
301 printf("You should run:\n\n\tsudo dnf install -y $install\n");
302}
303
304sub give_opensuse_hints()
305{
306 my %map = (
307 "python-sphinx" => "python3-sphinx",
308 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
309 "virtualenv" => "python3-virtualenv",
310 "pip" => "python3-pip",
311 "dot" => "graphviz",
312 "convert" => "ImageMagick",
313 "Pod::Usage" => "perl-Pod-Usage",
314 "xelatex" => "texlive-xetex-bin",
315 );
316
317 my @suse_tex_pkgs = (
318 "texlive-babel-english",
319 "texlive-caption",
320 "texlive-colortbl",
321 "texlive-courier",
322 "texlive-dvips",
323 "texlive-helvetic",
324 "texlive-makeindex",
325 "texlive-metafont",
326 "texlive-metapost",
327 "texlive-palatino",
328 "texlive-preview",
329 "texlive-times",
330 "texlive-zapfchan",
331 "texlive-zapfding",
332 );
333
334 check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
335 check_missing_tex(1) if ($pdf);
336 check_missing(\%map);
337
338 return if (!$need && !$optional);
339 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
340}
341
342sub give_arch_linux_hints()
343{
344 my %map = (
345 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
346 "virtualenv" => "python-virtualenv",
347 "pip" => "python-pip",
348 "dot" => "graphviz",
349 "convert" => "imagemagick",
350 "xelatex" => "texlive-bin",
351 );
352
353 my @archlinux_tex_pkgs = (
354 "texlive-core",
355 "texlive-latexextra",
356 "ttf-dejavu",
357 );
358 check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
359 check_missing(\%map);
360
361 return if (!$need && !$optional);
362 printf("You should run:\n\n\tsudo pacman -S $install\n");
363}
364
365sub give_gentoo_hints()
366{
367 my %map = (
368 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
369 "virtualenv" => "dev-python/virtualenv",
370 "pip" => "dev-python/pip",
371 "dot" => "media-gfx/graphviz",
372 "convert" => "media-gfx/imagemagick",
373 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
374 );
375
376 check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
377 "media-fonts/dejavu", 1) if ($pdf);
378
379 check_missing(\%map);
380
381 return if (!$need && !$optional);
382 printf("You should run:\n\n\tsudo emerge --ask $install\n");
383}
384
385sub check_distros()
386{
387 # Distro-specific hints
388 if ($system_release =~ /Red Hat Enterprise Linux/) {
389 give_redhat_hints;
390 return;
391 }
392 if ($system_release =~ /Fedora/) {
393 give_redhat_hints;
394 return;
395 }
396 if ($system_release =~ /Ubuntu/) {
397 give_debian_hints;
398 return;
399 }
400 if ($system_release =~ /Debian/) {
401 give_debian_hints;
402 return;
403 }
404 if ($system_release =~ /openSUSE/) {
405 give_opensuse_hints;
406 return;
407 }
408 if ($system_release =~ /Arch Linux/) {
409 give_arch_linux_hints;
410 return;
411 }
412 if ($system_release =~ /Gentoo/) {
413 give_gentoo_hints;
414 return;
415 }
416
417 #
418 # Fall-back to generic hint code for other distros
419 # That's far from ideal, specially for LaTeX dependencies.
420 #
421 my %map = (
422 "sphinx-build" => "sphinx"
423 );
424 check_missing_tex(1) if ($pdf);
425 check_missing(\%map);
426 print "I don't know distro $system_release.\n";
427 print "So, I can't provide you a hint with the install procedure.\n";
428 print "There are likely missing dependencies.\n";
429}
430
431#
432# Common dependencies
433#
434
435sub check_needs()
436{
437 if ($system_release) {
438 print "Checking if the needed tools for $system_release are available\n";
439 } else {
440 print "Checking if the needed tools are present\n";
441 }
442
443 # Check for needed programs/tools
444 check_sphinx();
445 check_perl_module("Pod::Usage", 0);
446 check_program("make", 0);
447 check_program("gcc", 0);
448 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
449 check_program("xelatex", 1) if ($pdf);
450 check_program("dot", 1);
451 check_program("convert", 1);
452
453 check_distros();
454
455 if ($need_symlink) {
456 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
457 which("sphinx-build-3");
458 }
459 if ($need_sphinx) {
5be33182
MCC
460 my $activate = "$virtenv_dir/bin/activate";
461 if (-e "$ENV{'PWD'}/$activate") {
462 printf "\nNeed to activate virtualenv with:\n";
463 printf "\t. $activate\n";
464 } else {
465 my $virtualenv = findprog("virtualenv-3");
466 $virtualenv = findprog("virtualenv") if (!$virtualenv);
467 $virtualenv = "virtualenv" if (!$virtualenv);
468
469 printf "\t$virtualenv $virtenv_dir\n";
470 printf "\t. $activate\n";
fb947f3f 471 printf "\tpip install -r $requirement_file\n";
5be33182
MCC
472 $need++;
473 }
24071ac1
MCC
474 }
475 printf "\n";
476
477 print "All optional dependenties are met.\n" if (!$optional);
478
479 if ($need == 1) {
480 die "Can't build as $need mandatory dependency is missing";
481 } elsif ($need) {
482 die "Can't build as $need mandatory dependencies are missing";
483 }
484
485 print "Needed package dependencies are met.\n";
486}
487
488#
489# Main
490#
491
492while (@ARGV) {
493 my $arg = shift(@ARGV);
494
495 if ($arg eq "--no-virtualenv") {
496 $virtualenv = 0;
497 } elsif ($arg eq "--no-pdf"){
498 $pdf = 0;
499 } else {
500 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
501 exit -1;
502 }
503}
504
505#
506# Determine the system type. There's no standard unique way that would
507# work with all distros with a minimal package install. So, several
508# methods are used here.
509#
510# By default, it will use lsb_release function. If not available, it will
511# fail back to reading the known different places where the distro name
512# is stored
513#
514
515$system_release = qx(lsb_release -d) if which("lsb_release");
516$system_release =~ s/Description:\s*// if ($system_release);
517$system_release = catcheck("/etc/system-release") if !$system_release;
518$system_release = catcheck("/etc/redhat-release") if !$system_release;
519$system_release = catcheck("/etc/lsb-release") if !$system_release;
520$system_release = catcheck("/etc/gentoo-release") if !$system_release;
521$system_release = catcheck("/etc/issue") if !$system_release;
522$system_release =~ s/\s+$//;
523
524check_needs;