style: apply ruff-format to entire codebase

First-time run of ruff-format via pre-commit hook normalises quote
style, trailing commas, and whitespace across 188 Python files.
No logic changes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
James Smith
2026-07-05 14:48:11 +01:00
parent 82e64104fe
commit 96172ca593
189 changed files with 19883 additions and 19552 deletions
+132 -196
View File
@@ -20,14 +20,14 @@ from urllib.request import Request, urlopen
import config
from utils.database import get_setting, set_setting
logger = logging.getLogger('intercept.updater')
logger = logging.getLogger("intercept.updater")
# Cache keys for settings
CACHE_KEY_LAST_CHECK = 'update.last_check'
CACHE_KEY_LATEST_VERSION = 'update.latest_version'
CACHE_KEY_RELEASE_URL = 'update.release_url'
CACHE_KEY_RELEASE_NOTES = 'update.release_notes'
CACHE_KEY_DISMISSED_VERSION = 'update.dismissed_version'
CACHE_KEY_LAST_CHECK = "update.last_check"
CACHE_KEY_LATEST_VERSION = "update.latest_version"
CACHE_KEY_RELEASE_URL = "update.release_url"
CACHE_KEY_RELEASE_NOTES = "update.release_notes"
CACHE_KEY_DISMISSED_VERSION = "update.dismissed_version"
# Default check interval (6 hours in seconds)
DEFAULT_CHECK_INTERVAL = 6 * 60 * 60
@@ -35,18 +35,18 @@ DEFAULT_CHECK_INTERVAL = 6 * 60 * 60
def _get_github_repo() -> str:
"""Get the configured GitHub repository."""
return getattr(config, 'GITHUB_REPO', 'smittix/intercept')
return getattr(config, "GITHUB_REPO", "smittix/intercept")
def _get_check_interval() -> int:
"""Get the configured check interval in seconds."""
hours = getattr(config, 'UPDATE_CHECK_INTERVAL_HOURS', 6)
hours = getattr(config, "UPDATE_CHECK_INTERVAL_HOURS", 6)
return hours * 60 * 60
def _is_update_check_enabled() -> bool:
"""Check if update checking is enabled."""
return getattr(config, 'UPDATE_CHECK_ENABLED', True)
return getattr(config, "UPDATE_CHECK_ENABLED", True)
def _compare_versions(current: str, latest: str) -> int:
@@ -58,14 +58,15 @@ def _compare_versions(current: str, latest: str) -> int:
0 if current == latest
1 if current > latest
"""
def parse_version(v: str) -> tuple:
# Strip 'v' prefix if present
v = v.lstrip('v')
v = v.lstrip("v")
# Split by dots and convert to integers
parts = []
for part in v.split('.'):
for part in v.split("."):
# Handle pre-release suffixes like 2.11.0-beta
match = re.match(r'^(\d+)', part)
match = re.match(r"^(\d+)", part)
if match:
parts.append(int(match.group(1)))
else:
@@ -97,27 +98,24 @@ def _fetch_github_release() -> dict[str, Any] | None:
Dict with release info or None on error
"""
repo = _get_github_repo()
url = f'https://api.github.com/repos/{repo}/releases/latest'
url = f"https://api.github.com/repos/{repo}/releases/latest"
try:
req = Request(url, headers={
'User-Agent': 'Intercept-SIGINT',
'Accept': 'application/vnd.github.v3+json'
})
req = Request(url, headers={"User-Agent": "Intercept-SIGINT", "Accept": "application/vnd.github.v3+json"})
with urlopen(req, timeout=10) as response:
# Check rate limit headers
remaining = response.headers.get('X-RateLimit-Remaining')
remaining = response.headers.get("X-RateLimit-Remaining")
if remaining and int(remaining) < 10:
logger.warning(f"GitHub API rate limit low: {remaining} remaining")
data = json.loads(response.read().decode('utf-8'))
data = json.loads(response.read().decode("utf-8"))
return {
'tag_name': data.get('tag_name', ''),
'html_url': data.get('html_url', ''),
'body': data.get('body', ''),
'published_at': data.get('published_at', ''),
'name': data.get('name', '')
"tag_name": data.get("tag_name", ""),
"html_url": data.get("html_url", ""),
"body": data.get("body", ""),
"published_at": data.get("published_at", ""),
"name": data.get("name", ""),
}
except HTTPError as e:
if e.code == 404:
@@ -148,12 +146,7 @@ def check_for_updates(force: bool = False) -> dict[str, Any]:
Dict with update status information
"""
if not _is_update_check_enabled():
return {
'success': True,
'update_available': False,
'disabled': True,
'message': 'Update checking is disabled'
}
return {"success": True, "update_available": False, "disabled": True, "message": "Update checking is disabled"}
current_version = config.VERSION
@@ -175,16 +168,16 @@ def check_for_updates(force: bool = False) -> dict[str, Any]:
show_notification = update_available and dismissed != cached_version
return {
'success': True,
'checked': True,
'update_available': update_available,
'show_notification': show_notification,
'current_version': current_version,
'latest_version': cached_version,
'release_url': get_setting(CACHE_KEY_RELEASE_URL) or '',
'release_notes': get_setting(CACHE_KEY_RELEASE_NOTES) or '',
'cached': True,
'last_check': datetime.fromtimestamp(last_check_time).isoformat()
"success": True,
"checked": True,
"update_available": update_available,
"show_notification": show_notification,
"current_version": current_version,
"latest_version": cached_version,
"release_url": get_setting(CACHE_KEY_RELEASE_URL) or "",
"release_notes": get_setting(CACHE_KEY_RELEASE_NOTES) or "",
"cached": True,
"last_check": datetime.fromtimestamp(last_check_time).isoformat(),
}
except (ValueError, TypeError):
pass
@@ -198,46 +191,43 @@ def check_for_updates(force: bool = False) -> dict[str, Any]:
if cached_version:
update_available = _compare_versions(current_version, cached_version) < 0
return {
'success': True,
'checked': True,
'update_available': update_available,
'current_version': current_version,
'latest_version': cached_version,
'release_url': get_setting(CACHE_KEY_RELEASE_URL) or '',
'release_notes': get_setting(CACHE_KEY_RELEASE_NOTES) or '',
'cached': True,
'network_error': True
"success": True,
"checked": True,
"update_available": update_available,
"current_version": current_version,
"latest_version": cached_version,
"release_url": get_setting(CACHE_KEY_RELEASE_URL) or "",
"release_notes": get_setting(CACHE_KEY_RELEASE_NOTES) or "",
"cached": True,
"network_error": True,
}
return {
'success': False,
'error': 'Failed to check for updates'
}
return {"success": False, "error": "Failed to check for updates"}
latest_version = release['tag_name'].lstrip('v')
latest_version = release["tag_name"].lstrip("v")
# Update cache
set_setting(CACHE_KEY_LAST_CHECK, str(time.time()))
set_setting(CACHE_KEY_LATEST_VERSION, latest_version)
set_setting(CACHE_KEY_RELEASE_URL, release['html_url'])
set_setting(CACHE_KEY_RELEASE_NOTES, release['body'][:2000] if release['body'] else '')
set_setting(CACHE_KEY_RELEASE_URL, release["html_url"])
set_setting(CACHE_KEY_RELEASE_NOTES, release["body"][:2000] if release["body"] else "")
update_available = _compare_versions(current_version, latest_version) < 0
dismissed = get_setting(CACHE_KEY_DISMISSED_VERSION)
show_notification = update_available and dismissed != latest_version
return {
'success': True,
'checked': True,
'update_available': update_available,
'show_notification': show_notification,
'current_version': current_version,
'latest_version': latest_version,
'release_url': release['html_url'],
'release_notes': release['body'] or '',
'release_name': release['name'] or f'v{latest_version}',
'published_at': release['published_at'],
'cached': False,
'last_check': datetime.now().isoformat()
"success": True,
"checked": True,
"update_available": update_available,
"show_notification": show_notification,
"current_version": current_version,
"latest_version": latest_version,
"release_url": release["html_url"],
"release_notes": release["body"] or "",
"release_name": release["name"] or f"v{latest_version}",
"published_at": release["published_at"],
"cached": False,
"last_check": datetime.now().isoformat(),
}
@@ -254,11 +244,7 @@ def get_update_status() -> dict[str, Any]:
dismissed = get_setting(CACHE_KEY_DISMISSED_VERSION)
if not cached_version:
return {
'success': True,
'checked': False,
'current_version': current_version
}
return {"success": True, "checked": False, "current_version": current_version}
update_available = _compare_versions(current_version, cached_version) < 0
show_notification = update_available and dismissed != cached_version
@@ -269,16 +255,16 @@ def get_update_status() -> dict[str, Any]:
last_check_time = datetime.fromtimestamp(float(last_check)).isoformat()
return {
'success': True,
'checked': True,
'update_available': update_available,
'show_notification': show_notification,
'current_version': current_version,
'latest_version': cached_version,
'release_url': get_setting(CACHE_KEY_RELEASE_URL) or '',
'release_notes': get_setting(CACHE_KEY_RELEASE_NOTES) or '',
'dismissed_version': dismissed,
'last_check': last_check_time
"success": True,
"checked": True,
"update_available": update_available,
"show_notification": show_notification,
"current_version": current_version,
"latest_version": cached_version,
"release_url": get_setting(CACHE_KEY_RELEASE_URL) or "",
"release_notes": get_setting(CACHE_KEY_RELEASE_NOTES) or "",
"dismissed_version": dismissed,
"last_check": last_check_time,
}
@@ -293,23 +279,20 @@ def dismiss_update(version: str) -> dict[str, Any]:
Status dict
"""
set_setting(CACHE_KEY_DISMISSED_VERSION, version)
return {
'success': True,
'dismissed_version': version
}
return {"success": True, "dismissed_version": version}
def _is_git_repo() -> bool:
"""Check if the current directory is a git repository."""
try:
result = subprocess.run(
['git', 'rev-parse', '--is-inside-work-tree'],
["git", "rev-parse", "--is-inside-work-tree"],
capture_output=True,
text=True,
timeout=5,
cwd=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
cwd=os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
)
return result.returncode == 0 and result.stdout.strip() == 'true'
return result.returncode == 0 and result.stdout.strip() == "true"
except Exception:
return False
@@ -321,39 +304,26 @@ def _get_git_status() -> dict[str, Any]:
try:
# Check for uncommitted changes
result = subprocess.run(
['git', 'status', '--porcelain'],
capture_output=True,
text=True,
timeout=10,
cwd=repo_root
["git", "status", "--porcelain"], capture_output=True, text=True, timeout=10, cwd=repo_root
)
has_changes = bool(result.stdout.strip())
changed_files = result.stdout.strip().split('\n') if has_changes else []
changed_files = result.stdout.strip().split("\n") if has_changes else []
# Get current branch
branch_result = subprocess.run(
['git', 'branch', '--show-current'],
capture_output=True,
text=True,
timeout=5,
cwd=repo_root
["git", "branch", "--show-current"], capture_output=True, text=True, timeout=5, cwd=repo_root
)
current_branch = branch_result.stdout.strip() or 'main'
current_branch = branch_result.stdout.strip() or "main"
return {
'has_changes': has_changes,
'changed_files': [f for f in changed_files if f],
'current_branch': current_branch
"has_changes": has_changes,
"changed_files": [f for f in changed_files if f],
"current_branch": current_branch,
}
except Exception as e:
logger.warning(f"Error getting git status: {e}")
return {
'has_changes': False,
'changed_files': [],
'current_branch': 'unknown',
'error': str(e)
}
return {"has_changes": False, "changed_files": [], "current_branch": "unknown", "error": str(e)}
def perform_update(stash_changes: bool = False) -> dict[str, Any]:
@@ -371,115 +341,92 @@ def perform_update(stash_changes: bool = False) -> dict[str, Any]:
# Check if this is a git repo
if not _is_git_repo():
return {
'success': False,
'error': 'Not a git repository',
'manual_update': True,
'message': 'This installation is not using git. Please update manually by downloading the latest release from GitHub.'
"success": False,
"error": "Not a git repository",
"manual_update": True,
"message": "This installation is not using git. Please update manually by downloading the latest release from GitHub.",
}
git_status = _get_git_status()
# Check for local changes
if git_status['has_changes'] and not stash_changes:
if git_status["has_changes"] and not stash_changes:
return {
'success': False,
'error': 'local_changes',
'message': 'You have uncommitted local changes. Either commit them, discard them, or enable "stash changes" to temporarily save them.',
'changed_files': git_status['changed_files']
"success": False,
"error": "local_changes",
"message": 'You have uncommitted local changes. Either commit them, discard them, or enable "stash changes" to temporarily save them.',
"changed_files": git_status["changed_files"],
}
try:
# Stash changes if requested
stashed = False
if stash_changes and git_status['has_changes']:
if stash_changes and git_status["has_changes"]:
stash_result = subprocess.run(
['git', 'stash', 'push', '-m', 'INTERCEPT auto-stash before update'],
["git", "stash", "push", "-m", "INTERCEPT auto-stash before update"],
capture_output=True,
text=True,
timeout=30,
cwd=repo_root
cwd=repo_root,
)
if stash_result.returncode == 0:
stashed = True
logger.info("Stashed local changes before update")
else:
return {
'success': False,
'error': 'Failed to stash changes',
'details': stash_result.stderr
}
return {"success": False, "error": "Failed to stash changes", "details": stash_result.stderr}
# Get current requirements.txt hash to detect changes
req_path = os.path.join(repo_root, 'requirements.txt')
req_path = os.path.join(repo_root, "requirements.txt")
req_hash_before = None
if os.path.exists(req_path):
with open(req_path, 'rb') as f:
with open(req_path, "rb") as f:
import hashlib
req_hash_before = hashlib.md5(f.read()).hexdigest()
# Fetch latest changes
fetch_result = subprocess.run(
['git', 'fetch', 'origin'],
capture_output=True,
text=True,
timeout=60,
cwd=repo_root
["git", "fetch", "origin"], capture_output=True, text=True, timeout=60, cwd=repo_root
)
if fetch_result.returncode != 0:
# Restore stash if we stashed
if stashed:
subprocess.run(['git', 'stash', 'pop'], cwd=repo_root, timeout=30)
return {
'success': False,
'error': 'Failed to fetch updates',
'details': fetch_result.stderr
}
subprocess.run(["git", "stash", "pop"], cwd=repo_root, timeout=30)
return {"success": False, "error": "Failed to fetch updates", "details": fetch_result.stderr}
# Get the main branch name
branch = git_status.get('current_branch', 'main')
branch = git_status.get("current_branch", "main")
# Pull changes
pull_result = subprocess.run(
['git', 'pull', 'origin', branch],
capture_output=True,
text=True,
timeout=120,
cwd=repo_root
["git", "pull", "origin", branch], capture_output=True, text=True, timeout=120, cwd=repo_root
)
if pull_result.returncode != 0:
# Check for merge conflict
if 'CONFLICT' in pull_result.stdout or 'CONFLICT' in pull_result.stderr:
if "CONFLICT" in pull_result.stdout or "CONFLICT" in pull_result.stderr:
# Abort merge
subprocess.run(['git', 'merge', '--abort'], cwd=repo_root, timeout=30)
subprocess.run(["git", "merge", "--abort"], cwd=repo_root, timeout=30)
# Restore stash if we stashed
if stashed:
subprocess.run(['git', 'stash', 'pop'], cwd=repo_root, timeout=30)
subprocess.run(["git", "stash", "pop"], cwd=repo_root, timeout=30)
return {
'success': False,
'error': 'merge_conflict',
'message': 'Merge conflict detected. The update was aborted. Please resolve conflicts manually or reset to a clean state.',
'details': pull_result.stdout + pull_result.stderr
"success": False,
"error": "merge_conflict",
"message": "Merge conflict detected. The update was aborted. Please resolve conflicts manually or reset to a clean state.",
"details": pull_result.stdout + pull_result.stderr,
}
# Restore stash if we stashed
if stashed:
subprocess.run(['git', 'stash', 'pop'], cwd=repo_root, timeout=30)
return {
'success': False,
'error': 'Failed to pull updates',
'details': pull_result.stderr
}
subprocess.run(["git", "stash", "pop"], cwd=repo_root, timeout=30)
return {"success": False, "error": "Failed to pull updates", "details": pull_result.stderr}
# Restore stashed changes
if stashed:
stash_pop_result = subprocess.run(
['git', 'stash', 'pop'],
capture_output=True,
text=True,
timeout=30,
cwd=repo_root
["git", "stash", "pop"], capture_output=True, text=True, timeout=30, cwd=repo_root
)
if stash_pop_result.returncode != 0:
logger.warning(f"Failed to restore stashed changes: {stash_pop_result.stderr}")
@@ -487,47 +434,40 @@ def perform_update(stash_changes: bool = False) -> dict[str, Any]:
# Check if requirements changed
requirements_changed = False
if req_hash_before and os.path.exists(req_path):
with open(req_path, 'rb') as f:
with open(req_path, "rb") as f:
import hashlib
req_hash_after = hashlib.md5(f.read()).hexdigest()
requirements_changed = req_hash_before != req_hash_after
# Determine if update actually happened
if 'Already up to date' in pull_result.stdout:
return {
'success': True,
'updated': False,
'message': 'Already up to date',
'stashed': stashed
}
if "Already up to date" in pull_result.stdout:
return {"success": True, "updated": False, "message": "Already up to date", "stashed": stashed}
# Clear update cache to reflect new version
set_setting(CACHE_KEY_LAST_CHECK, '')
set_setting(CACHE_KEY_LATEST_VERSION, '')
set_setting(CACHE_KEY_LAST_CHECK, "")
set_setting(CACHE_KEY_LATEST_VERSION, "")
return {
'success': True,
'updated': True,
'message': 'Update successful! Please restart the application.',
'restart_required': True,
'requirements_changed': requirements_changed,
'stashed': stashed,
'stash_restored': stashed,
'output': pull_result.stdout
"success": True,
"updated": True,
"message": "Update successful! Please restart the application.",
"restart_required": True,
"requirements_changed": requirements_changed,
"stashed": stashed,
"stash_restored": stashed,
"output": pull_result.stdout,
}
except subprocess.TimeoutExpired:
return {
'success': False,
'error': 'Operation timed out',
'message': 'The update operation timed out. Please check your network connection and try again.'
"success": False,
"error": "Operation timed out",
"message": "The update operation timed out. Please check your network connection and try again.",
}
except Exception as e:
logger.error(f"Update error: {e}")
return {
'success': False,
'error': str(e)
}
return {"success": False, "error": str(e)}
def restart_application() -> dict[str, Any]:
@@ -599,12 +539,8 @@ def restart_application() -> dict[str, Any]:
os.execv(python_executable, args)
# This code is never reached
return {'success': True, 'message': 'Restarting...'}
return {"success": True, "message": "Restarting..."}
except Exception as e:
logger.error(f"Restart failed: {e}")
return {
'success': False,
'error': str(e),
'message': 'Failed to restart application. Please restart manually.'
}
return {"success": False, "error": str(e), "message": "Failed to restart application. Please restart manually."}