Salome HOME
Adding Multinode method for smesh parallelism (with windows fixed)
[modules/smesh.git] / src / SMESH_SWIG / send_files.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 ## Copyright (C) 2021-2023  CEA/DEN, EDF R&D, 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 http://www.salome-platform.org/ or email :
20 ##     webmaster.salome@opencascade.com
21 ##
22 """
23 File to send files on remote ressource
24 """
25 from os import environ, path
26
27 from argparse import ArgumentParser
28 import pydefx
29 import pylauncher
30
31 def create_launcher():
32     """ Initialise pylauncher
33     """
34     launcher = pylauncher.Launcher_cpp()
35     launcher.SetResourcesManager(create_resources_manager())
36     return launcher
37
38 def create_resources_manager():
39     """ Look for the catalog file and create a ressource manager with it """
40     # localhost is defined anyway, even if the catalog file does not exist.
41     catalog_path = environ.get("USER_CATALOG_RESOURCES_FILE", "")
42     if not path.isfile(catalog_path):
43         salome_path = environ.get("ROOT_SALOME_INSTALL", "")
44         catalog_path = path.join(salome_path, "CatalogResources.xml")
45     if not path.isfile(catalog_path):
46         catalog_path = ""
47
48     return pylauncher.ResourcesManager_cpp(catalog_path)
49
50 def create_job_parameters():
51     """ Initialsie JobParameters """
52     jparam = pylauncher.JobParameters_cpp()
53     jparam.resource_required = create_resource_parameters()
54     return jparam
55
56 def create_resource_parameters():
57     """ Init resourceParams """
58     return pylauncher.resourceParams()
59
60 def send_file(args):
61     """ job to send a file to the cluster """
62     # salome launcher
63     launcher = create_launcher()
64
65     # See SALOME_Launcher documentation for parameters
66     job_params = create_job_parameters()
67     job_params.job_type = "command_salome" # creates CatalogResources.xml
68
69     local_dir = path.dirname(args.input_file)
70
71     # job_params.pre_command = pre_command # command to run on frontal
72     # script to run in batch mode
73     run_script = path.join(path.dirname(args.input_file), "run.sh")
74     with open(run_script, "w") as f:
75         f.write("#!/bin/bash\n")
76     job_params.job_file = run_script
77     job_params.resource_required.nb_proc = 1
78
79     # files to copy to remote working dir
80     # Directories are copied recursively.
81     # job_file script is automaticaly copied.
82     job_params.in_files = [args.input_file]
83     print("in_files", job_params.in_files)
84     # local path where to copy out_files
85     job_params.result_directory = local_dir
86
87     job_params.job_name = "SMESH_transfer"
88     job_params.resource_required.name = args.resource
89
90     # remote job directory
91     # Retrieve working dir from catalog
92     res_manager = create_resources_manager()
93     res_params = res_manager.GetResourceDefinition(args.resource)
94     job_params.work_directory = path.join(\
95             res_params.working_directory,
96             path.basename(path.dirname(args.input_file)))
97
98     print("work_directory", job_params.work_directory)
99
100     job_id = launcher.createJob(job_params) #SALOME id of the job
101     launcher.exportInputFiles(job_id)
102
103
104 def def_arg():
105     """ Define and parse arguments for the script """
106     parser = ArgumentParser()
107     parser.add_argument("input_file",\
108         help="file to copy")
109
110     # Run parameters
111
112     parser.add_argument("--resource",
113                         help="resource from SALOME Catalog")
114
115     args = parser.parse_args()
116
117     return args
118
119 def main():
120     """ Main function """
121     args = def_arg()
122     send_file(args)
123
124 if __name__ == "__main__":
125     main()