This repository was archived by the owner on Jan 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdater.py
More file actions
110 lines (82 loc) · 3.19 KB
/
updater.py
File metadata and controls
110 lines (82 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import time
import subprocess
from dotenv import load_dotenv
from functools import wraps
# Load environment variables from .env file
load_dotenv()
# Get the repository directory from the environment variable
REPO_DIR = os.getenv("REPO_DIR")
if not REPO_DIR:
raise ValueError("REPO_DIR not set in .env file.")
# Constants
MAX_RETRIES = 5
RETRY_DELAY = 300 # 5 minutes in seconds
def retry_on_failure(max_retries=MAX_RETRIES):
"""Decorator to retry a function on failure."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for retries in range(max_retries):
try:
return func(*args, **kwargs)
except subprocess.CalledProcessError as e:
print(
f"Attempt {retries + 1}: An error occurred while executing {func.__name__}: {e}"
)
time.sleep(RETRY_DELAY) # Wait before retrying
print(f"Reached maximum retries for {func.__name__}.")
return None # Indicate failure
return wrapper
return decorator
@retry_on_failure()
def fetch_updates():
"""Fetch updates from the remote repository."""
subprocess.run(["git", "fetch", "origin"], check=True)
@retry_on_failure()
def check_commits():
"""Check the current local and remote commits."""
local_commit = subprocess.run(
["git", "rev-parse", "HEAD"], check=True, capture_output=True, text=True
)
remote_commit = subprocess.run(
["git", "rev-parse", "@{u}"], check=True, capture_output=True, text=True
)
return local_commit.stdout.strip(), remote_commit.stdout.strip()
@retry_on_failure()
def pull_changes():
"""Pull changes from the remote repository."""
subprocess.run(["git", "pull", "origin", "main"], check=True)
@retry_on_failure()
def run_main_script():
"""Run the main.py script."""
subprocess.run(["python3", "main.py"], check=True)
def main():
"""Main execution loop to check for updates and run the script."""
while True:
try:
os.chdir(REPO_DIR) # Change to the repository directory
# Fetch the latest changes
fetch_updates()
# Check commits
commits = check_commits()
if commits is None:
break # Exit on failure after retries
local_commit, remote_commit = commits
# Compare local and remote commits
if local_commit == remote_commit:
print("No changes detected.")
time.sleep(RETRY_DELAY) # Wait before checking again
continue # Skip to the next iteration
print("Changes detected. Pulling new changes...")
if pull_changes() is None:
break # Exit on failure after retries
# Run the main.py script
print("Running the new main.py...")
if run_main_script() is None:
break # Exit on failure after retries
except Exception as e:
print(f"An unexpected error occurred in the main loop: {e}")
time.sleep(RETRY_DELAY) # Wait before trying again
if __name__ == "__main__":
main()