diff --git a/watch_script_params.py b/watch_script_params.py new file mode 100755 index 0000000..5d2e20e --- /dev/null +++ b/watch_script_params.py @@ -0,0 +1,63 @@ +import os +import time +import shutil +from datetime import datetime, date +import hashlib +import subprocess + +# File paths +SOURCE_FILE = "/home/pi/Conjurer/script.params" +BACKUP_DIR = "/home/pi/Conjurer" +GIT_REPO_DIR = "/home/pi/conjurer/conjurer_musician" +LAST_HASH_FILE = "/home/pi/Conjurer/.last_hash" +LAST_GIT_COMMIT_FILE = "/home/pi/Conjurer/.last_git_commit" + +def compute_file_hash(filepath): + with open(filepath, 'rb') as f: + return hashlib.sha256(f.read()).hexdigest() + +def backup_file(): + timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') + backup_path = os.path.join(BACKUP_DIR, f"{timestamp}_script.params") + shutil.copy2(SOURCE_FILE, backup_path) + +def commit_to_git(): + try: + subprocess.run(["cp", SOURCE_FILE, os.path.join(GIT_REPO_DIR, "script.params")], check=True) + subprocess.run(["git", "-C", GIT_REPO_DIR, "add", "script.params"], check=True) + subprocess.run(["git", "-C", GIT_REPO_DIR, "commit", "-m", f"Daily update: {datetime.now()}"], check=True) + subprocess.run(["git", "-C", GIT_REPO_DIR, "push"], check=True) + except subprocess.CalledProcessError as e: + print(f"Git operation failed: {e}") + +def main(): + if not os.path.exists(SOURCE_FILE): + return + + current_hash = compute_file_hash(SOURCE_FILE) + + # Detect change + last_hash = None + if os.path.exists(LAST_HASH_FILE): + with open(LAST_HASH_FILE, 'r') as f: + last_hash = f.read().strip() + + if current_hash != last_hash: + backup_file() + with open(LAST_HASH_FILE, 'w') as f: + f.write(current_hash) + + # Daily git commit + today = str(date.today()) + last_commit_date = "" + if os.path.exists(LAST_GIT_COMMIT_FILE): + with open(LAST_GIT_COMMIT_FILE, 'r') as f: + last_commit_date = f.read().strip() + + if today != last_commit_date: + commit_to_git() + with open(LAST_GIT_COMMIT_FILE, 'w') as f: + f.write(today) + +if __name__ == "__main__": + main()