From c0de346bdf96d9dd192b90a80e0de0416dd96fa7 Mon Sep 17 00:00:00 2001 From: Davte Date: Thu, 18 Jul 2019 20:24:41 +0200 Subject: [PATCH] Bash scripts to configure develop environment and run main script --- install.sh | 25 +++++++++++++++++++++++ run_me.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100755 install.sh create mode 100755 run_me.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..9051c71 --- /dev/null +++ b/install.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Get current directory +this_script_directory=$(cd `dirname $0` && pwd); +cd "$this_script_directory"; +configuration_file="$this_script_directory/my_config.sh"; +passwords_file="$this_script_directory/ciclopibot/data/passwords.py"; + +# Create intermediate path for passwords_file, if it does not exist +mkdir -p "$this_script_directory/ciclopibot/data/"; + +read -p "Enter a name for your virtual environment + " venv_name; +python3 -m venv "$venv_name"; +"$venv_name"/bin/pip install -r "$this_script_directory/requirements.txt"; + +# Please note that this will work only in a Unix-like operating system. +# Other systems may require a different path. +echo "python_virtual_environment=\"$(pwd)/$venv_name/bin\";" >> "$configuration_file"; +echo "python_script=\"$this_script_directory/ciclopibot/bot.py\";" >> "$configuration_file"; +read -p "Enter a valid Telegram bot token " bot_token; +echo "bot_token = \"$bot_token\"" >> $passwords_file; + +# Run bot +bash run_me.sh; diff --git a/run_me.sh b/run_me.sh new file mode 100755 index 0000000..4633294 --- /dev/null +++ b/run_me.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# This file must be executable, otherwise the service cannot run it. + +# Run `$python_script` while exit code is 65. +# At each iteration, pull news from repository and update dependencies. + +# Get current directory +this_script_directory=$(cd `dirname $0` && pwd) + +# Import variables from my_config.sh +# `$python_virtual_environment`: python virtual environment directory +# containing `python` and `pip` +# `$python_script`: complete path to python script to be run +source $this_script_directory/my_config.sh; + +# Ensure the success of importing procedure: +## variables of interest should be non-zero (`-z`) +if [ -z ${python_virtual_environment} ]; +then + printf "Please set in \"my_config.sh\" the path to bot python \ +virtual environment\n\ +\n\ +Example:\n\ +python_virtual_environment=\"path/to/virtual/env\"\n\ +"; + exit; +fi +if [ -z ${python_script} ]; +then + printf "Please set in \"my_config.sh\" the path to python script to be run \ +\n\ +Example:\n\ +python_script=\"path/to/python/script.py\"\n\ +"; + exit; +elif [ ! -e ${python_script} ]; +then + printf "File $python_script does not exist\n\ +Please point to a valid file.\n\ +Example:\n\ +python_script=\"path/to/python/script.py\"\n\ +"; +fi + +echo "Python script will be run while it exits with value===65."; +i=65; +while [ $i -eq 65 ] +do + echo "Pulling from repository..." + git pull; + echo "Updating dependencies"; + $python_virtual_environment/pip install --upgrade --no-cache-dir \ + --no-deps davtelepot; + echo "Running python script"; + $python_virtual_environment/python $python_script; + i=$?; +done