sphinx.rst: document scripts/sphinx-pre-install script
[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",
8e7d5d15 261 "rsvg-convert" => "librsvg2-bin",
24071ac1
MCC
262 );
263
264 if ($pdf) {
265 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
266 "fonts-dejavu", 1);
267 }
268
269 check_program("dvipng", 1) if ($pdf);
270 check_missing(\%map);
271
272 return if (!$need && !$optional);
273 printf("You should run:\n\n\tsudo apt-get install $install\n");
274}
275
276sub give_redhat_hints()
277{
278 my %map = (
279 "python-sphinx" => "python3-sphinx",
280 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
281 "virtualenv" => "python3-virtualenv",
282 "pip" => "python3-pip",
283 "dot" => "graphviz",
284 "convert" => "ImageMagick",
285 "Pod::Usage" => "perl-Pod-Usage",
286 "xelatex" => "texlive-xetex-bin",
8e7d5d15 287 "rsvg-convert" => "librsvg2-tools",
24071ac1
MCC
288 );
289
5d88953c
MCC
290 my @fedora26_opt_pkgs = (
291 "graphviz-gd", # Fedora 26: needed for PDF support
292 );
293
24071ac1
MCC
294 my @fedora_tex_pkgs = (
295 "texlive-collection-fontsrecommended",
296 "texlive-collection-latex",
297 "dejavu-sans-fonts",
298 "dejavu-serif-fonts",
299 "dejavu-sans-mono-fonts",
300 );
301
5d88953c
MCC
302 my $release;
303
304 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
305
306 check_rpm_missing(\@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
24071ac1
MCC
307 check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
308 check_missing_tex(1) if ($pdf);
309 check_missing(\%map);
310
311 return if (!$need && !$optional);
312 printf("You should run:\n\n\tsudo dnf install -y $install\n");
313}
314
315sub give_opensuse_hints()
316{
317 my %map = (
318 "python-sphinx" => "python3-sphinx",
319 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
320 "virtualenv" => "python3-virtualenv",
321 "pip" => "python3-pip",
322 "dot" => "graphviz",
323 "convert" => "ImageMagick",
324 "Pod::Usage" => "perl-Pod-Usage",
325 "xelatex" => "texlive-xetex-bin",
8e7d5d15 326 "rsvg-convert" => "rsvg-view",
24071ac1
MCC
327 );
328
329 my @suse_tex_pkgs = (
330 "texlive-babel-english",
331 "texlive-caption",
332 "texlive-colortbl",
333 "texlive-courier",
334 "texlive-dvips",
335 "texlive-helvetic",
336 "texlive-makeindex",
337 "texlive-metafont",
338 "texlive-metapost",
339 "texlive-palatino",
340 "texlive-preview",
341 "texlive-times",
342 "texlive-zapfchan",
343 "texlive-zapfding",
344 );
345
346 check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
347 check_missing_tex(1) if ($pdf);
348 check_missing(\%map);
349
350 return if (!$need && !$optional);
351 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
352}
353
354sub give_arch_linux_hints()
355{
356 my %map = (
357 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
358 "virtualenv" => "python-virtualenv",
359 "pip" => "python-pip",
360 "dot" => "graphviz",
361 "convert" => "imagemagick",
362 "xelatex" => "texlive-bin",
8e7d5d15 363 "rsvg-convert" => "extra/librsvg",
24071ac1
MCC
364 );
365
366 my @archlinux_tex_pkgs = (
367 "texlive-core",
368 "texlive-latexextra",
369 "ttf-dejavu",
370 );
371 check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
372 check_missing(\%map);
373
374 return if (!$need && !$optional);
375 printf("You should run:\n\n\tsudo pacman -S $install\n");
376}
377
378sub give_gentoo_hints()
379{
380 my %map = (
381 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
382 "virtualenv" => "dev-python/virtualenv",
383 "pip" => "dev-python/pip",
384 "dot" => "media-gfx/graphviz",
385 "convert" => "media-gfx/imagemagick",
386 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
8e7d5d15 387 "rsvg-convert" => "gnome-base/librsvg",
24071ac1
MCC
388 );
389
390 check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
391 "media-fonts/dejavu", 1) if ($pdf);
392
393 check_missing(\%map);
394
395 return if (!$need && !$optional);
bba1e4cb
MCC
396
397 printf("You should run:\n\n");
398 printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
399 printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
400 printf("\tsudo emerge --ask $install\n");
401
24071ac1
MCC
402}
403
404sub check_distros()
405{
406 # Distro-specific hints
407 if ($system_release =~ /Red Hat Enterprise Linux/) {
408 give_redhat_hints;
409 return;
410 }
411 if ($system_release =~ /Fedora/) {
412 give_redhat_hints;
413 return;
414 }
415 if ($system_release =~ /Ubuntu/) {
416 give_debian_hints;
417 return;
418 }
419 if ($system_release =~ /Debian/) {
420 give_debian_hints;
421 return;
422 }
423 if ($system_release =~ /openSUSE/) {
424 give_opensuse_hints;
425 return;
426 }
427 if ($system_release =~ /Arch Linux/) {
428 give_arch_linux_hints;
429 return;
430 }
431 if ($system_release =~ /Gentoo/) {
432 give_gentoo_hints;
433 return;
434 }
435
436 #
437 # Fall-back to generic hint code for other distros
438 # That's far from ideal, specially for LaTeX dependencies.
439 #
440 my %map = (
441 "sphinx-build" => "sphinx"
442 );
443 check_missing_tex(1) if ($pdf);
444 check_missing(\%map);
445 print "I don't know distro $system_release.\n";
446 print "So, I can't provide you a hint with the install procedure.\n";
447 print "There are likely missing dependencies.\n";
448}
449
450#
451# Common dependencies
452#
453
454sub check_needs()
455{
456 if ($system_release) {
457 print "Checking if the needed tools for $system_release are available\n";
458 } else {
459 print "Checking if the needed tools are present\n";
460 }
461
462 # Check for needed programs/tools
463 check_sphinx();
464 check_perl_module("Pod::Usage", 0);
465 check_program("make", 0);
466 check_program("gcc", 0);
467 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
468 check_program("xelatex", 1) if ($pdf);
469 check_program("dot", 1);
470 check_program("convert", 1);
8e7d5d15 471 check_program("rsvg-convert", 1) if ($pdf);
24071ac1
MCC
472
473 check_distros();
474
475 if ($need_symlink) {
476 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
477 which("sphinx-build-3");
478 }
479 if ($need_sphinx) {
5be33182
MCC
480 my $activate = "$virtenv_dir/bin/activate";
481 if (-e "$ENV{'PWD'}/$activate") {
482 printf "\nNeed to activate virtualenv with:\n";
483 printf "\t. $activate\n";
484 } else {
485 my $virtualenv = findprog("virtualenv-3");
486 $virtualenv = findprog("virtualenv") if (!$virtualenv);
487 $virtualenv = "virtualenv" if (!$virtualenv);
488
489 printf "\t$virtualenv $virtenv_dir\n";
490 printf "\t. $activate\n";
fb947f3f 491 printf "\tpip install -r $requirement_file\n";
5be33182
MCC
492 $need++;
493 }
24071ac1
MCC
494 }
495 printf "\n";
496
497 print "All optional dependenties are met.\n" if (!$optional);
498
499 if ($need == 1) {
500 die "Can't build as $need mandatory dependency is missing";
501 } elsif ($need) {
502 die "Can't build as $need mandatory dependencies are missing";
503 }
504
505 print "Needed package dependencies are met.\n";
506}
507
508#
509# Main
510#
511
512while (@ARGV) {
513 my $arg = shift(@ARGV);
514
515 if ($arg eq "--no-virtualenv") {
516 $virtualenv = 0;
517 } elsif ($arg eq "--no-pdf"){
518 $pdf = 0;
519 } else {
520 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
521 exit -1;
522 }
523}
524
525#
526# Determine the system type. There's no standard unique way that would
527# work with all distros with a minimal package install. So, several
528# methods are used here.
529#
530# By default, it will use lsb_release function. If not available, it will
531# fail back to reading the known different places where the distro name
532# is stored
533#
534
535$system_release = qx(lsb_release -d) if which("lsb_release");
536$system_release =~ s/Description:\s*// if ($system_release);
537$system_release = catcheck("/etc/system-release") if !$system_release;
538$system_release = catcheck("/etc/redhat-release") if !$system_release;
539$system_release = catcheck("/etc/lsb-release") if !$system_release;
540$system_release = catcheck("/etc/gentoo-release") if !$system_release;
541$system_release = catcheck("/etc/issue") if !$system_release;
542$system_release =~ s/\s+$//;
543
544check_needs;