Skip to content

Commit 2f9c502

Browse files
mchehabJonathan Corbet
authored andcommitted
scripts: sphinx-pre-install: add support for python -m venv
Since python 3.3, the recommended way to setup a virtual env is via "python -m venv". Set this as a default, if python version is compatible with such feature. While here, add more comments to it, as the script is getting more complex. So, better to add more things, to avoid accidentally breaking it while improving it. Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Link: https://lore.kernel.org/r/252cc849c79527ad496247e4c481961478adf41c.1586883286.git.mchehab+huawei@kernel.org Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent d6ebf18 commit 2f9c502

1 file changed

Lines changed: 75 additions & 38 deletions

File tree

‎scripts/sphinx-pre-install‎

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: GPL-2.0-or-later
33
use strict;
44

5-
# Copyright (c) 2017-2019 Mauro Carvalho Chehab <mchehab@kernel.org>
5+
# Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
66
#
77

88
my $prefix = "./";
@@ -22,9 +22,12 @@ my $need = 0;
2222
my $optional = 0;
2323
my $need_symlink = 0;
2424
my $need_sphinx = 0;
25+
my $need_venv = 0;
26+
my $need_virtualenv = 0;
2527
my $rec_sphinx_upgrade = 0;
2628
my $install = "";
2729
my $virtenv_dir = "";
30+
my $python_cmd = "";
2831
my $min_version;
2932

3033
#
@@ -147,7 +150,7 @@ sub check_program($$)
147150
my $prog = shift;
148151
my $is_optional = shift;
149152

150-
return if findprog($prog);
153+
return $prog if findprog($prog);
151154

152155
add_package($prog, $is_optional);
153156
}
@@ -168,9 +171,9 @@ sub check_python_module($$)
168171
my $prog = shift;
169172
my $is_optional = shift;
170173

171-
my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
172-
return if ($err == 0);
173-
my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
174+
return if (!$python_cmd);
175+
176+
my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
174177
return if ($err == 0);
175178

176179
add_package($prog, $is_optional);
@@ -225,16 +228,6 @@ sub get_sphinx_fname()
225228
return $fname;
226229
}
227230

228-
if ($virtualenv) {
229-
my $prog = findprog("virtualenv-3");
230-
$prog = findprog("virtualenv-3.5") if (!$prog);
231-
232-
check_program("virtualenv", 0) if (!$prog);
233-
$need_sphinx = 1;
234-
} else {
235-
add_package("python-sphinx", 0);
236-
}
237-
238231
return "";
239232
}
240233

@@ -268,7 +261,10 @@ sub check_sphinx()
268261
$virtenv_dir = $virtenv_prefix . $rec_version;
269262

270263
my $sphinx = get_sphinx_fname();
271-
return if ($sphinx eq "");
264+
if ($sphinx eq "") {
265+
$need_sphinx = 1;
266+
return;
267+
}
272268

273269
open IN, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
274270
while (<IN>) {
@@ -336,6 +332,7 @@ sub give_debian_hints()
336332
my %map = (
337333
"python-sphinx" => "python3-sphinx",
338334
"sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
335+
"ensurepip" => "python3-venv",
339336
"virtualenv" => "virtualenv",
340337
"dot" => "graphviz",
341338
"convert" => "imagemagick",
@@ -672,13 +669,13 @@ sub check_distros()
672669

673670
sub deactivate_help()
674671
{
675-
printf "\tIf you want to exit the virtualenv, you can use:\n";
672+
printf "\nIf you want to exit the virtualenv, you can use:\n";
676673
printf "\tdeactivate\n";
677674
}
678675

679676
sub check_needs()
680677
{
681-
# Check for needed programs/tools
678+
# Check if Sphinx is already accessible from current environment
682679
check_sphinx();
683680

684681
if ($system_release) {
@@ -689,6 +686,43 @@ sub check_needs()
689686

690687
print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
691688

689+
# Check python command line, trying first python3
690+
$python_cmd = findprog("python3");
691+
$python_cmd = check_program("python", 0) if (!$python_cmd);
692+
693+
# Check the type of virtual env, depending on Python version
694+
if ($python_cmd) {
695+
if ($virtualenv) {
696+
my $tmp = qx($python_cmd --version 2>&1);
697+
if ($tmp =~ m/(\d+\.)(\d+\.)/) {
698+
if ($1 >= 3 && $2 >= 3) {
699+
$need_venv = 1; # python 3.3 or upper
700+
} else {
701+
$need_virtualenv = 1;
702+
}
703+
if ($1 < 3) {
704+
# Complain if it finds python2 (or worse)
705+
printf "Warning: python$1 support is deprecated. Use it with caution!\n";
706+
}
707+
} else {
708+
die "Warning: couldn't identify $python_cmd version!";
709+
}
710+
} else {
711+
add_package("python-sphinx", 0);
712+
}
713+
}
714+
715+
# Set virtualenv command line, if python < 3.3
716+
my $virtualenv_cmd;
717+
if ($need_virtualenv) {
718+
$virtualenv_cmd = findprog("virtualenv-3");
719+
$virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
720+
if (!$virtualenv_cmd) {
721+
check_program("virtualenv", 0);
722+
$virtualenv_cmd = "virtualenv";
723+
}
724+
}
725+
692726
# Check for needed programs/tools
693727
check_perl_module("Pod::Usage", 0);
694728
check_program("make", 0);
@@ -702,12 +736,30 @@ sub check_needs()
702736
check_program("rsvg-convert", 2) if ($pdf);
703737
check_program("latexmk", 2) if ($pdf);
704738

739+
if ($need_sphinx || $rec_sphinx_upgrade) {
740+
check_python_module("ensurepip", 0) if ($need_venv);
741+
}
742+
743+
# Do distro-specific checks and output distro-install commands
705744
check_distros();
706745

746+
if (!$python_cmd) {
747+
if ($need == 1) {
748+
die "Can't build as $need mandatory dependency is missing";
749+
} elsif ($need) {
750+
die "Can't build as $need mandatory dependencies are missing";
751+
}
752+
}
753+
754+
# Check if sphinx-build is called sphinx-build-3
707755
if ($need_symlink) {
708756
printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
709757
which("sphinx-build-3");
710758
}
759+
760+
# NOTE: if the system has a too old Sphinx version installed,
761+
# it will recommend installing a newer version using virtualenv
762+
711763
if ($need_sphinx || $rec_sphinx_upgrade) {
712764
my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
713765
my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
@@ -721,27 +773,12 @@ sub check_needs()
721773
exit (1);
722774
} else {
723775
my $rec_activate = "$virtenv_dir/bin/activate";
724-
my $virtualenv = findprog("virtualenv-3");
725-
my $rec_python3 = "";
726-
$virtualenv = findprog("virtualenv-3.5") if (!$virtualenv);
727-
$virtualenv = findprog("virtualenv") if (!$virtualenv);
728-
$virtualenv = "virtualenv" if (!$virtualenv);
729-
730-
my $rel = "";
731-
if (index($system_release, "Ubuntu") != -1) {
732-
$rel = $1 if ($system_release =~ /Ubuntu\s+(\d+)[.]/);
733-
if ($rel && $rel >= 16) {
734-
$rec_python3 = " -p python3";
735-
}
736-
}
737-
if (index($system_release, "Debian") != -1) {
738-
$rel = $1 if ($system_release =~ /Debian\s+(\d+)/);
739-
if ($rel && $rel >= 7) {
740-
$rec_python3 = " -p python3";
741-
}
742-
}
743776

744-
printf "\t$virtualenv$rec_python3 $virtenv_dir\n";
777+
if ($need_venv) {
778+
printf "\t$python_cmd -m venv $virtenv_dir\n";
779+
} else {
780+
printf "\t$virtualenv_cmd $virtenv_dir\n";
781+
}
745782
printf "\t. $rec_activate\n";
746783
printf "\tpip install -r $requirement_file\n";
747784
deactivate_help();

0 commit comments

Comments
 (0)