Skip to content

Commit

Permalink
improve updater so new python modules are auto installed (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjelev authored Dec 4, 2024
1 parent 187198d commit b9cb6db
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import os
import subprocess
import config
import sys


def safe_literal_eval(node):
try:
return ast.literal_eval(node)
except ValueError:
return None



def get_assignments(filename):
with open(filename) as f:
tree = ast.parse(f.read(), filename)
Expand Down Expand Up @@ -54,6 +57,7 @@ def display_config_differences(current_config, example_config, display=True):
else:
return False


def check_git_version_remote(script_dir):
full_cmd = "/usr/bin/git -C {} ls-remote --tags origin | awk -F'/' '{{print $3}}' | sort -V | tail -n 1".format(script_dir)
try:
Expand All @@ -79,13 +83,28 @@ def update_config_version(version, script_dir):
f.write(line)


def install_requirements(script_dir):
main_dir_path = os.path.dirname(script_dir)
requirements_path = os.path.join(main_dir_path, 'requirements.txt')
activate_script = os.path.join(main_dir_path, 'rpi_mon_env' ,'bin', 'activate')
command = f"source {activate_script} && pip install -q -r {requirements_path}"

try:
subprocess.run(command, shell=True, check=True, executable='/bin/bash')
print("Requirements installed successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred while installing requirements: {e}")
sys.exit(1)


def do_update(script_dir, version=config.version, git_update=True, config_update=True):
print("Current version: {}".format(config.version))
if git_update:
print(":: Updating git repository", script_dir)
result = subprocess.run(['/usr/bin/git', '-C', script_dir, 'pull'], check=True, universal_newlines=True, stdout=subprocess.PIPE)
print(result.stdout)

install_requirements(script_dir)

if display_config_differences(script_dir + '/config.py', script_dir + '/config.py.example') and config_update:
print(":: Updating config.py")
update_config(script_dir + '/config.py',script_dir + '/config.py.example')
Expand All @@ -96,4 +115,5 @@ def do_update(script_dir, version=config.version, git_update=True, config_update

if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
do_update(script_dir,check_git_version_remote(script_dir))
do_update(script_dir,check_git_version_remote(script_dir))

0 comments on commit b9cb6db

Please sign in to comment.