Salome HOME
updated copyright message
[modules/kernel.git] / bin / virtual_salome.py
1 #! /usr/bin/env python3
2 # Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 """Create a virtual Salome installation
24
25 Based on a script created by Ian Bicking.
26
27 Typical use::
28
29   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
30
31 install module KERNEL in the current directory
32 """
33
34 import sys, os, optparse, shutil, glob, fnmatch
35
36
37 py_version = 'python%s.%s' % (sys.version_info[0], sys.version_info[1])
38
39 verbose=0
40
41 # -----------------------------------------------------------------------------
42
43 def mkdir(path):
44     """Create a directory and all the intermediate directories if path does not exist"""
45     if not os.path.exists(path):
46         if verbose:
47             print('Creating %s' % path)
48         os.makedirs(path)
49     else:
50         if verbose:
51             print('Directory %s already exists' % path)
52             pass
53         pass
54
55 # -----------------------------------------------------------------------------
56
57 def symlink(src, dest):
58     """Create a link if it does not exist"""
59     if not os.path.exists(dest):
60         if os.path.lexists(dest):
61             print("Do not create symlink %s. It already exists but it's broken" % dest)
62             return
63         if verbose:
64             print('Creating symlink %s' % dest)
65             pass
66         os.symlink(os.path.relpath(src,os.path.dirname(dest)), dest)
67     else:
68         if verbose:
69             print('Symlink %s already exists' % dest)
70         pass
71     pass
72
73 # -----------------------------------------------------------------------------
74
75 def rmtree(dir):
76     """Remove (recursive) a directory if it exists"""
77     if os.path.exists(dir):
78         print('Deleting tree %s' % dir)
79         shutil.rmtree(dir)
80     else:
81         if verbose:
82             print('Do not need to delete %s; already gone' % dir)
83             pass
84         pass
85     pass
86
87 # -----------------------------------------------------------------------------
88
89 __lib__dir__ = None
90 def get_lib_dir():
91     global __lib__dir__
92     if __lib__dir__: return __lib__dir__
93     import platform
94     __lib__dir__ = "lib"
95     return __lib__dir__
96
97 # -----------------------------------------------------------------------------
98
99 def link_module(options):
100     global verbose
101
102     if not options.module_path:
103         print("Option module is mandatory")
104         return
105
106     module_dir=os.path.abspath(options.module_path)
107     if not os.path.exists(module_dir):
108         print("Module %s does not exist" % module_dir)
109         return
110
111     verbose = options.verbose
112
113     home_dir = os.path.expanduser(options.prefix)
114     #try to find python version of salome application and put it in versio
115     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(home_dir,get_lib_dir(),"python*.*"))]
116     if not pys :
117       versio=None
118     else:
119       versio=pys[0]
120
121     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(module_dir,get_lib_dir(),"python*.*"))]
122     #check if the module has a python version compatible with application version
123     if not pys :
124       pyversio=versio or py_version
125     elif versio is None:
126       pyversio=pys[0]
127     elif versio in pys:
128       pyversio=versio
129     else:
130       #incompatible python versions
131       print("incompatible python versions : application has version %s and module %s has not" % (versio,module_dir))
132       return
133
134     module_bin_dir=os.path.join(module_dir,'bin','salome')
135     module_test_dir=os.path.join(module_dir,'bin','salome', 'test')
136     module_idl_dir=os.path.join(module_dir,'idl','salome')
137     module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome')
138     module_pvlib_dir=os.path.join(module_dir,get_lib_dir(),'paraview')
139     module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),pyversio,'site-packages','salome')
140     module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),pyversio,
141                                           'site-packages','salome')
142     module_share_dir=os.path.join(module_dir,'share','salome')
143     module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui')
144     module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui')
145     module_doc_dir=os.path.join(module_dir,'doc','salome')
146     module_sharedoc_dir=os.path.join(module_dir,'share','doc','salome')
147     module_sharedoc_gui_dir=os.path.join(module_dir,'share','doc','salome','gui')
148     module_sharedoc_tui_dir=os.path.join(module_dir,'share','doc','salome','tui')
149     module_sharedoc_examples=os.path.join(module_dir,'share','doc','salome','examples')
150     module_sharedoc_dev=os.path.join(module_dir,'share','doc','salome','dev')
151
152     bin_dir=os.path.join(home_dir,'bin','salome')
153     test_dir=os.path.join(home_dir,'bin','salome', 'test')
154     idl_dir=os.path.join(home_dir,'idl','salome')
155     lib_dir=os.path.join(home_dir,'lib','salome')
156     pvlib_dir=os.path.join(home_dir,'lib','paraview')
157     lib_py_dir=os.path.join(home_dir,'lib',pyversio,'site-packages','salome')
158     lib_py_shared_dir=os.path.join(home_dir,'lib',pyversio,
159                                    'site-packages','salome')
160     share_dir=os.path.join(home_dir,'share','salome')
161     doc_gui_dir=os.path.join(home_dir,'doc','salome','gui')
162     doc_tui_dir=os.path.join(home_dir,'doc','salome','tui')
163     doc_dir=os.path.join(home_dir,'doc','salome')
164     sharedoc_dir=os.path.join(home_dir,'share','doc','salome')
165     sharedoc_gui_dir=os.path.join(home_dir,'share','doc','salome','gui')
166     sharedoc_tui_dir=os.path.join(home_dir,'share','doc','salome','tui')
167     sharedoc_examples_dir=os.path.join(home_dir,'share','doc','salome','examples')
168     sharedoc_dev_dir=os.path.join(home_dir,'share','doc','salome','dev')
169
170     if options.clear:
171         rmtree(bin_dir)
172         rmtree(test_dir)
173         rmtree(idl_dir)
174         rmtree(lib_dir)
175         rmtree(lib_py_dir)
176         rmtree(share_dir)
177         rmtree(doc_dir)
178         rmtree(sharedoc_dir)
179         pass
180
181     #directory bin/salome : create it and link content
182     if os.path.exists(module_bin_dir):
183         mkdir(bin_dir)
184         mkdir(test_dir)
185         for fn in os.listdir(module_bin_dir):
186             if fn != "test":
187                 symlink(os.path.join(module_bin_dir, fn), os.path.join(bin_dir, fn))
188             pass
189         pass
190     else:
191         if verbose:
192             print(module_bin_dir, " doesn't exist")
193         pass
194
195     #directory bin/salome/test : create it and link content
196     if os.path.exists(module_test_dir):
197         # link <appli_path>/bin/salome/test/<module> to <module_path>/bin/salome/test
198         print("link %s --> %s"%(os.path.join(test_dir, options.module_name), module_test_dir))
199         symlink(module_test_dir, os.path.join(test_dir, options.module_name))
200         # register module for testing in CTestTestfile.cmake
201         with open(os.path.join(test_dir, "CTestTestfile.cmake"), "ab") as f:
202             aStr = "SUBDIRS(%s)\n"%options.module_name
203             f.write(aStr.encode())
204     else:
205         if verbose:
206             print(module_test_dir, " doesn't exist")
207         pass
208
209     #directory idl/salome : create it and link content
210     if os.path.exists(module_idl_dir):
211         mkdir(idl_dir)
212         for fn in os.listdir(module_idl_dir):
213             symlink(os.path.join(module_idl_dir, fn), os.path.join(idl_dir, fn))
214     else:
215         if verbose:
216             print(module_idl_dir, " doesn't exist")
217
218     #directory lib/salome : create it and link content
219     if os.path.exists(module_lib_dir):
220         mkdir(lib_dir)
221         for fn in os.listdir(module_lib_dir):
222             symlink(os.path.join(module_lib_dir, fn), os.path.join(lib_dir, fn))
223             pass
224         pass
225     else:
226         if verbose:
227             print(module_lib_dir, " doesn't exist")
228         pass
229
230     #directory lib/paraview : create it and link content
231     if os.path.exists(module_pvlib_dir):
232         mkdir(pvlib_dir)
233         for fn in os.listdir(module_pvlib_dir):
234             symlink(os.path.join(module_pvlib_dir, fn), os.path.join(pvlib_dir, fn))
235             pass
236         pass
237     else:
238         if verbose:
239             print(module_pvlib_dir, " doesn't exist")
240         pass
241
242     #directory lib/pyversio/site-packages/salome : create it and link content
243     if not os.path.exists(module_lib_py_dir):
244         print("Python directory %s does not exist" % module_lib_py_dir)
245     else:
246         # Specific action for the package salome
247         module_lib_pypkg_dir=os.path.join(module_lib_py_dir,"salome")
248         lib_pypkg_dir=os.path.join(lib_py_dir,"salome")
249         mkdir(lib_pypkg_dir)
250         mkdir(lib_py_shared_dir)
251         for fn in os.listdir(module_lib_py_dir):
252             if fn == "salome": continue
253             symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn))
254             pass
255         if os.path.exists(module_lib_py_shared_dir):
256             for fn in os.listdir(module_lib_py_shared_dir):
257                 symlink(os.path.join(module_lib_py_shared_dir, fn), os.path.join(lib_py_shared_dir, fn))
258                 pass
259             pass
260         if os.path.exists(module_lib_pypkg_dir):
261             for fn in os.listdir(module_lib_pypkg_dir):
262                 symlink(os.path.join(module_lib_pypkg_dir, fn), os.path.join(lib_pypkg_dir, fn))
263                 pass
264             pass
265         else:
266             if verbose:
267                 print(module_lib_py_shared_dir, " doesn't exist")
268             pass
269
270     #directory share/doc/salome (KERNEL doc) : create it and link content
271     if os.path.exists(module_sharedoc_dir):
272         mkdir(sharedoc_dir)
273         for fn in os.listdir(module_sharedoc_dir):
274             if fn == 'gui':continue
275             if fn == 'tui':continue
276             if fn == 'examples':continue
277             if fn == 'dev':continue
278             symlink(os.path.join(module_sharedoc_dir, fn), os.path.join(sharedoc_dir, fn))
279             pass
280         pass
281
282     #directory share/doc/salome/gui : create it and link content
283     if os.path.exists(module_sharedoc_gui_dir):
284         mkdir(sharedoc_gui_dir)
285         for fn in os.listdir(module_sharedoc_gui_dir):
286             symlink(os.path.join(module_sharedoc_gui_dir, fn), os.path.join(sharedoc_gui_dir, fn))
287             pass
288         pass
289
290     #directory share/doc/salome/tui : create it and link content
291     if os.path.exists(module_sharedoc_tui_dir):
292         mkdir(sharedoc_tui_dir)
293         for fn in os.listdir(module_sharedoc_tui_dir):
294             symlink(os.path.join(module_sharedoc_tui_dir, fn), os.path.join(sharedoc_tui_dir, fn))
295             pass
296         pass
297
298     #directory share/doc/salome/examples : create it and link content
299     if os.path.exists(module_sharedoc_examples):
300         mkdir(sharedoc_examples_dir)
301         for fn in os.listdir(module_sharedoc_examples):
302             symlink(os.path.join(module_sharedoc_examples, fn), os.path.join(sharedoc_examples_dir, fn))
303             pass
304         pass
305
306     #directory share/doc/salome/dev : create it and link content
307     if os.path.exists(module_sharedoc_dev):
308         mkdir(sharedoc_dev_dir)
309         for fn in os.listdir(module_sharedoc_dev):
310             symlink(os.path.join(module_sharedoc_dev, fn), os.path.join(sharedoc_dev_dir, fn))
311             pass
312         pass
313
314     #directory share/salome : create it and link content
315     if os.path.exists(module_share_dir):
316         mkdir(share_dir)
317         for fn in os.listdir(module_share_dir):
318             if fn in ["resources","plugins"] :
319                 # Create the directory and then link the content
320                 mkdir(os.path.join(share_dir,fn))
321                 for ffn in os.listdir(os.path.join(module_share_dir,fn)):
322                     symlink(os.path.join(module_share_dir, fn, ffn), os.path.join(share_dir,fn, ffn))
323             else:
324                 #other directories (not resources)
325                 symlink(os.path.join(module_share_dir, fn), os.path.join(share_dir, fn))
326     else:
327         print("share/salome directory %s does not exist" % module_share_dir)
328         pass
329
330     #html files in doc/salome directory
331     if os.path.exists(module_doc_dir):
332         mkdir(doc_dir)
333         for fn in os.listdir(module_doc_dir):
334             if fn == 'gui':continue
335             if fn == 'tui':continue
336             symlink(os.path.join(module_doc_dir, fn), os.path.join(doc_dir, fn))
337             pass
338         pass
339
340     #directory doc/salome/gui : create it and link content
341     if os.path.exists(module_doc_gui_dir):
342         mkdir(doc_gui_dir)
343         for fn in os.listdir(module_doc_gui_dir):
344             symlink(os.path.join(module_doc_gui_dir, fn), os.path.join(doc_gui_dir, fn))
345             pass
346         pass
347
348     #directory doc/salome/tui : create it and link content
349     if os.path.exists(module_doc_tui_dir):
350         mkdir(doc_tui_dir)
351         for fn in os.listdir(module_doc_tui_dir):
352             symlink(os.path.join(module_doc_tui_dir, fn), os.path.join(doc_tui_dir, fn))
353             pass
354         pass
355
356 def link_extra_test(options):
357     global verbose
358
359     if not options.extra_test_path:
360         print("Option extra_test is mandatory")
361         return
362
363     extra_test_dir=os.path.abspath(options.extra_test_path)
364     if not os.path.exists(extra_test_dir):
365         print("Test %s does not exist" % extra_test_dir)
366         return
367
368     verbose = options.verbose
369
370     home_dir = os.path.expanduser(options.prefix)
371     test_dir = os.path.join(home_dir,'bin','salome', 'test')
372
373     if options.clear:
374         rmtree(test_dir)
375         pass
376
377     #directory bin/salome/test : create it and link content
378     if os.path.exists(extra_test_dir):
379         # link <appli_path>/bin/salome/test/<extra_test> to <extra_test_path>/bin/salome/test
380         print("link %s --> %s"%(os.path.join(test_dir, options.extra_test_name), extra_test_dir))
381         symlink(extra_test_dir, os.path.join(test_dir, options.extra_test_name))
382         # register extra_test for testing in CTestTestfile.cmake
383         with open(os.path.join(test_dir, "CTestTestfile.cmake"), "ab") as f:
384             aStr = "SUBDIRS(%s)\n" % options.extra_test_name
385             f.write(aStr.encode())
386     else:
387         if verbose:
388             print(extra_test_dir, " doesn't exist")
389         pass
390
391 # -----------------------------------------------------------------------------
392
393 def main():
394     usage="""usage: %prog [options]
395 Typical use is:
396   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
397 """
398     parser = optparse.OptionParser(usage=usage)
399
400     parser.add_option('-v', '--verbose', action='count', dest='verbose',
401                       default=0, help="Increase verbosity")
402
403     parser.add_option('--prefix', dest="prefix", default='.',
404                       help="The base directory to install to (default .)")
405
406     parser.add_option('--module', dest="module",
407                       help="The module directory to install in (mandatory)")
408
409     parser.add_option('--clear', dest='clear', action='store_true',
410         help="Clear out the install and start from scratch")
411
412     options, args = parser.parse_args()
413     link_module(options)
414     pass
415
416 # -----------------------------------------------------------------------------
417
418 if __name__ == '__main__':
419     main()
420     pass