Last active
October 11, 2016 14:23
-
-
Save ldjebran/d99d6339db44f5df2b2e7e98b55a09cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Copyright 2016 Djebran Lezzoum All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# create env if not exist and run the python script in the env | |
# env.sh ver target_folder env_target_folder module/script/command python_module_or_script_path | |
# by default target_folder equal ~/bin | |
# by default env_target_folder equal ~/bin/env | |
# module or script are optional | |
# in case we want only to create the environment we omit module or script option | |
PY_MIN="${1}" | |
if [ ! ${PY_MIN} ] | |
then | |
# latest stable | |
PY_MIN="3.5" | |
fi | |
PY_VERSIONS="2.7 3.4 3.5 3.6" | |
# if `list_include_item ${PY_VERSIONS} ${PY_MIN}` | |
if [[ ${PY_VERSIONS} == *"${PY_MIN}"* ]] | |
then | |
echo ">> setup python ${PY_MIN}" | |
else | |
echo ">> python version not supported" | |
exit 1 | |
fi | |
PY_BIN="python${PY_MIN}" | |
TARGET_HOME="${2}" | |
ENV_TARGET_HOME="${3}" | |
if [[ ( ! ${TARGET_HOME} ) || ( ${TARGET_HOME} == "-" ) ]] | |
then | |
if [[ $BINARIES_ROOT ]] | |
then | |
TARGET_HOME=$BINARIES_ROOT | |
else | |
TARGET_HOME="${HOME}/bin" | |
fi | |
else | |
if [ ! -d ${TARGET_HOME} ] | |
then | |
echo "target does not exits: ${TARGET_HOME}" | |
exit 1 | |
fi | |
fi | |
if [[ ( ! ${ENV_TARGET_HOME} ) || ( ${ENV_TARGET_HOME} == "-" ) ]] | |
then | |
if [[ $ENV_BINARIES_ROOT ]] | |
then | |
ENV_TARGET_HOME=$ENV_BINARIES_ROOT | |
else | |
ENV_TARGET_HOME="${HOME}/bin/env" | |
fi | |
else | |
if [ ! -d ${ENV_TARGET_HOME} ] | |
then | |
echo "environment target does not exits: ${ENV_TARGET_HOME}" | |
exit 1 | |
fi | |
fi | |
LOCAL_PYTHON_HOME="${TARGET_HOME}/python/${PY_MIN}" | |
ENV_PYTHON_HOME="${ENV_TARGET_HOME}/python" | |
ENV_LOCAL_PYTHON_HOME="${ENV_PYTHON_HOME}/${PY_MIN}" | |
if [ ! -d ${LOCAL_PYTHON_HOME} ] | |
then | |
echo ">> python binary installation does not exit: ${LOCAL_PYTHON_HOME}" | |
exit 1 | |
fi | |
PYTHON="${LOCAL_PYTHON_HOME}/bin/${PY_BIN}" | |
# export LD_LIBRARY_PATH="${LOCAL_PYTHON_HOME}/lib" | |
if [ ! -d ${ENV_LOCAL_PYTHON_HOME} ] | |
then | |
if [ -f ${PYTHON} ] | |
then | |
if [ ! -d ${ENV_PYTHON_HOME} ] | |
then | |
echo ">> python env installation does not exit create one: ${ENV_PYTHON_HOME}" | |
mkdir -p ${ENV_PYTHON_HOME} | |
fi | |
${PYTHON} -m virtualenv ${ENV_LOCAL_PYTHON_HOME} | |
else | |
echo ">> environment does not exit,and local installation not defined" | |
exit 1 | |
fi | |
fi | |
echo ">> python home: ${LOCAL_PYTHON_HOME}" | |
echo ">> python environment home: ${ENV_LOCAL_PYTHON_HOME}" | |
# deactivate any existing env | |
# deactivate | |
# launch the env | |
SOURCE_CMD="source ${ENV_LOCAL_PYTHON_HOME}/bin/activate" | |
echo ">> activate environment: ${SOURCE_CMD}" | |
${SOURCE_CMD} | |
# python --version | |
# export LD_LIBRARY_PATH="${ENV_LOCAL_PYTHON_HOME}/lib" | |
PY_EXEC="${4}" | |
PY_EXEC_TARGET="${@:5}" | |
# echo "${PY_EXEC}" | |
# echo "${PY_EXEC_TARGET}" | |
ENV_PYTHON_BIN="${ENV_LOCAL_PYTHON_HOME}/bin/${PY_BIN}" | |
CMD="" | |
if [ ${PY_EXEC} ] | |
then | |
if [[ ! ${PY_EXEC_TARGET} ]] | |
then | |
echo ">> target to execute not supplied" | |
exit 1 | |
fi | |
case ${PY_EXEC} in | |
module) | |
CMD="${ENV_PYTHON_BIN} -m ${PY_EXEC_TARGET}" | |
;; | |
script) | |
CMD="${ENV_PYTHON_BIN} ${PY_EXEC_TARGET}" | |
;; | |
command) | |
CMD="${PY_EXEC_TARGET}" | |
;; | |
*) | |
echo ">> arg 4 must be module or script" | |
exit 1 | |
;; | |
esac | |
fi | |
if [[ ${CMD} ]] | |
then | |
echo ">> exec: ${CMD}" | |
${CMD} | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment