Exemplo n.º 1
0
 def mac_aton(mac_str, force_len=None):
     # we also accept None and '' for convenience.
     # - None yiels None
     # - '' yields []
     if mac_str is None:
         return mac_str
     i = 0
     b = []
     for c in mac_str:
         if i == 2:
             if c != ":":
                 raise MyError("not a valid MAC address: '%s'" % (mac_str))
             i = 0
             continue
         try:
             if i == 0:
                 n = int(c, 16) * 16
                 i = 1
             else:
                 if not i == 1:
                     raise AssertionError("i != 1 - value is {0}".format(i))
                 n = n + int(c, 16)
                 i = 2
                 b.append(n)
         except Exception:
             raise MyError("not a valid MAC address: '%s'" % (mac_str))
     if i == 1:
         raise MyError("not a valid MAC address: '%s'" % (mac_str))
     if force_len is not None:
         if force_len != len(b):
             raise MyError("not a valid MAC address of length %s: '%s'" %
                           (force_len, mac_str))
     return b
Exemplo n.º 2
0
    def call_async_method(cls, object_, action, args, mainloop_timeout=10):
        """Asynchronously call a NetworkManager method"""
        cancellable = cls.create_cancellable()
        async_action = action + "_async"
        # NM does not use a uniform naming for the async methods,
        # for checkpoints it is:
        # NMClient.checkpoint_create() and NMClient.checkpoint_create_finish(),
        # but for reapply it is:
        # NMDevice.reapply_async() and NMDevice.reapply_finish()
        # NMDevice.reapply() is a synchronous version
        # Therefore check if there is a method if an `async` suffix and use the
        # one without the suffix otherwise
        if not hasattr(object_, async_action):
            async_action = action
        finish = action + "_finish"
        user_data = {}

        fullargs = []
        fullargs += args
        fullargs += (cancellable, cls.create_callback(finish), user_data)

        getattr(object_, async_action)(*fullargs)

        if not cls.GMainLoop_run(mainloop_timeout):
            cancellable.cancel()
            raise MyError("failure to call %s.%s(): timeout" %
                          (object_, async_action))

        success = user_data.get("success", None)
        if success is not None:
            return success

        raise MyError(
            "failure to %s checkpoint: %s: %r" %
            (action, user_data.get("error", "unknown error"), user_data))
Exemplo n.º 3
0
 def parse_address(address, family=None):
     try:
         parts = address.split()
         addr_parts = parts[0].split("/")
         if len(addr_parts) != 2:
             raise MyError("expect two addr-parts: ADDR/PLEN")
         a, family = Util.parse_ip(addr_parts[0], family)
         prefix = int(addr_parts[1])
         if not Util.addr_family_valid_prefix(family, prefix):
             raise MyError("invalid prefix %s" % (prefix))
         if len(parts) > 1:
             raise MyError("too many parts")
         return {"address": a, "family": family, "prefix": prefix}
     except Exception:
         raise MyError("invalid address '%s'" % (address))
Exemplo n.º 4
0
 def addr_family_to_v(family):
     if family is None:
         return ""
     if family == socket.AF_INET:
         return "v4"
     if family == socket.AF_INET6:
         return "v6"
     raise MyError("invalid address family '%s'" % (family))
Exemplo n.º 5
0
    def boolean(arg):
        if arg is None or isinstance(arg, bool):
            return arg
        arg0 = arg
        if isinstance(arg, Util.STRING_TYPE):
            arg = arg.lower()

        if arg in ["y", "yes", "on", "1", "true", 1, True]:
            return True
        if arg in ["n", "no", "off", "0", "false", 0, False]:
            return False

        raise MyError("value '%s' is not a boolean" % (arg0))
Exemplo n.º 6
0
 def addr_family_check(family):
     if family != socket.AF_INET and family != socket.AF_INET6:
         raise MyError("invalid address family %s" % (family))