forked from Cambricon/mlu-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion_pre_check.py
239 lines (201 loc) · 7.85 KB
/
version_pre_check.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import sys
import json
import os
# version_check Module: packaging 1, distutils 0
version_check_module = 1
build_file = "build.property"
required_version = {}
# cntoolkit, cnnl
modules = ["cntoolkit", "cnnl"]
# NEUWARE_HOME = env_vars["NEUWARE_HOME"]
env_vars = dict(os.environ)
# version check status
version_status = {"not_found_version":2, "version_check_failed": 1,
"success": 0}
# version(str1) > version(str2)
def gtVersion(str1, str2):
global version_check_module
if version_check_module == 1:
try:
from packaging import version
return version.parse(str1) > version.parse(str2)
except ImportError:
print("packaging not exists, try import distutils")
version_check_module = 0
except Exception as e1:
print(f"version check error: {e1}")
if version_check_module == 0:
try:
from distutils.version import LooseVersion
return LooseVersion(str1) > LooseVersion(str2)
except ImportError:
print("distutils not exists, version check failed")
version_check_module = -1
except Exception as e1:
print(f"version check error: {e1}")
return False
def get_build_requires(print_mode=1):
global required_version
with open(build_file) as build_property:
data = json.load(build_property)
required_version = data["build_requires"]
for key in modules:
if print_mode == 1:
print(
"%s %s %s"
% (key, required_version[key][0], required_version[key][1])
)
required_version[key] = required_version[key][1].split("-")[0]
def check_cntoolkit():
toolkit_ver_path = env_vars["NEUWARE_HOME"] + "/version.txt"
if not os.path.exists(toolkit_ver_path):
print("Warning: Not found toolkit version")
return version_status["not_found_version"]
# check cntoolkit
with open(toolkit_ver_path) as tk_f:
data = tk_f.readlines()
for line in data:
if "Neuware Version" in line:
cur_tk_ver = line.strip("\n").split(" ")[-1]
if gtVersion(required_version["cntoolkit"], cur_tk_ver):
print(
"Warning: The version of cntoolkit needs to be at least "
+ required_version["cntoolkit"]
+ ", but local version is "
+ cur_tk_ver
)
return version_status["version_check_failed"]
return version_status["success"]
def check_cnnl():
cnnl_ver_pre = env_vars["NEUWARE_HOME"] + "/lib64/"
if not os.path.exists(cnnl_ver_pre):
print("Warning: Not found cnnl version")
return version_status["not_found_version"]
# check cnnl
for filePath in os.listdir(cnnl_ver_pre):
if "libcnnl.so." in filePath:
tmp = filePath.split(".")
if len(tmp) > 3:
cur_cnnl_ver = filePath[11:]
if gtVersion(required_version["cnnl"], cur_cnnl_ver):
print(
"Warning: The version of cnnl needs to be at least "
+ required_version["cnnl"]
+ ", but local version is "
+ cur_cnnl_ver
)
return version_status["version_check_failed"]
return version_status["success"]
def check_driver():
sys_out = os.popen("cnmon version").readline()
if len(sys_out) == 0:
print("Warning: Not found driver version.")
return version_status["not_found_version"]
sys_out = sys_out.strip("\n").split(":")[-1]
if gtVersion(required_version["driver"], sys_out):
print(
"Warning: The version of driver needs to be at least "
+ required_version["driver"]
+ ", but local version is "
+ sys_out
)
return version_status["version_check_failed"]
return version_status["success"]
def check_protoc():
sys_out = os.popen("protoc --version").readline()
if len(sys_out) == 0:
print("Warning: Not found protoc version")
return version_status["not_found_version"]
sys_out = sys_out.strip("\n").split(" ")[-1]
if gtVersion(sys_out, required_version["protoc"]):
print(
"Warning: The version of protoc needs to be at most "
+ required_version["protoc"]
+ ", but local version is "
+ sys_out
)
return version_status["version_check_failed"]
return version_status["success"]
def check_fmt():
sys_out = os.popen("pkg-config --modversion fmt").readline()
if len(sys_out) == 0:
print("Warning: Not found fmt version")
return version_status["not_found_version"]
sys_out = sys_out.strip("\n")
if gtVersion(sys_out, required_version["fmt"]):
print(
"Warning: The version of fmt needs to be at most "
+ required_version["fmt"]
+ ", but local version is "
+ sys_out
)
return version_status["version_check_failed"]
return version_status["success"]
def check_libxml2():
sys_out = os.popen("xml2-config --version").readline()
if len(sys_out) == 0:
print("Warning: Not found libxml2 version")
return version_status["not_found_version"]
sys_out = sys_out.strip("\n")
if gtVersion(required_version["libxml2"], sys_out):
print(
"Warning: The version of libxml2 needs to be at least "
+ required_version["libxml2"]
+ ", but local version is "
+ sys_out
)
return version_status["version_check_failed"]
return version_status["success"]
def check_eigen3():
if os.path.exists("/usr/local/include/eigen3/Eigen/src/Core/util/Macros.h"):
h_file = open("/usr/local/include/eigen3/Eigen/src/Core/util/Macros.h")
elif os.path.exists("/usr/include/eigen3/Eigen/src/Core/util/Macros.h"):
h_file = open("/usr/include/eigen3/Eigen/src/Core/util/Macros.h")
else:
print("Warning: Not found eigen3 version")
return 2
line = h_file.readline()
eigen_ver = ""
while len(line) > 0:
if "EIGEN_WORLD_VERSION" in line:
eigen_ver = line[28:-1]
if "EIGEN_MAJOR_VERSION" in line:
eigen_ver += "." + line[28:-1]
if "EIGEN_MINOR_VERSION" in line:
eigen_ver += "." + line[28:-1]
break
line = h_file.readline()
if gtVersion(required_version["eigen3"], eigen_ver):
print(
"Warning: The version of eigen3 needs to be at least "
+ required_version["eigen3"]
+ ", but local version is "
+ eigen_ver
)
return 1
return 0
def check_build_requires():
get_build_requires(0)
if check_cntoolkit() != version_status["success"]:
print("If compilation failed, please check cntoolkit version")
if check_cnnl() != version_status["success"]:
print("If compilation failed, please check cnnl version")
if check_driver() != version_status["success"]:
print("If compilation failed, please check driver version")
if check_protoc() != version_status["success"]:
print("If compilation failed, please check protoc version")
if check_libxml2() != version_status["success"]:
print("If compilation failed, please check libxml2 version")
if check_eigen3() != version_status["success"]:
print("If compilation failed, please check eigen3 version")
if check_fmt() != version_status["success"]:
print("If compilation failed, please check fmt version")
argvs = sys.argv[1:]
if len(argvs) == 1:
eval(argvs[0])()
elif len(argvs) == 2:
eval(argvs[0])(argvs[1])
elif len(argvs) == 3:
eval(argvs[0])(argvs[1], argvs[2])
else:
exit(3)