Best Python code snippet using avocado_python
pci.py
Source:pci.py  
...203                with open(filename, 'r') as fw_file:204                    firmware = fw_file.read().strip('\t\r\n\0').split()[0]205                    firmware = firmware.strip(',')206    return firmware207def get_pci_fun_list(pci_address):208    """209    Gets list of functions in the given PCI address.210    Example: in address 0000:03:00, functions are 0000:03:00.0 and 0000:03:00.1211    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)212    :return: list of functions in a PCI address.213    """214    return list(dev for dev in get_pci_addresses() if pci_address in dev)215def get_slot_from_sysfs(full_pci_address):216    """217    Gets the PCI slot of given address.218    :note: Specific for ppc64 processor.219    :param full_pci_address: Full PCI address including domain (0000:03:00.0)220    :return: Removed port related details using re, only returns till221             physical slot of the adapter.222    """223    if 'ppc64' not in platform.processor():224        return ""225    if not os.path.isfile('/sys/bus/pci/devices/%s/devspec' % full_pci_address):226        return227    filename = "/sys/bus/pci/devices/%s/devspec" % full_pci_address228    with open(filename, 'r') as file_obj:229        devspec = file_obj.read()230    if not os.path.isfile("/proc/device-tree/%s/ibm,loc-code" % devspec):231        return232    filename = "/proc/device-tree/%s/ibm,loc-code" % devspec233    with open(filename, 'r') as file_obj:234        slot = file_obj.read()235    slot_ibm = re.match(r'((\w+)[.])+(\w+)-[P(\d+)-]*C(\d+)', slot)236    if slot_ibm:237        return slot_ibm.group()238    slot_openpower = re.match(r'(\w+)[\s]*(\w+)(\d*)', slot)239    if slot_openpower:240        return slot_openpower.group()241    return ""242def get_slot_list():243    """244    Gets list of PCI slots in the system.245    :note: Specific for ppc64 processor.246    :return: list of slots in the system.247    """248    return list(set(get_slot_from_sysfs(dev) for dev in get_pci_addresses()))249def get_pci_id_from_sysfs(full_pci_address):250    """251    Gets the PCI ID from sysfs of given PCI address.252    :param full_pci_address: Full PCI address including domain (0000:03:00.0)253    :return: PCI ID of a PCI address from sysfs.254    """255    path = "/sys/bus/pci/devices/%s" % full_pci_address256    if os.path.isdir(path):257        path = "%s/%%s" % path258        return ":".join(["%04x" % int(open(path % param).read(), 16)259                         for param in ['vendor', 'device', 'subsystem_vendor',260                                       'subsystem_device']])261    return ""262def get_pci_prop(pci_address, prop):263    """264    Gets specific PCI ID of given PCI address. (first match only)265    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)266    :param part: prop of PCI ID.267    :return: specific PCI ID of a PCI address.268    """269    output = runcmd("lspci -Dnvmm -s %s" % pci_address)[1]270    if output:271        for line in output.splitlines():272            if prop == line.split(':')[0]:273                return line.split()[-1]274    return ""275def get_pci_id(pci_address):276    """277    Gets PCI id of given address. (first match only)278    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)279    :return: PCI ID of a PCI address.280    """281    pci_id = []282    for params in ['Vendor', 'Device', 'SVendor', 'SDevice']:283        output = get_pci_prop(pci_address, params)284        if not output:285            return ""286        pci_id.append(output)287    if pci_id:288        return ":".join(pci_id)289def get_pci_prop_name(pci_address, prop):290    """291    Gets specific PCI ID of given PCI address. (first match only)292    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)293    :param prop: prop of PCI ID.294    :return: specific PCI ID of a PCI address.295    """296    output = runcmd("lspci -Dvmm -s %s" % pci_address)[1]297    if output:298        for line in output.splitlines():299            if prop == line.split(':')[0]:300                return " ".join(line.split()[1:])301    return ""302def get_pci_name(pci_address):303    """304    Gets PCI id of given address. (first match only)305    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)306    :return: PCI ID of a PCI address.307    """308    pci_name = []309    for params in ['Vendor', 'Device']:310        output = get_pci_prop_name(pci_address, params)311        if not output:312            return313        pci_name.append(output)314    if pci_name:315        return " ".join(pci_name)316    return ""317def get_driver(pci_address):318    """319    Gets the kernel driver in use of given PCI address. (first match only)320    :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)321    :return: driver of a PCI address.322    """323    output = runcmd("lspci -ks %s" % pci_address)[1]324    if output:325        for line in output.splitlines():326            if 'Kernel driver in use:' in line:327                return line.rsplit(None, 1)[-1]328    return ""329def ioa_details():330    """331    Gets the IPR IOA details and returns332    return: list of dics, with keys ioa, serial, remote serial, pci, status333    """334    show_ioas = runcmd("iprconfig -c show-ioas")[1]335    ioas = []336    if show_ioas:337        for line in show_ioas.splitlines():338            if 'Operational' in line:339                ioa = line.split()[0]340                serial = r_serial = pci = status = ''341                ioa_details = runcmd('iprconfig -c show-details %s' % ioa)[1]342                for line in ioa_details.splitlines():343                    if line.startswith('PCI Address'):344                        pci = line.split()[-1]345                    if line.startswith('Serial Number'):346                        serial = line.split()[-1]347                    if line.startswith('Remote Adapter Serial Number'):348                        r_serial = line.split()[-1]349                    if line.startswith('Current Dual Adapter State'):350                        status = line.split()[-1]351                ioas.append({'ioa': ioa, 'pci': pci, 'serial': serial, 'r_serial': r_serial, 'status': status})352    return ioas353def get_primary_ioa(pci_address):354    """355    Gets the Primary IPR IOA in the given PCI address356    :param pci_address: PCI Address (0000:00:1f, 0000:00:1f.1, ...)357    :return: primary IOA358    """359    for ioa_detail in ioa_details():360        if pci_address in ioa_detail['pci'] and 'Primary' in ioa_detail['status']:361            return ioa_detail['ioa']362    return ''363def get_secondary_ioa(primary_ioa):364    """365    Gets the Secondary IPR IOA in the given Primary IPR IOA366    :param primary_ioa: Primary IPR IOA (sg1, sg22, ...)367    :return: secondary IOA368    """369    details = ioa_details()370    serial = ''371    for ioa_detail in details:372        if primary_ioa == ioa_detail['ioa']:373            serial = ioa_detail['r_serial']374    if not serial:375        return ''376    for ioa_detail in details:377        if serial == ioa_detail['serial']:378            return ioa_detail['ioa']379    return ''380def pci_info(pci_addrs, pci_type='', pci_blocklist='', type_blocklist=''):381    """382    Get all the information for given PCI addresses (comma separated).383    :param pci_addrs: PCI addresses384    :return: list of dictionaries of PCI information385    """386    if not pci_addrs:387        return []388    pci_addrs = pci_addrs.split(',')389    pci_addrs = [pci_addr.split('.')[0] for pci_addr in pci_addrs]390    pci_addrs = list(set(pci_addrs))391    if pci_blocklist:392        pci_blocklist = pci_blocklist.split(',')393        pci_blocklist = [pci_addr.split('.')[0] for pci_addr in pci_blocklist]394        pci_addrs = list(set(pci_addrs) - set(pci_blocklist))395    pci_addrs.sort()396    pci_list = []397    if pci_type:398        pci_type = list(set(pci_type.split(',')))399    else:400        pci_type = []401    if type_blocklist:402        type_blocklist = type_blocklist.split(',')403    for pci_addr in pci_addrs:404        pci_dic = {}405        root_disks = get_root_disks()406        pci_dic['functions'] = get_pci_fun_list(pci_addr)407        pci_dic['pci_root'] = pci_addr408        pci_dic['adapter_description'] = get_pci_name(pci_dic['functions'][0])409        pci_dic['adapter_id'] = get_pci_id(pci_dic['functions'][0])410        pci_dic['adapter_type'] = get_pci_type(pci_dic['functions'][0])411        pci_dic['driver'] = get_driver(pci_dic['functions'][0])412        pci_dic['slot'] = get_slot_from_sysfs(pci_dic['functions'][0])413        if pci_dic['adapter_type'] in type_blocklist:414            continue415        elif "All" not in pci_type and pci_dic['adapter_type'] not in pci_type:416            continue417        pci_dic['interfaces'] = []418        pci_dic['class'] = get_pci_class_name(pci_dic['functions'][0])419        for fun in pci_dic['functions']:420            pci_dic['interfaces'].extend(get_interfaces_in_pci_address(fun, pci_dic['class']))...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!!
