063d7877a7096d3bc2c153cd2c55b8f0449b6413
[opencv] / tests / swig_python / cvtestutils.py
1 # cvtestutils.py
2 #
3 # This module is meant to aid writing and running Python-based tests 
4 # within the OpenCV tree.  
5 #
6 # 2009-01-23, Roman Stanchak <rstanchak@gmail.com>
7 #
8 #
9 # Upon importing, this module adds the following python module 
10 # directories from the dev tree to sys.path (i.e. PYTHON_PATH):
11 #    opencv/interfaces/swig/python and opencv/lib
12 #
13 # Using it in a test case is simple, just be sure to import 
14 # cvtestutils before cv, highgui, etc
15 #
16 # Usage:
17 # import cvtestutils
18 # import cv
19
20 import os
21 import imp
22 import sys
23
24 def top_srcdir():
25     """
26     Return a string containing the top-level source directory in the OpenCV tree
27     """
28     dir = os.path.dirname(os.path.realpath(__file__))
29
30     # top level dir should be two levels up from this file
31     return os.path.realpath( os.path.join( dir, '..', '..' ) )
32
33 def top_builddir():
34     """
35     Return a string containing the top-level build directory in the OpenCV tree.
36     Either returns realpath(argv[1]) or top_srcdir();
37     """
38     if len(sys.argv)>1: return os.path.realpath( sys.argv[1] );
39     else: return top_srcdir();
40
41 def initpath():
42     """
43     Prepend the python module directories from the dev tree to sys.path 
44     (i.e. PYTHON_PATH)
45     """
46
47     # add path for OpenCV source directory (for adapters.py)
48     moduledir = os.path.join(top_srcdir(), 'interfaces','swig','python')
49     moduledir = os.path.realpath(moduledir)
50     sys.path.insert(0, moduledir) 
51
52     # add path for OpenCV build directory
53     moduledir = os.path.join(top_builddir(), 'interfaces','swig','python')
54     moduledir = os.path.realpath(moduledir)
55     sys.path.insert(0, moduledir) 
56
57     libdir = os.path.join(top_builddir(), 'lib' )
58     libdir = os.path.realpath(libdir)
59     sys.path.insert(0, libdir)
60
61 def which():
62     """
63     Print the directory containing cv.py
64     """
65     import cv 
66     print "Using OpenCV Python in: " + os.path.dirname(cv.__file__)
67
68 def datadir():
69     """
70     Return a string containing the full path to the python testdata directory
71     """
72     return os.path.sep.join([top_srcdir(), '..', 'opencv_extra', 'testdata', 'python'])
73
74 ### Module Initialization
75 try:
76     if MODULE_LOADED:
77         pass
78 except NameError:
79     initpath()
80     which()
81     MODULE_LOADED=1