home-backup/home_backup/__main__.py

51 lines
1.9 KiB
Python

import argparse
import asyncio
import os
from . import user_service, sys_service
from .sys_service import rpc
from .user_service import rpc
del rpc
def main(args):
# Parser
parser = argparse.ArgumentParser("home-backup", description="Manage backup tools")
sub_parser = parser.add_subparsers(dest="action")
parser_remote_add = sub_parser.add_parser("remote-add", help="Add remote for backups.")
parser_remote_list = sub_parser.add_parser("remote-list", help="List remotes.")
parser_remote_delete = sub_parser.add_parser("remote-delete", help="Delete remote.")
parser_system_server = sub_parser.add_parser("system-server", help="Run system service (root required).")
parser_system_server.add_argument("--fork", action="store_const", const=True, default=False, help="Makes a deamon through forking.")
parser_user_server = sub_parser.add_parser("user-server", help="Run user service.")
parser_user_server.add_argument("--fork", action="store_const", const=True, default=False, help="Makes a deamon through forking.")
# Arguments
result = parser.parse_args(args)
# Deamon mode
if result.action == "system-server":
# Checks
if os.getuid() != 0: # Check root
raise RuntimeError("System service has to run as root.")
# Run deamon
if result.fork:
if os.fork() != 0:
return # Stop as parent process
asyncio.run(sys_service.rpc.run_deamon(fork=result.fork)) # TODO: Change default path
elif result.action == "user-server":
asyncio.run(user_service.rpc.run_deamon(fork=result.fork)) # TODO: Change default path of user and system socket
# Client actions
# Not found action
elif result.action is None:
parser.parse_args(["--help"])
else:
raise NotImplementedError("Action %s isn't implemented." % result.action)
if __name__ == "__main__":
import sys
main(sys.argv[1:])