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