History

scripts: sphinx-pre-install: rework the sphinx install logic

The sphinx-pre-install supports installing sphinx via a virtual environment using pip/pypi or directly from the distribution's package, when --no-virtualenv is used. However, even when --no-virtualenv, the current logic is still recomending to install a virtual env, due to a regression. It turns that the logic there is complex, as it depends on several different conditions. Split the code which recommends Sphinx on two separate functions, in order to clean up the code.

Mauro Carvalho Chehab committed 5 years ago

Commit e5089911 parent c8237761 file · +140 −68Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/sphinx-pre-install+140−68
@@ -24,7 +24,6 @@ my $need_symlink = 0; · 23 unmodified lines
2424 my $need_sphinx = 0;
2525 my $need_venv = 0;
2626 my $need_virtualenv = 0;
27−my $rec_sphinx_upgrade = 0;
2827 my $install = "";
2928 my $virtenv_dir = "";
3029 my $python_cmd = "";
@@ -33,6 +32,7 @@ my $cur_version; · 2 unmodified lines
3332 my $rec_version = "1.7.9"; # PDF won't build here
3433 my $min_pdf_version = "2.4.4"; # Min version where pdf builds
3534
35+
3636 #
3737 # Command line arguments
3838 #
@@ -319,10 +319,7 @@ sub check_sphinx() · 280 unmodified lines
319319 return;
320320 }
321321
322− if ($cur_version lt $rec_version) {
323− $rec_sphinx_upgrade = 1;
324− return;
325− }
322+ return if ($cur_version lt $rec_version);
326323
327324 # On version check mode, just assume Sphinx has all mandatory deps
328325 exit (0) if ($version_check);
@@ -701,6 +698,141 @@ sub deactivate_help() · 372 unmodified lines
701698 printf "\tdeactivate\n";
702699 }
703700
701+sub get_virtenv()
702+{
703+ my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
704+ my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
705+
706+ @activates = sort {$b cmp $a} @activates;
707+ my ($activate, $ver);
708+ foreach my $f (@activates) {
709+ next if ($f lt $min_activate);
710+
711+ my $sphinx_cmd = $f;
712+ $sphinx_cmd =~ s/activate/sphinx-build/;
713+ next if (! -f $sphinx_cmd);
714+
715+ my $ver = get_sphinx_version($sphinx_cmd);
716+ if ($need_sphinx && ($ver ge $min_version)) {
717+ return ($f, $ver);
718+ } elsif ($ver gt $cur_version) {
719+ return ($f, $ver);
720+ }
721+ }
722+ return ("", "");
723+}
724+
725+#
726+# The logic here is complex, as it have to deal with different versions:
727+# - minimal supported version;
728+# - minimal PDF version;
729+# - recommended version.
730+# It also needs to work fine with both distro's package and venv/virtualenv
731+sub recommend_sphinx_version($)
732+{
733+ my $virtualenv_cmd = shift;
734+
735+ # Avoid running sphinx-builds from venv if $cur_version is good
736+ return if ($cur_version && ($cur_version ge $rec_version));
737+
738+ my $latest_avail_ver;
739+ my $rec_sphinx_upgrade = 0;
740+
741+ # Get the highest version from sphinx_*/bin/sphinx-build and the
742+ # corresponding command to activate the venv/virtenv
743+ my ($activate, $venv_ver) = get_virtenv();
744+
745+ if (($activate ne "") && ($venv_ver gt $cur_version)) {
746+ $latest_avail_ver = $venv_ver;
747+ } else {
748+ $latest_avail_ver = $cur_version if ($cur_version);
749+ }
750+
751+ if (!$need_sphinx) {
752+ # sphinx-build is present and its version is >= $min_version
753+
754+ #only recommend enabling a newer virtenv version if makes sense.
755+ if ($latest_avail_ver gt $cur_version) {
756+ printf "\nYou may also use the newer Sphinx version $venv_ver with:\n";
757+ printf "\tdeactivate\n" if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
758+ printf "\t. $activate\n";
759+ deactivate_help();
760+
761+ return;
762+ }
763+ return if ($latest_avail_ver ge $rec_version);
764+ }
765+
766+ if (!$virtualenv) {
767+ # No sphinx either via package or via virtenv. As we can't
768+ # Compare the versions here, just return, recommending the
769+ # user to install it from the package distro.
770+ return if (!$latest_avail_ver);
771+
772+ # User doesn't want a virtenv recommendation, but he already
773+ # installed one via virtenv with a newer version.
774+ # So, print commands to enable it
775+ if ($latest_avail_ver gt $cur_version) {
776+ printf "\nYou may also use the Sphinx virtualenv version $venv_ver with:\n";
777+ printf "\tdeactivate\n" if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
778+ printf "\t. $activate\n";
779+ deactivate_help();
780+
781+ return;
782+ }
783+ print "\n";
784+ } else {
785+ $need++ if ($need_sphinx);
786+ }
787+
788+ # Suggest newer versions if current ones are too old
789+ if ($latest_avail_ver && $cur_version ge $min_version) {
790+ # If there's a good enough version, ask the user to enable it
791+ if ($latest_avail_ver ge $rec_version) {
792+ printf "\nNeed to activate Sphinx (version $venv_ver) on virtualenv with:\n";
793+ printf "\t. $activate\n";
794+ deactivate_help();
795+
796+ return;
797+ }
798+
799+ # Version is above the minimal required one, but may be
800+ # below the recommended one. So, print warnings/notes
801+
802+ if ($latest_avail_ver lt $rec_version) {
803+ print "Warning: It is recommended at least Sphinx version $rec_version.\n";
804+ $rec_sphinx_upgrade = 1;
805+ }
806+ if ($latest_avail_ver lt $min_pdf_version) {
807+ print "note: If you want pdf, you need at least $min_pdf_version.\n";
808+ }
809+ }
810+
811+ # At this point, either it needs Sphinx or upgrade is recommended,
812+ # both via pip
813+
814+ if ($rec_sphinx_upgrade) {
815+ if (!$virtualenv) {
816+ print "Instead of install/upgrade Python Sphinx pkg, you could use pip/pypi with:\n\n";
817+ } else {
818+ print "To upgrade Sphinx, use:\n\n";
819+ }
820+ } else {
821+ print "Sphinx needs to be installed either as a package or via pip/pypi with:\n";
822+ }
823+
824+ $python_cmd = find_python_no_venv();
825+
826+ if ($need_venv) {
827+ printf "\t$python_cmd -m venv $virtenv_dir\n";
828+ } else {
829+ printf "\t$virtualenv_cmd $virtenv_dir\n";
830+ }
831+ printf "\t. $virtenv_dir/bin/activate\n";
832+ printf "\tpip install -r $requirement_file\n";
833+ deactivate_help();
834+}
835+
704836 sub check_needs()
705837 {
706838 # Check if Sphinx is already accessible from current environment
@@ -763,8 +895,8 @@ sub check_needs() · 56 unmodified lines
763895 check_program("rsvg-convert", 2) if ($pdf);
764896 check_program("latexmk", 2) if ($pdf);
765897
766− if ($need_sphinx || $rec_sphinx_upgrade) {
767− check_python_module("ensurepip", 0) if ($need_venv);
898+ if ($need_venv) {
899+ check_python_module("ensurepip", 0);
768900 }
769901
770902 # Do distro-specific checks and output distro-install commands
@@ -784,67 +916,7 @@ sub check_needs() · 13 unmodified lines
784916 which("sphinx-build-3");
785917 }
786918
787− # NOTE: if the system has a too old Sphinx version installed,
788− # it will recommend installing a newer version using virtualenv
789−
790− if ($need_sphinx || $rec_sphinx_upgrade) {
791− my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
792− my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
793−
794− if ($cur_version lt $rec_version) {
795− print "Warning: It is recommended at least Sphinx version $rec_version.\n";
796− print " If you want pdf, you need at least $min_pdf_version.\n";
797− }
798− if ($cur_version lt $min_pdf_version) {
799− print "Note: It is recommended at least Sphinx version $min_pdf_version if you need PDF support.\n";
800− }
801− @activates = sort {$b cmp $a} @activates;
802− my ($activate, $ver);
803− foreach my $f (@activates) {
804− next if ($f lt $min_activate);
805−
806− my $sphinx_cmd = $f;
807− $sphinx_cmd =~ s/activate/sphinx-build/;
808− next if (! -f $sphinx_cmd);
809−
810− $ver = get_sphinx_version($sphinx_cmd);
811− if ($need_sphinx && ($ver ge $min_version)) {
812− $activate = $f;
813− last;
814− } elsif ($ver gt $cur_version) {
815− $activate = $f;
816− last;
817− }
818− }
819− if ($activate ne "") {
820− if ($need_sphinx) {
821− printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
822− printf "\t. $activate\n";
823− deactivate_help();
824− exit (1);
825− } else {
826− printf "\nYou may also use a newer Sphinx (version $ver) with:\n";
827− printf "\tdeactivate && . $activate\n";
828− }
829− } else {
830− my $rec_activate = "$virtenv_dir/bin/activate";
831−
832− print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
833−
834− $python_cmd = find_python_no_venv();
835−
836− if ($need_venv) {
837− printf "\t$python_cmd -m venv $virtenv_dir\n";
838− } else {
839− printf "\t$virtualenv_cmd $virtenv_dir\n";
840− }
841− printf "\t. $rec_activate\n";
842− printf "\tpip install -r $requirement_file\n";
843− deactivate_help();
844−
845− $need++ if (!$rec_sphinx_upgrade);
846− }
847− }
919+ recommend_sphinx_version($virtualenv_cmd);
848920 printf "\n";
849921
850922 print "All optional dependencies are met.\n" if (!$optional);