cornsnake.util_os
Functions for checking the operating system and logging OS information. The functions determine if the OS is Windows, Mac, or Unix, and log relevant OS details.
1""" 2Functions for checking the operating system and logging OS information. The functions determine if the OS is Windows, Mac, or Unix, and log relevant OS details. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_os.html) 5""" 6 7import os 8import platform 9import typing 10 11from . import util_log 12from . import util_object 13 14logger = util_log.getLogger(__name__) 15 16 17def is_windows() -> bool: 18 """ 19 Check if the current OS is Windows. 20 21 Returns: 22 bool: True if the OS is Windows, False otherwise. 23 """ 24 return os.name == "nt" 25 26 27def is_mac() -> bool: 28 """ 29 Check if the current OS is Mac. 30 31 Returns: 32 bool: True if the OS is Mac, False otherwise. 33 """ 34 return platform.system() == "Darwin" 35 36 37def is_unix() -> bool: 38 """ 39 Check if the current OS is Unix. 40 41 Returns: 42 bool: True if the OS is Unix, False otherwise. 43 """ 44 return not is_windows() and not is_mac() and util_object.has_attribute(os, "uname") 45 46 47if is_windows(): 48 import winreg 49 50 def get_registry_key( 51 top_key: typing.Any, reg_path: str, name: str 52 ) -> typing.Any | None: 53 """ 54 Get a value from Windows registry. 55 56 Args: 57 top_key: The top level key in the registry. 58 reg_path: The path in the registry. 59 name: The name of the registry key. 60 61 Returns: 62 str: The value of the registry key, or None if not found. 63 """ 64 try: 65 registry_key = winreg.OpenKey(top_key, reg_path, 0, winreg.KEY_READ) 66 value, _regtype = winreg.QueryValueEx(registry_key, name) 67 winreg.CloseKey(registry_key) 68 return value 69 except WindowsError: 70 return None 71 72 def is_windows_max_path_setting_on() -> bool: 73 """ 74 Check if the Windows max path setting is enabled. 75 76 Returns: 77 bool: True if the max path setting is enabled, False otherwise. 78 """ 79 path = r"SYSTEM\CurrentControlSet\Control\FileSystem" # HKEY_LOCAL_MACHINE is implied 80 key = "LongPathsEnabled" 81 value = get_registry_key(winreg.HKEY_LOCAL_MACHINE, path, key) 82 if value is None: 83 return False 84 return str(value) == "1" 85 86 87def log_os() -> None: 88 """ 89 Log OS information including platform, system, name, and release. 90 """ 91 logger.info("=== OS ===") 92 logger.info(f"OS: {_os_platform()}") 93 logger.info(f"DETAILS: {platform.system()} - {os.name} - {platform.release()}") 94 95 96def _os_platform() -> str: 97 """ 98 Determine the platform of the OS. 99 100 Returns: 101 str: The platform of the OS (Windows, Mac, Unix, or unknown). 102 """ 103 if is_windows(): 104 return "Windows" 105 if is_mac(): 106 return "Mac" 107 if is_unix(): 108 return "Unix" 109 return "(unknown)"
18def is_windows() -> bool: 19 """ 20 Check if the current OS is Windows. 21 22 Returns: 23 bool: True if the OS is Windows, False otherwise. 24 """ 25 return os.name == "nt"
Check if the current OS is Windows.
Returns: bool: True if the OS is Windows, False otherwise.
28def is_mac() -> bool: 29 """ 30 Check if the current OS is Mac. 31 32 Returns: 33 bool: True if the OS is Mac, False otherwise. 34 """ 35 return platform.system() == "Darwin"
Check if the current OS is Mac.
Returns: bool: True if the OS is Mac, False otherwise.
38def is_unix() -> bool: 39 """ 40 Check if the current OS is Unix. 41 42 Returns: 43 bool: True if the OS is Unix, False otherwise. 44 """ 45 return not is_windows() and not is_mac() and util_object.has_attribute(os, "uname")
Check if the current OS is Unix.
Returns: bool: True if the OS is Unix, False otherwise.
88def log_os() -> None: 89 """ 90 Log OS information including platform, system, name, and release. 91 """ 92 logger.info("=== OS ===") 93 logger.info(f"OS: {_os_platform()}") 94 logger.info(f"DETAILS: {platform.system()} - {os.name} - {platform.release()}")
Log OS information including platform, system, name, and release.
51 def get_registry_key( 52 top_key: typing.Any, reg_path: str, name: str 53 ) -> typing.Any | None: 54 """ 55 Get a value from Windows registry. 56 57 Args: 58 top_key: The top level key in the registry. 59 reg_path: The path in the registry. 60 name: The name of the registry key. 61 62 Returns: 63 str: The value of the registry key, or None if not found. 64 """ 65 try: 66 registry_key = winreg.OpenKey(top_key, reg_path, 0, winreg.KEY_READ) 67 value, _regtype = winreg.QueryValueEx(registry_key, name) 68 winreg.CloseKey(registry_key) 69 return value 70 except WindowsError: 71 return None
Get a value from Windows registry.
Args: top_key: The top level key in the registry. reg_path: The path in the registry. name: The name of the registry key.
Returns: str: The value of the registry key, or None if not found.
73 def is_windows_max_path_setting_on() -> bool: 74 """ 75 Check if the Windows max path setting is enabled. 76 77 Returns: 78 bool: True if the max path setting is enabled, False otherwise. 79 """ 80 path = r"SYSTEM\CurrentControlSet\Control\FileSystem" # HKEY_LOCAL_MACHINE is implied 81 key = "LongPathsEnabled" 82 value = get_registry_key(winreg.HKEY_LOCAL_MACHINE, path, key) 83 if value is None: 84 return False 85 return str(value) == "1"
Check if the Windows max path setting is enabled.
Returns: bool: True if the max path setting is enabled, False otherwise.