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: assert i == 1 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
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))
def connection_find_master(name, connections, n_connections=None): c = ArgUtil.connection_find_by_name(name, connections, n_connections) if not c: raise MyError("invalid master/parent '%s'" % (name)) if c["interface_name"] is None: raise MyError( "invalid master/parent '%s' which needs an 'interface_name'" % (name)) if not Util.ifname_valid(c["interface_name"]): raise MyError( "invalid master/parent '%s' with invalid 'interface_name' ('%s')" % (name, c["interface_name"])) return c["interface_name"]
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))
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))
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))
def check_output(argv): # subprocess.check_output is python 2.7. with open("/dev/null", "wb") as DEVNULL: import subprocess env = os.environ.copy() env["LANG"] = "C" p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=DEVNULL, env=env) # FIXME: Can we assume this to always be UTF-8? out = p.communicate()[0].decode("UTF-8") if p.returncode != 0: raise MyError("failure calling %s: exit with %s" % (argv, p.returncode)) return out
def addr_family_check(family): if family != socket.AF_INET and family != socket.AF_INET6: raise MyError("invalid address family %s" % (family))
def connection_find_master_uuid(name, connections, n_connections=None): c = ArgUtil.connection_find_by_name(name, connections, n_connections) if not c: raise MyError("invalid master/parent '%s'" % (name)) return c["nm.uuid"]