def exec_sync(cmds): """Execute a command and convert returned values to native string. Native string format is bytes for Python 2 and unicode for Python 3. It is important to keep data in a native format for seamless usage of output data in the rest of Python codebase. Note that this function should not be used if output data could be undecodable bytes. """ retcode, out, err = exec_sync_bytes(cmds) return retcode, py2to3.to_str(out), py2to3.to_str(err)
def exec_sync(cmds): """Execute a command and convert returned values to native string. Native string format is bytes for Python 2 and unicode for Python 3. It is important to keep data in a native format for seamless usage of output data in the rest of Python codebase. Note that this function should not be used if output data could be undecodable bytes. """ retcode, out, err = exec_sync_bytes(cmds) return retcode, py2to3.to_str(out), py2to3.to_str(err)
def driver_name(device_name): """Returns the driver used by a device. Throws IOError ENODEV for non existing devices. Throws IOError EOPNOTSUPP for non supported devices, i.g., loopback. """ encoded_name = py2to3.to_binary(device_name) buff = array.array('b', b'\0' * struct.calcsize(DRVINFO_FORMAT)) cmds = struct.pack('= I', ETHTOOL_GDRVINFO) buff[0:len(cmds)] = array.array('b', cmds) data = struct.pack(IFREQ_FORMAT, encoded_name, *buff.buffer_info()) with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as sock: fcntl.ioctl(sock, SIOCETHTOOL, data) ( cmds, driver, version, fw_version, businfo, _, _, n_priv_flags, n_stats, testinfo_len, eedump_len, regdump_len, ) = struct.unpack(DRVINFO_FORMAT, buff) driver_str = py2to3.to_str(driver) return driver_str.rstrip('\0') # C string end with the leftmost null char
def setUp(self): if six.PY3: raise SkipTest( 'Passing non-ascii chars to cmdline is broken in Python 3') # First 3 Hebrew letters, in native string format # See http://unicode.org/charts/PDF/U0590.pdf bridge_name = py2to3.to_str(b'\xd7\x90\xd7\x91\xd7\x92') self._bridge = Bridge(bridge_name) self._bridge.addDevice()
def nl_object_get_type(obj): """Return the object's type. @arg obj object @return Name of the object type or None if not recognized """ _nl_object_get_type = _libnl('nl_object_get_type', c_char_p, c_void_p) object_type = _nl_object_get_type(obj) return py2to3.to_str(object_type) if object_type else None
def nl_geterror(error_code): """Return error message for an error code. @arg error_code error code @return error message """ _nl_geterror = _libnl('nl_geterror', c_char_p, c_int) error_message = _nl_geterror(error_code) return py2to3.to_str(error_message)
def nl_geterror(error_code): """Return error message for an error code. @arg error_code error code @return error message """ _nl_geterror = _libnl('nl_geterror', c_char_p, c_int) error_message = _nl_geterror(error_code) return py2to3.to_str(error_message)
def unicode_bridge(self): # First 3 Hebrew letters, in native string format # See http://unicode.org/charts/PDF/U0590.pdf bridge_name = py2to3.to_str(b'\xd7\x90\xd7\x91\xd7\x92') br = Bridge(bridge_name) br.addDevice() try: yield br finally: br.delDevice()
def nl_object_get_type(obj): """Return the object's type. @arg obj object @return Name of the object type or None if not recognized """ _nl_object_get_type = _libnl('nl_object_get_type', c_char_p, c_void_p) object_type = _nl_object_get_type(obj) return py2to3.to_str(object_type) if object_type else None
def setUp(self): if six.PY3: raise SkipTest( 'Passing non-ascii chars to cmdline is broken in Python 3') # First 3 Hebrew letters, in native string format # See http://unicode.org/charts/PDF/U0590.pdf bridge_name = py2to3.to_str(b'\xd7\x90\xd7\x91\xd7\x92') self._bridge = Bridge(bridge_name) self._bridge.addDevice()
def rtnl_link_get_type(link): """Return type of link. @arg link Link object @return Name of link type or None if not specified. """ _rtnl_link_get_type = _libnl_route('rtnl_link_get_type', c_char_p, c_void_p) link_type = _rtnl_link_get_type(link) return py2to3.to_str(link_type) if link_type else None
def rtnl_link_get_qdisc(link): """Return name of queueing discipline of link object. @arg link Link object @return Name of qdisc or None if not specified. """ _rtnl_link_get_qdisc = _libnl_route( 'rtnl_link_get_qdisc', c_char_p, c_void_p) qdisc = _rtnl_link_get_qdisc(link) return py2to3.to_str(qdisc) if qdisc else None
def rtnl_link_get_name(link): """Return name of link object. @arg link Link object @return Link name or None if name is not specified """ _rtnl_link_get_name = _libnl_route( 'rtnl_link_get_name', c_char_p, c_void_p) name = _rtnl_link_get_name(link) return py2to3.to_str(name) if name else None
def rtnl_link_get_type(link): """Return type of link. @arg link Link object @return Name of link type or None if not specified. """ _rtnl_link_get_type = _libnl_route( 'rtnl_link_get_type', c_char_p, c_void_p) link_type = _rtnl_link_get_type(link) return py2to3.to_str(link_type) if link_type else None
def nl_af2str(family): """Convert address family code to string. @arg family Address family code. @return Address family represented as string """ _nl_af2str = _libnl('nl_af2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() address_family = _nl_af2str(family, buf, sizeof(buf)) return py2to3.to_str(address_family)
def rtnl_link_get_name(link): """Return name of link object. @arg link Link object @return Link name or None if name is not specified """ _rtnl_link_get_name = _libnl_route('rtnl_link_get_name', c_char_p, c_void_p) name = _rtnl_link_get_name(link) return py2to3.to_str(name) if name else None
def rtnl_link_get_qdisc(link): """Return name of queueing discipline of link object. @arg link Link object @return Name of qdisc or None if not specified. """ _rtnl_link_get_qdisc = _libnl_route('rtnl_link_get_qdisc', c_char_p, c_void_p) qdisc = _rtnl_link_get_qdisc(link) return py2to3.to_str(qdisc) if qdisc else None
def nl_af2str(family): """Convert address family code to string. @arg family Address family code. @return Address family represented as string """ _nl_af2str = _libnl('nl_af2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() address_family = _nl_af2str(family, buf, sizeof(buf)) return py2to3.to_str(address_family)
def rtnl_addr_flags2str(flags_bitfield): """Return string representation of address flags bitfield. @arg flags_bitfield Bitfield of address' flags @return String represantion of given flags in format "flag1,flag2,flag3". """ _rtnl_addr_flags2str = _libnl_route( 'rtnl_addr_flags2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * (CHARBUFFSIZE * 2))() flags_str = _rtnl_addr_flags2str(flags_bitfield, buf, sizeof(buf)) return py2to3.to_str(flags_str)
def nl_addr2str(addr): """Convert abstract address object to string. @arg addr Abstract address object. @return Address represented as string """ _nl_addr2str = _libnl('nl_addr2str', c_char_p, c_void_p, c_char_p, c_size_t) buf = (c_char * HWADDRSIZE)() address = _nl_addr2str(addr, buf, sizeof(buf)) return py2to3.to_str(address)
def rtnl_scope2str(scope): """Convert address scope code to string. @arg scope Address scope code. @return Address scope represented as string """ _rtnl_scope2str = _libnl_route( 'rtnl_scope2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() address_scope = _rtnl_scope2str(scope, buf, sizeof(buf)) return py2to3.to_str(address_scope)
def nl_addr2str(addr): """Convert abstract address object to string. @arg addr Abstract address object. @return Address represented as string """ _nl_addr2str = _libnl( 'nl_addr2str', c_char_p, c_void_p, c_char_p, c_size_t) buf = (c_char * HWADDRSIZE)() address = _nl_addr2str(addr, buf, sizeof(buf)) return py2to3.to_str(address)
def rtnl_scope2str(scope): """Convert address scope code to string. @arg scope Address scope code. @return Address scope represented as string """ _rtnl_scope2str = _libnl_route('rtnl_scope2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() address_scope = _rtnl_scope2str(scope, buf, sizeof(buf)) return py2to3.to_str(address_scope)
def rtnl_link_operstate2str(operstate_code): """Convert operstate code to string. @arg operstate_code Operstate code. @return Operstate represented as string """ _rtnl_link_operstate2str = _libnl_route( 'rtnl_link_operstate2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() operstate = _rtnl_link_operstate2str(operstate_code, buf, sizeof(buf)) return py2to3.to_str(operstate)
def rtnl_addr_flags2str(flags_bitfield): """Return string representation of address flags bitfield. @arg flags_bitfield Bitfield of address' flags @return String represantion of given flags in format "flag1,flag2,flag3". """ _rtnl_addr_flags2str = _libnl_route('rtnl_addr_flags2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * (CHARBUFFSIZE * 2))() flags_str = _rtnl_addr_flags2str(flags_bitfield, buf, sizeof(buf)) return py2to3.to_str(flags_str)
def rtnl_link_operstate2str(operstate_code): """Convert operstate code to string. @arg operstate_code Operstate code. @return Operstate represented as string """ _rtnl_link_operstate2str = _libnl_route('rtnl_link_operstate2str', c_char_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() operstate = _rtnl_link_operstate2str(operstate_code, buf, sizeof(buf)) return py2to3.to_str(operstate)
def unicode_bridge(self): if six.PY3: pytest.skip( 'Passing non-ascii chars to cmdline is broken in Python 3') # First 3 Hebrew letters, in native string format # See http://unicode.org/charts/PDF/U0590.pdf bridge_name = py2to3.to_str(b'\xd7\x90\xd7\x91\xd7\x92') br = Bridge(bridge_name) br.addDevice() try: yield br finally: br.delDevice()
def rtnl_link_i2name(cache, ifindex): """Translate interface index to corresponding link name. @arg cache Link cache @arg ifindex Interface index Translates the specified interface index to the corresponding link name. @return Name of link or None if no match was found. """ _rtnl_link_i2name = _libnl_route( 'rtnl_link_i2name', c_char_p, c_void_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() name = _rtnl_link_i2name(cache, ifindex, buf, sizeof(buf)) return py2to3.to_str(name) if name else None
def rtnl_link_i2name(cache, ifindex): """Translate interface index to corresponding link name. @arg cache Link cache @arg ifindex Interface index Translates the specified interface index to the corresponding link name. @return Name of link or None if no match was found. """ _rtnl_link_i2name = _libnl_route('rtnl_link_i2name', c_char_p, c_void_p, c_int, c_char_p, c_size_t) buf = (c_char * CHARBUFFSIZE)() name = _rtnl_link_i2name(cache, ifindex, buf, sizeof(buf)) return py2to3.to_str(name) if name else None
def writeConfFile(self, fileName, configuration): '''Backs up the previous contents of the file referenced by fileName writes the new configuration and sets the specified access mode.''' self._backup(fileName) configuration = self.CONFFILE_HEADER + '\n' + configuration logging.debug('Writing to file %s configuration:\n%s', fileName, configuration) with open(fileName, 'w') as confFile: confFile.write(configuration) os.chmod(fileName, 0o664) try: # restorecon expects string type selinux.restorecon(py2to3.to_str(fileName)) except: logging.debug( 'ignoring restorecon error in case ' 'SElinux is disabled', exc_info=True)
def driver_name(device_name): """Returns the driver used by a device. Throws IOError ENODEV for non existing devices. Throws IOError EOPNOTSUPP for non supported devices, i.g., loopback. """ encoded_name = py2to3.to_binary(device_name) buff = array.array('b', b'\0' * struct.calcsize(DRVINFO_FORMAT)) cmds = struct.pack('= I', ETHTOOL_GDRVINFO) buff[0:len(cmds)] = array.array('b', cmds) data = struct.pack(IFREQ_FORMAT, encoded_name, *buff.buffer_info()) with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as sock: fcntl.ioctl(sock, SIOCETHTOOL, data) (cmds, driver, version, fw_version, businfo, _, _, n_priv_flags, n_stats, testinfo_len, eedump_len, regdump_len) = struct.unpack(DRVINFO_FORMAT, buff) driver_str = py2to3.to_str(driver) return driver_str.rstrip('\0') # C string end with the leftmost null char
def setUp(self): # First 3 Hebrew letters, in native string format # See http://unicode.org/charts/PDF/U0590.pdf bridge_name = py2to3.to_str(b'\xd7\x90\xd7\x91\xd7\x92') self._bridge = Bridge(bridge_name) self._bridge.addDevice()