Permalink
Browse files

python modules installer updated

  • Loading branch information...
1 parent 64656dc commit fa2b4faf58cc8726661f84e8289b5ccb626d40e7 @ktsaou ktsaou committed Jun 22, 2016
Showing with 144 additions and 11 deletions.
  1. +1 −0 build/subst.inc
  2. +5 −0 python.d/Makefile.am
  3. +0 −11 python.d/pip_install.sh
  4. +138 −0 python.d/python-modules-installer.sh.in
View
@@ -3,6 +3,7 @@
-e 's#[@]localstatedir_POST@#$(localstatedir)#g' \
-e 's#[@]sbindir_POST@#$(sbindir)#g' \
-e 's#[@]sysconfdir_POST@#$(sysconfdir)#g' \
+ -e 's#[@]pythondir_POST@#$(pythondir)#g' \
$< > $@.tmp; then \
mv "$@.tmp" "$@"; \
else \
@@ -1,8 +1,13 @@
MAINTAINERCLEANFILES= $(srcdir)/Makefile.in
+include $(top_srcdir)/build/subst.inc
+
+SUFFIXES = .in
+
dist_python_SCRIPTS = \
example.chart.py \
mysql.chart.py \
+ python-modules-installer.sh \
$(NULL)
dist_python_DATA = \
@@ -1,11 +0,0 @@
-#!/bin/bash
-
-# FIXME
-# 1. make sure you are in the proper directory
-# 2. In fedora 24, the python modules names are not known
-
-PIP=`which pip`
-
-${PIP} install --target="python_modules" yaml
-
-${PIP} install --target="python_modules" MySQL-python || echo "You need to install libmysqlclient-dev and python-dev"
@@ -0,0 +1,138 @@
+#!/bin/bash
+
+umask 022
+
+dir="@pythondir_POST@"
+target="${dir}/python_modules"
+pv="$(python -V 2>&1)"
+
+# parse parameters
+while [ ! -z "${1}" ]
+do
+ case "${1}" in
+ -p|--python)
+ pv="Python ${2}"
+ shift 2
+ ;;
+
+ -d|--dir)
+ dir="${2}"
+ target="${dir}/python_modules"
+ echo >&2 "Will install python modules in: '${target}'"
+ shift 2
+ ;;
+
+ -s|--system)
+ target=
+ echo >&2 "Will install python modules system-wide"
+ shift
+ ;;
+
+ -h|--help)
+ echo "${0} [--dir netdata-python.d-path] [--system]"
+ exit 0
+ ;;
+
+ *)
+ echo >&2 "Cannot understand parameter: ${1}"
+ exit 1
+ ;;
+ esac
+done
+
+
+if [ ! -z "${target}" -a ! -d "${target}" ]
+then
+ echo >&2 "Cannot find directory: '${target}'"
+ exit 1
+fi
+
+if [[ "${pv}" =~ ^Python\ 2.* ]]
+then
+ pv=2
+ pip="$(which pip2 2>/dev/null)"
+elif [[ "${pv}" =~ ^Python\ 3.* ]]
+then
+ pv=3
+ pip="$(which pip3 2>/dev/null)"
+else
+ echo >&2 "Cannot detect python version"
+ exit 1
+fi
+
+[ -z "${pip}" ] && pip="$(which pip 2>/dev/null)"
+if [ -z "${pip}" ]
+then
+ echo >& "pip command is required to install python v${pv} modules"
+ exit 1
+fi
+
+echo >&2 "Working for python version ${pv} (pip command: '${pip}')"
+echo >&2 "Installing netdata python modules in: '${target}'"
+
+run() {
+ printf "Running command:\n# "
+ printf "%q " "${@}"
+ printf "\n"
+ "${@}"
+}
+
+# try to install all the python modules given as parameters
+# until the first that succeeds
+failed=""
+installed=""
+errors=0
+pip_install() {
+ local ret x
+
+ echo >&2
+ echo >&2
+ echo >&2 "Installing one of: ${*}"
+
+ for x in "${@}"
+ do
+ echo >&2
+ echo >&2 "attempting to install: ${x}"
+ if [ ! -z "${target}" ]
+ then
+ run "${pip}" install --target "${target}" "${x}"
+ ret=$?
+ else
+ run "${pip}" install "${x}"
+ ret=$?
+ fi
+ [ ${ret} -eq 0 ] && break
+ echo >&2 "failed to install: ${x}"
+ done
+
+ if [ ${ret} -ne 0 ]
+ then
+ echo >&2 "ERROR: could not install any of: ${*}"
+ errors=$(( errors + 1 ))
+ failed="${failed}|${*}"
+ else
+ echo >&2 "SUCCESS: we have: ${x}"
+ installed="${installed} ${x}"
+ fi
+ return ${ret}
+}
+
+if [ "${pv}" = "2" ]
+then
+ pip_install pyyaml yaml
+ pip_install mysqlclient mysql-python pymysql
+else
+ pip_install yaml pyyaml
+ pip_install mysql-python mysqlclient pymysql
+fi
+
+echo >&2
+echo >&2
+if [ ${errors} -ne 0 ]
+then
+ echo >&2 "Failed to install ${errors} modules: ${failed}"
+ exit 1
+else
+ echo >&2 "All done. We have: ${installed}"
+ exit 0
+fi

0 comments on commit fa2b4fa

Please sign in to comment.