#!/usr/bin/python
# -*- coding: utf-8 -*-
# smolt - Fedora hardware profiler
#
# Copyright (C) 2008 James Meyer <james.meyer@operamail.com>
# Copyright (C) 2008 Yaakov M. Nemoy <loupgaroublond@gmail.com>
# Copyright (C) 2009 Carlos Gonçalves <mail@cgoncalves.info>
# Copyright (C) 2009 François Cami <fcami@fedoraproject.org>
# Copyright (C) 2010 Mike McGrath <mmcgrath@redhat.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 St, Fifth Floor, Boston, MA 02110-1301, USA.
import os
class OrderedType( type ):
nextorder = 0
def __new__(mcs, name, bases, attrs):
attrs['_order'] = mcs.nextorder
nextorder += 1
return type.__new__(mcs, name, bases, attrs)
class OS( object ):
__metaclass__ = OrderedType
def __init__(self, ostype='posix', func=None, inst=None):
self.ostype = ostype
self.func = func
self.inst = inst
if func:
self.__doc__ = func.__doc__
self.__name__ = func.__name__
self.__module__ = func.__module__
def __get__(self, inst, owner):
if inst is None:
return inst
func = self.func.__get__(inst, owner) if self.func else None
return self.__class__(self.ostype, func, inst)
def __call__(self, *args, **kwargs):
if self.inst is None:
# we want to add a function here
if self.func is not None:
raise SyntaxError("Function has already been defined")
if (len(args) != 1) or (len(kwargs) > 0):
raise SyntaxError(("Decorator must be called with function "
"as argument"))
self.func = args[0]
else:
if self.func is None:
raise SyntaxError("Test function must be defined")
if os.type() != self.ostype:
return False
try:
res = self.func()
except:
return False
else:
if res:
self.inst.name = res
return True
return False
class OSWithFile( OS ):
def __init__(self, filename, ostype='posix', func=None, inst=None):
self.filename = filename
super(OSWithFile, self).__init__(ostype, func, inst)
def __get__(self, inst, owner):
if inst is None:
return inst
func = self.func.__get__(inst, owner) if self.func else None
return self.__class__(self.filename, self.ostype, func, inst)
def __call__(self, *args, **kwargs):
if self.inst is None:
# we want to add a function here
if self.func is not None:
raise SyntaxError("Function has already been defined")
if (len(args) != 1) or (len(kwargs) > 0):
raise SyntaxError(("Decorator must be called with function "
"as argument"))
self.func = args[0]
else:
if os.type() != self.ostype:
return False
if not os.path.exists(self.filename):
return False
text = open(self.filename).read().strip()
if self.func:
try:
res = self.func(text)
except:
return False
else:
if res:
self.inst.name = text
return True
return False
self.inst.name = text
return True
class OSInfoType( type ):
def __new__(mcs, name, bases, attrs):
OSs = []
for k,v in attrs.items():
if isinstance(v, OS):
OSs.append((v._order, k))
attrs['_oslist'] = [i[1] for i in sorted(OSs)]
return type.__new__(mcs, name, bases, attrs)
def __call__(cls):
obj = cls.__new__(cls)
obj.__init__()
return obj.name
class get_os_info( object ):
__metaclass__ = OSInfoType
def __init__(self):
self.name = 'Unknown'
for attr in self._oslist:
if getattr(self, attr)():
return
@OS('nt')
def windows(self):
win_version = {
(1, 4, 0): '95',
(1, 4, 10): '98',
(1, 4, 90): 'ME',
(2, 4, 0): 'NT',
(2, 5, 0): '2000',
(2, 5, 1): 'XP'
}[os.sys.getwindowsversion()[3],
os.sys.getwindowsversion()[0],
os.sys.getwindowsversion()[1] ]
return "Windows " + win_version
blag_linux = OSWithFile('/etc/blag-release')
mythvantage = OSWithFile('/etc/mythvantage-release')
knoppmyth = OSWithFile('/etc/KnoppMyth-version')
linhes = OSWithFile('/etc/LinHES-release')
mythdora = OSWithFile('/etc/mythdora-release')
@OSWithFile('/etc/arch-release')
def archlinux(self):
return 'Arch Linux'
@OSWithFile('/etc/aurox-release')
def auroxlinux(self):
return 'Aurox Linux'
conectiva = OSWithFile('/etc/conectiva-release')
debian = OSWithFile('/etc/debian_release')
@OSWithFile('/etc/debian_version')
def ubuntu(self):
text = open('/etc/issue.net').read().strip()
if text.find('Ubuntu'):
mtext = open('/var/log/installer/media-info').read().strip()
if 'Mythbuntu' in mtext:
text = text.replace('Ubuntu', 'Mythbuntu')
return text
return False
debian2 = OSWithFile('/etc/debian_version')
fedora = OSWithFile('/etc/fedora-release')