-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvinfo.py
42 lines (36 loc) · 1.2 KB
/
envinfo.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
import os
import platform
import socket
import multiprocessing
from typing import Dict
def get_env_info() -> Dict[str, str]:
try:
return {
"os": platform.system(),
"os_release": platform.release(),
"os_version": platform.version(),
"architecture": platform.machine(),
"hostname": platform.node(),
"local_ip": _get_local_ip(),
"cpu_count": str(multiprocessing.cpu_count()),
"cwd": os.path.realpath(os.getcwd()),
"virtualized": _detect_virtualization()
}
except Exception as e:
return {"error": str(e)}
def _get_local_ip() -> str:
try:
with socket.create_connection(("8.8.8.8", 80), timeout=1) as s:
return s.getsockname()[0]
except Exception:
return "unknown"
def _detect_virtualization() -> str:
try:
if platform.system() == "Linux":
with open("/proc/cpuinfo", encoding="utf-8") as f:
text = f.read().lower()
if any(term in text for term in ("hypervisor", "kvm", "vmware", "xen", "qemu")):
return "yes"
return "no"
except Exception:
return "unknown"