3
# This Source Code Form is subject to the terms of the Mozilla Public
4
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
# You can obtain one at http://mozilla.org/MPL/2.0/.
8
file for interface to transform introspected system information to a format
12
- os : what operating system ['win', 'mac', 'linux', ...]
14
- processor : processor architecture ['x86', 'x86_64', 'ppc', ...]
15
- version : operating system version string
17
For windows, the service pack information is also included
20
# TODO: it might be a good idea of adding a system name (e.g. 'Ubuntu' for
21
# linux) to the information; I certainly wouldn't want anyone parsing this
22
# information and having behaviour depend on it
29
# keep a copy of the os module since updating globals overrides this
32
class unknown(object):
33
"""marker class for unknown information"""
34
def __nonzero__(self):
38
unknown = unknown() # singleton
40
# get system information
41
info = {'os': unknown,
45
(system, node, release, version, machine, processor) = platform.uname()
46
(bits, linkage) = platform.architecture()
48
# get os information and related data
49
if system in ["Microsoft", "Windows"]:
51
# There is a Python bug on Windows to determine platform values
52
# http://bugs.python.org/issue7860
53
if "PROCESSOR_ARCHITEW6432" in os.environ:
54
processor = os.environ.get("PROCESSOR_ARCHITEW6432", processor)
56
processor = os.environ.get('PROCESSOR_ARCHITECTURE', processor)
57
system = os.environ.get("OS", system).replace('_', ' ')
58
service_pack = os.sys.getwindowsversion()[4]
59
info['service_pack'] = service_pack
60
elif system == "Linux":
61
(distro, version, codename) = platform.dist()
62
version = "%s %s" % (distro, version)
66
elif system == "Darwin":
67
(release, versioninfo, machine) = platform.mac_ver()
68
version = "OS X %s" % release
70
elif sys.platform in ('solaris', 'sunos5'):
72
version = sys.platform
73
info['version'] = version # os version
75
# processor type and bits
76
if processor in ["i386", "i686"]:
81
elif processor == "AMD64":
84
elif processor == "Power Macintosh":
86
bits = re.search('(\d+)bit', bits).group(1)
87
info.update({'processor': processor,
91
# standard value of choices, for easy inspection
92
choices = {'os': ['linux', 'win', 'mac', 'unix'],
94
'processor': ['x86', 'x86_64', 'ppc']}
98
"""Do some sanitization of input values, primarily
99
to handle universal Mac builds."""
100
if "processor" in info and info["processor"] == "universal-x86-x86_64":
101
# If we're running on OS X 10.6 or newer, assume 64-bit
102
if release[:4] >= "10.6": # Note this is a string comparison
103
info["processor"] = "x86_64"
106
info["processor"] = "x86"
109
# method for updating information
110
def update(new_info):
111
"""update the info"""
112
info.update(new_info)
114
globals().update(info)
116
# convenience data for os access
117
for os_name in choices['os']:
118
globals()['is' + os_name.title()] = info['os'] == os_name
121
globals()['isUnix'] = True
126
__all__ = info.keys()
127
__all__ += ['is' + os_name.title() for os_name in choices['os']]
128
__all__ += ['info', 'unknown', 'main', 'choices', 'update']
133
# parse the command line
134
from optparse import OptionParser
135
parser = OptionParser(description=__doc__)
137
parser.add_option('--%s' % key, dest=key,
138
action='store_true', default=False,
139
help="display choices for %s" % key)
140
options, args = parser.parse_args()
142
# args are JSON blobs to override info
145
from json import loads
148
from simplejson import loads
151
"""*really* simple json; will not work with unicode"""
152
return eval(string, {'true': True, 'false': False, 'null': None})
154
if _os.path.exists(arg):
155
string = file(arg).read()
158
update(loads(string))
160
# print out choices if requested
162
for key, value in options.__dict__.items():
164
print '%s choices: %s' % (key, ' '.join([str(choice)
165
for choice in choices[key]]))
169
# otherwise, print out all info
170
for key, value in info.items():
171
print '%s: %s' % (key, value)
173
if __name__ == '__main__':