Salome HOME
Updated copyright comment
[modules/kernel.git] / bin / runSalomeOnDemand.py
1 #!/usr/bin/env python3
2 # -*- coding:utf-8 -*-
3 # Copyright (C) 2022-2024  CEA, EDF, OPEN CASCADE
4 #
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.
9 #
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.
14 #
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
18 #
19 # See https://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 #  File   : runSalomeOnDemand.py
23 #  Author : Konstantin Leontev, Open Cascade
24 #
25 ## @package runSalomeOnDemand
26 # \brief Module that provides services to launch SALOME with custom set of modules
27 #
28
29 """Run SALOME app in the context of adding modules as extensions.
30 """
31
32 import os,sys
33 import salomeContext
34 from SalomeOnDemandTK.extension_utilities import logger, \
35     set_selext_env, get_app_root, find_file
36 from SalomeOnDemandTK.extension_query import ext_by_dependants, dependency_tree
37
38
39 def set_ext_env(app_name='', version=''):
40     """
41     Set an environment to start SALOME as a set of extensions.
42
43     Args:
44         app_name - an application's name.
45         version - a version of an application.
46
47     Returns:
48         None.
49     """
50
51     logger.debug('Set an env for app: %s, version: %s...', app_name, version)
52
53     # Get the root directory
54     app_root = get_app_root()
55
56     # Set the root dir as env variable
57     context = salomeContext.SalomeContext(None)
58     context.setVariable('SALOME_APPLICATION_DIR', app_root, overwrite=True)
59
60     # Find and source all _env.py files for installed extensions
61     tree = dependency_tree(app_root)
62     installed_ext = ext_by_dependants(tree)
63     logger.debug('Installed extensions: %s', installed_ext)
64     if not installed_ext:
65         logger.debug('There are not any extensions in %s!', app_root)
66         return
67
68     # Execute env file as a module
69     for ext_name in installed_ext:
70         set_selext_env(app_root, ext_name, context)
71     for python_path in os.environ["PYTHONPATH"].split(':'):
72         sys.path[:0] = [python_path]
73
74 if __name__ == "__main__":
75     if len(sys.argv) == 3:
76         arg_1, arg_2 = sys.argv[1:]
77         set_ext_env(arg_1, arg_2)
78     else:
79         logger.error('You must provide all the arguments!')
80         logger.info(set_ext_env.__doc__)