Squashed 'utils_v2/' content from commit 88e444a
git-subtree-dir: utils_v2 git-subtree-split: 88e444ac57ca6c090736645616d8a8a2d2af367b
This commit is contained in:
@@ -0,0 +1,278 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
CREATED: Thu, 13th Nov, 2025
|
||||
UPDATED: Thu, 13th Nov, 2025
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To know which OS platform we are working on and which CPU architecture also. The internal module can already do
|
||||
that, this code just simplifies the decision matrix by providing convenient True-False answering functions.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
N/A
|
||||
|
||||
DOWNLOADS:
|
||||
|
||||
N/A
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** IMPORT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
# To know the platform:
|
||||
import platform
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# List out the kinds of OS supported:
|
||||
SYSTEM_WINDOWS = "Windows"
|
||||
SYSTEM_LINUX = "Linux"
|
||||
SYSTEM_MACOS = "MacOS"
|
||||
|
||||
# List out the kinds of Architectures:
|
||||
ARCH_ARM32 = "ARM32"
|
||||
ARCH_ARM64 = "ARM64"
|
||||
ARCH_x86 = "x86"
|
||||
ARCH_x86_64 = "x86_64"
|
||||
ARCH_AMD64 = "AMD64"
|
||||
ARCH_RISCV32 = "RISCV32"
|
||||
ARCH_RISCV64 = "RISCV64"
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** CLASSES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
def get_os_name() -> str | None:
|
||||
|
||||
"""
|
||||
Gives out one of three names of the major OSs used in the market.
|
||||
:return: String name of one of the OSs, None if a non-standard OS is used.
|
||||
"""
|
||||
|
||||
reported_system = platform.system()
|
||||
|
||||
match reported_system:
|
||||
case "Windows": figured_system = SYSTEM_WINDOWS
|
||||
case "Linux": figured_system = SYSTEM_LINUX
|
||||
case "Darwin": figured_system = SYSTEM_MACOS
|
||||
case _: figured_system = None
|
||||
|
||||
return figured_system
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_windows() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current system is a Windows system.
|
||||
:return: True if the OS is a Windows OS, False otherwise.
|
||||
"""
|
||||
|
||||
system = get_os_name()
|
||||
if system == SYSTEM_WINDOWS: return True
|
||||
else: return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_linux() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current system is a Linux system.
|
||||
:return: True if the OS is a Linux OS, False otherwise.
|
||||
"""
|
||||
|
||||
system = get_os_name()
|
||||
if system == SYSTEM_LINUX: return True
|
||||
else: return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_macos() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current system is a Mac (Darwin) system.
|
||||
:return: True if the OS is MacOS, False otherwise.
|
||||
"""
|
||||
|
||||
system = get_os_name()
|
||||
if system == SYSTEM_MACOS: return True
|
||||
else: return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def get_arch_name() -> str:
|
||||
|
||||
"""
|
||||
Gives you the name of the physical CPU architecture.
|
||||
:return: The name of the physical CPU architecture.
|
||||
"""
|
||||
|
||||
arch = None
|
||||
machine = platform.machine()
|
||||
|
||||
if machine in ["i386", "i486", "i586", "i686", "x86"]: arch = ARCH_x86
|
||||
elif machine in ["x86_64"]: arch = ARCH_x86_64
|
||||
elif machine in ["AMD64"]: arch = ARCH_AMD64
|
||||
elif machine in ["armv6l", "armv7l", "armv7", "arm"]: arch = ARCH_ARM32
|
||||
elif machine in ["arm64", "aarch64"]: arch = ARCH_ARM64
|
||||
elif machine in ["riscv32"]: arch = ARCH_RISCV32
|
||||
elif machine in ["riscv64"]: arch = ARCH_RISCV64
|
||||
|
||||
return arch
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_32_bit() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current CPU architecture is 32-bit.
|
||||
:return: True if the CPU architecture is 32-bit, False otherwise.
|
||||
"""
|
||||
|
||||
arch = get_arch_name()
|
||||
if arch in [ARCH_x86, ARCH_ARM32, ARCH_RISCV32]: return True
|
||||
return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_64_bit() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current CPU architecture is 64-bit.
|
||||
:return: True if the CPU architecture is 64-bit, False otherwise.
|
||||
"""
|
||||
|
||||
arch = get_arch_name()
|
||||
if arch in [ARCH_x86_64, ARCH_AMD64, ARCH_ARM64, ARCH_RISCV64]: return True
|
||||
return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_x86() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current CPU architecture is x86 (Intel or AMD).
|
||||
:return: True if the CPU architecture is x86, False otherwise.
|
||||
"""
|
||||
|
||||
arch = get_arch_name()
|
||||
if arch in [ARCH_x86, ARCH_x86_64, ARCH_AMD64]: return True
|
||||
return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_arm() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current CPU architecture is ARM.
|
||||
:return: True if the CPU architecture is ARM, False otherwise.
|
||||
"""
|
||||
|
||||
arch = get_arch_name()
|
||||
if arch in [ARCH_ARM32, ARCH_ARM64]: return True
|
||||
return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def is_riscv() -> bool:
|
||||
|
||||
"""
|
||||
Checks if the current CPU architecture is RISC-V.
|
||||
:return: True if the CPU architecture is RISC-V, False otherwise.
|
||||
"""
|
||||
|
||||
arch = get_arch_name()
|
||||
if arch in [ARCH_RISCV32, ARCH_RISCV64]: return True
|
||||
return False
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Get the system name:
|
||||
print("OS :", get_os_name())
|
||||
print("WIN :", is_windows())
|
||||
print("LIN :", is_linux())
|
||||
print("MAC :", is_macos())
|
||||
|
||||
print("\n\n---\n\n")
|
||||
|
||||
# Get hardware architecture:
|
||||
print("ARCH :", get_arch_name())
|
||||
print("is 32b :", is_32_bit())
|
||||
print("is 64b :", is_64_bit())
|
||||
print("is x86 :", is_x86())
|
||||
print("is ARM :", is_arm())
|
||||
print("is R-V :", is_riscv())
|
||||
Reference in New Issue
Block a user