-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
version.py
63 lines (53 loc) · 2 KB
/
version.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
from dataclasses import dataclass
import platform
import sys
import subprocess
from typing import Optional
@dataclass
class Version:
major: int = 2
minor: int = 2
patch: int = 0
tag: Optional[str] = None
def __str__(self) -> str:
return f"{self.major}.{self.minor}.{self.patch}" + (f"-{self.tag}" if self.tag else "")
VERSION = Version()
class SystemInfo:
logger = logging.getLogger("SystemInfo")
@staticmethod
def get_os_info() -> dict:
return {
'system': platform.system(),
'release': platform.release(),
'version': platform.version(),
'machine': platform.machine(),
'processor': platform.processor()
}
@staticmethod
def get_python_info() -> dict:
return {
'version': sys.version,
'implementation': platform.python_implementation(),
'compiler': platform.python_compiler()
}
@staticmethod
def get_rpi_info() -> Optional[str]:
try:
with open('/proc/device-tree/model', 'r') as f:
return f.read().strip('\x00')
except FileNotFoundError:
SystemInfo.logger.warning("Raspberry Pi information not found.")
return None
@staticmethod
def get_git_info() -> Optional[dict]:
try:
# Check if git is available first
if subprocess.call(['which', 'git'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) != 0:
raise FileNotFoundError("Git not available")
commit = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()
branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode('ascii').strip()
return {'commit': commit, 'branch': branch}
except (subprocess.CalledProcessError, FileNotFoundError) as e:
SystemInfo.logger.warning("Git information could not be retrieved: %s", e)
return None