1 | #!/usr/bin/env bash |
2 |
|
3 | # Script variables |
4 | PYTHON_MAJOR=3 |
5 | PYTHON_MINOR=6 |
6 | ACCEPTABLE_PYTHON_COMMANDS="python3 python3.10 python3.9 python3.8 python3.7 python3.6 python" |
7 | REQUIRED_PYTHON_MODULES=$(cat requirements.txt) |
8 |
|
9 |
|
10 | # Plumbing |
11 | set -o pipefail |
12 |
|
13 | RESTORE=$(echo -en '\033[0m') |
14 | STANDOUT=$(echo -en '\033[7m') |
15 | RED=$(echo -en '\033[00;31m') |
16 | GREEN=$(echo -en '\033[00;32m') |
17 | YELLOW=$(echo -en '\033[00;33m') |
18 | PURPLE=$(echo -en '\033[00;35m') |
19 | LIGHTGRAY=$(echo -en '\033[00;37m') |
20 | LRED=$(echo -en '\033[01;31m') |
21 | LYELLOW=$(echo -en '\033[01;33m') |
22 | LBLUE=$(echo -en '\033[01;34m') |
23 | LCYAN=$(echo -en '\033[01;36m') |
24 |
|
25 | SCRIPT_SOURCE_DIR=$(dirname -- "${BASH_SOURCE[0]}") |
26 |
|
27 |
|
28 | # Handle argument input |
29 | case $1 in |
30 | h|help|?|-h|-help|-?|--h|--help|--?) |
31 | echo "Usage: ./install.sh [install|uninstall] [<python executable>]" |
32 | exit 0 |
33 | ;; |
34 | install) |
35 | PYTHON_COMMAND=$2 |
36 | ;; |
37 | uninstall) |
38 | UNINSTALL=1 |
39 | PYTHON_COMMAND=$2 |
40 | ;; |
41 | *) |
42 | PYTHON_COMMAND=$1 |
43 | ;; |
44 | esac |
45 |
|
46 |
|
47 | # Report operating mode |
48 | if [ -z $UNINSTALL ]; then |
49 | echo "${YELLOW}Running in ${LYELLOW}INSTALL${YELLOW} mode${RESTORE}" |
50 | else |
51 | echo "${YELLOW}Running in ${LYELLOW}UNINSTALL${YELLOW} mode${RESTORE}" |
52 | REQUIRED_PYTHON_MODULES="pip" |
53 | fi |
54 |
|
55 |
|
56 | # Determine Python interpreter to use |
57 | echo "${LIGHTGRAY}Determining Python interpreter${RESTORE}" |
58 | if [ -z $PYTHON_COMMAND ]; then |
59 | for COMMAND in $ACCEPTABLE_PYTHON_COMMANDS |
60 | do |
61 | if command -v $COMMAND &> /dev/null; then |
62 | PYTHON_COMMAND=$COMMAND |
63 | break |
64 | fi |
65 | done |
66 | fi |
67 |
|
68 | # Expand interpreter command, verify with `import sys` test instruction |
69 | PYTHON_COMMAND=$(command -v $PYTHON_COMMAND) |
70 | if [ -z $PYTHON_COMMAND ]; then |
71 | echo " ${RED}Python interpreter not found${RESTORE}" |
72 | exit 1 |
73 | fi |
74 | if [ -h "$PYTHON_COMMAND" ]; then |
75 | PYTHON_COMMAND=$(readlink -f $PYTHON_COMMAND) |
76 | fi |
77 | echo " Trying interpreter [ ${LYELLOW}$PYTHON_COMMAND${RESTORE} ]" |
78 | if ! $PYTHON_COMMAND -c "import sys"; then |
79 | echo " ${RED}Executable is not Python${RESTORE}" |
80 | exit 1 |
81 | fi |
82 |
|
83 |
|
84 | # Verifying Python version |
85 | echo "${LIGHTGRAY}Checking Python version${RESTORE} [ needs ${LIGHTGRAY}>=$PYTHON_MAJOR.$PYTHON_MINOR${RESTORE} ]" |
86 | PYTHON_VERSION_STRING=$($PYTHON_COMMAND -c "print('.'.join([str(a) for a in __import__('sys').version_info[:3]]))") |
87 | if ! $PYTHON_COMMAND -c "import sys;exit(not(sys.version_info.major==$PYTHON_MAJOR and sys.version_info.minor>=$PYTHON_MINOR))"; then |
88 | echo " ${RED}Python version must be ${RESTORE}[ ${LCYAN}>=$PYTHON_MAJOR.$PYTHON_MINOR${RESTORE} ]${RED}."\ |
89 | "Installed is ${RESTORE}[ ${LCYAN}$PYTHON_VERSION_STRING${RESTORE} ]" |
90 | exit 1 |
91 | fi |
92 | echo " Version [ ${LCYAN}$PYTHON_VERSION_STRING${RESTORE} ] acceptable" |
93 |
|
94 |
|
95 | # Verifying required modules are installed |
96 | echo "${LIGHTGRAY}Checking Python modules installed${RESTORE}" |
97 | for MODULE in $REQUIRED_PYTHON_MODULES |
98 | do |
99 | if ! $PYTHON_COMMAND -c "import $MODULE" &> /dev/null; then |
100 | echo " ${RED}Required Python module ${RESTORE}[ ${LBLUE}$MODULE${RESTORE} ] ${RED}not found${RESTORE}" |
101 | exit 1 |
102 | fi |
103 | echo " Module [ ${LBLUE}$MODULE${RESTORE} ] found" |
104 | done |
105 | echo " ${GREEN}All required modules found${RESTORE}" |
106 |
|
107 |
|
108 | # Set pip command based on script mode |
109 | if [ -z $UNINSTALL ]; then |
110 | PIP_COMMAND="$PYTHON_COMMAND -m pip install $SCRIPT_SOURCE_DIR/" |
111 | else |
112 | PIP_COMMAND="$PYTHON_COMMAND -m pip uninstall --yes s3-bsync" |
113 | fi |
114 |
|
115 |
|
116 | # Run pip command and handle output |
117 | function indent() { |
118 | sed 's/^/ /'; |
119 | } |
120 | echo "${LIGHTGRAY}Running [ ${PURPLE}$PIP_COMMAND${LIGHTGRAY} ]${RESTORE}" |
121 | if $PIP_COMMAND 2>&1 | indent; then |
122 | echo "${GREEN}Success${RESTORE}" |
123 | exit 0 |
124 | else |
125 | ERROR_CODE=$? |
126 | echo "${LRED}${STANDOUT}pip did not exit successfully (status code $ERROR_CODE)${RESTORE}" |
127 | exit $ERROR_CODE |
128 | fi |
129 |
|