コード例 #1
0
ファイル: nabd.py プロジェクト: sgillott/pynab
    def main(argv):
        nablogging.setup_logging("nabd")
        pidfilepath = "/run/nabd.pid"
        usage = (
            f"nabd [options]\n"
            f" -h                  display this message\n"
            f" --pidfile=<pidfile> define pidfile (default = {pidfilepath})\n")
        try:
            opts, args = getopt.getopt(argv, "h", ["pidfile=", "nabio="])
        except getopt.GetoptError:
            print(usage)
            exit(2)
        for opt, arg in opts:
            if opt == "-h":
                print(usage)
                exit(0)
            elif opt == "--pidfile":
                pidfilepath = arg
        pidfile = PIDLockFile(pidfilepath, timeout=-1)
        try:
            with pidfile:
                from .nabio_hw import NabIOHW

                nabio = NabIOHW()
                Nabd.leds_boot(nabio, 1)
                nabd = Nabd(nabio)
                nabd.run()
        except AlreadyLocked:
            print(f"nabd already running? (pid={pidfile.read_pid()})")
            exit(1)
        except LockFailed:
            print(f"Cannot write pid file to {pidfilepath}, please fix "
                  f"permissions")
            exit(1)
コード例 #2
0
    def main(argv):
        nablogging.setup_logging("nabd")
        pidfilepath = "/run/nabd.pid"
        hardware_platform = hardware.device_model()
        matchObj = re.match(r"Raspberry Pi Zero", hardware_platform)
        if matchObj:
            # running on Pi Zero or Zero 2 hardware
            from .nabio_hw import NabIOHW

            nabiocls = NabIOHW
        else:
            # other hardware: go virtual
            from .nabio_virtual import NabIOVirtual

            nabiocls = NabIOVirtual
        usage = (
            f"nabd [options]\n"
            f" -h                  display this message\n"
            f" --pidfile=<pidfile> define pidfile (default = {pidfilepath})\n"
            " --nabio=<nabio> define nabio class "
            f"(default = {nabiocls.__module__}.{nabiocls.__name__})\n")
        try:
            opts, args = getopt.getopt(argv, "h", ["pidfile=", "nabio="])
        except getopt.GetoptError:
            print(usage)
            exit(2)
        for opt, arg in opts:
            if opt == "-h":
                print(usage)
                exit(0)
            elif opt == "--pidfile":
                pidfilepath = arg
            elif opt == "--nabio":
                from pydoc import locate

                nabiocls = locate(arg)
        pidfile = PIDLockFile(pidfilepath, timeout=-1)
        try:
            with pidfile:
                nabio = nabiocls()
                Nabd.leds_boot(nabio, 1)
                nabd = Nabd(nabio)
                logging.info(f"running on {hardware_platform}")
                nabd.run()
        except AlreadyLocked:
            error_msg = f"nabd already running? (pid={pidfile.read_pid()})"
            print(error_msg)
            logging.critical(error_msg)
            exit(1)
        except LockFailed:
            error_msg = (f"Cannot write pid file to {pidfilepath}, please fix "
                         f"permissions")
            print(error_msg)
            logging.critical(error_msg)
            exit(1)
        except Exception:
            error_msg = f"Unhandled error: {traceback.format_exc()}"
            print(error_msg)
            logging.critical(error_msg)
            exit(3)
コード例 #3
0
    def main(argv):
        nablogging.setup_logging("nabd")
        pidfilepath = "/run/nabd.pid"
        if sys.platform == "linux" and platform.machine() == "armv6l":
            from .nabio_hw import NabIOHW

            nabiocls = NabIOHW
        else:
            from .nabio_virtual import NabIOVirtual

            nabiocls = NabIOVirtual
        usage = (
            f"nabd [options]\n"
            f" -h                  display this message\n"
            f" --pidfile=<pidfile> define pidfile (default = {pidfilepath})\n"
            " --nabio=<nabio> define nabio class "
            f"(default = {nabiocls.__module__}.{nabiocls.__name__})\n")
        try:
            opts, args = getopt.getopt(argv, "h", ["pidfile=", "nabio="])
        except getopt.GetoptError:
            print(usage)
            exit(2)
        for opt, arg in opts:
            if opt == "-h":
                print(usage)
                exit(0)
            elif opt == "--pidfile":
                pidfilepath = arg
            elif opt == "--nabio":
                from pydoc import locate

                nabiocls = locate(arg)
        pidfile = PIDLockFile(pidfilepath, timeout=-1)
        try:
            with pidfile:
                nabio = nabiocls()
                Nabd.leds_boot(nabio, 1)
                nabd = Nabd(nabio)
                nabd.run()
        except AlreadyLocked:
            print(f"nabd already running? (pid={pidfile.read_pid()})")
            exit(1)
        except LockFailed:
            print(f"Cannot write pid file to {pidfilepath}, please fix "
                  f"permissions")
            exit(1)
コード例 #4
0
 def main(cls, argv):
     service_name = cls.__name__.lower()
     nablogging.setup_logging(service_name)
     pidfilepath = f"/run/{service_name}.pid"
     usage = (
         f"{service_name} [options]\n"
         f" -h                   display this message\n"
         f" --pidfile=<pidfile>  define pidfile (default = {pidfilepath})\n"
     )
     try:
         opts, args = getopt.getopt(argv, "h", ["pidfile="])
     except getopt.GetoptError:
         print(usage)
         exit(2)
     for opt, arg in opts:
         if opt == "-h":
             print(usage)
             exit(0)
         elif opt == "--pidfile":
             pidfilepath = arg
     pidfile = PIDLockFile(pidfilepath, timeout=-1)
     try:
         with pidfile:
             service = cls()
             service.run()
     except AlreadyLocked:
         error_msg = (
             f"{service_name} already running? (pid={pidfile.read_pid()})")
         print(error_msg)
         logging.critical(error_msg)
         exit(1)
     except LockFailed:
         error_msg = (f"Cannot write pid file to {pidfilepath}, please fix "
                      f"permissions")
         print(error_msg)
         logging.critical(error_msg)
         exit(1)
     except Exception:
         error_msg = f"Unhandled error: {traceback.format_exc()}"
         print(error_msg)
         logging.critical(error_msg)
         exit(3)