Exemplo n.º 1
0
Arquivo: cmd.py Projeto: kkoojjyy/vdsm
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)
Exemplo n.º 2
0
Arquivo: cmd.py Projeto: nirs/vdsm
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)
Exemplo n.º 3
0
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
Exemplo n.º 4
0
    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()
Exemplo n.º 5
0
Arquivo: libnl.py Projeto: nirs/vdsm
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
Exemplo n.º 6
0
Arquivo: libnl.py Projeto: nirs/vdsm
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)
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
 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()
Exemplo n.º 9
0
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
Exemplo n.º 10
0
    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()
Exemplo n.º 11
0
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
Exemplo n.º 12
0
Arquivo: libnl.py Projeto: nirs/vdsm
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
Exemplo n.º 13
0
Arquivo: libnl.py Projeto: nirs/vdsm
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
Exemplo n.º 14
0
Arquivo: libnl.py Projeto: nirs/vdsm
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
Exemplo n.º 15
0
Arquivo: libnl.py Projeto: nirs/vdsm
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)
Exemplo n.º 16
0
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
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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)
Exemplo n.º 19
0
Arquivo: libnl.py Projeto: nirs/vdsm
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)
Exemplo n.º 20
0
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)
Exemplo n.º 21
0
Arquivo: libnl.py Projeto: nirs/vdsm
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)
Exemplo n.º 22
0
Arquivo: libnl.py Projeto: nirs/vdsm
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)
Exemplo n.º 23
0
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)
Exemplo n.º 24
0
Arquivo: libnl.py Projeto: nirs/vdsm
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)
Exemplo n.º 25
0
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)
Exemplo n.º 26
0
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)
Exemplo n.º 27
0
    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()
Exemplo n.º 28
0
Arquivo: libnl.py Projeto: nirs/vdsm
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
Exemplo n.º 29
0
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
Exemplo n.º 30
0
    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)
Exemplo n.º 31
0
Arquivo: ethtool.py Projeto: nirs/vdsm
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
Exemplo n.º 32
0
 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()