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