89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
"""Registro de arranque automático al login (Windows / Linux)."""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from ..constants import APP_DIR, CONFIG_DIR, IS_WINDOWS
|
|
from .env_loader import _env_bool, load_env_file
|
|
|
|
|
|
def _marker_path() -> Path:
|
|
return CONFIG_DIR / ".autostart_registered"
|
|
|
|
|
|
def _executable_command() -> tuple[str, list[str]]:
|
|
if getattr(sys, "frozen", False):
|
|
exe = Path(sys.executable).resolve()
|
|
return str(exe), ["--start-engine"]
|
|
runner = (APP_DIR / "runner.py").resolve()
|
|
return sys.executable, [str(runner), "--start-engine"]
|
|
|
|
|
|
def _register_windows(exe: str, args: list[str]) -> None:
|
|
task_name = "CloudRestoreAS"
|
|
arg_str = " ".join(f'"{a}"' if " " in a else a for a in args)
|
|
tr = f'"{exe}" {arg_str}'
|
|
check = subprocess.run(
|
|
["schtasks", "/Query", "/TN", task_name],
|
|
capture_output=True,
|
|
creationflags=subprocess.CREATE_NO_WINDOW if hasattr(subprocess, "CREATE_NO_WINDOW") else 0,
|
|
)
|
|
if check.returncode == 0:
|
|
return
|
|
subprocess.run(
|
|
[
|
|
"schtasks",
|
|
"/Create",
|
|
"/TN",
|
|
task_name,
|
|
"/TR",
|
|
tr,
|
|
"/SC",
|
|
"ONLOGON",
|
|
"/RL",
|
|
"LIMITED",
|
|
"/F",
|
|
],
|
|
check=False,
|
|
creationflags=subprocess.CREATE_NO_WINDOW if hasattr(subprocess, "CREATE_NO_WINDOW") else 0,
|
|
)
|
|
|
|
|
|
def _register_linux(exe: str, args: list[str]) -> None:
|
|
autostart_dir = Path.home() / ".config" / "autostart"
|
|
autostart_dir.mkdir(parents=True, exist_ok=True)
|
|
desktop = autostart_dir / "cloudrestoreas.desktop"
|
|
if desktop.exists():
|
|
return
|
|
exec_line = " ".join([exe, *args])
|
|
content = f"""[Desktop Entry]
|
|
Type=Application
|
|
Name=CloudRestoreAS
|
|
Comment=Restauración automática SQL Server
|
|
Exec={exec_line}
|
|
Path={APP_DIR}
|
|
Terminal=false
|
|
X-GNOME-Autostart-enabled=true
|
|
"""
|
|
desktop.write_text(content, encoding="utf-8")
|
|
|
|
|
|
def register_autostart_if_requested() -> None:
|
|
load_env_file()
|
|
if not _env_bool("CLOUDRESTORE_REGISTER_AUTOSTART", True):
|
|
return
|
|
if _marker_path().exists():
|
|
return
|
|
|
|
exe, args = _executable_command()
|
|
try:
|
|
if IS_WINDOWS:
|
|
_register_windows(exe, args)
|
|
else:
|
|
_register_linux(exe, args)
|
|
_marker_path().write_text("ok", encoding="utf-8")
|
|
except OSError:
|
|
pass
|