-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
266 lines (210 loc) · 7.99 KB
/
Copy pathmonitor.py
File metadata and controls
266 lines (210 loc) · 7.99 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
"""Grid folder monitor and auto-restore service.
Detects when Steam has wiped custom grid images and automatically
restores from the latest backup. Integrates with systemd user services.
"""
import json
import logging
import os
import subprocess
from pathlib import Path
from typing import Optional
logger = logging.getLogger(__name__)
SERVICE_NAME = "sgm-monitor"
SERVICE_DIR = Path.home() / '.config' / 'systemd' / 'user'
# Where to find sgm.py - resolve relative to this module
SGM_DIR = Path(__file__).resolve().parent
def _get_service_content() -> str:
"""Generate systemd service unit content."""
sgm_py = SGM_DIR / 'sgm.py'
return f"""[Unit]
Description=SteamGrid Manager - Grid Folder Monitor
Documentation=https://github.com/steamgriddb/sgm
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 {sgm_py} monitor run
"""
def _get_timer_content() -> str:
"""Generate systemd timer unit content."""
return f"""[Unit]
Description=SteamGrid Manager - Periodic Grid Check
Documentation=https://github.com/steamgriddb/sgm
[Timer]
OnBootSec=2min
OnUnitActiveSec=30min
Persistent=true
[Install]
WantedBy=timers.target
"""
def install_monitor() -> int:
"""Install the systemd user service and timer.
Returns:
Exit code (0 for success).
"""
SERVICE_DIR.mkdir(parents=True, exist_ok=True)
service_file = SERVICE_DIR / f"{SERVICE_NAME}.service"
timer_file = SERVICE_DIR / f"{SERVICE_NAME}.timer"
# Write service unit
with open(service_file, 'w', encoding='utf-8') as f:
f.write(_get_service_content())
print(f" Created {service_file}")
# Write timer unit
with open(timer_file, 'w', encoding='utf-8') as f:
f.write(_get_timer_content())
print(f" Created {timer_file}")
# Reload systemd and enable timer
try:
subprocess.run(
['systemctl', '--user', 'daemon-reload'],
check=True, capture_output=True, text=True,
)
subprocess.run(
['systemctl', '--user', 'enable', '--now', f'{SERVICE_NAME}.timer'],
check=True, capture_output=True, text=True,
)
print(f"\n [OK] Monitor installed and started!")
print(f" Checks every 30 minutes after boot")
print(f" Run 'sgm monitor status' to check service state")
except subprocess.CalledProcessError as e:
print(f" Warning: Could not enable service: {e.stderr}")
print(f" You may need to run: systemctl --user enable --now {SERVICE_NAME}.timer")
return 1
except FileNotFoundError:
print(f" Warning: systemctl not found. You may need to start the timer manually.")
return 1
return 0
def uninstall_monitor() -> int:
"""Remove the systemd user service and timer.
Returns:
Exit code (0 for success).
"""
# Stop and disable
try:
subprocess.run(
['systemctl', '--user', 'disable', '--now', f'{SERVICE_NAME}.timer'],
capture_output=True, text=True,
)
subprocess.run(
['systemctl', '--user', 'daemon-reload'],
capture_output=True, text=True,
)
except FileNotFoundError:
pass
# Remove files
service_file = SERVICE_DIR / f"{SERVICE_NAME}.service"
timer_file = SERVICE_DIR / f"{SERVICE_NAME}.timer"
for f in [service_file, timer_file]:
if f.exists():
f.unlink()
print(f" Removed {f}")
print(f"\n [OK] Monitor uninstalled.")
return 0
def is_monitor_installed() -> bool:
"""Check if the monitor service is installed and enabled."""
try:
result = subprocess.run(
['systemctl', '--user', 'is-enabled', f'{SERVICE_NAME}.timer'],
capture_output=True, text=True,
)
return result.returncode == 0
except FileNotFoundError:
return False
def monitor_status() -> int:
"""Show monitor service status.
Returns:
Exit code (0 for success).
"""
print(f"\n Monitor Status\n")
# Check if installed
service_file = SERVICE_DIR / f"{SERVICE_NAME}.service"
timer_file = SERVICE_DIR / f"{SERVICE_NAME}.timer"
print(f" Service file: {'exists' if service_file.exists() else 'NOT FOUND'}")
print(f" Timer file: {'exists' if timer_file.exists() else 'NOT FOUND'}")
# Check systemd status
try:
result = subprocess.run(
['systemctl', '--user', 'status', f'{SERVICE_NAME}.timer'],
capture_output=True, text=True,
)
if result.returncode == 0:
# Parse key info
for line in result.stdout.split('\n'):
line = line.strip()
if any(x in line for x in ['Active:', 'Trigger:', 'Loaded:']):
print(f" {line}")
else:
print(f" Timer: Not active")
except FileNotFoundError:
print(f" systemctl: Not available")
print()
return 0
def run_monitor_check() -> int:
"""Run a single monitoring check.
Compares current grid state against the last known state.
If a significant number of images are missing, auto-restores.
Returns:
Exit code (0 for success).
"""
from config import config_exists, get_resolved_config, DATA_DIR
from steam import find_grid_path
from backup import get_grid_state, load_state, save_state, list_backups, restore_backup
logger.info("Running monitor check...")
if not config_exists():
logger.error("No config found. Run 'sgm config init' first.")
return 1
config = get_resolved_config()
steam_path = Path(config['steam_path'])
user_id = config.get('steam_user_id', 'auto')
backup_path = Path(config.get('backup_path', ''))
threshold = config.get('auto_restore_threshold', 0.5)
auto_restore = config.get('auto_restore', True)
try:
grid_path = find_grid_path(steam_path, user_id if user_id != 'auto' else None)
except FileNotFoundError:
logger.error("Grid folder not found")
return 1
# Get current state
current = get_grid_state(grid_path)
state_file = DATA_DIR / 'state.json'
# Load last known state
last_state = load_state(state_file)
if last_state is None:
# First run - save current state and exit
logger.info(f"First run. Saving baseline state: {current['file_count']} files")
save_state(current, state_file)
return 0
last_count = last_state.get('file_count', 0)
current_count = current['file_count']
# Check for wipe
if last_count == 0:
logger.info("No previous files recorded. Updating state.")
save_state(current, state_file)
return 0
ratio = current_count / last_count if last_count > 0 else 1.0
logger.info(f"Grid check: {current_count}/{last_count} files (ratio: {ratio:0.2f})")
if ratio < threshold:
logger.warning(
f"Grid images appear wiped! "
f"({current_count}/{last_count} files, ratio {ratio:0.2f} < threshold {threshold})"
)
if not auto_restore:
logger.info("Auto-restore disabled. Run 'sgm restore' manually.")
return 0
# Check for available backups
backups = list_backups(backup_path)
if not backups:
logger.error("No backups available for auto-restore!")
return 1
logger.info(f"Auto-restoring from backup: {backups[0]['timestamp']}")
result = restore_backup(grid_path, backup_path, dry_run=False, force=True)
if result == 0:
# Update state after successful restore
new_state = get_grid_state(grid_path)
save_state(new_state, state_file)
logger.info(f"Auto-restore complete. {new_state['file_count']} files restored.")
return result
else:
# Update state
save_state(current, state_file)
logger.info("Grid images OK. State updated.")
return 0