summaryrefslogtreecommitdiffstats
path: root/host.py
blob: 881a174ec90ba038548268655ba2b90fbc7e0a54 (plain)
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
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
import platform
import re
import socket
from typing import Union

import common


class Info:
    def __init__(self, name: str, os: str, cpu: str) -> None:
        self.name = name
        self.os = os
        self.cpu = cpu

    @staticmethod
    async def gather() -> Union["Info", common.Error]:
        name = socket.gethostname()
        os = platform.platform()
        cpu = await Info.get_cpu()
        match cpu:
            case common.Error() as error:
                return error

        return Info(name=name, os=os, cpu=cpu)

    @staticmethod
    async def get_cpu() -> Union[str, common.Error]:
        with open("/proc/cpuinfo") as f:
            match = re.search(
                pattern=r"^model name\s*: ([^ ].*)$", string=f.read(), flags=re.MULTILINE
            )
        if not match:
            return common.Error("Failed to parse /proc/cpuinfo")
        else:
            return match[1]