def shorten_pci_addr()

in pci_lib/pci_lib.py [0:0]


def shorten_pci_addr(pci_addr):
    '''
    Convert a long pci address to the short version, nothing to be done if pci
    address is already a short version.

    Short addresses do not necessarily uniquely identify a device! Only use
    this for displaying address to humans. This will raise NonZeroBus if passed
    an address that cannot be shortened. Consider `maybe_shorten_pci_addr`
    '''

    m1 = LONG_PCI_ADDR_REGEX.match(pci_addr)
    m2 = SHORT_PCI_ADDR_REGEX.match(pci_addr)
    if m1:
        if m1.group(1) != '0000':
            raise NonZeroDomain()
        pci_addr = '{}:{}.{}'.format(
            m1.group(2), m1.group(3), m1.group(4))
    elif m2:
        pass
    else:
        log.error('Invalid pci address %s', pci_addr)
        pci_addr = None

    return pci_addr