mirror of
https://github.com/cottongin/cardputerzero-sync.git
synced 2026-11-20 14:44:47 -06:00
58 lines
1.8 KiB
Plaintext
58 lines
1.8 KiB
Plaintext
|
|
#!/usr/bin/env python3
|
||
|
|
"""Service entrypoint for the CardputerZero background photo sync daemon."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import logging
|
||
|
|
import os
|
||
|
|
import signal
|
||
|
|
import sys
|
||
|
|
import threading
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
|
||
|
|
def _ensure_package_on_path() -> None:
|
||
|
|
script_root = str(Path(os.path.realpath(__file__)).resolve().parents[1])
|
||
|
|
if script_root not in sys.path:
|
||
|
|
sys.path.insert(0, script_root)
|
||
|
|
installed_lib = "/usr/lib/cp0-sync"
|
||
|
|
if os.path.isdir(installed_lib) and installed_lib not in sys.path:
|
||
|
|
sys.path.insert(0, installed_lib)
|
||
|
|
|
||
|
|
|
||
|
|
_ensure_package_on_path()
|
||
|
|
|
||
|
|
from cp0_syncd.daemon import SyncDaemon # noqa: E402
|
||
|
|
|
||
|
|
|
||
|
|
def main(argv: list[str] | None = None) -> int:
|
||
|
|
parser = argparse.ArgumentParser(description="Run the CardputerZero background photo sync daemon.")
|
||
|
|
parser.add_argument("--config", type=Path, default=None, help="Path to config.json.")
|
||
|
|
parser.add_argument("--db", type=Path, default=None, help="Path to SQLite state.db.")
|
||
|
|
parser.add_argument("--sock", type=Path, default=None, help="Path to Unix IPC socket.")
|
||
|
|
parser.add_argument("--verbose", action="store_true", help="Enable debug logging.")
|
||
|
|
args = parser.parse_args(argv)
|
||
|
|
|
||
|
|
logging.basicConfig(
|
||
|
|
level=logging.DEBUG if args.verbose else logging.INFO,
|
||
|
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||
|
|
)
|
||
|
|
|
||
|
|
stop_event = threading.Event()
|
||
|
|
|
||
|
|
def _handle_signal(_signum: int, _frame: object) -> None:
|
||
|
|
stop_event.set()
|
||
|
|
|
||
|
|
signal.signal(signal.SIGINT, _handle_signal)
|
||
|
|
signal.signal(signal.SIGTERM, _handle_signal)
|
||
|
|
|
||
|
|
daemon = SyncDaemon(config_path=args.config, db_path=args.db, sock_path=args.sock)
|
||
|
|
daemon.run_forever(stop_event=stop_event)
|
||
|
|
logging.getLogger(__name__).info("cp0-syncd stopped cleanly")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|