[RFC v2] scripts/sphinx-pre-install: add a script to check Sphinx install
From: Mauro Carvalho Chehab
Date: Fri Jul 14 2017 - 21:20:17 EST
Solving Sphinx dependencies can be painful. Add a script to
check if everything is ok.
Tested on Fedora 26 and Ubuntu 17.04.
Signed-off-by: Mauro Carvalho Chehab <mchehab@xxxxxxxxxxxxxxxx>
---
v2: add support for Fedora 26
scripts/sphinx-pre-install | 321 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 321 insertions(+)
create mode 100755 scripts/sphinx-pre-install
diff --git a/scripts/sphinx-pre-install b/scripts/sphinx-pre-install
new file mode 100755
index 000000000000..140e01b9bb86
--- /dev/null
+++ b/scripts/sphinx-pre-install
@@ -0,0 +1,321 @@
+#!/usr/bin/perl
+use strict;
+
+#############
+# Static vars
+#############
+
+my @missing;
+my @opt_missing;
+my $system_release;
+my $need = 0;
+my $optional = 0;
+my $need_symlink = 0;
+my $install = "";
+
+##################################################
+# Subroutines used at the check missing deps logic
+##################################################
+
+sub catcheck($)
+{
+ my $res = "";
+ $res = qx(cat $_[0]) if (-r $_[0]);
+ return $res;
+}
+
+sub check_missing(%)
+{
+ my %map = %{$_[0]};
+
+ foreach my $prog (@missing) {
+ print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
+ if (defined($map{$prog})) {
+ $install .= " " . $map{$prog};
+ } else {
+ $install .= " " . $prog;
+ }
+ }
+ foreach my $prog (@opt_missing) {
+ print "Warning: better to also install \"$prog\".\n";
+ if (defined($map{$prog})) {
+ $install .= " " . $map{$prog};
+ } else {
+ $install .= " " . $prog;
+ }
+ }
+
+ $install =~ s/^\s//;
+}
+
+sub add_package($$)
+{
+ my $package = shift;
+ my $is_optional = shift;
+
+ if ($is_optional) {
+ push @opt_missing, $package;
+
+ $optional++;
+ } else {
+ push @missing, $package;
+
+ $need++;
+ }
+}
+
+sub check_missing_file($$$)
+{
+ my $file = shift;
+ my $package = shift;
+ my $is_optional = shift;
+
+ return if(-e $file);
+
+ add_package($package, $is_optional);
+}
+
+sub findprog($)
+{
+ foreach(split(/:/, $ENV{PATH})) {
+ return "$_/$_[0]" if(-x "$_/$_[0]");
+ }
+}
+
+sub check_program($$)
+{
+ my $prog = shift;
+ my $is_optional = shift;
+
+ return if findprog($prog);
+
+ 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;
+
+ my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
+ return if ($err == 0);
+ my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
+ return if ($err == 0);
+
+ add_package($prog, $is_optional);
+}
+
+sub check_sphinx()
+{
+ return if findprog("sphinx-build");
+
+ if (findprog("sphinx-build-3")) {
+ $need_symlink = 1;
+ return;
+ }
+
+ add_package("python-sphinx", 0);
+}
+
+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;
+}
+
+sub give_debian_hints()
+{
+ my %map = (
+ "python-sphinx" => "python3-sphinx",
+ "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
+ "dot" => "graphviz",
+ "convert" => "imagemagick",
+ "Pod::Usage" => "perl-modules",
+ "xelatex" => "texlive-xetex",
+ );
+
+ check_missing(\%map);
+
+ return if (!$need && !$optional);
+ printf("You should run:\n\n\tsudo apt-get install $install\n");
+}
+
+sub give_redhat_hints()
+{
+ my %map = (
+ "python-sphinx" => "python3-sphinx",
+ "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
+ "dot" => "graphviz",
+ "convert" => "ImageMagick",
+ "Pod::Usage" => "perl-Pod-Usage",
+ "xelatex" => "texlive-xetex-bin python-sphinx-latex.noarch",
+ );
+
+ check_missing_file("/usr/share/licenses/python-sphinx-latex/LICENSE",
+ "python-sphinx-latex", 1);
+ check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
+ "dejavu-sans-fonts", 1);
+ check_missing_file("/usr/share/fonts/dejavu/DejaVuSerif.ttf",
+ "dejavu-serif-fonts", 1);
+ check_missing_file("/usr/share/fonts/dejavu/DejaVuSansMono.ttf",
+ "dejavu-sans-mono-fonts", 1);
+ check_missing_file("/usr/share/texlive/texmf-dist/tex/latex/adjustbox/adjustbox.sty",
+ "texlive-adjustbox", 1);
+
+ check_missing(\%map);
+
+ return if (!$need && !$optional);
+ printf("You should run:\n\n\tyum install -y $install\n");
+}
+
+sub give_opensuse_hints()
+{
+ my %map = (
+ # FIXME: add hints for SUSE here
+ );
+
+ check_missing(\%map);
+
+ return if (!$need && !$optional);
+ printf("You should run:\n\n\tsudo zypper install $install\n");
+}
+
+sub give_arch_linux_hints()
+{
+ my %map = (
+ # FIXME: add hints for arch-linux here
+ );
+
+ check_missing(\%map);
+
+ return if (!$need && !$optional);
+ printf("You should install those package(s) (repository/package): $install\n");
+}
+
+sub give_gentoo_hints()
+{
+ my %map = (
+ # FIXME: add hints for Gentoo here
+ );
+
+ check_missing(\%map);
+
+ return if (!$need && !$optional);
+ printf("You should emerge those package(s): $install\n");
+}
+
+sub check_distros()
+{
+ # Distro-specific hints
+ if ($system_release =~ /Red Hat Enterprise Linux/) {
+ give_redhat_hints;
+ return;
+ }
+ 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;
+ }
+ if ($system_release =~ /Arch Linux/) {
+ give_arch_linux_hints;
+ return;
+ }
+ if ($system_release =~ /Gentoo/) {
+ give_gentoo_hints;
+ return;
+ }
+ # Fall-back to generic hint code
+ my %map = (
+ "sphinx-build" => "sphinx"
+ );
+ check_missing(\%map);
+ print "I don't know distro $system_release. So, I can't provide you a hint with the install procedure.\n";
+}
+
+sub check_needs()
+{
+ if ($system_release) {
+ print "Checking if the needed tools for $system_release are available\n";
+ } else {
+ print "Checking if the needed tools are present\n";
+ }
+
+ # Check for needed programs/tools
+ check_sphinx();
+ check_python_module("sphinx_rtd_theme", 0);
+ check_perl_module("Pod::Usage", 0);
+ check_program("make", 0);
+ check_program("gcc", 0);
+ check_program("dot", 1);
+ check_program("convert", 1);
+ check_program("xelatex", 1);
+
+ check_distros();
+
+ if ($need_symlink) {
+ printf "\tln -sf %s /usr/bin/sphinx-build\n\n",
+ which("sphinx-build-3");
+ }
+ printf "\n";
+
+ print "All optional dependenties are met.\n" if (!$optional);
+
+ 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.\n";
+}
+
+######
+# Main
+######
+
+# 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;
+$system_release = catcheck("/etc/issue") if !$system_release;
+$system_release =~ s/\s+$//;
+
+check_needs;
--
2.13.0