summaryrefslogtreecommitdiffstatshomepage
path: root/project/os.py
blob: 4ddf703f67687e94b939f11761dcb6c041112334 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Copyright (c) 2020 Egor Tensin <egor@tensin.name>
# This file is part of the "cmake-common" project.
# For details, see https://github.com/egor-tensin/cmake-common
# Distributed under the MIT License.

from enum import Enum
import platform


class OS(Enum):
    WINDOWS = "Windows"
    LINUX = "Linux"
    MACOS = "macOS"

    def __str__(self):
        return str(self.value)

    @staticmethod
    def current():
        system = platform.system()
        mapping = {
            "Windows": OS.WINDOWS,
            "Linux": OS.LINUX,
            "Darwin": OS.MACOS,
        }
        if system not in mapping:
            raise NotImplementedError(f"unsupported OS: {system}")
        return mapping[system]


def on_windows():
    return OS.current() is OS.WINDOWS


def on_windows_like():
    os = OS.current()
    return os is OS.WINDOWS


def on_linux():
    return OS.current() is OS.LINUX


def on_linux_like():
    os = OS.current()
    return os is OS.LINUX or os is OS.MACOS