forked from The-OpenROAD-Project/OpenLane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_info.py
179 lines (147 loc) · 5.44 KB
/
env_info.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
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
148
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
#!/usr/bin/env python3
# Copyright 2021 Efabless Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import sys
import json
import platform
import subprocess
try:
from typing import Optional
except ImportError:
pass
class StringRepresentable(object):
def __str__(self):
return str(self.__dict__)
def __repr__(self):
return str(self.__dict__)
class ContainerInfo(StringRepresentable):
engine = "UNKNOWN" # type: str
version = "UNKNOWN" # type: str
conmon = False # type: bool
rootless = False # type : bool
def __init__(self):
self.engine = "UNKNOWN"
self.version = "UNKNOWN"
self.conmon = False
self.rootless = False
@staticmethod
def get():
# type: () -> Optional['ContainerInfo']
try:
cinfo = ContainerInfo()
info = ""
try:
info = subprocess.check_output(
["docker", "info", "--format", "{{json .}}"]
).decode("utf8")
except Exception:
return None
try:
info = json.loads(info)
except Exception:
raise Exception("Docker info was not valid JSON.")
if info.get("host") is not None:
if info["host"].get("conmon") is not None:
cinfo.conmon = True
if (
info["host"].get("remoteSocket") is not None
and "podman" in info["host"]["remoteSocket"]["path"]
):
cinfo.engine = "podman"
cinfo.version = info["version"]["Version"]
elif (
info.get("Docker Root Dir") is not None
or info.get("DockerRootDir") is not None
):
cinfo.engine = "docker"
# Get Version
try:
version_output = (
subprocess.check_output(["docker", "--version"])
.decode("utf8")
.strip()
)
cinfo.version = re.split(r"\s", version_output)[2].strip(",")
except Exception:
print("Could not extract Docker version.", file=sys.stderr)
security_options = info.get("SecurityOptions")
for option in security_options:
if "rootless" in option:
cinfo.rootless = True
return cinfo
except Exception as e:
print(e, file=sys.stderr)
return None
class OSInfo(StringRepresentable):
kernel = "" # type: str
python_version = "" # type: str
kernel_version = "" # type: str
distro = None # type: Optional[str]
distro_version = None # type: Optional[str]
container_info = None # type: Optional[ContainerInfo]
def __init__(self):
self.kernel = platform.system()
self.python_version = platform.python_version()
self.kernel_version = (
platform.release()
) # Unintuitively enough, it's the kernel's release
self.distro = None
self.distro_version = None
self.container_info = None
@staticmethod
def get():
# type: () -> Optional['OSInfo']
osinfo = OSInfo()
if osinfo.kernel not in ["Linux", "Darwin"]:
print("Platform %s is unsupported." % osinfo.name, file=sys.stderr)
return None
if osinfo.kernel == "Darwin":
osinfo.distro = "macOS"
osinfo.distro_version = platform.mac_ver()[0]
osinfo.kernel_version = platform.release()
osinfo.package_manager = None
try:
subprocess.check_output(["brew", "--version"])
osinfo.package_manager = "brew"
except Exception:
pass
if osinfo.kernel == "Linux":
os_release = ""
try:
os_release += open("/etc/lsb-release").read()
except Exception:
pass
try:
os_release += open("/etc/os-release").read()
except Exception:
pass
if os_release.strip() != "":
config = {}
for line in os_release.split("\n"):
if line.strip() == "":
continue
key, value = line.split("=")
value = value.strip('"')
config[key] = value
osinfo.distro = config.get("ID") or config.get("DISTRIB_ID")
osinfo.distro_version = config.get("VERSION_ID") or config.get(
"DISTRIB_RELEASE"
)
else:
print("Failed to get distribution info.", file=sys.stderr)
osinfo.container_info = ContainerInfo.get()
return osinfo
if __name__ == "__main__":
print(OSInfo.get())