From dcd1c83597647b6b3e6893a340b91a02a50f7f77 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Fri, 14 Mar 2008 21:47:19 +0000 Subject: [PATCH] Add the missing features of pypciscan to pypci's get_devices, and drop the former entirely. Install pypci.py. Package not made noarch due to (old?) yum issues with going from arch to noarch. --- pypci.py | 102 ++++++++++++++++++++++++-------------------------- pypcilib.spec | 24 +++++++----- pypciscan.c | 97 ----------------------------------------------- setup.py | 8 ++-- 4 files changed, 67 insertions(+), 164 deletions(-) delete mode 100644 pypciscan.c diff --git a/pypci.py b/pypci.py index 34124e1..1632bc5 100644 --- a/pypci.py +++ b/pypci.py @@ -1,56 +1,52 @@ import os -try: - from pypciscan import get_devices -except: - def get_devices(): - """ This is a replacement to the version in pypciscan library for 3.3 and lower bootcds - that will help maintain backward compatibility. This version has limitations wrt accuracy - that the library does not. In particular it is limited to the output of - lspci and 'forces' all devices to appear on the '0000' domain, rather than - where they actually are.""" - - ret = {} - pci_cmd = os.popen("""/sbin/lspci -nvm | sed -e 's/\t/ /g' -e 's/ Class //' -e 's/^/"/' -e 's/$/"/' -e 's/$/,/' -e 's/^"",$/],[/'""", 'r') - pci_str = "[" + pci_cmd.read() + "]" - pci_list = eval(pci_str) - - pci_devlist = [] - # convert each entry into a dict. and convert strings to ints. - for dev in pci_list: - rec = {} - for field in dev: - s = field.split(":") - if len(s) > 2: - # There are two 'device' fields in the output. Append - # 'addr' for the bus address, identified by the extra ':'. - end=":".join(s[1:]) - key = s[0].lower() + "addr" - value = end.strip() - else: - key = s[0].lower() - value = int(s[1].strip(), 16) - - rec[key] = value - - pci_devlist.append(rec) - - ret = {} - # convert this list of devices into the format expected by the - # consumer of get_devices() - for dev in pci_devlist: - if 'device' not in dev: - continue - - if 'sdevice' in dev: subdev = dev['sdevice'] - else: subdev = 0xffffffffL - - if 'svendor' in dev: subvend = dev['svendor'] - else: subvend = 0xffffffffL - - key = "0000:%s" % dev['deviceaddr'] - value = (dev['vendor'], dev['device'], subvend, subdev, dev['class'] << 8) - ret[key] = value - - return ret + +def get_devices(): + """ This is a replacement to the pypciscan library.""" + + ret = {} + pci_cmd = os.popen("""/sbin/lspci -Dnvm | sed -e 's/\t/ /g' -e 's/^/"/' -e 's/$/"/' -e 's/$/,/' -e 's/^"",$/],[/'""", 'r') + pci_str = "[" + pci_cmd.read() + "]" + pci_list = eval(pci_str) + + pci_devlist = [] + # convert each entry into a dict. and convert strings to ints. + for dev in pci_list: + rec = {} + for field in dev: + s = field.split(":") + if len(s) > 2: + # There are two 'device' fields in the output. Append + # 'addr' for the bus address, identified by the extra ':'. + end=":".join(s[1:]) + key = s[0].lower() + "addr" + value = end.strip() + else: + key = s[0].lower() + value = int(s[1].strip(), 16) + + rec[key] = value + + pci_devlist.append(rec) + + ret = {} + # convert this list of devices into the format expected by the + # consumer of get_devices() + for dev in pci_devlist: + if 'deviceaddr' not in dev: + continue + + if 'sdevice' in dev: subdev = dev['sdevice'] + else: subdev = 0xffffffffL + + if 'svendor' in dev: subvend = dev['svendor'] + else: subvend = 0xffffffffL + + if 'progif' in dev: progif = dev['progif'] + else: progif = 0 + + value = (dev['vendor'], dev['device'], subvend, subdev, dev['class'] << 8 | progif) + ret[dev['deviceaddr']] = value + + return ret import pypcimap diff --git a/pypcilib.spec b/pypcilib.spec index 9ff74a8..0b49cdc 100644 --- a/pypcilib.spec +++ b/pypcilib.spec @@ -4,7 +4,7 @@ %define url $URL$ %define name pypcilib -%define version 0.1 +%define version 0.2 %define taglevel 1 %define release %{taglevel}%{?pldistro:.%{pldistro}}%{?date:.%{date}} @@ -24,10 +24,10 @@ Distribution: PlanetLab %{plrelease} #URL: http://svn.planet-lab.org/wiki/pypcilib URL: %(echo %{url} | cut -d ' ' -f 2) -BuildRequires: pciutils-devel - Source0: pypcilib-%{version}.tar.bz2 +Requires: /sbin/lspci + BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(id -un) %description @@ -40,13 +40,14 @@ pciutils' libpci, and to parse the modules.pcimap file. %build -CFLAGS="%{optflags}" %{__python} setup.py build +%{__python} setup.py build %install rm -fr "%{buildroot}" %{__python} setup.py install -O1 --skip-build --root "%{buildroot}" -touch %{buildroot}%{python_sitearch}/pypcimap.py{c,o} +touch %{buildroot}%{python_sitelib}/pypcimap.py{c,o} +touch %{buildroot}%{python_sitelib}/pypci.py{c,o} %clean @@ -55,12 +56,17 @@ rm -fr "%{buildroot}" %files %defattr(-,root,root,-) -%{python_sitearch}/pypciscan.so -%{python_sitearch}/pypcimap.py -%ghost %{python_sitearch}/pypcimap.pyc -%ghost %{python_sitearch}/pypcimap.pyo +%{python_sitelib}/pypcimap.py +%ghost %{python_sitelib}/pypcimap.pyc +%ghost %{python_sitelib}/pypcimap.pyo +%{python_sitelib}/pypci.py +%ghost %{python_sitelib}/pypci.pyc +%ghost %{python_sitelib}/pypci.pyo %changelog +* Fri Mar 14 2008 Daniel Hokka Zakrisson - 0.2-1 +- Remove pypciscan library, reimplemented in Python + * Mon Nov 19 2007 Daniel Hokka Zakrisson - 0.1-1 - initial release diff --git a/pypciscan.c b/pypciscan.c deleted file mode 100644 index 24b81a9..0000000 --- a/pypciscan.c +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright 2007 The Trustees of Princeton University - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -* Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following -disclaimer in the documentation and/or other materials provided -with the distribution. - -* Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived -from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON -UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS -OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED -AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY -WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. - -*/ - -#include - -#include -#include - -#include - - -static PyObject *get_devices(PyObject *self, PyObject *args) -{ - struct pci_access *pacc; - struct pci_dev *dev; - PyObject *ret; - char buf[128]; - - if ((pacc = pci_alloc()) == NULL) - return PyErr_SetFromErrno(PyExc_OSError); - - pci_init(pacc); - - pci_scan_bus(pacc); - - ret = PyDict_New(); - if (!ret) - return PyErr_SetFromErrno(PyExc_OSError); - - for (dev = pacc->devices; dev; dev = dev->next) { - u16 subvendor = -1, subdevice = -1; - PyObject *value; - u8 progif = 0; - - pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS); - if (dev->hdrtype == PCI_HEADER_TYPE_NORMAL) { - subvendor = pci_read_word(dev, PCI_SUBSYSTEM_VENDOR_ID); - subdevice = pci_read_word(dev, PCI_SUBSYSTEM_ID); - } - progif = pci_read_byte(dev, PCI_CLASS_PROG); - - snprintf(buf, sizeof(buf), "%04x:%02x:%02x.%02x", dev->domain, dev->bus, dev->dev, dev->func); - value = Py_BuildValue("iiiii", dev->vendor_id, dev->device_id, - subvendor, subdevice, dev->device_class << 8 | progif); - if (!value) - return NULL; - if (PyDict_SetItemString(ret, buf, value) == -1) - return NULL; - } - - return ret; -} - - -static PyMethodDef methods[] = { - { "get_devices", get_devices, METH_VARARGS, - "Returns a dict of PCI devices, indexed by the domain:bus:dev.func string, " - "each item consisting of [vendor, device, subvendor, subdevice, class]" }, - { NULL, NULL, 0, NULL } -}; - -PyMODINIT_FUNC -initpypciscan(void) -{ - PyObject *mod; - mod = Py_InitModule("pypciscan", methods); -} diff --git a/setup.py b/setup.py index 4295492..782ed1e 100644 --- a/setup.py +++ b/setup.py @@ -3,11 +3,9 @@ from distutils.core import setup, Extension setup(name='pypciscan', - version='0.1', + version='0.2', description='PCI scanning from Python', - author='Daniel Hokka Zakrisson', + author='Daniel Hokka Zakrisson and Stephen Soltesz', author_email='daniel@hozac.com', - py_modules=['pypcimap'], - ext_modules=[Extension('pypciscan', ['pypciscan.c'], - libraries=['pci', 'z'])], + py_modules=['pypcimap', 'pypci'], ) -- 2.43.0