Build and Release Barcode Reader Server (Multi-Platform) / build-linux (push) Failing after 39s
Build and Release Barcode Reader Server (Multi-Platform) / build-windows (push) Successful in 3m42s
Build and Release Barcode Reader Server (Multi-Platform) / build-macos (push) Successful in 1m5s
152 lines
5.0 KiB
Python
152 lines
5.0 KiB
Python
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
APP_NAME = "BarcodeReaderServer"
|
|
PLIST_LABEL = "com.catellisrl.barcodereader"
|
|
|
|
|
|
def get_launch_command() -> list[str]:
|
|
"""Ottiene il comando eseguibile appropriato per l'avvio automatico."""
|
|
if getattr(sys, "frozen", False):
|
|
# Su Linux AppImage usa il percorso del file .AppImage invece del mount /tmp/.mount_*
|
|
appimage_path = os.environ.get("APPIMAGE")
|
|
if sys.platform.startswith("linux") and appimage_path:
|
|
return [appimage_path, "--minimized"]
|
|
# Se compilato con PyInstaller
|
|
return [sys.executable, "--minimized"]
|
|
|
|
# Se eseguito come script Python, preferisci pythonw.exe su Windows per evitare la finestra CMD
|
|
python_exe = sys.executable
|
|
if sys.platform == "win32":
|
|
exe_path = Path(sys.executable)
|
|
if exe_path.name.lower() == "python.exe":
|
|
pythonw_exe = exe_path.with_name("pythonw.exe")
|
|
if pythonw_exe.exists():
|
|
python_exe = str(pythonw_exe)
|
|
|
|
script_path = str(Path(__file__).resolve().parent / "main.py")
|
|
return [python_exe, script_path, "--minimized"]
|
|
|
|
|
|
def is_autostart_enabled() -> bool:
|
|
"""Verifica se l'avvio automatico è abilitato per il sistema operativo corrente."""
|
|
if sys.platform == "win32":
|
|
try:
|
|
import winreg
|
|
key = winreg.OpenKey(
|
|
winreg.HKEY_CURRENT_USER,
|
|
r"Software\Microsoft\Windows\CurrentVersion\Run",
|
|
0,
|
|
winreg.KEY_READ,
|
|
)
|
|
try:
|
|
winreg.QueryValueEx(key, APP_NAME)
|
|
winreg.CloseKey(key)
|
|
return True
|
|
except FileNotFoundError:
|
|
winreg.CloseKey(key)
|
|
return False
|
|
except Exception:
|
|
return False
|
|
|
|
elif sys.platform == "darwin":
|
|
plist_path = Path.home() / "Library" / "LaunchAgents" / f"{PLIST_LABEL}.plist"
|
|
return plist_path.exists()
|
|
|
|
elif sys.platform.startswith("linux"):
|
|
desktop_path = Path.home() / ".config" / "autostart" / f"{APP_NAME}.desktop"
|
|
return desktop_path.exists()
|
|
|
|
return False
|
|
|
|
|
|
def set_autostart(enabled: bool) -> bool:
|
|
"""Abilita o disabilita l'avvio automatico al boot del sistema operativo."""
|
|
cmd = get_launch_command()
|
|
|
|
try:
|
|
if sys.platform == "win32":
|
|
import winreg
|
|
key = winreg.OpenKey(
|
|
winreg.HKEY_CURRENT_USER,
|
|
r"Software\Microsoft\Windows\CurrentVersion\Run",
|
|
0,
|
|
winreg.KEY_SET_VALUE,
|
|
)
|
|
if enabled:
|
|
cmd_str = f'"{cmd[0]}"'
|
|
if len(cmd) > 1:
|
|
cmd_str += " " + " ".join(f'"{arg}"' for arg in cmd[1:])
|
|
winreg.SetValueEx(key, APP_NAME, 0, winreg.REG_SZ, cmd_str)
|
|
else:
|
|
try:
|
|
winreg.DeleteValue(key, APP_NAME)
|
|
except FileNotFoundError:
|
|
pass
|
|
winreg.CloseKey(key)
|
|
return True
|
|
|
|
elif sys.platform == "darwin":
|
|
launch_agents_dir = Path.home() / "Library" / "LaunchAgents"
|
|
plist_path = launch_agents_dir / f"{PLIST_LABEL}.plist"
|
|
|
|
if enabled:
|
|
launch_agents_dir.mkdir(parents=True, exist_ok=True)
|
|
args_xml = "\n".join(f" <string>{arg}</string>" for arg in cmd)
|
|
plist_content = f"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
<plist version="1.0">
|
|
<dict>
|
|
<key>Label</key>
|
|
<string>{PLIST_LABEL}</string>
|
|
<key>ProgramArguments</key>
|
|
<array>
|
|
{args_xml}
|
|
</array>
|
|
<key>RunAtLoad</key>
|
|
<true/>
|
|
<key>KeepAlive</key>
|
|
<false/>
|
|
<key>StandardOutPath</key>
|
|
<string>/tmp/barcode_server.log</string>
|
|
<key>StandardErrorPath</key>
|
|
<string>/tmp/barcode_server_error.log</string>
|
|
</dict>
|
|
</plist>
|
|
"""
|
|
plist_path.write_text(plist_content, encoding="utf-8")
|
|
else:
|
|
if plist_path.exists():
|
|
plist_path.unlink()
|
|
return True
|
|
|
|
elif sys.platform.startswith("linux"):
|
|
autostart_dir = Path.home() / ".config" / "autostart"
|
|
desktop_path = autostart_dir / f"{APP_NAME}.desktop"
|
|
|
|
if enabled:
|
|
autostart_dir.mkdir(parents=True, exist_ok=True)
|
|
cmd_str = " ".join(f'"{arg}"' for arg in cmd)
|
|
content = f"""[Desktop Entry]
|
|
Type=Application
|
|
Exec={cmd_str}
|
|
Hidden=false
|
|
NoDisplay=false
|
|
X-GNOME-Autostart-enabled=true
|
|
Name=Barcode Reader Server
|
|
Comment=Server emulatore tastiera per codici a barre
|
|
"""
|
|
desktop_path.write_text(content, encoding="utf-8")
|
|
else:
|
|
if desktop_path.exists():
|
|
desktop_path.unlink()
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"Errore nella configurazione dell'autostart: {e}")
|
|
return False
|
|
|
|
return False
|
|
|