Ejemplo n.º 1
0
    def start(self):
        """ starts rolekit """
        log.debug1("start()")

        try:
            os.makedirs(ETC_ROLEKIT_ROLES)
        except OSError as e:
            if e.errno == errno.EEXIST:
                if not os.path.isdir(ETC_ROLEKIT_ROLES):
                    log.fatal("'%s' is not a directory.", e.strerror)
            else:
                log.fatal("Failed to create '%s': %s", e.strerror)
                raise
        else:
            log.info1("Created missing '%s'.", ETC_ROLEKIT_ROLES)

        path = ROLEKIT_ROLES

        if not os.path.exists(path) or not os.path.isdir(path):
            log.error("Role directory '%s' does not exist.", path)
            return

        for name in sorted(os.listdir(path)):
            directory = "%s/%s" % (path, name)
            if not os.path.isdir(directory):
                continue

            if not os.path.exists(os.path.join(directory, "role.py")):
                continue

            log.debug1("Loading role '%s'", name)
            escaped_name = dbus_label_escape(name)

            try:
                if os.path.exists(os.path.join(directory, "role.py")):
                    mod = imp.load_source(name, "%s/role.py" % directory)

                # get Role from module
                role = getattr(mod, "Role")

                # create role object that contains the role instance class
                obj = DBusRole(role,
                               name,
                               directory,
                               self._path,
                               "%s/%s" % (DBUS_PATH_ROLES, escaped_name),
                               persistent=self.persistent)

                if obj in self._roles:
                    log.error("Duplicate role '%s'", obj.name)
                else:
                    self._roles.append(obj)
            except RolekitError as msg:
                log.error("Failed to load role '%s': %s", name, msg)
                continue
            except Exception as msg:
                log.error("Failed to load role '%s':", name)
                log.exception()
                continue
Ejemplo n.º 2
0
 def error_handler_with_conversion(e):
     # We can’t use log.exception() because the traceback is no longer available.
     # So the three cases in dbus_handle_exceptions amount to just this.
     if not isinstance(e, DBusException):
         log.error("{0}: {1}".format(type(e).__name__, str(e)))
         if not isinstance(e, RolekitError):
             log.exception()
         e = DBusException(str(e))
     error_handler(e)
Ejemplo n.º 3
0
    def start(self):
        """ starts rolekit """
        log.debug1("start()")

        try:
            os.makedirs(ETC_ROLEKIT_ROLES)
        except OSError as e:
            if e.errno == errno.EEXIST:
                if not os.path.isdir(ETC_ROLEKIT_ROLES):
                    log.fatal("'%s' is not a directory.", e.strerror)
            else:
                log.fatal("Failed to create '%s': %s", e.strerror)
                raise
        else:
            log.info1("Created missing '%s'.", ETC_ROLEKIT_ROLES)

        path = ROLEKIT_ROLES

        if not os.path.exists(path) or not os.path.isdir(path):
            log.error("Role directory '%s' does not exist.", path)
            return

        for name in sorted(os.listdir(path)):
            directory = "%s/%s" % (path, name)
            if not os.path.isdir(directory):
                continue

            if not os.path.exists(os.path.join(directory, "role.py")):
                continue

            log.debug1("Loading role '%s'", name)
            escaped_name = dbus_label_escape(name)

            try:
                if os.path.exists(os.path.join(directory, "role.py")):
                    mod = imp.load_source(name, "%s/role.py" % directory)

                    # get Role from module
                    role = getattr(mod, "Role")

                    # create role object that contains the role instance class
                    obj = DBusRole(role, name, directory, self.busname,
                                    "%s/%s" % (DBUS_PATH_ROLES, escaped_name),
                                   persistent=self.persistent)

                    if obj in self._roles:
                        log.error("Duplicate role '%s'", obj.get_name())
                    else:
                        self._roles.append(obj)
            except RolekitError as msg:
                log.error("Failed to load role '%s': %s", name, msg)
                continue
            except Exception as msg:
                log.error("Failed to load role '%s':", name)
                log.exception()
                continue
Ejemplo n.º 4
0
def handle_exceptions(func, *args, **kwargs):
    """Decorator to handle exceptions and log them. Used if not connected
    to D-Bus.
    """
    try:
        return func(*args, **kwargs)
    except RolekitError as error:
        log.error("{0}: {1}".format(type(error).__name__, str(error)))
    except Exception:
        log.exception()
Ejemplo n.º 5
0
def dbus_handle_exceptions(func, *args, **kwargs):
    """Decorator to handle exceptions, log and convert into DBusExceptions.

    :Raises DBusException: on any exception raised by the decorated function.
    """
    # Keep this in sync with async.start_with_dbus_callbacks()
    try:
        return func(*args, **kwargs)
    except RolekitError as error:
        log.error(str(error))
        raise DBusException(str(error))
    except DBusException:
        # only log DBusExceptions once, pass it through
        raise
    except Exception as e:
        log.exception()
        raise DBusException(str(e))