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

Unified · Split

scripts/sphinx-pre-install+140−68
@@ -24,7 +24,6 @@ my $need_symlink = 0; · 23 unmodified lines
24my $need_sphinx = 0;24my $need_sphinx = 0;
25my $need_venv = 0;25my $need_venv = 0;
26my $need_virtualenv = 0;26my $need_virtualenv = 0;
27my $rec_sphinx_upgrade = 0;
28my $install = "";27my $install = "";
29my $virtenv_dir = "";28my $virtenv_dir = "";
30my $python_cmd = "";29my $python_cmd = "";
@@ -33,6 +32,7 @@ my $cur_version; · 2 unmodified lines
33my $rec_version = "1.7.9"; # PDF won't build here32my $rec_version = "1.7.9"; # PDF won't build here
34my $min_pdf_version = "2.4.4"; # Min version where pdf builds33my $min_pdf_version = "2.4.4"; # Min version where pdf builds
35 34
35
36#36#
37# Command line arguments37# Command line arguments
38#38#
@@ -319,10 +319,7 @@ sub check_sphinx() · 280 unmodified lines
319 return;319 return;
320 }320 }
321 321
322 if ($cur_version lt $rec_version) {322 return if ($cur_version lt $rec_version);
323 $rec_sphinx_upgrade = 1;
324 return;
325 }
326 323
327 # On version check mode, just assume Sphinx has all mandatory deps324 # On version check mode, just assume Sphinx has all mandatory deps
328 exit (0) if ($version_check);325 exit (0) if ($version_check);
@@ -701,6 +698,141 @@ sub deactivate_help() · 372 unmodified lines
701 printf "\tdeactivate\n";698 printf "\tdeactivate\n";
702}699}
703 700
701sub 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
731sub 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
704sub check_needs()836sub check_needs()
705{837{
706 # Check if Sphinx is already accessible from current environment838 # Check if Sphinx is already accessible from current environment
@@ -763,8 +895,8 @@ sub check_needs() · 56 unmodified lines
763 check_program("rsvg-convert", 2) if ($pdf);895 check_program("rsvg-convert", 2) if ($pdf);
764 check_program("latexmk", 2) if ($pdf);896 check_program("latexmk", 2) if ($pdf);
765 897
766 if ($need_sphinx || $rec_sphinx_upgrade) {898 if ($need_venv) {
767 check_python_module("ensurepip", 0) if ($need_venv);899 check_python_module("ensurepip", 0);
768 }900 }
769 901
770 # Do distro-specific checks and output distro-install commands902 # Do distro-specific checks and output distro-install commands
@@ -784,67 +916,7 @@ sub check_needs() · 13 unmodified lines
784 which("sphinx-build-3");916 which("sphinx-build-3");
785 }917 }
786 918
787 # NOTE: if the system has a too old Sphinx version installed,919 recommend_sphinx_version($virtualenv_cmd);
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 }
848 printf "\n";920 printf "\n";
849 921
850 print "All optional dependencies are met.\n" if (!$optional);922 print "All optional dependencies are met.\n" if (!$optional);