1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2014-2023 CEA, EDF
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 This script prepares the test environment and runs a test script:
24 - clean and create test directory
25 - create a SALOME application
27 - launch the test script within SALOME environment
30 This script uses the following environment variables:
31 - ROOT_SALOME : directory which contains salome_context.cfg.
32 This variable is usually defined in salome_prerequisites.sh
33 - KERNEL_ROOT_DIR and YACS_ROOT_DIR : directories of modules installation
34 Those variables are usually defined in salome_modules.sh
35 Environment variables can be passed to the script using the -d option.
41 class TestEnvironment:
44 shutil.rmtree("appli", ignore_errors=True)
46 # create config_appli.xml in current directory
47 salome_path = os.getenv("ROOT_SALOME", "")
48 salome_context_file = os.path.join(salome_path, "salome_context.cfg")
49 if not os.path.isfile(salome_context_file):
50 print("File salome_context.cfg not found.")
51 print("Search path:" + salome_path)
52 print("This test needs ROOT_SALOME environment variable in order to run")
55 config_appli_text = '''<application>
56 <context path="''' + salome_context_file + '''"/>
58 <module name="KERNEL" path="'''
59 kernel_path = os.getenv("KERNEL_ROOT_DIR", "")
60 if not os.path.isdir(kernel_path) :
61 print("KERNEL_ROOT_DIR not defined")
65 config_appli_text += kernel_path + '"/>'
67 # some tests need YACS module.
68 yacs_path = os.getenv("YACS_ROOT_DIR", "")
69 if os.path.isdir(yacs_path):
70 config_appli_text += '''
71 <module name="YACS" path="'''
72 config_appli_text += yacs_path + '"/>'
74 config_appli_text += '''
78 f = open("config_appli.xml", 'w')
79 f.write(config_appli_text)
82 # create a SALOME application
83 appli_gen_file = os.path.join(kernel_path,
84 "bin","salome","appli_gen.py")
86 os.system(appli_gen_file + " --prefix="+appli_dir+
87 " --config=config_appli.xml")
91 sys.path[:0] = [os.path.join(appli_dir, "bin", "salome", "appliskel")]
92 self.salome_module = imp.load_source("SALOME", os.path.join(appli_dir, "salome"))
94 self.salome_module.main(["start", "-t"])
95 except SystemExit as e:
96 # There is an exit() call in salome.main. Just ignore it.
99 def run(self, script):
102 ret = self.salome_module.main(["shell", script])
103 except SystemExit as e:
110 self.salome_module.main(["killall"])
111 except SystemExit as e:
114 pass #class TestEnvironment
116 if __name__ == '__main__':
118 parser = argparse.ArgumentParser(description=usage,
119 formatter_class=argparse.RawDescriptionHelpFormatter)
120 parser.add_argument('command', help="Test command to be run.")
121 parser.add_argument('-d', '--define', action='append', help="VARIABLE=VALUE")
123 args = parser.parse_args()
124 for opt in args.define:
125 opts = opt.split('=', 1)
126 os.environ[opts[0]] = opts[1]
128 envTest = TestEnvironment()
130 ret = envTest.run(args.command)