|
1 | | -#!/bin/bash -x |
| 1 | +#!/bin/bash |
2 | 2 |
|
3 | | -JYTHON_VERSION="2.7.0" |
4 | | -JYTHON_HOME="${OPENHAB_CONF}/automation/jython" |
| 3 | +## Jython Version |
| 4 | +# The Jython verion to install when using the default download location. |
| 5 | +# |
| 6 | +# For example: |
| 7 | +# JYTHON_VERSION="2.7.1" |
| 8 | +# |
| 9 | +# Leave empty to use the default version (2.7.0) |
| 10 | +JYTHON_VERSION= |
| 11 | + |
| 12 | + |
| 13 | +## Additional Python Modules |
| 14 | +# List of additional python module(s) that need to be installed. |
| 15 | +# |
| 16 | +# NOTE: Due to bugs in the Jython 2.7.0 and 2.7.1 installer, pip is only installed when |
| 17 | +# using a Debian based image on x86 architecture. When using an Alpine based image or |
| 18 | +# when using ARM (e.g. Raspberry Pi) architecture pip will not be installed and this |
| 19 | +# parameter is ignored. |
| 20 | +# |
| 21 | +# For example: |
| 22 | +# PYPI_MODULES="requests python-dateutil" |
| 23 | +# |
| 24 | +# Leave empty when no additional python module need to be installed. |
| 25 | +PYPI_MODULES= |
| 26 | + |
| 27 | + |
| 28 | +## Jython Installer Download Location |
| 29 | +# The location from where to download the Jython Installer. |
| 30 | +# |
| 31 | +# For example: |
| 32 | +# JYTHON_INSTALLER_URI=http://www.example.com/jython-installer-2.7.0.jar |
| 33 | +# |
| 34 | +# Or file location inside the container: |
| 35 | +# JYTHON_INSTALLER_URI=/openhab/jython-installer/jython-installer-2.7.0.jar |
| 36 | +# |
| 37 | +# Leave empty to use the default location. |
| 38 | +JYTHON_INSTALLER_URI= |
| 39 | + |
| 40 | + |
| 41 | +## Jython Installer Options |
| 42 | +# Specifiy the command line options that will be used when running the jython-installer.jar. |
| 43 | +# |
| 44 | +# For example: |
| 45 | +# JYTHON_INSTALLER_OPTIONS="-t standard -e demo doc src ensurepip" |
| 46 | +# |
| 47 | +# Leave empty to use the default options. |
| 48 | +JYTHON_INSTALLER_OPTIONS= |
5 | 49 |
|
6 | | -export EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Xbootclasspath/a:${JYTHON_HOME}/jython.jar -Dpython.home=${JYTHON_HOME} -Dpython.path=${JYTHON_HOME}/Lib:${OPENHAB_CONF}/automation/lib/python" |
7 | 50 |
|
8 | 51 |
|
| 52 | +#################################################################################### |
| 53 | +# End of configuration |
| 54 | +#################################################################################### |
| 55 | + |
| 56 | +IFS=" " |
| 57 | +set +e |
| 58 | + |
| 59 | +function beginswith() { case $2 in "$1"*) true;; *) false;; esac; } |
| 60 | +function contains() { case $2 in *"$1"*) true;; *) false;; esac; } |
| 61 | + |
| 62 | +function check_addons_config() { |
| 63 | + # Check if the Next Generation Rule Engine is enabled |
| 64 | + MISC_LINE=$(grep '^[[:space:]]\?misc' $1) |
| 65 | + if [ $? -eq 0 ]; then |
| 66 | + # ensure we have ruleengine enabled |
| 67 | + if [[ ${MISC_LINE} == *"ruleengine"* ]]; then |
| 68 | + return 0 |
| 69 | + fi |
| 70 | + fi |
| 71 | + |
| 72 | + return 1 |
| 73 | +} |
| 74 | + |
| 75 | +function check_next_generation_rule_engine() { |
| 76 | + |
| 77 | + if check_addons_config "${OPENHAB_CONF}/services/addons.cfg"; then |
| 78 | + echo "New rule engine is already enabled in the addons.cfg." |
| 79 | + elif check_addons_config "$OPENHAB_USERDATA/config/org/openhab/addons.config"; then |
| 80 | + echo "New rule engine is already enabled in the addons.config." |
| 81 | + else |
| 82 | + echo "Please enable the Next Generation Rule Engine." |
| 83 | + echo "See https://www.openhab.org/docs/configuration/rules-ng.html" |
| 84 | + fi |
| 85 | +} |
| 86 | + |
9 | 87 | function cleanup_compiled_classes() { |
10 | 88 | # Clears Jython's compiled classes |
11 | 89 | if [ -d "${OPENHAB_CONF}/automation/lib/python" ]; then |
12 | | - find ${OPENHAB_CONF}/automation/lib/python -name '*\$py.class' -delete |
| 90 | + find "${OPENHAB_CONF}/automation/lib/python" -name '*\$py.class' -delete |
13 | 91 | fi |
14 | 92 | if [ -d "${JYTHON_HOME}/Lib" ]; then |
15 | | - find ${JYTHON_HOME}/Lib -name '*\$py.class' -delete |
| 93 | + find "${JYTHON_HOME}/Lib" -name '*\$py.class' -delete |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +function create_directory() { |
| 98 | + local DIRECTORY=$1 |
| 99 | + if [ ! -d "${DIRECTORY}" ]; then |
| 100 | + mkdir -p "${DIRECTORY}" |
| 101 | + if [ $? -ne 0 ]; then |
| 102 | + echo "ERROR: Could not create directory ${DIRECTORY}." |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | + else |
| 106 | + echo "Directory ${DIRECTORY} already exists." |
16 | 107 | fi |
17 | 108 | } |
18 | 109 |
|
19 | 110 | function download_jython_libraries() { |
20 | | - # Download and install Jython |
21 | | - if [ ! -d "${JYTHON_HOME}" ]; then |
22 | | - mkdir -p "${JYTHON_HOME}" |
23 | | - wget -nv -O /tmp/jython-installer.jar http://central.maven.org/maven2/org/python/jython-installer/${JYTHON_VERSION}/jython-installer-${JYTHON_VERSION}.jar |
24 | | - java -jar /tmp/jython-installer.jar -s -d ${JYTHON_HOME}/ -t standard -e demo doc src |
25 | | - rm /tmp/jython-installer.jar |
26 | | - # chmod -R o+w ${JYTHON_HOME} |
27 | | - chown -R openhab:openhab ${JYTHON_HOME} |
| 111 | + # Download or copy Jython installer |
| 112 | + if [ -z "${JYTHON_INSTALLER_URI}" ]; then |
| 113 | + JYTHON_INSTALLER_URI=${JYTHON_DEFAULT_INSTALLER_URI} |
| 114 | + fi |
| 115 | + |
| 116 | + if beginswith http "${JYTHON_INSTALLER_URI}"; then |
| 117 | + wget -nv -O /tmp/jython-installer.jar "${JYTHON_INSTALLER_URI}" |
| 118 | + if [ $? -ne 0 ]; then |
| 119 | + echo "ERROR: Failed to download the helper libraries form ${JYTHON_INSTALLER_URI}" |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | + else |
| 123 | + cp "${JYTHON_INSTALLER_URI}" /tmp/jython-installer.jar |
| 124 | + if [ $? -ne 0 ]; then |
| 125 | + echo "ERROR: Failed to copy the helper libraries form ${JYTHON_INSTALLER_URI} to /tmp/jython-installer.jar" |
| 126 | + exit 1 |
| 127 | + fi |
28 | 128 | fi |
29 | 129 | } |
30 | 130 |
|
31 | | -function enable_next_generation_rule_engine() { |
32 | | - # Enable the Next Generation Rule Engine |
33 | | - set +e |
34 | | - MISC_LINE=$(grep '^[[:space:]]\?misc' ${OPENHAB_CONF}/services/addons.cfg) |
35 | | - if [ $? -eq 0 ]; then |
36 | | - # ensure we have ruleengine enabled |
37 | | - if [[ ${MISC_LINE} == *"ruleengine"* ]]; then |
38 | | - echo "New rule engine is already included in the addons.cfg" |
| 131 | +function install_additional_python_modules() { |
| 132 | + if [ ! -z "${PYPI_MODULES}" ]; then |
| 133 | + if is_pip_installed; then |
| 134 | + "${JYTHON_HOME}/bin/jython" -m pip install ${PYPI_MODULES} |
| 135 | + chown -R openhab:openhab "${JYTHON_HOME}/Lib" |
| 136 | + if [ $? -ne 0 ]; then |
| 137 | + echo "ERROR: Failed to install additional python packages." |
| 138 | + exit 1 |
| 139 | + fi |
39 | 140 | else |
40 | | - sed -i 's/misc\s\?=\s\?/misc = ruleengine,/' ${OPENHAB_CONF}/services/addons.cfg |
| 141 | + echo "ERROR: Cannot install additional Python modules because pip is not installed." |
41 | 142 | fi |
| 143 | + fi |
| 144 | +} |
| 145 | + |
| 146 | +function install_jython_libraries() { |
| 147 | + create_directory "${JYTHON_HOME}" |
| 148 | + |
| 149 | + if [ -z ${JYTHON_INSTALLER_OPTIONS} ]; then |
| 150 | + JYTHON_INSTALLER_OPTIONS=${JYTHON_DEFAULT_INSTALLER_OPTIONS} |
| 151 | + if ! is_distro_pip_compatible; then |
| 152 | + echo "The distro and/or platform architecture is not compatible with pip!" |
| 153 | + echo "Pip will not be installed." |
| 154 | + JYTHON_INSTALLER_OPTIONS="${JYTHON_INSTALLER_OPTIONS} ensurepip" |
| 155 | + fi |
| 156 | + fi |
| 157 | + |
| 158 | + java -jar /tmp/jython-installer.jar -s -d "${JYTHON_HOME}/" ${JYTHON_INSTALLER_OPTIONS} |
| 159 | + rm /tmp/jython-installer.jar |
| 160 | + chown -R openhab:openhab "${JYTHON_HOME}" |
| 161 | +} |
| 162 | + |
| 163 | +function is_distro_pip_compatible() { |
| 164 | + if [ "${ARCH}" != "x86_64" ] || contains alpline "${DOCKER_REPO}"; then |
| 165 | + return 1 |
42 | 166 | else |
43 | | - # Just append last line |
44 | | - echo "Append 'misc = ruleengine' to ${OPENHAB_CONF}/services/addons.cfg" |
45 | | - echo "misc = ruleengine" >> ${OPENHAB_CONF}/services/addons.cfg |
| 167 | + return 0 |
46 | 168 | fi |
47 | 169 | } |
48 | 170 |
|
| 171 | +function is_pip_installed() { |
| 172 | + if [ -e "${JYTHON_HOME}/bin/pip" ]; then |
| 173 | + return 0 |
| 174 | + else |
| 175 | + return 1 |
| 176 | + fi |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +if [ -z ${JYTHON_VERSION} ]; then |
| 181 | + JYTHON_VERSION="2.7.0" |
| 182 | +fi |
49 | 183 |
|
50 | | -download_jython_libraries |
| 184 | +JYTHON_DEFAULT_INSTALLER_URI=http://central.maven.org/maven2/org/python/jython-installer/${JYTHON_VERSION}/jython-installer-${JYTHON_VERSION}.jar |
| 185 | +JYTHON_DEFAULT_INSTALLER_OPTIONS="-t standard -e demo doc src" |
| 186 | + |
| 187 | +JYTHON_HOME="${OPENHAB_CONF}/automation/jython" |
| 188 | +export EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Xbootclasspath/a:${JYTHON_HOME}/jython.jar -Dpython.home=${JYTHON_HOME} -Dpython.path=${JYTHON_HOME}/Lib:${OPENHAB_CONF}/automation/lib/python" |
| 189 | + |
| 190 | + |
| 191 | +if [ ! -d "${JYTHON_HOME}" ]; then |
| 192 | + download_jython_libraries |
| 193 | + install_jython_libraries |
| 194 | +fi |
| 195 | + |
| 196 | +check_next_generation_rule_engine |
| 197 | +install_additional_python_modules |
51 | 198 | cleanup_compiled_classes |
52 | | -enable_next_generation_rule_engine |
| 199 | + |
0 commit comments