/usr/lib/python2.7/dist-packages/isenkram/lookup.py is in isenkram-cli 0.22.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | #!/usr/bin/python
# Copyright (C) 2013 Petter Reinholdtsen <pere@hungry.com>
#
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
__author__ = "Petter Reinholdtsen <pere@hungry.com>"
import sys
import apt
import gc
import gi
import fnmatch
import os
import urllib
import errno
pkgdir = "/usr/share/isenkram"
def my_modaliases():
modaliases = set()
for dirpath, _, filenames in os.walk("/sys/devices"):
if "modalias" in filenames:
with open(dirpath + "/modalias") as sysfile:
modaliases.add(sysfile.read().strip())
with open("/proc/modules") as procfile:
line = procfile.readline()
while line:
modalias = "lkmodule:%s" % (line.split(" "))[0]
modaliases.add(modalias)
line = procfile.readline()
procfile.close()
return list(modaliases)
def modalias_match(alias, candidates):
# print "modalias_match('%s', %s)" % (alias, candidates)
for part in candidates.split(')'):
part = part.strip(', ')
if not part:
continue
module, lst = part.split('(')
for candidate in lst.split(','):
candidate = candidate.strip()
bus = candidate.split(':', 1)[0]
# print bus, alias, candidate
# print candidate, alias
if fnmatch.fnmatch(alias, candidate):
# print "Match"
return True
return False
def pkgs_handling_apt_modaliases(modaliaslist):
cache = apt.Cache()
cache.open(None)
thepkgs = {}
for pkg in cache:
version = pkg.candidate
if version is None:
version = pkg.installed
if version is None:
continue
record = version.record
if not 'Modaliases' in record:
continue
modaliases = record['Modaliases']
for modalias in modaliaslist:
if modalias_match(modalias, modaliases):
thepkgs[pkg.name] = True
# Workaround for bug #745487 in python-apt, fix resource leak
cache = None
gc.collect()
return thepkgs.keys()
def pkgs_handling_appstream_modaliases(modaliaslist):
thepkgs = {}
gi.require_version('AppStream', '1.0')
from gi.repository import AppStream
db = AppStream.Database()
db.open()
cpts = db.get_all_components()
ma_cpts = list()
for cpt in cpts:
provided = cpt.get_provided_for_kind(AppStream.ProvidedKind.MODALIAS)
if provided:
ma_cpts.append(cpt)
for cpt in ma_cpts:
prov = cpt.get_provided_for_kind(AppStream.ProvidedKind.MODALIAS)
packages = cpt.get_pkgnames()
for modalias in modaliaslist:
for item in prov.get_items():
pkgmodalias = str(item)
# Transport modalias to module(alias) format to be
# able to use modalias_match(). Perhaps better to
# create a new modalias_match() like function without
# that formatting requirement.
if modalias_match(modalias, 'dummy(' + pkgmodalias + ')'):
for pkg in packages:
thepkgs[pkg] = True
return thepkgs.keys()
def check_packages_file(pkghash, fileref, modaliaslist):
line = fileref.readline()
while line:
header, sep, packagename = line.strip().partition(": ")
header, sep, modaliases = fileref.readline().strip().partition(": ")
blank = fileref.readline()
for modalias in modaliaslist:
# print "Match ", packagename, " ", modalias, " ", modaliases
if modalias_match(modalias, modaliases):
pkghash[packagename] = True
line = fileref.readline()
fileref.close()
def pkgs_handling_extra_modaliases(modaliaslist):
"""
Look up package-hardware mappings from svn and local file
"""
thepkgs = {}
url = "http://anonscm.debian.org/gitweb/?p=collab-maint/isenkram.git;a=blob_plain;f=modaliases;hb=HEAD"
try:
f = urllib.urlopen(url)
check_packages_file(thepkgs, f, modaliaslist)
except IOError, e:
if errno.ENOENT == e.errno: # Most likely lack network connection
pass
try:
try:
f = open("modaliases")
check_packages_file(thepkgs, f, modaliaslist)
except IOError as e:
f = open(pkgdir + "/modaliases")
check_packages_file(thepkgs, f, modaliaslist)
f.close()
for dirpath, _, filenames in os.walk(pkgdir + "/modaliases.d"):
for filename in filenames:
with open(dirpath + "/modalias") as aliasfile:
check_packages_file(thepkgs, aliasfile, modaliaslist)
except: # Ignore errors
pass
# print thepkgs
return thepkgs.keys()
def main():
if 1 < len(sys.argv):
hwaliases = sys.argv[1:]
else:
hwaliases = my_modaliases()
print "Locating packages supporting this hardware (AppStream):"
for pkg in sorted(pkgs_handling_appstream_modaliases(hwaliases)):
print " %s" % pkg
print "Locating packages supporting this hardware (extra, svn/local):"
for pkg in sorted(pkgs_handling_extra_modaliases(hwaliases)):
print " %s" % pkg
print "Locating packages supporting this hardware (APT control field):"
for pkg in sorted(pkgs_handling_apt_modaliases(hwaliases)):
print " %s" % pkg
if __name__ == '__main__':
main()
|