Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.26 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # smolt - Fedora hardware profiler
  5. #
  6. # Copyright (C) 2008 James Meyer <james.meyer@operamail.com>
  7. # Copyright (C) 2008 Yaakov M. Nemoy <loupgaroublond@gmail.com>
  8. # Copyright (C) 2009 Carlos Gonçalves <mail@cgoncalves.info>
  9. # Copyright (C) 2009 François Cami <fcami@fedoraproject.org>
  10. # Copyright (C) 2010 Mike McGrath <mmcgrath@redhat.com>
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program; if not, write to the Free Software
  24. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  25.  
  26. import os
  27.  
  28. class OrderedType( type ):
  29.     nextorder = 0
  30.     def __new__(mcs, name, bases, attrs):
  31.         attrs['_order'] = mcs.nextorder
  32.         nextorder += 1
  33.         return type.__new__(mcs, name, bases, attrs)
  34.  
  35. class OS( object ):
  36.     __metaclass__ = OrderedType
  37.     def __init__(self, ostype='posix', func=None, inst=None):
  38.         self.ostype = ostype
  39.         self.func = func
  40.         self.inst = inst
  41.  
  42.         if func:
  43.             self.__doc__ = func.__doc__
  44.             self.__name__ = func.__name__
  45.             self.__module__ = func.__module__
  46.  
  47.     def __get__(self, inst, owner):
  48.         if inst is None:
  49.             return inst
  50.         func = self.func.__get__(inst, owner) if self.func else None
  51.         return self.__class__(self.ostype, func, inst)
  52.  
  53.     def __call__(self, *args, **kwargs):
  54.         if self.inst is None:
  55.             # we want to add a function here
  56.             if self.func is not None:
  57.                 raise SyntaxError("Function has already been defined")
  58.             if (len(args) != 1) or (len(kwargs) > 0):
  59.                 raise SyntaxError(("Decorator must be called with function "
  60.                                    "as argument"))
  61.             self.func = args[0]
  62.         else:
  63.             if self.func is None:
  64.                 raise SyntaxError("Test function must be defined")
  65.             if os.type() != self.ostype:
  66.                 return False
  67.  
  68.             try:
  69.                 res = self.func()
  70.             except:
  71.                 return False
  72.             else:
  73.                 if res:
  74.                     self.inst.name = res
  75.                     return True
  76.                 return False
  77.  
  78. class OSWithFile( OS ):
  79.     def __init__(self, filename, ostype='posix', func=None, inst=None):
  80.         self.filename = filename
  81.         super(OSWithFile, self).__init__(ostype, func, inst)
  82.  
  83.     def __get__(self, inst, owner):
  84.         if inst is None:
  85.             return inst
  86.         func = self.func.__get__(inst, owner) if self.func else None
  87.         return self.__class__(self.filename, self.ostype, func, inst)
  88.  
  89.     def __call__(self, *args, **kwargs):
  90.         if self.inst is None:
  91.             # we want to add a function here
  92.             if self.func is not None:
  93.                 raise SyntaxError("Function has already been defined")
  94.             if (len(args) != 1) or (len(kwargs) > 0):
  95.                 raise SyntaxError(("Decorator must be called with function "
  96.                                    "as argument"))
  97.             self.func = args[0]
  98.  
  99.         else:
  100.             if os.type() != self.ostype:
  101.                 return False
  102.             if not os.path.exists(self.filename):
  103.                 return False
  104.  
  105.             text = open(self.filename).read().strip()
  106.             if self.func:
  107.                 try:
  108.                     res = self.func(text)
  109.                 except:
  110.                     return False
  111.                 else:
  112.                     if res:
  113.                         self.inst.name = text
  114.                         return True
  115.                     return False
  116.             self.inst.name = text
  117.             return True
  118.  
  119. class OSInfoType( type ):
  120.     def __new__(mcs, name, bases, attrs):
  121.         OSs = []
  122.         for k,v in attrs.items():
  123.             if isinstance(v, OS):
  124.                 OSs.append((v._order, k))
  125.         attrs['_oslist'] = [i[1] for i in sorted(OSs)]
  126.         return type.__new__(mcs, name, bases, attrs)
  127.  
  128.     def __call__(cls):
  129.         obj = cls.__new__(cls)
  130.         obj.__init__()
  131.         return obj.name
  132.  
  133. class get_os_info( object ):
  134.     __metaclass__ = OSInfoType
  135.     def __init__(self):
  136.         self.name = 'Unknown'
  137.         for attr in self._oslist:
  138.             if getattr(self, attr)():
  139.                 return
  140.  
  141.     @OS('nt')
  142.     def windows(self):
  143.         win_version = {
  144.               (1, 4, 0): '95',
  145.               (1, 4, 10): '98',
  146.               (1, 4, 90): 'ME',
  147.               (2, 4, 0): 'NT',
  148.               (2, 5, 0): '2000',
  149.               (2, 5, 1): 'XP'
  150.         }[os.sys.getwindowsversion()[3],
  151.           os.sys.getwindowsversion()[0],
  152.           os.sys.getwindowsversion()[1] ]
  153.         return "Windows " + win_version
  154.  
  155.     blag_linux  = OSWithFile('/etc/blag-release')
  156.     mythvantage = OSWithFile('/etc/mythvantage-release')
  157.     knoppmyth   = OSWithFile('/etc/KnoppMyth-version')
  158.     linhes      = OSWithFile('/etc/LinHES-release')
  159.     mythdora    = OSWithFile('/etc/mythdora-release')
  160.  
  161.     @OSWithFile('/etc/arch-release')
  162.     def archlinux(self):
  163.         return 'Arch Linux'
  164.  
  165.     @OSWithFile('/etc/aurox-release')
  166.     def auroxlinux(self):
  167.         return 'Aurox Linux'
  168.  
  169.     conectiva   = OSWithFile('/etc/conectiva-release')
  170.     debian      = OSWithFile('/etc/debian_release')
  171.  
  172.     @OSWithFile('/etc/debian_version')
  173.     def ubuntu(self):
  174.         text = open('/etc/issue.net').read().strip()
  175.         if text.find('Ubuntu'):
  176.             mtext = open('/var/log/installer/media-info').read().strip()
  177.             if 'Mythbuntu' in mtext:
  178.                 text = text.replace('Ubuntu', 'Mythbuntu')
  179.             return text
  180.         return False
  181.  
  182.     debian2     = OSWithFile('/etc/debian_version')
  183.     fedora      = OSWithFile('/etc/fedora-release')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement