Last active
August 8, 2023 06:09
-
-
Save GLMeece/6df9c6561892aa5183b6690ccc59378d to your computer and use it in GitHub Desktop.
Get OS info at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import platform | |
""" | |
Utility functions for determining Operating System info at runtime. | |
- *Module*: runtime_os_info | |
- *Platform*: Unix, Mac, Windows | |
- *Author*: [mailto:[email protected]?subject=About runtime_os_info.py|Greg Meece] | |
== Functions with Example Returns == | |
- ``this_platform = base_system()`` - Returns one of Windows, Linux, or Mac | |
- ``os_detail_list = detailed_os()`` - Example return: ['Microsoft', 'Windows', '7', '(SP1)'] | |
- ``macos_name = mac_brand_name()`` - Returns either ``Mac OS X`` _or_ ``macOS`` | |
- ``mac_ver_name = mac_version_name(11)`` - Would return ``El Capitan``. | |
== Notes == | |
- Since I use Robot Framework for testing purposes, I've formatted the docstrings to take advantage | |
of the libdoc functionality to generate documentation. Ask the author for more info on this. | |
""" | |
def base_system(): | |
"""== Determines the basic OS the current machine/VM is running == | |
Understands: | |
- Windows | |
- Linux | |
- Mac OS X/macOS (Darwin). | |
== Calling == | |
| *Args* | [none] | | | |
| *Returns* | ``string`` | Either ``Windows``, ``Linux``, or ``Mac``. | | |
| *Raises* | [none] | | | |
""" | |
cur_system = platform.system() | |
if cur_system == 'Darwin': | |
this_system = 'Mac' | |
else: | |
this_system = cur_system | |
return this_system | |
def detailed_os(): | |
"""== Returns detailed information on the OS of the current machine/VM == | |
Recognizes detailed OS information for: | |
- Windows | |
- Linux | |
- Mac OS X/macOS | |
== Calling == | |
| *Args* | [none] | | | |
| *Returns* | ``list`` | Vendor, Basic OS, Version of OS, additional OS information. | | |
| *Raises* | [none] | | | |
=== Example Return === | |
``Microsoft``, ``Windows``, ``7``, ``(SP1)`` | |
""" | |
what_os = base_system() | |
label = '' | |
version = '' | |
version_name = '' | |
add_info = '' | |
if what_os == 'Windows': | |
label = 'Microsoft' | |
version = platform.release() | |
version_name = platform.win32_ver()[2] | |
elif what_os == 'Mac': | |
label = 'Apple' | |
mac_ver_tup = platform.mac_ver() | |
mac_ver_str = mac_ver_tup[0] | |
mac_ver_list = mac_ver_str.split('.') | |
maj_ver = int(mac_ver_list[0]) | |
min_ver = int(mac_ver_list[1]) | |
version = '{}.{}'.format(maj_ver, min_ver) | |
(brand_name, version_name) = mac_brand_name() | |
version_name = '{} {}'.format(brand_name, version_name) | |
elif what_os == 'Linux': | |
label = platform.dist()[0] | |
version = platform.dist()[1] | |
with open("/etc/os-release") as osfileinfo: | |
for line in osfileinfo: | |
the_key, the_val = line.partition("=")[::2] | |
if the_key == 'VERSION': | |
open_paren = the_val.find('(') | |
close_paren = the_val.find(')') | |
version_name = the_val[open_paren + 1:close_paren] | |
break | |
return (label, what_os, version, '({})'.format(version_name)) | |
def mac_brand_name(): | |
"""== Returns the Branding-name of the Mac OS at Runtime == | |
*Note*: This function assumes it is being run on a Macintosh. | |
== Calling == | |
| *Args* | (none) | | | |
| *Returns* | ``string`` | Either ``Mac OS X`` (versions 10.0-10.11) _or_ ``macOS`` (version 10.12 and above). | | |
| *Raises* | [none] | | | |
=== Example Calls/Returns === | |
| ${mac_brand} = Mac Brand Name | |
Example Return: ``Mac OS X`` | |
""" | |
mac_ver_tup = platform.mac_ver() | |
mac_ver_str = mac_ver_tup[0] | |
mac_ver_list = mac_ver_str.split('.') | |
maj_ver = int(mac_ver_list[0]) | |
min_ver = int(mac_ver_list[1]) | |
brand_name = 'macOS' | |
version_name = '' | |
# return_tuple | |
if maj_ver >= 10: | |
if min_ver <= 11: # El Capitan and earlier | |
brand_name = 'Mac OS X' | |
version_name = mac_version_name(maj_ver, min_ver) | |
else: | |
print "Version of the Mac OS unknown!" | |
return (brand_name, version_name) | |
def mac_version_name(major_version, minor_version): | |
"""== Returns the Version Name of the Mac OS at Runtime == | |
*Note*: This function assumes it is being run on a Macintosh. | |
== Calling == | |
| *Args* | ``minor_version`` (int) | The minor version of the Mac OS the name is desired for. | | |
| *Returns* | ``string`` | Returns the code-name of the Mac OS version. | | |
| *Raises* | [none] | | | |
=== Example Calls/Returns === | |
| ${mac_brand} = Mac Version Name ${11} | |
Example Return: ``El Capitan`` | |
""" | |
if major_version = 10: | |
if minor_version == 0: | |
return 'Cheetah' | |
elif minor_version == 1: | |
return 'Puma' | |
elif minor_version == 2: | |
return 'Jaguar' | |
elif minor_version == 3: | |
return 'Panther' | |
elif minor_version == 4: | |
return 'Tiger' | |
elif minor_version == 5: | |
return 'Leopard' | |
elif minor_version == 6: | |
return 'Snow Leopard' | |
elif minor_version == 7: | |
return 'Lion' | |
elif minor_version == 8: | |
return 'Mountain Lion' | |
elif minor_version == 9: | |
return 'Mavericks' | |
elif minor_version == 10: | |
return 'Yosemite' | |
elif minor_version == 11: # OS X ^ | |
return 'El Capitan' | |
elif minor_version == 12: # macOS v | |
return 'Sierra' | |
elif minor_version == 13: | |
return 'High Sierra' | |
elif minor_version == 14: | |
return 'Mojave' | |
elif minor_version == 15: | |
return 'Catalina' | |
elif major_version == 11: | |
return 'Big Sur' | |
elif major_version == 12: | |
return 'Monterey' | |
elif major_version == 13: | |
return 'Ventura' | |
else: | |
return 'Mac OS version name unknown!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
... just for my future_me 😄