Skip to content

Commit f632f5c

Browse files
committed
Merge pull request opencv#7529 from alalek:backport_7526
2 parents f0d592e + 8577f71 commit f632f5c

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

modules/ts/misc/run_suite.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def getTestList(self, white, black):
8585
return set(res)
8686

8787
def isTest(self, fullpath):
88-
if fullpath == "java":
89-
return True
88+
if fullpath in ['java', 'python', 'python2', 'python3']:
89+
return self.options.mode == 'test'
9090
if not os.path.isfile(fullpath):
9191
return False
9292
if self.cache.getOS() == "nt" and not fullpath.endswith(".exe"):
@@ -102,13 +102,37 @@ def wrapInValgrind(self, cmd = []):
102102
return res + cmd
103103
return cmd
104104

105+
def tryCommand(self, cmd):
106+
try:
107+
if 0 == execute(cmd, cwd = workingDir):
108+
return True
109+
except:
110+
pass
111+
return False
112+
105113
def runTest(self, path, logfile, workingDir, args = []):
106114
args = args[:]
107115
exe = os.path.abspath(path)
108116
if path == "java":
109117
cmd = [self.cache.ant_executable, "-Dopencv.build.type=%s" % self.cache.build_type, "buildAndTest"]
110118
ret = execute(cmd, cwd = self.cache.java_test_binary_dir + "/.build")
111119
return None, ret
120+
elif path in ['python', 'python2', 'python3']:
121+
executable = os.getenv('OPENCV_PYTHON_BINARY', None)
122+
if executable is None:
123+
executable = path
124+
if not self.tryCommand([executable, '--version']):
125+
executable = 'python'
126+
cmd = [executable, self.cache.opencv_home + '/modules/python/test/test.py', '--repo', self.cache.opencv_home, '-v'] + args
127+
module_suffix = '' if not 'Visual Studio' in self.cache.cmake_generator else '/' + self.cache.build_type
128+
env = {}
129+
env['PYTHONPATH'] = self.cache.opencv_build + '/lib' + module_suffix + os.pathsep + os.getenv('PYTHONPATH', '')
130+
if self.cache.getOS() == 'nt':
131+
env['PATH'] = self.cache.opencv_build + '/bin' + module_suffix + os.pathsep + os.getenv('PATH', '')
132+
else:
133+
env['LD_LIBRARY_PATH'] = self.cache.opencv_build + '/bin' + os.pathsep + os.getenv('LD_LIBRARY_PATH', '')
134+
ret = execute(cmd, cwd = workingDir, env = env)
135+
return None, ret
112136
else:
113137
if isColorEnabled(args):
114138
args.append("--gtest_color=yes")
@@ -140,12 +164,15 @@ def runTests(self, tests, black, workingDir, args = []):
140164
more_args = []
141165
exe = self.getTest(test)
142166

143-
userlog = [a for a in args if a.startswith("--gtest_output=")]
144-
if len(userlog) == 0:
145-
logname = self.getLogName(exe, date)
146-
more_args.append("--gtest_output=xml:" + logname)
167+
if exe in ["java", "python", "python2", "python3"]:
168+
logname = None
147169
else:
148-
logname = userlog[0][userlog[0].find(":")+1:]
170+
userlog = [a for a in args if a.startswith("--gtest_output=")]
171+
if len(userlog) == 0:
172+
logname = self.getLogName(exe, date)
173+
more_args.append("--gtest_output=xml:" + logname)
174+
else:
175+
logname = userlog[0][userlog[0].find(":")+1:]
149176

150177
log.debug("Running the test: %s (%s) ==> %s in %s", exe, args + more_args, logname, workingDir)
151178
if self.options.dry_run:

modules/ts/misc/run_utils.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ class Err(Exception):
2222
def __init__(self, msg, *args):
2323
self.msg = msg % args
2424

25-
def execute(cmd, silent = False, cwd = "."):
25+
def execute(cmd, silent = False, cwd = ".", env = None):
2626
try:
2727
log.debug("Run: %s", cmd)
28+
if env:
29+
for k in env:
30+
log.debug(" Environ: %s=%s", k, env[k])
31+
env = os.environ.update(env)
2832
if silent:
29-
return check_output(cmd, stderr = STDOUT, cwd = cwd).decode("latin-1")
33+
return check_output(cmd, stderr = STDOUT, cwd = cwd, env = env).decode("latin-1")
3034
else:
31-
return check_call(cmd, cwd = cwd)
35+
return check_call(cmd, cwd = cwd, env = env)
3236
except CalledProcessError as e:
3337
if silent:
3438
log.debug("Process returned: %d", e.returncode)
@@ -171,6 +175,9 @@ def testSIMD(compiler, cxx_flags, compiler_arg = None):
171175
{'name': "cuda_library", 'default': None, 'pattern': re.compile(r"^CUDA_CUDA_LIBRARY:FILEPATH=(.+)$")},
172176
{'name': "cuda_version", 'default': None, 'pattern': re.compile(r"^CUDA_VERSION:STRING=(.+)$")},
173177
{'name': "core_dependencies", 'default': None, 'pattern': re.compile(r"^opencv_core_LIB_DEPENDS:STATIC=(.+)$")},
178+
{'name': "python", 'default': None, 'pattern': re.compile(r"^BUILD_opencv_python:BOOL=(.*)$")},
179+
{'name': "python2", 'default': None, 'pattern': re.compile(r"^BUILD_opencv_python2:BOOL=(.*)$")},
180+
{'name': "python3", 'default': None, 'pattern': re.compile(r"^BUILD_opencv_python3:BOOL=(.*)$")},
174181
)
175182

176183
class CMakeCache:
@@ -247,18 +254,33 @@ def gatherTests(self, mask, isGood = None):
247254
files = glob.glob(os.path.join(d, mask))
248255
if not self.getOS() == "android" and self.withJava():
249256
files.append("java")
257+
if self.withPython():
258+
files.append("python")
259+
if self.withPython2():
260+
files.append("python2")
261+
if self.withPython3():
262+
files.append("python3")
250263
return [f for f in files if isGood(f)]
251264
return []
252265

253266
def isMainModule(self, name):
254-
return name in self.main_modules
267+
return name in self.main_modules + ['python', 'python2', 'python3']
255268

256269
def withCuda(self):
257270
return self.cuda_version and self.with_cuda == "ON" and self.cuda_library and not self.cuda_library.endswith("-NOTFOUND")
258271

259272
def withJava(self):
260273
return self.ant_executable and self.java_test_binary_dir
261274

275+
def withPython(self):
276+
return self.python == 'ON'
277+
278+
def withPython2(self):
279+
return self.python2 == 'ON'
280+
281+
def withPython3(self):
282+
return self.python3 == 'ON'
283+
262284
def getGitVersion(self):
263285
if self.cmake_home_vcver:
264286
if self.cmake_home_vcver == self.opencv_home_vcver:

0 commit comments

Comments
 (0)