Best Python code snippet using avocado_python
pci.py
Source:pci.py  
...42    """43    addresses = []44    cmd = "lspci -D"45    for line in process.system_output(cmd).splitlines():46        if not get_pci_prop(line.split()[0], 'Class').startswith('06'):47            addresses.append(line.split()[0])48    if addresses:49        return addresses50def get_num_interfaces_in_pci(dom_pci_address):51    """52    Gets number of interfaces of a given partial PCI address starting with53    full domain address.54    :param dom_pci_address: Partial PCI address including domain55                            address (0000, 0000:00:1f, 0000:00:1f.2, etc)56    :return: number of devices in a PCI domain.57    """58    cmd = "ls -l /sys/class/*/ -1"59    output = process.system_output(cmd, ignore_status=True, shell=True)60    if output:61        filt = '/%s' % dom_pci_address62        count = 063        for line in output.splitlines():64            if filt in line:65                count += 166        return count67def get_disks_in_pci_address(pci_address):68    """69    Gets disks in a PCI address.70    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)71    :return: list of disks in a PCI address.72    """73    disks_path = "/dev/disk/by-path/"74    disk_list = []75    for dev in os.listdir(disks_path):76        if pci_address in dev:77            link = os.readlink(os.path.join(disks_path, dev))78            disk_list.append(os.path.abspath(os.path.join(disks_path, link)))79    return disk_list80def get_nics_in_pci_address(pci_address):81    """82    Gets network interface(nic) in a PCI address.83    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)84    :return: list of network interfaces in a PCI address.85    """86    return get_interfaces_in_pci_address(pci_address, "net")87def get_interfaces_in_pci_address(pci_address, pci_class):88    """89    Gets interface in a PCI address.90    e.g: host = pci.get_interfaces_in_pci_address("0001:01:00.0", "net")91         ['enP1p1s0f0']92         host = pci.get_interfaces_in_pci_address("0004:01:00.0", "fc_host")93         ['host6']94    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)95    :param class: Adapter type (FC(fc_host), FCoE(net), NIC(net), SCSI(scsi)..)96    :return: list of generic interfaces in a PCI address.97    """98    pci_class_path = "/sys/class/%s/" % pci_class99    if not os.path.isdir(pci_class_path):100        raise ValueError("Please provide valid class name")101    return [interface for interface in os.listdir(pci_class_path)102            if pci_address in os.readlink(os.path.join(pci_class_path,103                                                       interface))]104def get_pci_class_name(pci_address):105    """106    Gets pci class name for given pci bus address107    e.g: >>> pci.get_pci_class_name("0000:01:00.0")108             'scsi_host'109    :param pci_address: Any segment of a PCI address(1f, 0000:00:if, ...)110    :return: class name for corresponding pci bus address111    """112    pci_class_dic = {'0104': 'scsi_host', '0c04': 'fc_host',113                     '0200': 'net', '0108': 'nvme', '0280': 'net'}114    pci_class_id = get_pci_prop(pci_address, "Class")115    if pci_class_id not in pci_class_dic:116        if pci_class_id is None:117            raise ValueError("Unable to get 'Class' property of given pci "118                             "address %s" % pci_address)119        else:120            raise ValueError("Class ID %s is not defined in this library"121                             "please send an update" % pci_class_id)122    return pci_class_dic.get(pci_class_id)123def get_pci_fun_list(pci_address):124    """125    Gets list of functions in the given PCI address.126    Example: in address 0000:03:00, functions are 0000:03:00.0 and 0000:03:00.1127    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)128    :return: list of functions in a PCI address.129    """130    return list(dev for dev in get_pci_addresses() if pci_address in dev)131def get_slot_from_sysfs(full_pci_address):132    """133    Gets the PCI slot of given address.134    :note: Specific for ppc64 processor.135    :param full_pci_address: Full PCI address including domain (0000:03:00.0)136    :return: Removed port related details using re, only returns till137             physical slot of the adapter.138    """139    if not os.path.isfile('/sys/bus/pci/devices/%s/devspec' % full_pci_address):140        return141    devspec = genio.read_file("/sys/bus/pci/devices/%s/devspec"142                              % full_pci_address)143    if not os.path.isfile("/proc/device-tree/%s/ibm,loc-code" % devspec):144        return145    slot = genio.read_file("/proc/device-tree/%s/ibm,loc-code" % devspec)146    slot_ibm = re.match(r'((\w+)[.])+(\w+)-[P(\d+)-]*C(\d+)', slot)147    if slot_ibm:148        return slot_ibm.group()149    slot_openpower = re.match(r'(\w+)[\s]*(\w+)(\d*)', slot)150    if slot_openpower:151        return slot_openpower.group()152    raise ValueError("Failed to get slot from: '%s'" % slot)153def get_slot_list():154    """155    Gets list of PCI slots in the system.156    :note: Specific for ppc64 processor.157    :return: list of slots in the system.158    """159    return list(set(get_slot_from_sysfs(dev) for dev in get_pci_addresses()))160def get_pci_id_from_sysfs(full_pci_address):161    """162    Gets the PCI ID from sysfs of given PCI address.163    :param full_pci_address: Full PCI address including domain (0000:03:00.0)164    :return: PCI ID of a PCI address from sysfs.165    """166    path = "/sys/bus/pci/devices/%s" % full_pci_address167    if os.path.isdir(path):168        path = "%s/%%s" % path169        return ":".join(["%04x" % int(open(path % param).read(), 16)170                         for param in ['vendor', 'device', 'subsystem_vendor',171                                       'subsystem_device']])172def get_pci_prop(pci_address, prop):173    """174    Gets specific PCI ID of given PCI address. (first match only)175    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)176    :param part: prop of PCI ID.177    :return: specific PCI ID of a PCI address.178    """179    cmd = "lspci -Dnvmm -s %s" % pci_address180    output = process.system_output(cmd, ignore_status=True)181    if output:182        for line in output.splitlines():183            if prop == line.split(':')[0]:184                return line.split()[-1]185def get_pci_id(pci_address):186    """187    Gets PCI id of given address. (first match only)188    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)189    :return: PCI ID of a PCI address.190    """191    pci_id = []192    for params in ['Vendor', 'Device', 'SVendor', 'SDevice']:193        output = get_pci_prop(pci_address, params)194        if not output:195            return196        pci_id.append(output)197    if pci_id:198        return ":".join(pci_id)199def get_driver(pci_address):200    """201    Gets the kernel driver in use of given PCI address. (first match only)202    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)203    :return: driver of a PCI address.204    """205    cmd = "lspci -ks %s" % pci_address206    output = process.system_output(cmd, ignore_status=True)207    if output:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
