Blame view

scripts/sphinx-pre-install 24.4 KB
c25ce589d   Finn Behrens   tweewide: Fix mos...
1
  #!/usr/bin/env perl
c942fddf8   Thomas Gleixner   treewide: Replace...
2
  # SPDX-License-Identifier: GPL-2.0-or-later
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
3
  use strict;
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
4
  # Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
5
  #
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
6

8c69b77a0   Mike Rapoport   scripts/sphinx-pr...
7
8
9
10
11
  my $prefix = "./";
  $prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
  
  my $conf = $prefix . "Documentation/conf.py";
  my $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
44f421651   Mauro Carvalho Chehab   scripts/sphinx-pr...
12
  my $virtenv_prefix = "sphinx_";
5be33182d   Mauro Carvalho Chehab   sphinx-pre-instal...
13

24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
14
15
16
17
18
19
20
21
22
23
  #
  # Static vars
  #
  
  my %missing;
  my $system_release;
  my $need = 0;
  my $optional = 0;
  my $need_symlink = 0;
  my $need_sphinx = 0;
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
24
  my $need_pip = 0;
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
25
  my $need_virtualenv = 0;
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
26
  my $rec_sphinx_upgrade = 0;
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
27
  my $install = "";
44f421651   Mauro Carvalho Chehab   scripts/sphinx-pr...
28
  my $virtenv_dir = "";
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
29
  my $python_cmd = "";
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
30
  my $activate_cmd;
44f421651   Mauro Carvalho Chehab   scripts/sphinx-pr...
31
  my $min_version;
2834a7412   Mauro Carvalho Chehab   scripts: sphinx-p...
32
  my $cur_version;
1ef70ced5   Mauro Carvalho Chehab   scripts: sphinx-p...
33
34
  my $rec_version = "1.7.9";	# PDF won't build here
  my $min_pdf_version = "2.4.4";	# Min version where pdf builds
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
35
  my $latest_avail_ver;
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
36

24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
37
38
39
40
41
42
  #
  # Command line arguments
  #
  
  my $pdf = 1;
  my $virtualenv = 1;
9b88ad546   Mauro Carvalho Chehab   scripts/sphinx-pr...
43
  my $version_check = 0;
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
44
45
46
47
48
49
  
  #
  # List of required texlive packages on Fedora and OpenSuse
  #
  
  my %texlive = (
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  	'amsfonts.sty'       => 'texlive-amsfonts',
  	'amsmath.sty'        => 'texlive-amsmath',
  	'amssymb.sty'        => 'texlive-amsfonts',
  	'amsthm.sty'         => 'texlive-amscls',
  	'anyfontsize.sty'    => 'texlive-anyfontsize',
  	'atbegshi.sty'       => 'texlive-oberdiek',
  	'bm.sty'             => 'texlive-tools',
  	'capt-of.sty'        => 'texlive-capt-of',
  	'cmap.sty'           => 'texlive-cmap',
  	'ecrm1000.tfm'       => 'texlive-ec',
  	'eqparbox.sty'       => 'texlive-eqparbox',
  	'eu1enc.def'         => 'texlive-euenc',
  	'fancybox.sty'       => 'texlive-fancybox',
  	'fancyvrb.sty'       => 'texlive-fancyvrb',
  	'float.sty'          => 'texlive-float',
  	'fncychap.sty'       => 'texlive-fncychap',
  	'footnote.sty'       => 'texlive-mdwtools',
  	'framed.sty'         => 'texlive-framed',
  	'luatex85.sty'       => 'texlive-luatex85',
  	'multirow.sty'       => 'texlive-multirow',
  	'needspace.sty'      => 'texlive-needspace',
  	'palatino.sty'       => 'texlive-psnfss',
  	'parskip.sty'        => 'texlive-parskip',
  	'polyglossia.sty'    => 'texlive-polyglossia',
  	'tabulary.sty'       => 'texlive-tabulary',
  	'threeparttable.sty' => 'texlive-threeparttable',
  	'titlesec.sty'       => 'texlive-titlesec',
  	'ucs.sty'            => 'texlive-ucs',
  	'upquote.sty'        => 'texlive-upquote',
  	'wrapfig.sty'        => 'texlive-wrapfig',
21c9a4d2c   Mauro Carvalho Chehab   scripts: sphinx-p...
80
  	'ctexhook.sty'       => 'texlive-ctex',
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
81
82
83
84
85
86
87
88
89
90
91
92
  );
  
  #
  # Subroutines that checks if a feature exists
  #
  
  sub check_missing(%)
  {
  	my %map = %{$_[0]};
  
  	foreach my $prog (sort keys %missing) {
  		my $is_optional = $missing{$prog};
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
93
94
95
96
97
98
99
100
101
102
  		# At least on some LTS distros like CentOS 7, texlive doesn't
  		# provide all packages we need. When such distros are
  		# detected, we have to disable PDF output.
  		#
  		# So, we need to ignore the packages that distros would
  		# need for LaTeX to work
  		if ($is_optional == 2 && !$pdf) {
  			$optional--;
  			next;
  		}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  		if ($is_optional) {
  			print "Warning: better to also install \"$prog\".
  ";
  		} else {
  			print "ERROR: please install \"$prog\", otherwise, build won't work.
  ";
  		}
  		if (defined($map{$prog})) {
  			$install .= " " . $map{$prog};
  		} else {
  			$install .= " " . $prog;
  		}
  	}
  
  	$install =~ s/^\s//;
  }
  
  sub add_package($$)
  {
  	my $package = shift;
  	my $is_optional = shift;
  
  	$missing{$package} = $is_optional;
  	if ($is_optional) {
  		$optional++;
  	} else {
  		$need++;
  	}
  }
  
  sub check_missing_file($$$)
  {
ff8fdb36a   Jeremy MAURO   scripts/sphinx-pr...
135
  	my $files = shift;
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
136
137
  	my $package = shift;
  	my $is_optional = shift;
ff8fdb36a   Jeremy MAURO   scripts/sphinx-pr...
138
139
140
  	for (@$files) {
  		return if(-e $_);
  	}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
141
142
143
144
145
146
147
148
149
150
  
  	add_package($package, $is_optional);
  }
  
  sub findprog($)
  {
  	foreach(split(/:/, $ENV{PATH})) {
  		return "$_/$_[0]" if(-x "$_/$_[0]");
  	}
  }
412b09dda   Mauro Carvalho Chehab   scripts: sphinx-p...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  sub find_python_no_venv()
  {
  	my $prog = shift;
  
  	my $cur_dir = qx(pwd);
  	$cur_dir =~ s/\s+$//;
  
  	foreach my $dir (split(/:/, $ENV{PATH})) {
  		next if ($dir =~ m,($cur_dir)/sphinx,);
  		return "$dir/python3" if(-x "$dir/python3");
  	}
  	foreach my $dir (split(/:/, $ENV{PATH})) {
  		next if ($dir =~ m,($cur_dir)/sphinx,);
  		return "$dir/python" if(-x "$dir/python");
  	}
  	return "python";
  }
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
168
169
170
171
  sub check_program($$)
  {
  	my $prog = shift;
  	my $is_optional = shift;
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
172
  	return $prog if findprog($prog);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  
  	add_package($prog, $is_optional);
  }
  
  sub check_perl_module($$)
  {
  	my $prog = shift;
  	my $is_optional = shift;
  
  	my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
  	return if ($err == 0);
  
  	add_package($prog, $is_optional);
  }
  
  sub check_python_module($$)
  {
  	my $prog = shift;
  	my $is_optional = shift;
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
192
193
194
  	return if (!$python_cmd);
  
  	my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  	return if ($err == 0);
  
  	add_package($prog, $is_optional);
  }
  
  sub check_rpm_missing($$)
  {
  	my @pkgs = @{$_[0]};
  	my $is_optional = $_[1];
  
  	foreach my $prog(@pkgs) {
  		my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
  		add_package($prog, $is_optional) if ($err);
  	}
  }
  
  sub check_pacman_missing($$)
  {
  	my @pkgs = @{$_[0]};
  	my $is_optional = $_[1];
  
  	foreach my $prog(@pkgs) {
  		my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
  		add_package($prog, $is_optional) if ($err);
  	}
  }
  
  sub check_missing_tex($)
  {
  	my $is_optional = shift;
  	my $kpsewhich = findprog("kpsewhich");
  
  	foreach my $prog(keys %texlive) {
  		my $package = $texlive{$prog};
  		if (!$kpsewhich) {
  			add_package($package, $is_optional);
  			next;
  		}
  		my $file = qx($kpsewhich $prog);
  		add_package($package, $is_optional) if ($file =~ /^\s*$/);
  	}
  }
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
237
  sub get_sphinx_fname()
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
238
  {
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
239
240
  	my $fname = "sphinx-build";
  	return $fname if findprog($fname);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
241

77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
242
243
  	$fname = "sphinx-build-3";
  	if (findprog($fname)) {
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
244
  		$need_symlink = 1;
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
245
  		return $fname;
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
246
  	}
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
247
248
  	return "";
  }
a8b380c37   Mauro Carvalho Chehab   scripts: sphinx-p...
249
250
251
252
253
254
255
  sub get_sphinx_version($)
  {
  	my $cmd = shift;
  	my $ver;
  
  	open IN, "$cmd --version 2>&1 |";
  	while (<IN>) {
e9dfeed25   Jonathan Corbet   docs: sphinx-pre-...
256
  		if (m/^\s*sphinx-build\s+([\d\.]+)((\+\/[\da-f]+)|(b\d+))?$/) {
a8b380c37   Mauro Carvalho Chehab   scripts: sphinx-p...
257
258
259
260
261
262
263
264
265
266
267
268
  			$ver=$1;
  			last;
  		}
  		# Sphinx 1.2.x uses a different format
  		if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
  			$ver=$1;
  			last;
  		}
  	}
  	close IN;
  	return $ver;
  }
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
269
270
  sub check_sphinx()
  {
1ef70ced5   Mauro Carvalho Chehab   scripts: sphinx-p...
271
  	my $default_version;
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  
  	open IN, $conf or die "Can't open $conf";
  	while (<IN>) {
  		if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
  			$min_version=$1;
  			last;
  		}
  	}
  	close IN;
  
  	die "Can't get needs_sphinx version from $conf" if (!$min_version);
  
  	open IN, $requirement_file or die "Can't open $requirement_file";
  	while (<IN>) {
  		if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
1ef70ced5   Mauro Carvalho Chehab   scripts: sphinx-p...
287
  			$default_version=$1;
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
288
289
290
291
  			last;
  		}
  	}
  	close IN;
1ef70ced5   Mauro Carvalho Chehab   scripts: sphinx-p...
292
  	die "Can't get default sphinx version from $requirement_file" if (!$default_version);
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
293

1ef70ced5   Mauro Carvalho Chehab   scripts: sphinx-p...
294
  	$virtenv_dir = $virtenv_prefix . $default_version;
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
295
296
  
  	my $sphinx = get_sphinx_fname();
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
297
298
299
300
  	if ($sphinx eq "") {
  		$need_sphinx = 1;
  		return;
  	}
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
301

a8b380c37   Mauro Carvalho Chehab   scripts: sphinx-p...
302
303
  	$cur_version = get_sphinx_version($sphinx);
  	die ("$sphinx returned an error") if (!$cur_version);
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
304
305
  
  	die "$sphinx didn't return its version" if (!$cur_version);
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
306
  	if ($cur_version lt $min_version) {
9b88ad546   Mauro Carvalho Chehab   scripts/sphinx-pr...
307
308
  		printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)
  ",
1ef70ced5   Mauro Carvalho Chehab   scripts: sphinx-p...
309
  		       $cur_version, $min_version, $default_version;
77d09ad9d   Mauro Carvalho Chehab   scripts/sphinx-pr...
310
311
312
  		$need_sphinx = 1;
  		return;
  	}
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
313
  	return if ($cur_version lt $rec_version);
9b88ad546   Mauro Carvalho Chehab   scripts/sphinx-pr...
314
315
316
  
  	# On version check mode, just assume Sphinx has all mandatory deps
  	exit (0) if ($version_check);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
  }
  
  #
  # Ancillary subroutines
  #
  
  sub catcheck($)
  {
    my $res = "";
    $res = qx(cat $_[0]) if (-r $_[0]);
    return $res;
  }
  
  sub which($)
  {
  	my $file = shift;
  	my @path = split ":", $ENV{PATH};
  
  	foreach my $dir(@path) {
  		my $name = $dir.'/'.$file;
  		return $name if (-x $name );
  	}
  	return undef;
  }
  
  #
  # Subroutines that check distro-specific hints
  #
  
  sub give_debian_hints()
  {
  	my %map = (
  		"python-sphinx"		=> "python3-sphinx",
  		"sphinx_rtd_theme"	=> "python3-sphinx-rtd-theme",
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
351
  		"ensurepip"		=> "python3-venv",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
352
  		"virtualenv"		=> "virtualenv",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
353
354
355
356
  		"dot"			=> "graphviz",
  		"convert"		=> "imagemagick",
  		"Pod::Usage"		=> "perl-modules",
  		"xelatex"		=> "texlive-xetex",
8e7d5d15e   Mauro Carvalho Chehab   sphinx-pre-instal...
357
  		"rsvg-convert"		=> "librsvg2-bin",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
358
359
360
  	);
  
  	if ($pdf) {
4ea9b8ed6   Mauro Carvalho Chehab   scripts: sphinx-p...
361
362
  		check_missing_file(["/usr/share/texlive/texmf-dist/tex/latex/ctex/ctexhook.sty"],
  				   "texlive-lang-chinese", 2);
ff8fdb36a   Jeremy MAURO   scripts/sphinx-pr...
363
  		check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
364
  				   "fonts-dejavu", 2);
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
365

9692f2fdb   Jeremy MAURO   scripts/sphinx-pr...
366
  		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
bfc7f4281   Mauro Carvalho Chehab   scripts: sphinx-p...
367
368
  				    "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
  				    "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
369
  				   "fonts-noto-cjk", 2);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
370
  	}
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
371
  	check_program("dvipng", 2) if ($pdf);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  	check_missing(\%map);
  
  	return if (!$need && !$optional);
  	printf("You should run:
  
  \tsudo apt-get install $install
  ");
  }
  
  sub give_redhat_hints()
  {
  	my %map = (
  		"python-sphinx"		=> "python3-sphinx",
  		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
  		"virtualenv"		=> "python3-virtualenv",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
387
388
389
390
  		"dot"			=> "graphviz",
  		"convert"		=> "ImageMagick",
  		"Pod::Usage"		=> "perl-Pod-Usage",
  		"xelatex"		=> "texlive-xetex-bin",
8e7d5d15e   Mauro Carvalho Chehab   sphinx-pre-instal...
391
  		"rsvg-convert"		=> "librsvg2-tools",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
392
  	);
5d88953c3   Mauro Carvalho Chehab   sphinx-pre-instal...
393
394
395
  	my @fedora26_opt_pkgs = (
  		"graphviz-gd",		# Fedora 26: needed for PDF support
  	);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
396
397
398
  	my @fedora_tex_pkgs = (
  		"texlive-collection-fontsrecommended",
  		"texlive-collection-latex",
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
399
  		"texlive-xecjk",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
400
401
402
403
  		"dejavu-sans-fonts",
  		"dejavu-serif-fonts",
  		"dejavu-sans-mono-fonts",
  	);
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
404
405
406
  	#
  	# Checks valid for RHEL/CentOS version 7.x.
  	#
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
407
408
409
  	my $old = 0;
  	my $rel;
  	$rel = $1 if ($system_release =~ /release\s+(\d+)/);
b308467c9   Mauro Carvalho Chehab   scripts/sphinx-pr...
410
  	if (!($system_release =~ /Fedora/)) {
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
411
  		$map{"virtualenv"} = "python-virtualenv";
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
412

56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
413
414
415
  		if ($rel && $rel < 8) {
  			$old = 1;
  			$pdf = 0;
5d88953c3   Mauro Carvalho Chehab   sphinx-pre-instal...
416

56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
  			printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output
  ");
  			printf("If you want to build PDF, please read:
  ");
  			printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/
  ");
  		}
  	} else {
  		if ($rel && $rel < 26) {
  			$old = 1;
  		}
  	}
  	if (!$rel) {
  		printf("Couldn't identify release number
  ");
  		$old = 1;
  		$pdf = 0;
  	}
5d88953c3   Mauro Carvalho Chehab   sphinx-pre-instal...
435

27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
436
  	if ($pdf) {
ff8fdb36a   Jeremy MAURO   scripts/sphinx-pr...
437
  		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
438
439
  				   "google-noto-sans-cjk-ttc-fonts", 2);
  	}
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
440
441
442
  	check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
  	check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
  	check_missing_tex(2) if ($pdf);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
443
444
445
  	check_missing(\%map);
  
  	return if (!$need && !$optional);
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
446

56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
447
  	if (!$old) {
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
448
449
450
451
452
453
454
455
456
457
458
459
  		# dnf, for Fedora 18+
  		printf("You should run:
  
  \tsudo dnf install -y $install
  ");
  	} else {
  		# yum, for RHEL (and clones) or Fedora version < 18
  		printf("You should run:
  
  \tsudo yum install -y $install
  ");
  	}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
460
461
462
463
464
465
466
467
  }
  
  sub give_opensuse_hints()
  {
  	my %map = (
  		"python-sphinx"		=> "python3-sphinx",
  		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
  		"virtualenv"		=> "python3-virtualenv",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
468
469
470
471
472
  		"dot"			=> "graphviz",
  		"convert"		=> "ImageMagick",
  		"Pod::Usage"		=> "perl-Pod-Usage",
  		"xelatex"		=> "texlive-xetex-bin",
  	);
b3df6223b   Mauro Carvalho Chehab   scripts: sphinx-p...
473
474
  	# On Tumbleweed, this package is also named rsvg-convert
  	$map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  	my @suse_tex_pkgs = (
  		"texlive-babel-english",
  		"texlive-caption",
  		"texlive-colortbl",
  		"texlive-courier",
  		"texlive-dvips",
  		"texlive-helvetic",
  		"texlive-makeindex",
  		"texlive-metafont",
  		"texlive-metapost",
  		"texlive-palatino",
  		"texlive-preview",
  		"texlive-times",
  		"texlive-zapfchan",
  		"texlive-zapfding",
  	);
353290a9e   Mauro Carvalho Chehab   scripts/sphinx-pr...
491
  	$map{"latexmk"} = "texlive-latexmk-bin";
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
492
493
494
495
  	# FIXME: add support for installing CJK fonts
  	#
  	# I tried hard, but was unable to find a way to install
  	# "Noto Sans CJK SC" on openSUSE
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
496
497
  	check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
  	check_missing_tex(2) if ($pdf);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
498
499
500
501
502
503
504
505
  	check_missing(\%map);
  
  	return if (!$need && !$optional);
  	printf("You should run:
  
  \tsudo zypper install --no-recommends $install
  ");
  }
800d408a3   Mauro Carvalho Chehab   sphinx-pre-instal...
506
507
508
509
510
511
  sub give_mageia_hints()
  {
  	my %map = (
  		"python-sphinx"		=> "python3-sphinx",
  		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
  		"virtualenv"		=> "python3-virtualenv",
800d408a3   Mauro Carvalho Chehab   sphinx-pre-instal...
512
513
514
515
  		"dot"			=> "graphviz",
  		"convert"		=> "ImageMagick",
  		"Pod::Usage"		=> "perl-Pod-Usage",
  		"xelatex"		=> "texlive",
d6ebf1890   Mauro Carvalho Chehab   scripts: sphinx-p...
516
  		"rsvg-convert"		=> "librsvg2",
800d408a3   Mauro Carvalho Chehab   sphinx-pre-instal...
517
518
519
520
521
  	);
  
  	my @tex_pkgs = (
  		"texlive-fontsextra",
  	);
353290a9e   Mauro Carvalho Chehab   scripts/sphinx-pr...
522
  	$map{"latexmk"} = "texlive-collection-basic";
d6ebf1890   Mauro Carvalho Chehab   scripts: sphinx-p...
523
524
525
526
527
528
529
530
531
532
  	my $packager_cmd;
  	my $noto_sans;
  	if ($system_release =~ /OpenMandriva/) {
  		$packager_cmd = "dnf install";
  		$noto_sans = "noto-sans-cjk-fonts";
  		@tex_pkgs = ( "texlive-collection-fontsextra" );
  	} else {
  		$packager_cmd = "urpmi";
  		$noto_sans = "google-noto-sans-cjk-ttc-fonts";
  	}
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
533
  	if ($pdf) {
d6ebf1890   Mauro Carvalho Chehab   scripts: sphinx-p...
534
535
536
  		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
  				    "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
  				   $noto_sans, 2);
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
537
  	}
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
538
  	check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
800d408a3   Mauro Carvalho Chehab   sphinx-pre-instal...
539
540
541
  	check_missing(\%map);
  
  	return if (!$need && !$optional);
d6ebf1890   Mauro Carvalho Chehab   scripts: sphinx-p...
542
543
544
545
  	printf("You should run:
  
  \tsudo $packager_cmd $install
  ");
800d408a3   Mauro Carvalho Chehab   sphinx-pre-instal...
546
  }
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
547
548
549
550
551
  sub give_arch_linux_hints()
  {
  	my %map = (
  		"sphinx_rtd_theme"	=> "python-sphinx_rtd_theme",
  		"virtualenv"		=> "python-virtualenv",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
552
553
554
  		"dot"			=> "graphviz",
  		"convert"		=> "imagemagick",
  		"xelatex"		=> "texlive-bin",
0d0da9aa0   Louis Taylor   scripts/sphinx-pr...
555
  		"latexmk"		=> "texlive-core",
8e7d5d15e   Mauro Carvalho Chehab   sphinx-pre-instal...
556
  		"rsvg-convert"		=> "extra/librsvg",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
557
558
559
560
561
562
563
  	);
  
  	my @archlinux_tex_pkgs = (
  		"texlive-core",
  		"texlive-latexextra",
  		"ttf-dejavu",
  	);
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
564
  	check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
565
  	if ($pdf) {
ff8fdb36a   Jeremy MAURO   scripts/sphinx-pr...
566
  		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
567
568
  				   "noto-fonts-cjk", 2);
  	}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
569
570
571
572
573
574
575
576
577
578
579
580
581
582
  	check_missing(\%map);
  
  	return if (!$need && !$optional);
  	printf("You should run:
  
  \tsudo pacman -S $install
  ");
  }
  
  sub give_gentoo_hints()
  {
  	my %map = (
  		"sphinx_rtd_theme"	=> "dev-python/sphinx_rtd_theme",
  		"virtualenv"		=> "dev-python/virtualenv",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
583
584
585
  		"dot"			=> "media-gfx/graphviz",
  		"convert"		=> "media-gfx/imagemagick",
  		"xelatex"		=> "dev-texlive/texlive-xetex media-fonts/dejavu",
8e7d5d15e   Mauro Carvalho Chehab   sphinx-pre-instal...
586
  		"rsvg-convert"		=> "gnome-base/librsvg",
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
587
  	);
ff8fdb36a   Jeremy MAURO   scripts/sphinx-pr...
588
  	check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
589
  			   "media-fonts/dejavu", 2) if ($pdf);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
590

27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
591
  	if ($pdf) {
e45a63174   Mauro Carvalho Chehab   scripts: sphinx-p...
592
593
  		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
  				    "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
27eed923f   Mauro Carvalho Chehab   scripts/sphinx-pr...
594
595
  				   "media-fonts/noto-cjk", 2);
  	}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
596
597
598
  	check_missing(\%map);
  
  	return if (!$need && !$optional);
bba1e4cbd   Mauro Carvalho Chehab   sphinx-pre-instal...
599
600
601
602
  
  	printf("You should run:
  
  ");
4ea96d57b   Mauro Carvalho Chehab   scripts/sphinx-pr...
603
604
605
606
607
  
  	my $imagemagick = "media-gfx/imagemagick svg png";
  	my $cairo = "media-gfx/graphviz cairo pdf";
  	my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
  	my $portage_cairo = "/etc/portage/package.use/graphviz";
e45a63174   Mauro Carvalho Chehab   scripts: sphinx-p...
608
  	if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
4ea96d57b   Mauro Carvalho Chehab   scripts/sphinx-pr...
609
610
611
  		printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'
  ")
  	}
e45a63174   Mauro Carvalho Chehab   scripts: sphinx-p...
612
  	if (qx(grep graphviz $portage_cairo 2>/dev/null) eq  "") {
4ea96d57b   Mauro Carvalho Chehab   scripts/sphinx-pr...
613
614
615
  		printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'
  ");
  	}
bba1e4cbd   Mauro Carvalho Chehab   sphinx-pre-instal...
616
617
  	printf("\tsudo emerge --ask $install
  ");
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
618
619
620
621
622
623
624
625
626
  }
  
  sub check_distros()
  {
  	# Distro-specific hints
  	if ($system_release =~ /Red Hat Enterprise Linux/) {
  		give_redhat_hints;
  		return;
  	}
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
627
628
629
630
631
632
633
634
635
636
637
638
  	if ($system_release =~ /CentOS/) {
  		give_redhat_hints;
  		return;
  	}
  	if ($system_release =~ /Scientific Linux/) {
  		give_redhat_hints;
  		return;
  	}
  	if ($system_release =~ /Oracle Linux Server/) {
  		give_redhat_hints;
  		return;
  	}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
  	if ($system_release =~ /Fedora/) {
  		give_redhat_hints;
  		return;
  	}
  	if ($system_release =~ /Ubuntu/) {
  		give_debian_hints;
  		return;
  	}
  	if ($system_release =~ /Debian/) {
  		give_debian_hints;
  		return;
  	}
  	if ($system_release =~ /openSUSE/) {
  		give_opensuse_hints;
  		return;
  	}
800d408a3   Mauro Carvalho Chehab   sphinx-pre-instal...
655
656
657
658
  	if ($system_release =~ /Mageia/) {
  		give_mageia_hints;
  		return;
  	}
d6ebf1890   Mauro Carvalho Chehab   scripts: sphinx-p...
659
660
661
662
  	if ($system_release =~ /OpenMandriva/) {
  		give_mageia_hints;
  		return;
  	}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  	if ($system_release =~ /Arch Linux/) {
  		give_arch_linux_hints;
  		return;
  	}
  	if ($system_release =~ /Gentoo/) {
  		give_gentoo_hints;
  		return;
  	}
  
  	#
  	# Fall-back to generic hint code for other distros
  	# That's far from ideal, specially for LaTeX dependencies.
  	#
  	my %map = (
  		"sphinx-build" => "sphinx"
  	);
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
679
  	check_missing_tex(2) if ($pdf);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
680
681
682
683
684
685
686
687
688
689
690
691
  	check_missing(\%map);
  	print "I don't know distro $system_release.
  ";
  	print "So, I can't provide you a hint with the install procedure.
  ";
  	print "There are likely missing dependencies.
  ";
  }
  
  #
  # Common dependencies
  #
2730ce017   Shuah Khan   scripts/sphinx-pr...
692
693
  sub deactivate_help()
  {
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
694
695
696
  	printf "
  If you want to exit the virtualenv, you can use:
  ";
2730ce017   Shuah Khan   scripts/sphinx-pr...
697
698
699
  	printf "\tdeactivate
  ";
  }
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
700
701
  sub get_virtenv()
  {
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
702
  	my $ver;
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
703
704
705
706
  	my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
  	my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
  
  	@activates = sort {$b cmp $a} @activates;
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
707

e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
  	foreach my $f (@activates) {
  		next if ($f lt $min_activate);
  
  		my $sphinx_cmd = $f;
  		$sphinx_cmd =~ s/activate/sphinx-build/;
  		next if (! -f $sphinx_cmd);
  
  		my $ver = get_sphinx_version($sphinx_cmd);
  		if ($need_sphinx && ($ver ge $min_version)) {
  			return ($f, $ver);
  		} elsif ($ver gt $cur_version) {
  			return ($f, $ver);
  		}
  	}
  	return ("", "");
  }
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
724
  sub recommend_sphinx_upgrade()
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
725
  {
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
726
  	my $venv_ver;
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
727
728
  
  	# Avoid running sphinx-builds from venv if $cur_version is good
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
729
730
731
732
  	if ($cur_version && ($cur_version ge $rec_version)) {
  		$latest_avail_ver = $cur_version;
  		return;
  	}
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
733
734
735
  
  	# Get the highest version from sphinx_*/bin/sphinx-build and the
  	# corresponding command to activate the venv/virtenv
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
736
  	$activate_cmd = get_virtenv();
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
737

a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
738
739
  	# Store the highest version from Sphinx existing virtualenvs
  	if (($activate_cmd ne "") && ($venv_ver gt $cur_version)) {
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
740
741
742
743
  		$latest_avail_ver = $venv_ver;
  	} else {
  		$latest_avail_ver = $cur_version if ($cur_version);
  	}
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
  	# As we don't know package version of Sphinx, and there's no
  	# virtual environments, don't check if upgrades are needed
  	if (!$virtualenv) {
  		return if (!$latest_avail_ver);
  	}
  
  	# Either there are already a virtual env or a new one should be created
  	$need_pip = 1;
  
  	# Return if the reason is due to an upgrade or not
  	if ($latest_avail_ver lt $rec_version) {
  		$rec_sphinx_upgrade = 1;
  	}
  }
  
  #
  # The logic here is complex, as it have to deal with different versions:
  #	- minimal supported version;
  #	- minimal PDF version;
  #	- recommended version.
  # It also needs to work fine with both distro's package and venv/virtualenv
  sub recommend_sphinx_version($)
  {
  	my $virtualenv_cmd = shift;
  
  	if ($latest_avail_ver lt $min_pdf_version) {
  		print "note: If you want pdf, you need at least Sphinx $min_pdf_version.
  ";
  	}
  
  	# Version is OK. Nothing to do.
  	return if ($cur_version && ($cur_version ge $rec_version));
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
776
777
778
779
780
  	if (!$need_sphinx) {
  		# sphinx-build is present and its version is >= $min_version
  
  		#only recommend enabling a newer virtenv version if makes sense.
  		if ($latest_avail_ver gt $cur_version) {
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
781
782
783
  			printf "
  You may also use the newer Sphinx version $latest_avail_ver with:
  ";
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
784
785
  			printf "\tdeactivate
  "  if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
786
787
  			printf "\t. $activate_cmd
  ";
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
  			deactivate_help();
  
  			return;
  		}
  		return if ($latest_avail_ver ge $rec_version);
  	}
  
  	if (!$virtualenv) {
  		# No sphinx either via package or via virtenv. As we can't
  		# Compare the versions here, just return, recommending the
  		# user to install it from the package distro.
  		return if (!$latest_avail_ver);
  
  		# User doesn't want a virtenv recommendation, but he already
  		# installed one via virtenv with a newer version.
  		# So, print commands to enable it
  		if ($latest_avail_ver gt $cur_version) {
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
805
806
807
  			printf "
  You may also use the Sphinx virtualenv version $latest_avail_ver with:
  ";
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
808
809
  			printf "\tdeactivate
  "  if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
810
811
  			printf "\t. $activate_cmd
  ";
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
812
813
814
815
816
817
818
819
820
821
822
823
824
825
  			deactivate_help();
  
  			return;
  		}
  		print "
  ";
  	} else {
  		$need++ if ($need_sphinx);
  	}
  
  	# Suggest newer versions if current ones are too old
  	if ($latest_avail_ver && $cur_version ge $min_version) {
  		# If there's a good enough version, ask the user to enable it
  		if ($latest_avail_ver ge $rec_version) {
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
826
827
828
829
830
  			printf "
  Need to activate Sphinx (version $latest_avail_ver) on virtualenv with:
  ";
  			printf "\t. $activate_cmd
  ";
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
831
832
833
834
835
836
837
838
839
840
841
  			deactivate_help();
  
  			return;
  		}
  
  		# Version is above the minimal required one, but may be
  		# below the recommended one. So, print warnings/notes
  
  		if ($latest_avail_ver lt $rec_version) {
  			print "Warning: It is recommended at least Sphinx version $rec_version.
  ";
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
  		}
  	}
  
  	# At this point, either it needs Sphinx or upgrade is recommended,
  	# both via pip
  
  	if ($rec_sphinx_upgrade) {
  		if (!$virtualenv) {
  			print "Instead of install/upgrade Python Sphinx pkg, you could use pip/pypi with:
  
  ";
  		} else {
  			print "To upgrade Sphinx, use:
  
  ";
  		}
  	} else {
  		print "Sphinx needs to be installed either as a package or via pip/pypi with:
  ";
  	}
  
  	$python_cmd = find_python_no_venv();
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
864
865
  	printf "\t$virtualenv_cmd $virtenv_dir
  ";
e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
866
867
868
869
870
871
  	printf "\t. $virtenv_dir/bin/activate
  ";
  	printf "\tpip install -r $requirement_file
  ";
  	deactivate_help();
  }
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
872
873
  sub check_needs()
  {
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
874
  	# Check if Sphinx is already accessible from current environment
9b88ad546   Mauro Carvalho Chehab   scripts/sphinx-pr...
875
  	check_sphinx();
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
876
  	if ($system_release) {
ec43a27ff   Mauro Carvalho Chehab   scripts: sphinx-p...
877
878
  		print "Detected OS: $system_release.
  ";
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
879
  	} else {
ec43a27ff   Mauro Carvalho Chehab   scripts: sphinx-p...
880
881
  		print "Unknown OS
  ";
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
882
  	}
ec43a27ff   Mauro Carvalho Chehab   scripts: sphinx-p...
883
884
885
  	printf "Sphinx version: %s
  
  ", $cur_version if ($cur_version);
9b756a9d0   Mauro Carvalho Chehab   scripts/sphinx-pr...
886

2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
887
888
889
890
891
892
893
894
895
  	# Check python command line, trying first python3
  	$python_cmd = findprog("python3");
  	$python_cmd = check_program("python", 0) if (!$python_cmd);
  
  	# Check the type of virtual env, depending on Python version
  	if ($python_cmd) {
  		if ($virtualenv) {
  			my $tmp = qx($python_cmd --version 2>&1);
  			if ($tmp =~ m/(\d+\.)(\d+\.)/) {
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
896
  				if ($1 < 3) {
4217e5074   Jonathan Corbet   Docs: drop Python...
897
898
899
  					# Fail if it finds python2 (or worse)
  					die "Python 3 is required to build the kernel docs
  ";
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
900
  				}
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
901
902
903
904
  				if ($1 == 3 && $2 < 3) {
  					# Need Python 3.3 or upper for venv
  					$need_virtualenv = 1;
  				}
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
905
906
907
908
909
910
911
  			} else {
  				die "Warning: couldn't identify $python_cmd version!";
  			}
  		} else {
  			add_package("python-sphinx", 0);
  		}
  	}
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
912
  	recommend_sphinx_upgrade();
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
913
  	my $virtualenv_cmd;
a5f785f10   Mauro Carvalho Chehab   scripts: sphinx-p...
914
915
916
917
918
919
920
921
922
923
924
925
926
  
  	if ($need_pip) {
  		# Set virtualenv command line, if python < 3.3
  		if ($need_virtualenv) {
  			$virtualenv_cmd = findprog("virtualenv-3");
  			$virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
  			if (!$virtualenv_cmd) {
  				check_program("virtualenv", 0);
  				$virtualenv_cmd = "virtualenv";
  			}
  		} else {
  			$virtualenv_cmd = "$python_cmd -m venv";
  			check_python_module("ensurepip", 0);
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
927
928
  		}
  	}
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
929
  	# Check for needed programs/tools
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
930
931
932
933
  	check_perl_module("Pod::Usage", 0);
  	check_program("make", 0);
  	check_program("gcc", 0);
  	check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
934
935
  	check_program("dot", 1);
  	check_program("convert", 1);
56e5a6339   Mauro Carvalho Chehab   scripts/sphinx-pr...
936
937
938
939
940
  
  	# Extra PDF files - should use 2 for is_optional
  	check_program("xelatex", 2) if ($pdf);
  	check_program("rsvg-convert", 2) if ($pdf);
  	check_program("latexmk", 2) if ($pdf);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
941

2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
942
  	# Do distro-specific checks and output distro-install commands
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
943
  	check_distros();
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
944
945
946
947
948
949
950
951
952
  	if (!$python_cmd) {
  		if ($need == 1) {
  			die "Can't build as $need mandatory dependency is missing";
  		} elsif ($need) {
  			die "Can't build as $need mandatory dependencies are missing";
  		}
  	}
  
  	# Check if sphinx-build is called sphinx-build-3
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
953
954
955
956
957
958
  	if ($need_symlink) {
  		printf "\tsudo ln -sf %s /usr/bin/sphinx-build
  
  ",
  		       which("sphinx-build-3");
  	}
2f9c50255   Mauro Carvalho Chehab   scripts: sphinx-p...
959

e50899122   Mauro Carvalho Chehab   scripts: sphinx-p...
960
  	recommend_sphinx_version($virtualenv_cmd);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
961
962
  	printf "
  ";
54002b56b   Bjorn Helgaas   scripts/sphinx-pr...
963
964
  	print "All optional dependencies are met.
  " if (!$optional);
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
  
  	if ($need == 1) {
  		die "Can't build as $need mandatory dependency is missing";
  	} elsif ($need) {
  		die "Can't build as $need mandatory dependencies are missing";
  	}
  
  	print "Needed package dependencies are met.
  ";
  }
  
  #
  # Main
  #
  
  while (@ARGV) {
  	my $arg = shift(@ARGV);
  
  	if ($arg eq "--no-virtualenv") {
  		$virtualenv = 0;
  	} elsif ($arg eq "--no-pdf"){
  		$pdf = 0;
9b88ad546   Mauro Carvalho Chehab   scripts/sphinx-pr...
987
988
  	} elsif ($arg eq "--version-check"){
  		$version_check = 1;
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
989
  	} else {
9b88ad546   Mauro Carvalho Chehab   scripts/sphinx-pr...
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
  		print "Usage:
  \t$0 <--no-virtualenv> <--no-pdf> <--version-check>
  
  ";
  		print "Where:
  ";
  		print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv
  ";
  		print "\t--version-check\t- if version is compatible, don't check for missing dependencies
  ";
  		print "\t--no-pdf\t- don't check for dependencies required to build PDF docs
  
  ";
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
  		exit -1;
  	}
  }
  
  #
  # Determine the system type. There's no standard unique way that would
  # work with all distros with a minimal package install. So, several
  # methods are used here.
  #
  # By default, it will use lsb_release function. If not available, it will
  # fail back to reading the known different places where the distro name
  # is stored
  #
  
  $system_release = qx(lsb_release -d) if which("lsb_release");
  $system_release =~ s/Description:\s*// if ($system_release);
  $system_release = catcheck("/etc/system-release") if !$system_release;
  $system_release = catcheck("/etc/redhat-release") if !$system_release;
  $system_release = catcheck("/etc/lsb-release") if !$system_release;
  $system_release = catcheck("/etc/gentoo-release") if !$system_release;
d14d0c1ae   Mauro Carvalho Chehab   scripts: sphinx-p...
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
  
  # This seems more common than LSB these days
  if (!$system_release) {
  	my %os_var;
  	if (open IN, "cat /etc/os-release|") {
  		while (<IN>) {
  			if (m/^([\w\d\_]+)=\"?([^\"]*)\"?
  /) {
  				$os_var{$1}=$2;
  			}
  		}
  		$system_release = $os_var{"NAME"};
  		if (defined($os_var{"VERSION_ID"})) {
  			$system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
  		} else {
  			$system_release .= " " . $os_var{"VERSION"};
  		}
  	}
  }
24071ac1a   Mauro Carvalho Chehab   scripts/sphinx-pr...
1042
1043
1044
1045
  $system_release = catcheck("/etc/issue") if !$system_release;
  $system_release =~ s/\s+$//;
  
  check_needs;