26 from builtins
import object
34 attrs[
'_order'] = mcs.nextorder
36 return type.__new__(mcs, name, bases, attrs)
38 class OS(metaclass=OrderedType):
40 def __init__(self, ostype=-1, func=None, inst=None):
47 self.
ostype = ostype
if ostype != -1
else 'posix'
49 if (func
is not None)
and not callable(func):
50 raise TypeError((self.__class__.__name__ +
"() requires a " \
51 "provided function be callable."))
66 return self.__class__(self.
ostype, func, inst)
73 if self.
func is not None:
74 raise TypeError((self.__class__.__name__ +
"() has already " \
75 "been given a processing function"))
76 if (func
is None)
or not callable(func):
77 raise TypeError((self.__class__.__name__ +
"() takes exactly " \
78 "one callable as a function, (0 given)"))
82 raise RuntimeError((self.__class__.__name__ +
"() has not " \
83 "been given a callable function"))
87 if (self.
ostype is not None)
and (os.name != self.
ostype):
96 res = self.
func(*args, **kwargs)
108 _requires_func =
False
109 def __init__(self, filename, ostype='posix', func=None, inst=None):
111 super(OSWithFile, self).
__init__(ostype, func, inst)
120 if not os.path.exists(self.
filename):
127 return super(OSWithFile, self).
do_test(text)
130 self.
inst.name = text
138 except AttributeError:
pass
142 from subprocess
import Popen, PIPE
148 path =
'/usr/bin/uname'
149 if not os.path.exists(path):
154 for k,v
in ((
'OS',
'-s'),
156 p = Popen([path, v], stdout=PIPE)
158 self.
_uname[k] = p.stdout.read().strip()
163 if len(self.
uname) == 0:
170 for k,v
in list(attrs.items()):
171 if isinstance(v, OS):
173 OSs.append((v._order, k))
175 attrs[
'_oslist'] = [i[1]
for i
in sorted(OSs)]
176 return type.__new__(mcs, name, bases, attrs)
181 for attr
in obj._oslist:
183 if getattr(obj, attr)():
200 }[os.sys.getwindowsversion()[3],
201 os.sys.getwindowsversion()[0],
202 os.sys.getwindowsversion()[1] ]
203 return "Windows " + win_version
224 text = open(
'/etc/issue.net').
read().strip()
225 if text.find(
'Ubuntu'):
227 mtext = open(
'/var/log/installer/media-info').
read().strip()
231 if 'Mythbuntu' in mtext:
232 text.replace(
'Ubuntu',
'Mythbuntu')
251 text = text.split(
'\n')[0].strip()
252 return re.sub(
'\(\w*\)$',
'', text)
260 return 'FreeBSD '+version
264 def OSX(self, OS, version):
267 major,minor,point = [int(a)
for a
in version.split(
'.')]
268 return 'OS X 10.%s.%s' % (major-4, minor)
272 from subprocess
import Popen, PIPE
273 executable =
'lsb_release'
274 for path
in os.environ[
'PATH'].split(
':'):
275 fullpath = os.path.join(path, executable)
276 if os.path.exists(fullpath):
281 p = Popen([fullpath,
'--id',
'--codename',
'--release',
'--short'],
282 stdout=PIPE, close_fds=
True)
284 return p.stdout.read().decode().strip().replace(
'\n',
' ')
289 return 'Unknown Linux '+version
293 if __name__ ==
'__main__':
295 print(
'Test results="{}"'.format(results))