/usr/share/pyshared/NvidiaDetector/alternatives.py is in nvidia-common 1:0.2.44.
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 | #
# alternatives.py
#
# Copyright 2010 Canonical Services Ltd
# Author: Alberto Milone <alberto.milone@canonical.com>
#
# 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., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import os
import re
import subprocess
from subprocess import Popen, PIPE, CalledProcessError
class MultiArchUtils(object):
def __init__(self):
# We have 2 alternatives, one for each architecture
self._supported_architectures = {'i386': 'i386', 'amd64': 'x86_64'}
self._main_arch = self._get_architecture()
self._other_arch = self._supported_architectures.values()[
int(not self._supported_architectures
.values().index(self._main_arch))]
# Make sure that the PATH environment variable is set
if not os.environ.get('PATH'):
os.environ['PATH'] = '/sbin:/usr/sbin:/bin:/usr/bin'
def _get_architecture(self):
dev_null = open('/dev/null', 'w')
p1 = Popen(['dpkg', '--print-architecture'], stdout=PIPE,
stderr=dev_null)
p = p1.communicate()[0]
dev_null.close()
architecture = p.strip()
return self._supported_architectures.get(architecture)
def _get_alternative_name_from_arch(self, architecture):
alternative = '%s-linux-gnu_gl_conf' % architecture
return alternative
def get_main_alternative_name(self):
return self._get_alternative_name_from_arch(self._main_arch)
def get_other_alternative_name(self):
return self._get_alternative_name_from_arch(self._other_arch)
class Alternatives(object):
def __init__(self, master_link):
self._open_drivers_alternative = 'mesa/ld.so.conf'
self._open_egl_drivers_alternative = 'mesa-egl/ld.so.conf'
self._command = 'update-alternatives'
self._master_link = master_link
# Make sure that the PATH environment variable is set
if not os.environ.get('PATH'):
os.environ['PATH'] = '/sbin:/usr/sbin:/bin:/usr/bin'
def list_alternatives(self):
'''Get the list of alternatives for the master link'''
dev_null = open('/dev/null', 'w')
alternatives = []
p1 = Popen([self._command, '--list', self._master_link],
stdout=PIPE, stderr=dev_null)
p = p1.communicate()[0]
dev_null.close()
c = p.split('\n')
for line in c:
line.strip() and alternatives.append(line.strip())
return alternatives
def get_current_alternative(self):
'''Get the alternative in use'''
dev_null = open('/dev/null', 'w')
current_alternative = None
p1 = Popen([self._command, '--query', self._master_link],
stdout=PIPE, stderr=dev_null)
p = p1.communicate()[0]
dev_null.close()
c = p.split('\n')
for line in c:
if line.strip().startswith('Value:'):
return line.replace('Value:', '').strip()
return None
def get_alternative_by_name(self, name, ignore_pattern=None):
'''Get the alternative link by providing the driver name
ignore_pattern allows ignoring a substring in the name'''
if ignore_pattern:
name = name.replace(ignore_pattern, '')
alternatives = self.list_alternatives()
for alternative in alternatives:
if alternative.split('/')[-2] == name:
return alternative
return None
def get_open_drivers_alternative(self):
'''Get the alternative link for open drivers'''
return self.get_alternative_by_name(self._open_drivers_alternative)
def get_open_egl_drivers_alternative(self):
'''Get the alternative link for open EGL/GLES drivers'''
return self.get_alternative_by_name(self._open_egl_drivers_alternative)
def update_gmenu(self):
'''Trigger gmenu so that the icons will show up in the menu'''
try:
subprocess.check_call(['dpkg-trigger', '--by-package=fakepackage',
'gmenucache'])
subprocess.check_call(['dpkg', '--configure', '-a'])
except (OSError, CalledProcessError):
pass
def set_alternative(self, path):
'''Tries to set an alternative and returns the boolean exit status'''
try:
subprocess.check_call([self._command, '--set',
self._master_link, path])
self.ldconfig()
except CalledProcessError:
return False
self.update_gmenu()
return True
def ldconfig(self):
'''Call ldconfig'''
try:
subprocess.check_call(['ldconfig'])
except CalledProcessError:
return False
return True
def resolve_module_alias(self, alias):
'''Get the 1st kernel module name matching an alias'''
dev_null = open('/dev/null', 'w')
current_alternative = None
p1 = Popen(['modprobe', '--resolve-alias', alias], stdout=PIPE,
stderr=dev_null)
p = p1.communicate()[0]
dev_null.close()
c = p.split('\n')
for line in c:
if line.strip().startswith('Usage:'):
return None
return line.strip()
return None
|