From 63772f5e39d057165de1cb6520258bf5ee9cb8dd Mon Sep 17 00:00:00 2001 From: Hristo Date: Sun, 28 Jan 2024 01:01:32 +0200 Subject: [PATCH] add update.py that can update the repo and config.py based on new variables in config.py.example --- src/config.py.example | 2 +- src/update.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/update.py diff --git a/src/config.py.example b/src/config.py.example index d5e9607..191a334 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -51,4 +51,4 @@ wifi_signal = False wifi_signal_dbm = False # this works only on raspbery pi version 5 with stock fan -rpi5_fan_speed = False +rpi5_fan_speed = False \ No newline at end of file diff --git a/src/update.py b/src/update.py new file mode 100644 index 0000000..a90f7d3 --- /dev/null +++ b/src/update.py @@ -0,0 +1,50 @@ +import ast +import os +import subprocess + + +script_dir = os.path.dirname(os.path.realpath(__file__)) +os.chdir(script_dir) + +def get_assignments(filename): + with open(filename) as f: + tree = ast.parse(f.read(), filename) + + assignments = {node.targets[0].id: ast.literal_eval(node.value) for node in ast.walk(tree) if isinstance(node, ast.Assign)} + return assignments + +def update_config(current_config, example_config): + current_assignments = get_assignments(current_config) + example_assignments = get_assignments(example_config) + + missing_assignments = {var: value for var, value in example_assignments.items() if var not in current_assignments} + + if missing_assignments: + with open(current_config, 'a') as f: + for var, value in missing_assignments.items(): + f.write(f'\n{var} = {value!r}') + +def display_config_differences(current_config, example_config): + current_assignments = get_assignments(current_config) + example_assignments = get_assignments(example_config) + + missing_assignments = {var: value for var, value in example_assignments.items() if var not in current_assignments} + + if missing_assignments: + print("Missing variables:") + for var, value in missing_assignments.items(): + print(f'{var} = {value!r}') + return True + else: + return False + +repo_path = os.path.dirname(os.path.realpath(__file__)) + +result = subprocess.run(['git', '-C', repo_path, 'pull'], check=True, text=True, capture_output=True) +print(result.stdout) + +if display_config_differences('config.py', 'config.py.example'): + print("Updating config.py") + update_config('config.py', 'config.py.example') +else: + print("No config.py updates needed") \ No newline at end of file