26 from __future__
import print_function
27 from builtins
import object
35 attrs[
'_order'] = mcs.nextorder
37 return type.__new__(mcs, name, bases, attrs)
39 class OS(metaclass=OrderedType):
41 def __init__(self, ostype=-1, func=None, inst=None):
48 self.
ostype = ostype
if ostype != -1
else 'posix'
50 if (func
is not None)
and not callable(func):
51 raise TypeError((self.__class__.__name__ +
"() requires a " \
52 "provided function be callable."))
67 return self.__class__(self.
ostype, func, inst)
74 if self.
func is not None:
75 raise TypeError((self.__class__.__name__ +
"() has already " \
76 "been given a processing function"))
77 if (func
is None)
or not callable(func):
78 raise TypeError((self.__class__.__name__ +
"() takes exactly " \
79 "one callable as a function, (0 given)"))
83 raise RuntimeError((self.__class__.__name__ +
"() has not " \
84 "been given a callable function"))
88 if (self.
ostype is not None)
and (os.name != self.
ostype):
97 res = self.
func(*args, **kwargs)
109 _requires_func =
False
110 def __init__(self, filename, ostype='posix', func=None, inst=None):
112 super(OSWithFile, self).
__init__(ostype, func, inst)
121 if not os.path.exists(self.
filename):
128 return super(OSWithFile, self).
do_test(text)
131 self.
inst.name = text
139 except AttributeError:
pass
143 from subprocess
import Popen, PIPE
149 path =
'/usr/bin/uname'
150 if not os.path.exists(path):
155 for k,v
in ((
'OS',
'-s'),
157 p = Popen([path, v], stdout=PIPE)
159 self.
_uname[k] = p.stdout.read().strip()
164 if len(self.
uname) == 0:
171 for k,v
in list(attrs.items()):
172 if isinstance(v, OS):
174 OSs.append((v._order, k))
176 attrs[
'_oslist'] = [i[1]
for i
in sorted(OSs)]
177 return type.__new__(mcs, name, bases, attrs)
182 for attr
in obj._oslist:
184 if getattr(obj, attr)():
201 }[os.sys.getwindowsversion()[3],
202 os.sys.getwindowsversion()[0],
203 os.sys.getwindowsversion()[1] ]
204 return "Windows " + win_version
225 text = open(
'/etc/issue.net').
read().strip()
226 if text.find(
'Ubuntu'):
228 mtext = open(
'/var/log/installer/media-info').
read().strip()
232 if 'Mythbuntu' in mtext:
233 text.replace(
'Ubuntu',
'Mythbuntu')
252 text = text.split(
'\n')[0].strip()
253 return re.sub(
'\(\w*\)$',
'', text)
261 return 'FreeBSD '+version
265 def OSX(self, OS, version):
268 major,minor,point = [int(a)
for a
in version.split(
'.')]
269 return 'OS X 10.%s.%s' % (major-4, minor)
273 from subprocess
import Popen, PIPE
274 executable =
'lsb_release'
275 for path
in os.environ[
'PATH'].split(
':'):
276 fullpath = os.path.join(path, executable)
277 if os.path.exists(fullpath):
282 p = Popen([fullpath,
'--id',
'--codename',
'--release',
'--short'],
283 stdout=PIPE, close_fds=
True)
285 return p.stdout.read().
decode().strip().replace(
'\n',
' ')
290 return 'Unknown Linux '+version
294 if __name__ ==
'__main__':
296 print(
'Test results="{}"'.format(results))