A Python port of the Invisible Internet Project (I2P)
at main 87 lines 2.2 kB view raw
1"""SystemVersion — OS and platform detection. 2 3Ported from net.i2p.util.SystemVersion. 4""" 5 6import os 7import platform 8import struct 9import sys 10 11 12class SystemVersion: 13 """Static utilities for detecting OS, architecture, and capabilities.""" 14 15 @staticmethod 16 def get_os() -> str: 17 s = platform.system().lower() 18 if s == "darwin": 19 return "mac" 20 if s.startswith("win"): 21 return "windows" 22 return s # "linux", "freebsd", etc. 23 24 @staticmethod 25 def get_arch() -> str: 26 machine = platform.machine().lower() 27 if machine in ("x86_64", "amd64"): 28 return "amd64" 29 if machine in ("aarch64", "arm64"): 30 return "arm64" 31 if machine.startswith("arm"): 32 return "arm" 33 if machine in ("i386", "i686", "x86"): 34 return "386" 35 return "unknown" 36 37 @staticmethod 38 def is_windows() -> bool: 39 return platform.system().lower().startswith("win") 40 41 @staticmethod 42 def is_mac() -> bool: 43 return platform.system().lower() == "darwin" 44 45 @staticmethod 46 def is_linux() -> bool: 47 return platform.system().lower() == "linux" 48 49 @staticmethod 50 def is_android() -> bool: 51 return False # Not applicable in CPython 52 53 @staticmethod 54 def is_arm() -> bool: 55 return SystemVersion.get_arch() in ("arm", "arm64") 56 57 @staticmethod 58 def is_x86() -> bool: 59 return SystemVersion.get_arch() in ("amd64", "386") 60 61 @staticmethod 62 def is_64bit() -> bool: 63 return struct.calcsize("P") * 8 == 64 64 65 @staticmethod 66 def get_cores() -> int: 67 return os.cpu_count() or 1 68 69 @staticmethod 70 def get_max_memory() -> int: 71 """Approximate max memory in bytes.""" 72 try: 73 import resource 74 soft, hard = resource.getrlimit(resource.RLIMIT_AS) 75 if hard > 0: 76 return hard 77 except (ImportError, ValueError): 78 pass 79 return 2 * 1024 * 1024 * 1024 # Default 2GB 80 81 @staticmethod 82 def is_slow() -> bool: 83 return SystemVersion.get_arch() in ("arm",) 84 85 @staticmethod 86 def python_version() -> str: 87 return platform.python_version()