Skip to content

File: Broken/Core/BrokenPlatform.py

Broken.Core.BrokenPlatform

SystemEnum

Bases: str, MultiEnum

Source code in Broken/Core/BrokenPlatform.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class SystemEnum(str, MultiEnum):
    Linux:   str = "linux"
    Windows: str = ("windows", "win")
    MacOS:   str = ("macos", "darwin", "osx")
    BSD:     str = ("bsd", "freebsd", "openbsd", "netbsd")

    @property
    def extension(self) -> str:
        if (self == self.Windows):
            return ".exe"
        return ".bin"

    def is_linux(self) -> bool:
        return (self == self.Linux)

    def is_windows(self) -> bool:
        return (self == self.Windows)

    def is_macos(self) -> bool:
        return (self == self.MacOS)

    def is_bsd(self) -> bool:
        return (self == self.BSD)

Linux

Linux: str = 'linux'

Windows

Windows: str = ('windows', 'win')

MacOS

MacOS: str = ('macos', 'darwin', 'osx')

BSD

BSD: str = ('bsd', 'freebsd', 'openbsd', 'netbsd')

extension

extension: str

is_linux

is_linux() -> bool
Source code in Broken/Core/BrokenPlatform.py
25
26
def is_linux(self) -> bool:
    return (self == self.Linux)

is_windows

is_windows() -> bool
Source code in Broken/Core/BrokenPlatform.py
28
29
def is_windows(self) -> bool:
    return (self == self.Windows)

is_macos

is_macos() -> bool
Source code in Broken/Core/BrokenPlatform.py
31
32
def is_macos(self) -> bool:
    return (self == self.MacOS)

is_bsd

is_bsd() -> bool
Source code in Broken/Core/BrokenPlatform.py
34
35
def is_bsd(self) -> bool:
    return (self == self.BSD)

ArchEnum

Bases: str, MultiEnum

Source code in Broken/Core/BrokenPlatform.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class ArchEnum(str, MultiEnum):
    AMD32: str = ("amd32", "x86", "i686")
    AMD64: str = ("amd64", "x86_64")
    ARM32: str = "arm32"
    ARM64: str = "arm64"

    def is_arm(self) -> bool:
        return ("arm" in self.value)

    def is_amd(self) -> bool:
        return ("amd" in self.value)

    def is_32_bits(self) -> bool:
        return ("32" in self.value)

    def is_64_bits(self) -> bool:
        return ("64" in self.value)

AMD32

AMD32: str = ('amd32', 'x86', 'i686')

AMD64

AMD64: str = ('amd64', 'x86_64')

ARM32

ARM32: str = 'arm32'

ARM64

ARM64: str = 'arm64'

is_arm

is_arm() -> bool
Source code in Broken/Core/BrokenPlatform.py
44
45
def is_arm(self) -> bool:
    return ("arm" in self.value)

is_amd

is_amd() -> bool
Source code in Broken/Core/BrokenPlatform.py
47
48
def is_amd(self) -> bool:
    return ("amd" in self.value)

is_32_bits

is_32_bits() -> bool
Source code in Broken/Core/BrokenPlatform.py
50
51
def is_32_bits(self) -> bool:
    return ("32" in self.value)

is_64_bits

is_64_bits() -> bool
Source code in Broken/Core/BrokenPlatform.py
53
54
def is_64_bits(self) -> bool:
    return ("64" in self.value)

PlatformEnum

Bases: str, BrokenEnum

List of common platforms targets for releases

Source code in Broken/Core/BrokenPlatform.py
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class PlatformEnum(str, BrokenEnum):
    """List of common platforms targets for releases"""
    WindowsAMD64: str = "windows-amd64"
    WindowsARM64: str = "windows-arm64"
    LinuxAMD64:   str = "linux-amd64"
    LinuxARM64:   str = "linux-arm64"
    MacosAMD64:   str = "macos-amd64"
    MacosARM64:   str = "macos-arm64"

    @property
    def system(self) -> SystemEnum:
        return SystemEnum(self.value.split("-")[0])

    @property
    def arch(self) -> ArchEnum:
        return ArchEnum(self.value.split("-")[1])

    @property
    def extension(self) -> str:
        return self.system.extension

    @property
    def triple(self) -> str:
        """Get the Rust target triple"""
        return {
            self.WindowsAMD64: "x86_64-pc-windows-" + ("msvc" if BrokenPlatform.OnWindows else "gnu"),
            self.WindowsARM64: "aarch64-pc-windows-" + ("msvc" if BrokenPlatform.OnWindows else "gnullvm"),
            self.LinuxAMD64:   "x86_64-unknown-linux-gnu",
            self.LinuxARM64:   "aarch64-unknown-linux-gnu",
            self.MacosAMD64:   "x86_64-apple-darwin",
            self.MacosARM64:   "aarch64-apple-darwin",
        }[self]

    @property
    def pip_platform(self) -> Iterable[str]:

        # https://en.wikipedia.org/wiki/MacOS_version_history
        def mac_versions() -> Iterable[str]:
            for minor in range(0, 16):
                yield (10, minor)
            for major in range(11, 16):
                yield (major, 0)

        # We MUST output ALL the platforms
        if (self == self.WindowsAMD64):
            yield "win_amd64"

        elif (self == self.WindowsARM64):
            yield "win_arm64"

        elif (self == self.LinuxAMD64):
            yield "linux_x86_64"
            yield "manylinux2014_x86_64"
            yield "manylinux2010_x86_64"
            yield "manylinux1_x86_64"

        elif (self == self.LinuxARM64):
            yield "manylinux2014_aarch64"
            yield "linux_aarch64"

        elif (self == self.MacosAMD64):
            for (major, minor) in reversed(list(mac_versions())):
                yield f"macosx_{major}_{minor}_x86_64"

        elif (self == self.MacosARM64):
            for (major, minor) in reversed(list(mac_versions())):
                yield f"macosx_{major}_{minor}_arm64"

    _AllAMD64: str = "all-amd64"
    _AllARM64: str = "all-arm64"
    _AllHost:  str = "all-host"
    _All:      str = "all"

    def get_all(self) -> Iterable[Self]:
        if ("all" in self.value):
            for option in PlatformEnum.options():
                if ("all" in option.value):
                    continue
                elif (self == self._All):
                    yield option
                elif (self == self._AllAMD64):
                    if (option.arch == ArchEnum.AMD64):
                        yield option
                elif (self == self._AllARM64):
                    if (option.arch == ArchEnum.ARM64):
                        yield option
                elif (self == self._AllHost):
                    if (option.system == BrokenPlatform.System):
                        yield option
        else:
            yield self

WindowsAMD64

WindowsAMD64: str = 'windows-amd64'

WindowsARM64

WindowsARM64: str = 'windows-arm64'

LinuxAMD64

LinuxAMD64: str = 'linux-amd64'

LinuxARM64

LinuxARM64: str = 'linux-arm64'

MacosAMD64

MacosAMD64: str = 'macos-amd64'

MacosARM64

MacosARM64: str = 'macos-arm64'

system

system: SystemEnum

arch

arch: ArchEnum

extension

extension: str

triple

triple: str

Get the Rust target triple

pip_platform

pip_platform: Iterable[str]

get_all

get_all() -> Iterable[Self]
Source code in Broken/Core/BrokenPlatform.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def get_all(self) -> Iterable[Self]:
    if ("all" in self.value):
        for option in PlatformEnum.options():
            if ("all" in option.value):
                continue
            elif (self == self._All):
                yield option
            elif (self == self._AllAMD64):
                if (option.arch == ArchEnum.AMD64):
                    yield option
            elif (self == self._AllARM64):
                if (option.arch == ArchEnum.ARM64):
                    yield option
            elif (self == self._AllHost):
                if (option.system == BrokenPlatform.System):
                    yield option
    else:
        yield self

BrokenPlatform

Source code in Broken/Core/BrokenPlatform.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
class BrokenPlatform:
    Arch: ArchEnum = ArchEnum.get(
        platform.machine().lower())
    """The current machine's architecture"""

    System: SystemEnum = SystemEnum.get(
        platform.system().lower())
    """The current machine's operating system"""

    Host: PlatformEnum = PlatformEnum.get(
        f"{System.value}-{Arch.value}")
    """The current machine's full platform"""

    # Primary platforms
    OnLinux:   bool = (System == SystemEnum.Linux)
    OnWindows: bool = (System == SystemEnum.Windows)
    OnMacOS:   bool = (System == SystemEnum.MacOS)
    OnBSD:     bool = (System == SystemEnum.BSD)

    # Distro IDs: https://distro.readthedocs.io/en/latest/
    LinuxDistro: str = distro.id()

    # Family of platforms
    OnUnix: bool = (OnLinux or OnMacOS or OnBSD)

    # Ubuntu family
    OnUbuntu:    bool = (LinuxDistro == "ubuntu")
    OnDebian:    bool = (LinuxDistro == "debian")
    OnMint:      bool = (LinuxDistro == "linuxmint")
    OnRaspberry: bool = (LinuxDistro == "raspbian")
    UbuntuLike:  bool = (OnUbuntu or OnDebian or OnMint or OnRaspberry)

    # Arch Linux family
    OnArch:    bool = (LinuxDistro == "arch")
    OnManjaro: bool = (LinuxDistro == "manjaro")
    ArchLike:  bool = (OnArch or OnManjaro)

    # RedHat family
    OnFedora:   bool = (LinuxDistro == "fedora")
    OnCentOS:   bool = (LinuxDistro == "centos")
    OnRedHat:   bool = (LinuxDistro == "rhel")
    FedoraLike: bool = (OnFedora or OnCentOS or OnRedHat)

    # Others
    OnGentoo: bool = (LinuxDistro == "gentoo")

    # BSD family
    OnOpenBSD: bool = (LinuxDistro == "openbsd")
    OnFreeBSD: bool = (LinuxDistro == "freebsd")
    OnNetBSD:  bool = (LinuxDistro == "netbsd")
    OnBSDLike: bool = (OnFreeBSD or OnOpenBSD)

    @staticmethod
    def log_system_info() -> None:
        log.info(f"• System Info: {platform.system()} {platform.release()}, Python {platform.python_version()} {platform.machine()}")

    @staticmethod
    def clear_terminal() -> None:
        os.system("cls" if BrokenPlatform.OnWindows else "clear")

    # Literally, why Windows/Python have different directory names for scripts? ...
    # https://github.com/pypa/virtualenv/commit/993ba1316a83b760370f5a3872b3f5ef4dd904c1
    PyBinDir = ("Scripts" if OnWindows else "bin")

    try:
        Root: bool = (os.getuid() == 0)
    except AttributeError:
        Root: bool = ctypes.windll.shell32.IsUserAnAdmin() != 0

    class DeveloperMode:
        # https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging

        @staticmethod
        def status() -> bool:
            import winreg
            key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
            key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_READ)
            value, _ = winreg.QueryValueEx(key, "AllowDevelopmentWithoutDevLicense")
            winreg.CloseKey(key)
            return bool(value)

        @staticmethod
        def set(state: bool=True):
            import winreg
            if (not BrokenPlatform.Root):
                raise PermissionError("Administrator privileges are required to enable Developer Mode")
            key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
            key = winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_ALL_ACCESS)
            winreg.SetValueEx(key, "AllowDevelopmentWithoutDevLicense", 0, winreg.REG_DWORD, int(state))
            winreg.CloseKey(key)

        @staticmethod
        def enable() -> bool:
            return BrokenPlatform.DeveloperMode.set(True)

        @staticmethod
        def enabled() -> bool:
            return BrokenPlatform.DeveloperMode.status()

Arch

Arch: ArchEnum = ArchEnum.get(platform.machine().lower())

The current machine's architecture

System

System: SystemEnum = SystemEnum.get(
    platform.system().lower()
)

The current machine's operating system

Host

Host: PlatformEnum = PlatformEnum.get(
    f"{System.value}-{Arch.value}"
)

The current machine's full platform

OnLinux

OnLinux: bool = System == SystemEnum.Linux

OnWindows

OnWindows: bool = System == SystemEnum.Windows

OnMacOS

OnMacOS: bool = System == SystemEnum.MacOS

OnBSD

OnBSD: bool = System == SystemEnum.BSD

LinuxDistro

LinuxDistro: str = distro.id()

OnUnix

OnUnix: bool = OnLinux or OnMacOS or OnBSD

OnUbuntu

OnUbuntu: bool = LinuxDistro == 'ubuntu'

OnDebian

OnDebian: bool = LinuxDistro == 'debian'

OnMint

OnMint: bool = LinuxDistro == 'linuxmint'

OnRaspberry

OnRaspberry: bool = LinuxDistro == 'raspbian'

UbuntuLike

UbuntuLike: bool = (
    OnUbuntu or OnDebian or OnMint or OnRaspberry
)

OnArch

OnArch: bool = LinuxDistro == 'arch'

OnManjaro

OnManjaro: bool = LinuxDistro == 'manjaro'

ArchLike

ArchLike: bool = OnArch or OnManjaro

OnFedora

OnFedora: bool = LinuxDistro == 'fedora'

OnCentOS

OnCentOS: bool = LinuxDistro == 'centos'

OnRedHat

OnRedHat: bool = LinuxDistro == 'rhel'

FedoraLike

FedoraLike: bool = OnFedora or OnCentOS or OnRedHat

OnGentoo

OnGentoo: bool = LinuxDistro == 'gentoo'

OnOpenBSD

OnOpenBSD: bool = LinuxDistro == 'openbsd'

OnFreeBSD

OnFreeBSD: bool = LinuxDistro == 'freebsd'

OnNetBSD

OnNetBSD: bool = LinuxDistro == 'netbsd'

OnBSDLike

OnBSDLike: bool = OnFreeBSD or OnOpenBSD

log_system_info

log_system_info() -> None
Source code in Broken/Core/BrokenPlatform.py
201
202
203
@staticmethod
def log_system_info() -> None:
    log.info(f"• System Info: {platform.system()} {platform.release()}, Python {platform.python_version()} {platform.machine()}")

clear_terminal

clear_terminal() -> None
Source code in Broken/Core/BrokenPlatform.py
205
206
207
@staticmethod
def clear_terminal() -> None:
    os.system("cls" if BrokenPlatform.OnWindows else "clear")

PyBinDir

PyBinDir = 'Scripts' if OnWindows else 'bin'

Root

Root: bool = os.getuid() == 0

DeveloperMode

Source code in Broken/Core/BrokenPlatform.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
class DeveloperMode:
    # https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging

    @staticmethod
    def status() -> bool:
        import winreg
        key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_READ)
        value, _ = winreg.QueryValueEx(key, "AllowDevelopmentWithoutDevLicense")
        winreg.CloseKey(key)
        return bool(value)

    @staticmethod
    def set(state: bool=True):
        import winreg
        if (not BrokenPlatform.Root):
            raise PermissionError("Administrator privileges are required to enable Developer Mode")
        key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
        key = winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_ALL_ACCESS)
        winreg.SetValueEx(key, "AllowDevelopmentWithoutDevLicense", 0, winreg.REG_DWORD, int(state))
        winreg.CloseKey(key)

    @staticmethod
    def enable() -> bool:
        return BrokenPlatform.DeveloperMode.set(True)

    @staticmethod
    def enabled() -> bool:
        return BrokenPlatform.DeveloperMode.status()
status
status() -> bool
Source code in Broken/Core/BrokenPlatform.py
221
222
223
224
225
226
227
228
@staticmethod
def status() -> bool:
    import winreg
    key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
    key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_READ)
    value, _ = winreg.QueryValueEx(key, "AllowDevelopmentWithoutDevLicense")
    winreg.CloseKey(key)
    return bool(value)
set
set(state: bool = True)
Source code in Broken/Core/BrokenPlatform.py
230
231
232
233
234
235
236
237
238
@staticmethod
def set(state: bool=True):
    import winreg
    if (not BrokenPlatform.Root):
        raise PermissionError("Administrator privileges are required to enable Developer Mode")
    key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
    key = winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_ALL_ACCESS)
    winreg.SetValueEx(key, "AllowDevelopmentWithoutDevLicense", 0, winreg.REG_DWORD, int(state))
    winreg.CloseKey(key)
enable
enable() -> bool
Source code in Broken/Core/BrokenPlatform.py
240
241
242
@staticmethod
def enable() -> bool:
    return BrokenPlatform.DeveloperMode.set(True)
enabled
enabled() -> bool
Source code in Broken/Core/BrokenPlatform.py
244
245
246
@staticmethod
def enabled() -> bool:
    return BrokenPlatform.DeveloperMode.status()