scripts: sphinx-pre-install: only ask to activate valid venvs
[linux-block.git] / scripts / sphinx-pre-install
CommitLineData
24071ac1 1#!/usr/bin/perl
c942fddf 2# SPDX-License-Identifier: GPL-2.0-or-later
24071ac1
MCC
3use strict;
4
2f9c5025 5# Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
24071ac1 6#
24071ac1 7
8c69b77a
MR
8my $prefix = "./";
9$prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
10
11my $conf = $prefix . "Documentation/conf.py";
12my $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
44f42165 13my $virtenv_prefix = "sphinx_";
5be33182 14
24071ac1
MCC
15#
16# Static vars
17#
18
19my %missing;
20my $system_release;
21my $need = 0;
22my $optional = 0;
23my $need_symlink = 0;
24my $need_sphinx = 0;
2f9c5025
MCC
25my $need_venv = 0;
26my $need_virtualenv = 0;
77d09ad9 27my $rec_sphinx_upgrade = 0;
24071ac1 28my $install = "";
44f42165 29my $virtenv_dir = "";
2f9c5025 30my $python_cmd = "";
44f42165 31my $min_version;
24071ac1
MCC
32
33#
34# Command line arguments
35#
36
37my $pdf = 1;
38my $virtualenv = 1;
9b88ad54 39my $version_check = 0;
24071ac1
MCC
40
41#
42# List of required texlive packages on Fedora and OpenSuse
43#
44
45my %texlive = (
24071ac1
MCC
46 'amsfonts.sty' => 'texlive-amsfonts',
47 'amsmath.sty' => 'texlive-amsmath',
48 'amssymb.sty' => 'texlive-amsfonts',
49 'amsthm.sty' => 'texlive-amscls',
50 'anyfontsize.sty' => 'texlive-anyfontsize',
51 'atbegshi.sty' => 'texlive-oberdiek',
52 'bm.sty' => 'texlive-tools',
53 'capt-of.sty' => 'texlive-capt-of',
54 'cmap.sty' => 'texlive-cmap',
55 'ecrm1000.tfm' => 'texlive-ec',
56 'eqparbox.sty' => 'texlive-eqparbox',
57 'eu1enc.def' => 'texlive-euenc',
58 'fancybox.sty' => 'texlive-fancybox',
59 'fancyvrb.sty' => 'texlive-fancyvrb',
60 'float.sty' => 'texlive-float',
61 'fncychap.sty' => 'texlive-fncychap',
62 'footnote.sty' => 'texlive-mdwtools',
63 'framed.sty' => 'texlive-framed',
64 'luatex85.sty' => 'texlive-luatex85',
65 'multirow.sty' => 'texlive-multirow',
66 'needspace.sty' => 'texlive-needspace',
67 'palatino.sty' => 'texlive-psnfss',
68 'parskip.sty' => 'texlive-parskip',
69 'polyglossia.sty' => 'texlive-polyglossia',
70 'tabulary.sty' => 'texlive-tabulary',
71 'threeparttable.sty' => 'texlive-threeparttable',
72 'titlesec.sty' => 'texlive-titlesec',
73 'ucs.sty' => 'texlive-ucs',
74 'upquote.sty' => 'texlive-upquote',
75 'wrapfig.sty' => 'texlive-wrapfig',
76);
77
78#
79# Subroutines that checks if a feature exists
80#
81
82sub check_missing(%)
83{
84 my %map = %{$_[0]};
85
86 foreach my $prog (sort keys %missing) {
87 my $is_optional = $missing{$prog};
88
56e5a633
MCC
89 # At least on some LTS distros like CentOS 7, texlive doesn't
90 # provide all packages we need. When such distros are
91 # detected, we have to disable PDF output.
92 #
93 # So, we need to ignore the packages that distros would
94 # need for LaTeX to work
95 if ($is_optional == 2 && !$pdf) {
96 $optional--;
97 next;
98 }
99
24071ac1
MCC
100 if ($is_optional) {
101 print "Warning: better to also install \"$prog\".\n";
102 } else {
103 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
104 }
105 if (defined($map{$prog})) {
106 $install .= " " . $map{$prog};
107 } else {
108 $install .= " " . $prog;
109 }
110 }
111
112 $install =~ s/^\s//;
113}
114
115sub add_package($$)
116{
117 my $package = shift;
118 my $is_optional = shift;
119
120 $missing{$package} = $is_optional;
121 if ($is_optional) {
122 $optional++;
123 } else {
124 $need++;
125 }
126}
127
128sub check_missing_file($$$)
129{
ff8fdb36 130 my $files = shift;
24071ac1
MCC
131 my $package = shift;
132 my $is_optional = shift;
133
ff8fdb36
JM
134 for (@$files) {
135 return if(-e $_);
136 }
24071ac1
MCC
137
138 add_package($package, $is_optional);
139}
140
141sub findprog($)
142{
143 foreach(split(/:/, $ENV{PATH})) {
144 return "$_/$_[0]" if(-x "$_/$_[0]");
145 }
146}
147
148sub check_program($$)
149{
150 my $prog = shift;
151 my $is_optional = shift;
152
2f9c5025 153 return $prog if findprog($prog);
24071ac1
MCC
154
155 add_package($prog, $is_optional);
156}
157
158sub check_perl_module($$)
159{
160 my $prog = shift;
161 my $is_optional = shift;
162
163 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
164 return if ($err == 0);
165
166 add_package($prog, $is_optional);
167}
168
169sub check_python_module($$)
170{
171 my $prog = shift;
172 my $is_optional = shift;
173
2f9c5025
MCC
174 return if (!$python_cmd);
175
176 my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
24071ac1
MCC
177 return if ($err == 0);
178
179 add_package($prog, $is_optional);
180}
181
182sub check_rpm_missing($$)
183{
184 my @pkgs = @{$_[0]};
185 my $is_optional = $_[1];
186
187 foreach my $prog(@pkgs) {
188 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
189 add_package($prog, $is_optional) if ($err);
190 }
191}
192
193sub check_pacman_missing($$)
194{
195 my @pkgs = @{$_[0]};
196 my $is_optional = $_[1];
197
198 foreach my $prog(@pkgs) {
199 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
200 add_package($prog, $is_optional) if ($err);
201 }
202}
203
204sub check_missing_tex($)
205{
206 my $is_optional = shift;
207 my $kpsewhich = findprog("kpsewhich");
208
209 foreach my $prog(keys %texlive) {
210 my $package = $texlive{$prog};
211 if (!$kpsewhich) {
212 add_package($package, $is_optional);
213 next;
214 }
215 my $file = qx($kpsewhich $prog);
216 add_package($package, $is_optional) if ($file =~ /^\s*$/);
217 }
218}
219
77d09ad9 220sub get_sphinx_fname()
24071ac1 221{
77d09ad9
MCC
222 my $fname = "sphinx-build";
223 return $fname if findprog($fname);
24071ac1 224
77d09ad9
MCC
225 $fname = "sphinx-build-3";
226 if (findprog($fname)) {
24071ac1 227 $need_symlink = 1;
77d09ad9 228 return $fname;
24071ac1
MCC
229 }
230
77d09ad9
MCC
231 return "";
232}
233
a8b380c3
MCC
234sub get_sphinx_version($)
235{
236 my $cmd = shift;
237 my $ver;
238
239 open IN, "$cmd --version 2>&1 |";
240 while (<IN>) {
241 if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
242 $ver=$1;
243 last;
244 }
245 # Sphinx 1.2.x uses a different format
246 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
247 $ver=$1;
248 last;
249 }
250 }
251 close IN;
252 return $ver;
253}
254
77d09ad9
MCC
255sub check_sphinx()
256{
77d09ad9
MCC
257 my $rec_version;
258 my $cur_version;
259
260 open IN, $conf or die "Can't open $conf";
261 while (<IN>) {
262 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
263 $min_version=$1;
264 last;
265 }
266 }
267 close IN;
268
269 die "Can't get needs_sphinx version from $conf" if (!$min_version);
270
271 open IN, $requirement_file or die "Can't open $requirement_file";
272 while (<IN>) {
273 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
274 $rec_version=$1;
275 last;
276 }
277 }
278 close IN;
279
280 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
281
44f42165 282 $virtenv_dir = $virtenv_prefix . $rec_version;
77d09ad9
MCC
283
284 my $sphinx = get_sphinx_fname();
2f9c5025
MCC
285 if ($sphinx eq "") {
286 $need_sphinx = 1;
287 return;
288 }
77d09ad9 289
a8b380c3
MCC
290 $cur_version = get_sphinx_version($sphinx);
291 die ("$sphinx returned an error") if (!$cur_version);
77d09ad9
MCC
292
293 die "$sphinx didn't return its version" if (!$cur_version);
294
77d09ad9 295 if ($cur_version lt $min_version) {
9b88ad54
MCC
296 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
297 $cur_version, $min_version, $rec_version;;
77d09ad9
MCC
298 $need_sphinx = 1;
299 return;
300 }
301
302 if ($cur_version lt $rec_version) {
9b88ad54 303 printf "Sphinx version %s\n", $cur_version;
77d09ad9 304 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
77d09ad9 305 $rec_sphinx_upgrade = 1;
9b88ad54 306 return;
77d09ad9 307 }
9b88ad54
MCC
308
309 # On version check mode, just assume Sphinx has all mandatory deps
310 exit (0) if ($version_check);
24071ac1
MCC
311}
312
313#
314# Ancillary subroutines
315#
316
317sub catcheck($)
318{
319 my $res = "";
320 $res = qx(cat $_[0]) if (-r $_[0]);
321 return $res;
322}
323
324sub which($)
325{
326 my $file = shift;
327 my @path = split ":", $ENV{PATH};
328
329 foreach my $dir(@path) {
330 my $name = $dir.'/'.$file;
331 return $name if (-x $name );
332 }
333 return undef;
334}
335
336#
337# Subroutines that check distro-specific hints
338#
339
340sub give_debian_hints()
341{
342 my %map = (
343 "python-sphinx" => "python3-sphinx",
344 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
2f9c5025 345 "ensurepip" => "python3-venv",
24071ac1 346 "virtualenv" => "virtualenv",
24071ac1
MCC
347 "dot" => "graphviz",
348 "convert" => "imagemagick",
349 "Pod::Usage" => "perl-modules",
350 "xelatex" => "texlive-xetex",
8e7d5d15 351 "rsvg-convert" => "librsvg2-bin",
24071ac1
MCC
352 );
353
354 if ($pdf) {
ff8fdb36 355 check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
56e5a633 356 "fonts-dejavu", 2);
27eed923 357
9692f2fd 358 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
bfc7f428
MCC
359 "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
360 "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
27eed923 361 "fonts-noto-cjk", 2);
24071ac1
MCC
362 }
363
56e5a633 364 check_program("dvipng", 2) if ($pdf);
24071ac1
MCC
365 check_missing(\%map);
366
367 return if (!$need && !$optional);
368 printf("You should run:\n\n\tsudo apt-get install $install\n");
369}
370
371sub give_redhat_hints()
372{
373 my %map = (
374 "python-sphinx" => "python3-sphinx",
375 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
376 "virtualenv" => "python3-virtualenv",
24071ac1
MCC
377 "dot" => "graphviz",
378 "convert" => "ImageMagick",
379 "Pod::Usage" => "perl-Pod-Usage",
380 "xelatex" => "texlive-xetex-bin",
8e7d5d15 381 "rsvg-convert" => "librsvg2-tools",
24071ac1
MCC
382 );
383
5d88953c
MCC
384 my @fedora26_opt_pkgs = (
385 "graphviz-gd", # Fedora 26: needed for PDF support
386 );
387
24071ac1
MCC
388 my @fedora_tex_pkgs = (
389 "texlive-collection-fontsrecommended",
390 "texlive-collection-latex",
27eed923 391 "texlive-xecjk",
24071ac1
MCC
392 "dejavu-sans-fonts",
393 "dejavu-serif-fonts",
394 "dejavu-sans-mono-fonts",
395 );
396
9b756a9d
MCC
397 #
398 # Checks valid for RHEL/CentOS version 7.x.
399 #
56e5a633
MCC
400 my $old = 0;
401 my $rel;
402 $rel = $1 if ($system_release =~ /release\s+(\d+)/);
403
b308467c 404 if (!($system_release =~ /Fedora/)) {
9b756a9d 405 $map{"virtualenv"} = "python-virtualenv";
9b756a9d 406
56e5a633
MCC
407 if ($rel && $rel < 8) {
408 $old = 1;
409 $pdf = 0;
5d88953c 410
56e5a633
MCC
411 printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
412 printf("If you want to build PDF, please read:\n");
413 printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
414 }
415 } else {
416 if ($rel && $rel < 26) {
417 $old = 1;
418 }
419 }
420 if (!$rel) {
421 printf("Couldn't identify release number\n");
422 $old = 1;
423 $pdf = 0;
424 }
5d88953c 425
27eed923 426 if ($pdf) {
ff8fdb36 427 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
27eed923
MCC
428 "google-noto-sans-cjk-ttc-fonts", 2);
429 }
430
56e5a633
MCC
431 check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
432 check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
433 check_missing_tex(2) if ($pdf);
24071ac1
MCC
434 check_missing(\%map);
435
436 return if (!$need && !$optional);
9b756a9d 437
56e5a633 438 if (!$old) {
9b756a9d
MCC
439 # dnf, for Fedora 18+
440 printf("You should run:\n\n\tsudo dnf install -y $install\n");
441 } else {
442 # yum, for RHEL (and clones) or Fedora version < 18
443 printf("You should run:\n\n\tsudo yum install -y $install\n");
444 }
24071ac1
MCC
445}
446
447sub give_opensuse_hints()
448{
449 my %map = (
450 "python-sphinx" => "python3-sphinx",
451 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
452 "virtualenv" => "python3-virtualenv",
24071ac1
MCC
453 "dot" => "graphviz",
454 "convert" => "ImageMagick",
455 "Pod::Usage" => "perl-Pod-Usage",
456 "xelatex" => "texlive-xetex-bin",
457 );
458
b3df6223
MCC
459 # On Tumbleweed, this package is also named rsvg-convert
460 $map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
461
24071ac1
MCC
462 my @suse_tex_pkgs = (
463 "texlive-babel-english",
464 "texlive-caption",
465 "texlive-colortbl",
466 "texlive-courier",
467 "texlive-dvips",
468 "texlive-helvetic",
469 "texlive-makeindex",
470 "texlive-metafont",
471 "texlive-metapost",
472 "texlive-palatino",
473 "texlive-preview",
474 "texlive-times",
475 "texlive-zapfchan",
476 "texlive-zapfding",
477 );
478
353290a9
MCC
479 $map{"latexmk"} = "texlive-latexmk-bin";
480
27eed923
MCC
481 # FIXME: add support for installing CJK fonts
482 #
483 # I tried hard, but was unable to find a way to install
484 # "Noto Sans CJK SC" on openSUSE
485
56e5a633
MCC
486 check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
487 check_missing_tex(2) if ($pdf);
24071ac1
MCC
488 check_missing(\%map);
489
490 return if (!$need && !$optional);
491 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
492}
493
800d408a
MCC
494sub give_mageia_hints()
495{
496 my %map = (
497 "python-sphinx" => "python3-sphinx",
498 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
499 "virtualenv" => "python3-virtualenv",
800d408a
MCC
500 "dot" => "graphviz",
501 "convert" => "ImageMagick",
502 "Pod::Usage" => "perl-Pod-Usage",
503 "xelatex" => "texlive",
d6ebf189 504 "rsvg-convert" => "librsvg2",
800d408a
MCC
505 );
506
507 my @tex_pkgs = (
508 "texlive-fontsextra",
509 );
510
353290a9
MCC
511 $map{"latexmk"} = "texlive-collection-basic";
512
d6ebf189
MCC
513 my $packager_cmd;
514 my $noto_sans;
515 if ($system_release =~ /OpenMandriva/) {
516 $packager_cmd = "dnf install";
517 $noto_sans = "noto-sans-cjk-fonts";
518 @tex_pkgs = ( "texlive-collection-fontsextra" );
519 } else {
520 $packager_cmd = "urpmi";
521 $noto_sans = "google-noto-sans-cjk-ttc-fonts";
522 }
523
524
27eed923 525 if ($pdf) {
d6ebf189
MCC
526 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
527 "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
528 $noto_sans, 2);
27eed923
MCC
529 }
530
56e5a633 531 check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
800d408a
MCC
532 check_missing(\%map);
533
534 return if (!$need && !$optional);
d6ebf189 535 printf("You should run:\n\n\tsudo $packager_cmd $install\n");
800d408a
MCC
536}
537
24071ac1
MCC
538sub give_arch_linux_hints()
539{
540 my %map = (
541 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
542 "virtualenv" => "python-virtualenv",
24071ac1
MCC
543 "dot" => "graphviz",
544 "convert" => "imagemagick",
545 "xelatex" => "texlive-bin",
0d0da9aa 546 "latexmk" => "texlive-core",
8e7d5d15 547 "rsvg-convert" => "extra/librsvg",
24071ac1
MCC
548 );
549
550 my @archlinux_tex_pkgs = (
551 "texlive-core",
552 "texlive-latexextra",
553 "ttf-dejavu",
554 );
56e5a633
MCC
555 check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
556
27eed923 557 if ($pdf) {
ff8fdb36 558 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
27eed923
MCC
559 "noto-fonts-cjk", 2);
560 }
561
24071ac1
MCC
562 check_missing(\%map);
563
564 return if (!$need && !$optional);
565 printf("You should run:\n\n\tsudo pacman -S $install\n");
566}
567
568sub give_gentoo_hints()
569{
570 my %map = (
571 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
572 "virtualenv" => "dev-python/virtualenv",
24071ac1
MCC
573 "dot" => "media-gfx/graphviz",
574 "convert" => "media-gfx/imagemagick",
575 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
8e7d5d15 576 "rsvg-convert" => "gnome-base/librsvg",
24071ac1
MCC
577 );
578
ff8fdb36 579 check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
56e5a633 580 "media-fonts/dejavu", 2) if ($pdf);
24071ac1 581
27eed923 582 if ($pdf) {
e45a6317
MCC
583 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
584 "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
27eed923
MCC
585 "media-fonts/noto-cjk", 2);
586 }
587
24071ac1
MCC
588 check_missing(\%map);
589
590 return if (!$need && !$optional);
bba1e4cb
MCC
591
592 printf("You should run:\n\n");
4ea96d57
MCC
593
594 my $imagemagick = "media-gfx/imagemagick svg png";
595 my $cairo = "media-gfx/graphviz cairo pdf";
596 my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
597 my $portage_cairo = "/etc/portage/package.use/graphviz";
598
e45a6317 599 if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
4ea96d57
MCC
600 printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
601 }
e45a6317 602 if (qx(grep graphviz $portage_cairo 2>/dev/null) eq "") {
4ea96d57
MCC
603 printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
604 }
605
bba1e4cb
MCC
606 printf("\tsudo emerge --ask $install\n");
607
24071ac1
MCC
608}
609
610sub check_distros()
611{
612 # Distro-specific hints
613 if ($system_release =~ /Red Hat Enterprise Linux/) {
614 give_redhat_hints;
615 return;
616 }
9b756a9d
MCC
617 if ($system_release =~ /CentOS/) {
618 give_redhat_hints;
619 return;
620 }
621 if ($system_release =~ /Scientific Linux/) {
622 give_redhat_hints;
623 return;
624 }
625 if ($system_release =~ /Oracle Linux Server/) {
626 give_redhat_hints;
627 return;
628 }
24071ac1
MCC
629 if ($system_release =~ /Fedora/) {
630 give_redhat_hints;
631 return;
632 }
633 if ($system_release =~ /Ubuntu/) {
634 give_debian_hints;
635 return;
636 }
637 if ($system_release =~ /Debian/) {
638 give_debian_hints;
639 return;
640 }
641 if ($system_release =~ /openSUSE/) {
642 give_opensuse_hints;
643 return;
644 }
800d408a
MCC
645 if ($system_release =~ /Mageia/) {
646 give_mageia_hints;
647 return;
648 }
d6ebf189
MCC
649 if ($system_release =~ /OpenMandriva/) {
650 give_mageia_hints;
651 return;
652 }
24071ac1
MCC
653 if ($system_release =~ /Arch Linux/) {
654 give_arch_linux_hints;
655 return;
656 }
657 if ($system_release =~ /Gentoo/) {
658 give_gentoo_hints;
659 return;
660 }
661
662 #
663 # Fall-back to generic hint code for other distros
664 # That's far from ideal, specially for LaTeX dependencies.
665 #
666 my %map = (
667 "sphinx-build" => "sphinx"
668 );
56e5a633 669 check_missing_tex(2) if ($pdf);
24071ac1
MCC
670 check_missing(\%map);
671 print "I don't know distro $system_release.\n";
672 print "So, I can't provide you a hint with the install procedure.\n";
673 print "There are likely missing dependencies.\n";
674}
675
676#
677# Common dependencies
678#
679
2730ce01
SK
680sub deactivate_help()
681{
2f9c5025 682 printf "\nIf you want to exit the virtualenv, you can use:\n";
2730ce01
SK
683 printf "\tdeactivate\n";
684}
685
24071ac1
MCC
686sub check_needs()
687{
2f9c5025 688 # Check if Sphinx is already accessible from current environment
9b88ad54
MCC
689 check_sphinx();
690
24071ac1 691 if ($system_release) {
9b88ad54 692 print "Detected OS: $system_release.\n\n";
24071ac1 693 } else {
9b88ad54 694 print "Unknown OS\n\n";
9b756a9d
MCC
695 }
696
9b88ad54
MCC
697 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
698
2f9c5025
MCC
699 # Check python command line, trying first python3
700 $python_cmd = findprog("python3");
701 $python_cmd = check_program("python", 0) if (!$python_cmd);
702
703 # Check the type of virtual env, depending on Python version
704 if ($python_cmd) {
705 if ($virtualenv) {
706 my $tmp = qx($python_cmd --version 2>&1);
707 if ($tmp =~ m/(\d+\.)(\d+\.)/) {
708 if ($1 >= 3 && $2 >= 3) {
709 $need_venv = 1; # python 3.3 or upper
710 } else {
711 $need_virtualenv = 1;
712 }
713 if ($1 < 3) {
714 # Complain if it finds python2 (or worse)
715 printf "Warning: python$1 support is deprecated. Use it with caution!\n";
716 }
717 } else {
718 die "Warning: couldn't identify $python_cmd version!";
719 }
720 } else {
721 add_package("python-sphinx", 0);
722 }
723 }
724
725 # Set virtualenv command line, if python < 3.3
726 my $virtualenv_cmd;
727 if ($need_virtualenv) {
728 $virtualenv_cmd = findprog("virtualenv-3");
729 $virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
730 if (!$virtualenv_cmd) {
731 check_program("virtualenv", 0);
732 $virtualenv_cmd = "virtualenv";
733 }
734 }
735
24071ac1 736 # Check for needed programs/tools
24071ac1
MCC
737 check_perl_module("Pod::Usage", 0);
738 check_program("make", 0);
739 check_program("gcc", 0);
740 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
24071ac1
MCC
741 check_program("dot", 1);
742 check_program("convert", 1);
56e5a633
MCC
743
744 # Extra PDF files - should use 2 for is_optional
745 check_program("xelatex", 2) if ($pdf);
746 check_program("rsvg-convert", 2) if ($pdf);
747 check_program("latexmk", 2) if ($pdf);
24071ac1 748
2f9c5025
MCC
749 if ($need_sphinx || $rec_sphinx_upgrade) {
750 check_python_module("ensurepip", 0) if ($need_venv);
751 }
752
753 # Do distro-specific checks and output distro-install commands
24071ac1
MCC
754 check_distros();
755
2f9c5025
MCC
756 if (!$python_cmd) {
757 if ($need == 1) {
758 die "Can't build as $need mandatory dependency is missing";
759 } elsif ($need) {
760 die "Can't build as $need mandatory dependencies are missing";
761 }
762 }
763
764 # Check if sphinx-build is called sphinx-build-3
24071ac1
MCC
765 if ($need_symlink) {
766 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
767 which("sphinx-build-3");
768 }
2f9c5025
MCC
769
770 # NOTE: if the system has a too old Sphinx version installed,
771 # it will recommend installing a newer version using virtualenv
772
77d09ad9 773 if ($need_sphinx || $rec_sphinx_upgrade) {
44f42165 774 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
9b88ad54 775 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
44f42165 776
9b88ad54 777 @activates = sort {$b cmp $a} @activates;
a8b380c3
MCC
778 my ($activate, $ver);
779 foreach my $f (@activates) {
780 $activate = $f;
781 next if ($activate lt $min_activate);
782
783 my $sphinx_cmd = $activate;
784 $sphinx_cmd =~ s/activate/sphinx-build/;
785 next if (! -f $sphinx_cmd);
44f42165 786
a8b380c3
MCC
787 $ver = get_sphinx_version($sphinx_cmd);
788 last if ($ver ge $min_version);
789 }
790 if ($need_sphinx && ($activate ne "")) {
791 printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
792 printf "\t. $activate\n";
2730ce01 793 deactivate_help();
9b88ad54 794 exit (1);
5be33182 795 } else {
44f42165 796 my $rec_activate = "$virtenv_dir/bin/activate";
c428cd52 797
2f9c5025
MCC
798 if ($need_venv) {
799 printf "\t$python_cmd -m venv $virtenv_dir\n";
800 } else {
801 printf "\t$virtualenv_cmd $virtenv_dir\n";
802 }
44f42165 803 printf "\t. $rec_activate\n";
fb947f3f 804 printf "\tpip install -r $requirement_file\n";
2730ce01 805 deactivate_help();
77d09ad9
MCC
806
807 $need++ if (!$rec_sphinx_upgrade);
5be33182 808 }
24071ac1
MCC
809 }
810 printf "\n";
811
54002b56 812 print "All optional dependencies are met.\n" if (!$optional);
24071ac1
MCC
813
814 if ($need == 1) {
815 die "Can't build as $need mandatory dependency is missing";
816 } elsif ($need) {
817 die "Can't build as $need mandatory dependencies are missing";
818 }
819
820 print "Needed package dependencies are met.\n";
821}
822
823#
824# Main
825#
826
827while (@ARGV) {
828 my $arg = shift(@ARGV);
829
830 if ($arg eq "--no-virtualenv") {
831 $virtualenv = 0;
832 } elsif ($arg eq "--no-pdf"){
833 $pdf = 0;
9b88ad54
MCC
834 } elsif ($arg eq "--version-check"){
835 $version_check = 1;
24071ac1 836 } else {
9b88ad54
MCC
837 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
838 print "Where:\n";
839 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
840 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
841 print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
24071ac1
MCC
842 exit -1;
843 }
844}
845
846#
847# Determine the system type. There's no standard unique way that would
848# work with all distros with a minimal package install. So, several
849# methods are used here.
850#
851# By default, it will use lsb_release function. If not available, it will
852# fail back to reading the known different places where the distro name
853# is stored
854#
855
856$system_release = qx(lsb_release -d) if which("lsb_release");
857$system_release =~ s/Description:\s*// if ($system_release);
858$system_release = catcheck("/etc/system-release") if !$system_release;
859$system_release = catcheck("/etc/redhat-release") if !$system_release;
860$system_release = catcheck("/etc/lsb-release") if !$system_release;
861$system_release = catcheck("/etc/gentoo-release") if !$system_release;
d14d0c1a
MCC
862
863# This seems more common than LSB these days
864if (!$system_release) {
865 my %os_var;
866 if (open IN, "cat /etc/os-release|") {
867 while (<IN>) {
868 if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
869 $os_var{$1}=$2;
870 }
871 }
872 $system_release = $os_var{"NAME"};
873 if (defined($os_var{"VERSION_ID"})) {
874 $system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
875 } else {
876 $system_release .= " " . $os_var{"VERSION"};
877 }
878 }
879}
24071ac1
MCC
880$system_release = catcheck("/etc/issue") if !$system_release;
881$system_release =~ s/\s+$//;
882
883check_needs;