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