home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import re
- import subprocess
- from subprocess import Popen, PIPE
- import sys
- import logging
- aliasesPath = '/usr/share/jockey/modaliases/'
-
- class NoDatadirError(Exception):
- '''Exception thrown when no modaliases dir can be found'''
- pass
-
-
- class NvidiaDetection(object):
- '''
- A simple class to:
- * Detect the available graphics cards
- * See what drivers support them (If they are
- NVIDIA cards). If more than one card is
- available, try to find the highest common
- driver version which supports them all.
- (READ the comments in the code for further
- details)
- * Return the recommended driver version
- '''
- oldPackages = [
- 'nvidia-glx',
- 'nvidia-glx-new',
- 'nvidia-glx-legacy',
- 'nvidia-glx-envy',
- 'nvidia-glx-new-envy',
- 'nvidia-glx-legacy-envy',
- 'nvidia-glx-177']
-
- def __init__(self, printonly = None, verbose = None, datadir = aliasesPath):
- """
- printonly = if set to None will make an instance
- of this class return the selected
- driver.
- If set to True it won't return
- anything. It will simply and print
- the choice.
-
- verbose = if set to True will make the methods
- print what is happening.
- """
- self.printonly = printonly
- self.verbose = verbose
- if not os.path.isdir(datadir):
- print 'none'
- logging.debug('dir %s not found' % datadir)
- raise NoDatadirError, "datadir '%s' not found" % datadir
- os.path.isdir(datadir)
- self.datadir = datadir
- self.detection()
- self.getData()
- self.getCards()
- self.removeUnsupported()
- if printonly == True:
- self.printSelection()
- else:
- self.selectDriver()
-
-
- def detection(self):
- '''
- Detect the models of the graphics cards
- and store them in self.cards
- '''
- self.cards = []
- p1 = Popen([
- 'lspci',
- '-n'], stdout = PIPE)
- p = p1.communicate()[0].split('\n')
- indentifier1 = re.compile('.*0300: *(.+):(.+) \\(.+\\)')
- indentifier2 = re.compile('.*0300: *(.+):(.+)')
- for line in p:
- m1 = indentifier1.match(line)
- m2 = indentifier2.match(line)
- if m1:
- id1 = m1.group(1).strip().lower()
- id2 = m1.group(2).strip().lower()
- id = id1 + ':' + id2
- self.cards.append(id)
- continue
- if m2:
- id1 = m2.group(1).strip().lower()
- id2 = m2.group(2).strip().lower()
- id = id1 + ':' + id2
- self.cards.append(id)
- continue
-
-
-
- def getData(self):
- '''
- Get the data from the modaliases for each driver
- and store them in self.drivers
- '''
- files = os.listdir(self.datadir)
- self.drivers = { }
- for file in files:
- a = open(self.datadir + file, 'r')
- lines = a.readlines()
- indentifier = re.compile('.*alias pci:v0000(.+)d0000(.+)sv.*nvidia.*nvidia-glx-(.+).*')
- for line in lines:
- m1 = indentifier.match(line)
- if m1:
- id1 = m1.group(1).strip().lower()
- id2 = m1.group(2).strip().lower()
- drivername = m1.group(3).strip().lower()
- fullname = 'nvidia-glx-%s' % drivername.strip()
- if id1 in ('10de', '12d2') and fullname not in self.oldPackages:
- self.drivers.setdefault(int(drivername), []).append(id1 + ':' + id2)
-
- fullname not in self.oldPackages
-
- a.close()
-
- if len(self.drivers.keys()) == 0:
- print 'none'
-
-
-
- def getCards(self):
- '''
- See if the detected graphics cards are NVIDIA cards.
- If they are NVIDIA cards, append them to self.nvidiaCards
- '''
- self.driversForCards = { }
- self.nvidiaCards = []
- for card in self.cards:
- if card[0:card.find(':')] == '10de':
- if self.verbose:
- print 'NVIDIA card found (' + card + ')'
-
- self.nvidiaCards.append(card)
- continue
-
- self.orderedList = self.drivers.keys()
- self.orderedList.sort(reverse = True)
- for card in self.nvidiaCards:
- supported = False
- for driver in self.orderedList:
- if card in self.drivers[driver]:
- supported = True
- if self.verbose:
- print 'Card', card, 'supported by driver', driver
-
- self.driversForCards.setdefault(card, []).append(driver)
- continue
-
- if supported == False:
- self.driversForCards.setdefault(card, []).append(None)
- continue
-
-
-
- def removeUnsupported(self):
- '''
- Remove unsupported cards from self.nvidiaCards and from
- self.driversForCards
- '''
- unsupportedCards = []
- for card in self.driversForCards:
- if None in self.driversForCards[card]:
- unsupportedCards.append(card)
- continue
-
- for unsupported in unsupportedCards:
- if self.verbose:
- print 'Removing unsupported card', unsupported
-
- self.nvidiaCards.remove(unsupported)
- del self.driversForCards[unsupported]
-
-
-
- def selectDriver(self):
- '''
- If more than one card is available, try to get the highest common driver
- '''
- cardsNumber = len(self.nvidiaCards)
- if cardsNumber > 0:
- if cardsNumber > 1:
- occurrence = { }
- for card in self.driversForCards:
- for drv in self.driversForCards[card]:
- occurrence.setdefault(drv, 0)
- occurrence[drv] += 1
-
-
- occurrences = occurrence.keys()
- occurrences.sort(reverse = True)
- candidates = []
- for driver in occurrences:
- if occurrence[driver] == cardsNumber:
- candidates.append(driver)
- continue
-
- if len(candidates) > 0:
- candidates.sort(reverse = True)
- choice = candidates[0]
- if self.verbose and not (self.printonly):
- print 'Recommended NVIDIA driver:', choice
-
- else:
- choice = occurrences[0]
- if self.verbose and not (self.printonly):
- print 'Recommended NVIDIA driver:', choice
-
- else:
- choice = self.driversForCards[self.driversForCards.keys()[0]][0]
- if self.verbose and not (self.printonly):
- print 'Recommended NVIDIA driver:', choice
-
- choice = 'nvidia-glx-' + str(choice)
- elif self.verbose:
- print 'No NVIDIA package to install'
-
- choice = None
- return choice
-
-
- def checkpkg(self, pkglist):
- '''
- USAGE:
- * pkglist is the list of packages you want to check
- * use lists for one or more packages
- * use a string if it is only one package
- * lists will work well in both cases
- '''
- lines = []
- notinstalled = []
- p1 = Popen([
- 'dpkg',
- '--get-selections'], stdout = PIPE)
- p = p1.communicate()[0]
- c = p.split('\n')
- for line in c:
- if line.find('\tinstall') != -1:
- lines.append(line.split('\t')[0])
- continue
-
- if self.isstr(pkglist) == True:
-
- try:
- if lines.index(pkglist):
- pass
- except ValueError:
- notinstalled.append(pkglist)
- except:
- None<EXCEPTION MATCH>ValueError
-
-
- None<EXCEPTION MATCH>ValueError
- for pkg in pkglist:
-
- try:
- if lines.index(pkg):
- pass
- continue
- except ValueError:
- notinstalled.append(pkg)
- continue
-
-
-
- return notinstalled
-
-
- def isstr(self, elem):
- if not isinstance(elem, type('')):
- pass
- return isinstance(elem, type(u''))
-
-
- def islst(self, elem):
- if not isinstance(elem, type(())):
- pass
- return isinstance(elem, type([]))
-
-
- def getDrivers(self):
- '''
- oldPackages = a list of the names of the obsolete drivers
- notInstalled = a list of the obsolete drivers which are not
- installed
- '''
- installedPackage = None
- notInstalled = self.checkpkg(self.oldPackages)
- for package in self.oldPackages:
- if package not in notInstalled:
- installedPackage = package
- continue
-
- return len(notInstalled) != len(self.oldPackages)
-
-
- def printSelection(self):
- '''
- Part for the kernel postinst.d/ hook
- '''
- driver = self.selectDriver()
- if self.getDrivers():
- if driver:
- print driver
- else:
- print 'none'
- else:
- print 'none'
-
-
-