3
import sys, os, platform, re, tempfile, glob, getpass, logging
4
from subprocess import check_call, check_output, CalledProcessError, STDOUT
6
hostos = os.name # 'nt', 'posix'
7
hostmachine = platform.machine() # 'x86', 'AMD64', 'x86_64'
10
l = logging.getLogger("run.py")
11
l.setLevel(logging.DEBUG)
12
ch = logging.StreamHandler(sys.stderr)
13
ch.setFormatter(logging.Formatter("%(message)s"))
19
#===================================================================================================
22
def __init__(self, msg, *args):
25
def execute(cmd, silent = False, cwd = "."):
27
log.debug("Run: %s", cmd)
29
return check_output(cmd, stderr = STDOUT, cwd = cwd).decode("latin-1")
31
return check_call(cmd, cwd = cwd)
32
except CalledProcessError as e:
34
log.debug("Process returned: %d", e.returncode)
35
return e.output.decode("latin-1")
37
log.error("Process returned: %d", e.returncode)
40
def isColorEnabled(args):
41
usercolor = [a for a in args if a.startswith("--gtest_color=")]
42
return len(usercolor) == 0 and sys.stdout.isatty() and hostos != "nt"
44
#===================================================================================================
46
def getPlatformVersion():
47
mv = platform.mac_ver()
49
return "Darwin" + mv[0]
51
wv = platform.win32_ver()
53
return "Windows" + wv[0]
55
lv = platform.linux_distribution()
60
def readGitVersion(git, path):
61
if not path or not git or not os.path.isdir(os.path.join(path, ".git")):
64
output = execute([git, "-C", path, "rev-parse", "--short", "HEAD"], silent = True)
67
log.warning("Git version read failed")
70
SIMD_DETECTION_PROGRAM="""
113
def testSIMD(compiler, cxx_flags, compiler_arg = None):
118
_, tmpfile = tempfile.mkstemp(suffix=".cpp", text = True)
119
with open(tmpfile, "w+") as fd:
120
fd.write(SIMD_DETECTION_PROGRAM)
123
options.append(compiler_arg)
126
for opt in " ".join(cxx_flags).split():
127
if opt.count('\"') % 2 == 1:
128
if prev_option is None:
131
options.append(prev_option + " " + opt)
133
elif prev_option is None:
136
prev_option = prev_option + " " + opt
137
options.append(tmpfile)
138
compiler_output = execute(options, silent = True)
140
m = re.search("#error\W+(\w+)", compiler_output)
145
log.debug("SIMD detection failed")
148
#==============================================================================
151
{'name': "cmake_home", 'default': None, 'pattern': re.compile(r"^CMAKE_HOME_DIRECTORY:INTERNAL=(.+)$")},
152
{'name': "opencv_home", 'default': None, 'pattern': re.compile(r"^OpenCV_SOURCE_DIR:STATIC=(.+)$")},
153
{'name': "opencv_build", 'default': None, 'pattern': re.compile(r"^OpenCV_BINARY_DIR:STATIC=(.+)$")},
154
{'name': "tests_dir", 'default': None, 'pattern': re.compile(r"^EXECUTABLE_OUTPUT_PATH:PATH=(.+)$")},
155
{'name': "build_type", 'default': "Release", 'pattern': re.compile(r"^CMAKE_BUILD_TYPE:\w+=(.*)$")},
156
{'name': "git_executable", 'default': None, 'pattern': re.compile(r"^GIT_EXECUTABLE:FILEPATH=(.*)$")},
157
{'name': "cxx_flags", 'default': "", 'pattern': re.compile(r"^CMAKE_CXX_FLAGS:STRING=(.*)$")},
158
{'name': "cxx_flags_debug", 'default': "", 'pattern': re.compile(r"^CMAKE_CXX_FLAGS_DEBUG:STRING=(.*)$")},
159
{'name': "cxx_flags_release", 'default': "", 'pattern': re.compile(r"^CMAKE_CXX_FLAGS_RELEASE:STRING=(.*)$")},
160
{'name': "opencv_cxx_flags", 'default': "", 'pattern': re.compile(r"^OPENCV_EXTRA_C_FLAGS:INTERNAL=(.*)$")},
161
{'name': "cxx_flags_android", 'default': None, 'pattern': re.compile(r"^ANDROID_CXX_FLAGS:INTERNAL=(.*)$")},
162
{'name': "android_abi", 'default': None, 'pattern': re.compile(r"^ANDROID_ABI:STRING=(.*)$")},
163
{'name': "android_executable", 'default': None, 'pattern': re.compile(r"^ANDROID_EXECUTABLE:FILEPATH=(.*android.*)$")},
164
{'name': "ant_executable", 'default': None, 'pattern': re.compile(r"^ANT_EXECUTABLE:FILEPATH=(.*ant.*)$")},
165
{'name': "java_test_binary_dir", 'default': None, 'pattern': re.compile(r"^opencv_test_java_BINARY_DIR:STATIC=(.*)$")},
166
{'name': "is_x64", 'default': "OFF", 'pattern': re.compile(r"^CUDA_64_BIT_DEVICE_CODE:BOOL=(ON)$")},#ugly(
167
{'name': "cmake_generator", 'default': None, 'pattern': re.compile(r"^CMAKE_GENERATOR:INTERNAL=(.+)$")},
168
{'name': "cxx_compiler", 'default': None, 'pattern': re.compile(r"^CMAKE_CXX_COMPILER:\w*PATH=(.+)$")},
169
{'name': "cxx_compiler_arg1", 'default': None, 'pattern': re.compile(r"^CMAKE_CXX_COMPILER_ARG1:[A-Z]+=(.+)$")},
170
{'name': "with_cuda", 'default': "OFF", 'pattern': re.compile(r"^WITH_CUDA:BOOL=(ON)$")},
171
{'name': "cuda_library", 'default': None, 'pattern': re.compile(r"^CUDA_CUDA_LIBRARY:FILEPATH=(.+)$")},
172
{'name': "cuda_version", 'default': None, 'pattern': re.compile(r"^CUDA_VERSION:STRING=(.+)$")},
173
{'name': "core_dependencies", 'default': None, 'pattern': re.compile(r"^opencv_core_LIB_DEPENDS:STATIC=(.+)$")},
177
def __init__(self, cfg = None):
178
self.setDefaultAttrs()
179
self.cmake_home_vcver = None
180
self.opencv_home_vcver = None
181
self.featuresSIMD = None
182
self.main_modules = []
184
self.build_type = cfg
186
def setDummy(self, path):
187
self.tests_dir = os.path.normpath(path)
189
def read(self, path, fname):
190
rx = re.compile(r'^opencv_(\w+)_SOURCE_DIR:STATIC=(.*)$')
191
module_paths = {} # name -> path
192
with open(fname, "rt") as cachefile:
193
for l in cachefile.readlines():
195
if not ll or ll.startswith("#"):
197
for p in parse_patterns:
198
match = p["pattern"].match(ll)
200
value = match.groups()[0]
201
if value and not value.endswith("-NOTFOUND"):
202
setattr(self, p["name"], value)
203
# log.debug("cache value: %s = %s", p["name"], value)
205
match = rx.search(ll)
207
module_paths[match.group(1)] = match.group(2)
209
if not self.tests_dir:
210
self.tests_dir = path
212
rel = os.path.relpath(self.tests_dir, self.opencv_build)
213
self.tests_dir = os.path.join(path, rel)
214
self.tests_dir = os.path.normpath(self.tests_dir)
216
# fix VS test binary path (add Debug or Release)
217
if "Visual Studio" in self.cmake_generator:
218
self.tests_dir = os.path.join(self.tests_dir, self.build_type)
220
self.cmake_home_vcver = readGitVersion(self.git_executable, self.cmake_home)
221
if self.opencv_home == self.cmake_home:
222
self.opencv_home_vcver = self.cmake_home_vcver
224
self.opencv_home_vcver = readGitVersion(self.git_executable, self.opencv_home)
226
for module,path in module_paths.items():
227
rel = os.path.relpath(path, self.opencv_home)
229
self.main_modules.append(module)
232
self.cxx_flags_android,
234
self.cxx_flags_release,
235
self.opencv_cxx_flags,
236
self.cxx_flags_release]
237
self.flags = [f for f in self.flags if f]
238
self.featuresSIMD = testSIMD(self.cxx_compiler, self.flags, self.cxx_compiler_arg1)
240
def setDefaultAttrs(self):
241
for p in parse_patterns:
242
setattr(self, p["name"], p["default"])
244
def gatherTests(self, mask, isGood = None):
245
if self.tests_dir and os.path.isdir(self.tests_dir):
246
d = os.path.abspath(self.tests_dir)
247
files = glob.glob(os.path.join(d, mask))
248
if not self.getOS() == "android" and self.withJava():
250
return [f for f in files if isGood(f)]
253
def isMainModule(self, name):
254
return name in self.main_modules
257
return self.cuda_version and self.with_cuda == "ON" and self.cuda_library and not self.cuda_library.endswith("-NOTFOUND")
260
return self.ant_executable and self.java_test_binary_dir
262
def getGitVersion(self):
263
if self.cmake_home_vcver:
264
if self.cmake_home_vcver == self.opencv_home_vcver:
265
rev = self.cmake_home_vcver
266
elif self.opencv_home_vcver:
267
rev = self.cmake_home_vcver + "-" + self.opencv_home_vcver
269
rev = self.cmake_home_vcver
273
rev = rev.replace(":","to")
278
def getTestFullName(self, shortname):
279
return os.path.join(self.tests_dir, shortname)
281
def getSIMDFeatures(self):
282
return self.featuresSIMD
285
if self.android_executable:
292
if self.getOS() == "android":
293
if "armeabi-v7a" in self.android_abi:
295
elif "armeabi-v6" in self.android_abi:
297
elif "armeabi" in self.android_abi:
299
elif "x86" in self.android_abi:
301
elif "mips" in self.android_abi:
305
elif self.is_x64 and hostmachine in ["AMD64", "x86_64"]:
307
elif hostmachine in ["x86", "AMD64", "x86_64"]:
311
def getDependencies(self):
312
if self.core_dependencies:
313
candidates = ["tbb", "ippicv", "ipp", "pthreads"]
314
return [a for a in self.core_dependencies.split(";") if a and a in candidates]
318
#==============================================================================
320
def getRunningProcessExePathByName_win32(name):
321
from ctypes import windll, POINTER, pointer, Structure, sizeof
322
from ctypes import c_long , c_int , c_uint , c_char , c_ubyte , c_char_p , c_void_p
324
class PROCESSENTRY32(Structure):
325
_fields_ = [ ( 'dwSize' , c_uint ) ,
326
( 'cntUsage' , c_uint) ,
327
( 'th32ProcessID' , c_uint) ,
328
( 'th32DefaultHeapID' , c_uint) ,
329
( 'th32ModuleID' , c_uint) ,
330
( 'cntThreads' , c_uint) ,
331
( 'th32ParentProcessID' , c_uint) ,
332
( 'pcPriClassBase' , c_long) ,
333
( 'dwFlags' , c_uint) ,
334
( 'szExeFile' , c_char * 260 ) ,
335
( 'th32MemoryBase' , c_long) ,
336
( 'th32AccessKey' , c_long ) ]
338
class MODULEENTRY32(Structure):
339
_fields_ = [ ( 'dwSize' , c_long ) ,
340
( 'th32ModuleID' , c_long ),
341
( 'th32ProcessID' , c_long ),
342
( 'GlblcntUsage' , c_long ),
343
( 'ProccntUsage' , c_long ) ,
344
( 'modBaseAddr' , c_long ) ,
345
( 'modBaseSize' , c_long ) ,
346
( 'hModule' , c_void_p ) ,
347
( 'szModule' , c_char * 256 ),
348
( 'szExePath' , c_char * 260 ) ]
350
TH32CS_SNAPPROCESS = 2
351
TH32CS_SNAPMODULE = 0x00000008
353
## CreateToolhelp32Snapshot
354
CreateToolhelp32Snapshot= windll.kernel32.CreateToolhelp32Snapshot
355
CreateToolhelp32Snapshot.reltype = c_long
356
CreateToolhelp32Snapshot.argtypes = [ c_int , c_int ]
358
Process32First = windll.kernel32.Process32First
359
Process32First.argtypes = [ c_void_p , POINTER( PROCESSENTRY32 ) ]
360
Process32First.rettype = c_int
362
Process32Next = windll.kernel32.Process32Next
363
Process32Next.argtypes = [ c_void_p , POINTER(PROCESSENTRY32) ]
364
Process32Next.rettype = c_int
366
CloseHandle = windll.kernel32.CloseHandle
367
CloseHandle.argtypes = [ c_void_p ]
368
CloseHandle.rettype = c_int
370
Module32First = windll.kernel32.Module32First
371
Module32First.argtypes = [ c_void_p , POINTER(MODULEENTRY32) ]
372
Module32First.rettype = c_int
374
hProcessSnap = c_void_p(0)
375
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS , 0 )
377
pe32 = PROCESSENTRY32()
378
pe32.dwSize = sizeof( PROCESSENTRY32 )
379
ret = Process32First( hProcessSnap , pointer( pe32 ) )
383
if name + ".exe" == pe32.szExeFile:
384
hModuleSnap = c_void_p(0)
385
me32 = MODULEENTRY32()
386
me32.dwSize = sizeof( MODULEENTRY32 )
387
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pe32.th32ProcessID )
389
ret = Module32First( hModuleSnap, pointer(me32) )
390
path = me32.szExePath
391
CloseHandle( hModuleSnap )
394
ret = Process32Next( hProcessSnap, pointer(pe32) )
395
CloseHandle( hProcessSnap )
399
def getRunningProcessExePathByName_posix(name):
400
pids= [pid for pid in os.listdir('/proc') if pid.isdigit()]
403
path = os.readlink(os.path.join('/proc', pid, 'exe'))
404
if path and path.endswith(name):
409
def getRunningProcessExePathByName(name):
412
return getRunningProcessExePathByName_win32(name)
413
elif hostos == "posix":
414
return getRunningProcessExePathByName_posix(name)
422
def __init__(self, envname, prefix):
423
self.envname = envname
425
self.saved_name = None
429
self.saved_name = os.environ.get(self.envname)
430
self.new_name = tempfile.mkdtemp(prefix=self.prefix, dir=self.saved_name or None)
431
os.environ[self.envname] = self.new_name
435
os.environ[self.envname] = self.saved_name
437
del os.environ[self.envname]
439
shutil.rmtree(self.new_name)
443
#===================================================================================================
445
if __name__ == "__main__":
446
log.error("This is utility file, please execute run.py script")