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.

Documentation

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

Check if the current OS is Windows.

Returns: bool: True if the OS is Windows, False otherwise.

def is_mac():
27def is_mac():
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"

Check if the current OS is Mac.

Returns: bool: True if the OS is Mac, False otherwise.

def is_unix():
37def is_unix():
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")

Check if the current OS is Unix.

Returns: bool: True if the OS is Unix, False otherwise.

def log_os():
85def log_os():
86    """
87    Log OS information including platform, system, name, and release.
88    """
89    logger.info("=== OS ===")
90    logger.info(f"OS: {_os_platform()}")
91    logger.info(f"DETAILS: {platform.system()} - {os.name} - {platform.release()}")

Log OS information including platform, system, name, and release.

def get_registry_key(top_key, reg_path, name):
50    def get_registry_key(top_key, reg_path, name):
51        """
52        Get a value from Windows registry.
53
54        Args:
55        top_key: The top level key in the registry.
56        reg_path: The path in the registry.
57        name: The name of the registry key.
58
59        Returns:
60        str: The value of the registry key, or None if not found.
61        """
62        try:
63            registry_key = winreg.OpenKey(top_key, reg_path, 0, winreg.KEY_READ)
64            value, _regtype = winreg.QueryValueEx(registry_key, name)
65            winreg.CloseKey(registry_key)
66            return value
67        except WindowsError:
68            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.

def is_windows_max_path_setting_on():
70    def is_windows_max_path_setting_on():
71        """
72        Check if the Windows max path setting is enabled.
73
74        Returns:
75        bool: True if the max path setting is enabled, False otherwise.
76        """
77        path = r"SYSTEM\CurrentControlSet\Control\FileSystem"  # HKEY_LOCAL_MACHINE is implied
78        key = "LongPathsEnabled"
79        value = get_registry_key(winreg.HKEY_LOCAL_MACHINE, path, key)
80        if value is None:
81            return False
82        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.