Salome HOME
Try to kill Salome application when a YACS Launcher job is killed
[modules/kernel.git] / bin / virtual_salome.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2011  CEA/DEN, EDF R&D, 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.
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
24 """Create a virtual Salome installation
25
26 Based on a script created by Ian Bicking.
27
28 Typical use::
29
30   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
31
32 install module KERNEL in the current directory
33 """
34
35 import sys, os, optparse, shutil,glob,fnmatch
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:
102         print "Option module is mandatory"
103         return
104
105     module_dir=os.path.abspath(options.module)
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_idl_dir=os.path.join(module_dir,'idl','salome')
135     module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome')
136     module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),pyversio,'site-packages','salome')
137     module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),pyversio,
138                                           'site-packages','salome','shared_modules')
139     module_share_dir=os.path.join(module_dir,'share','salome')
140     module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui')
141     module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui')
142     module_doc_dir=os.path.join(module_dir,'doc','salome')
143     module_sharedoc_dir=os.path.join(module_dir,'share','doc','salome')
144     module_sharedoc_gui_dir=os.path.join(module_dir,'share','doc','salome','gui')
145     module_sharedoc_tui_dir=os.path.join(module_dir,'share','doc','salome','tui')
146
147     bin_dir=os.path.join(home_dir,'bin','salome')
148     idl_dir=os.path.join(home_dir,'idl','salome')
149     lib_dir=os.path.join(home_dir,'lib','salome')
150     lib_py_dir=os.path.join(home_dir,'lib',pyversio,'site-packages','salome')
151     lib_py_shared_dir=os.path.join(home_dir,'lib',pyversio,
152                                    'site-packages','salome','shared_modules')
153     share_dir=os.path.join(home_dir,'share','salome')
154     doc_gui_dir=os.path.join(home_dir,'doc','salome','gui')
155     doc_tui_dir=os.path.join(home_dir,'doc','salome','tui')
156     doc_dir=os.path.join(home_dir,'doc','salome')
157     sharedoc_dir=os.path.join(home_dir,'share','doc','salome')
158     sharedoc_gui_dir=os.path.join(home_dir,'share','doc','salome','gui')
159     sharedoc_tui_dir=os.path.join(home_dir,'share','doc','salome','tui')
160
161     if options.clear:
162         rmtree(bin_dir)
163         rmtree(idl_dir)
164         rmtree(lib_dir)
165         rmtree(lib_py_dir)
166         rmtree(share_dir)
167         rmtree(doc_dir)
168         rmtree(sharedoc_dir)
169         pass
170
171     #directory bin/salome : create it and link content
172     if os.path.exists(module_bin_dir):
173         mkdir(bin_dir)
174         for fn in os.listdir(module_bin_dir):
175             symlink(os.path.join(module_bin_dir, fn), os.path.join(bin_dir, fn))
176             pass
177         pass
178     else:
179         if verbose:
180             print module_bin_dir, " doesn't exist"
181         pass
182
183     #directory idl/salome : create it and link content
184     if os.path.exists(module_idl_dir):
185         mkdir(idl_dir)
186         for fn in os.listdir(module_idl_dir):
187             symlink(os.path.join(module_idl_dir, fn), os.path.join(idl_dir, fn))
188     else:
189         if verbose:
190             print module_idl_dir, " doesn't exist"
191
192     #directory lib/salome : create it and link content
193     if os.path.exists(module_lib_dir):
194         mkdir(lib_dir)
195         for fn in os.listdir(module_lib_dir):
196             symlink(os.path.join(module_lib_dir, fn), os.path.join(lib_dir, fn))
197             pass
198         pass
199     else:
200         if verbose:
201             print module_lib_dir, " doesn't exist"
202         pass
203
204     #directory lib/pyversio/site-packages/salome : create it and link content
205     if not os.path.exists(module_lib_py_dir):
206         print "Python directory %s does not exist" % module_lib_py_dir
207     else:
208         # __GBO__ specific action for the package salome
209         module_lib_pypkg_dir=os.path.join(module_lib_py_dir,"salome")
210         lib_pypkg_dir=os.path.join(lib_py_dir,"salome")
211         mkdir(lib_pypkg_dir)
212         # __GBO__
213         mkdir(lib_py_shared_dir)
214         for fn in os.listdir(module_lib_py_dir):
215             if fn == "shared_modules": continue
216             # __GBO__
217             if fn == "salome": continue
218             # __GBO__
219             symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn))
220             pass
221         if os.path.exists(module_lib_py_shared_dir):
222             for fn in os.listdir(module_lib_py_shared_dir):
223                 symlink(os.path.join(module_lib_py_shared_dir, fn), os.path.join(lib_py_shared_dir, fn))
224                 pass
225             pass
226         # __GBO__
227         if os.path.exists(module_lib_pypkg_dir):
228             for fn in os.listdir(module_lib_pypkg_dir):
229                 symlink(os.path.join(module_lib_pypkg_dir, fn), os.path.join(lib_pypkg_dir, fn))
230                 pass
231             pass
232         # __GBO__
233         else:
234             if verbose:
235                 print module_lib_py_shared_dir, " doesn't exist"
236             pass
237
238     #directory share/doc/salome (KERNEL doc) : create it and link content
239     if os.path.exists(module_sharedoc_dir):
240         mkdir(sharedoc_dir)
241         for fn in os.listdir(module_sharedoc_dir):
242             if fn == 'gui':continue
243             if fn == 'tui':continue
244             symlink(os.path.join(module_sharedoc_dir, fn), os.path.join(sharedoc_dir, fn))
245             pass
246         pass
247
248     #directory share/doc/salome/gui : create it and link content
249     if os.path.exists(module_sharedoc_gui_dir):
250         mkdir(sharedoc_gui_dir)
251         for fn in os.listdir(module_sharedoc_gui_dir):
252             symlink(os.path.join(module_sharedoc_gui_dir, fn), os.path.join(sharedoc_gui_dir, fn))
253             pass
254         pass
255
256     #directory share/doc/salome/tui : create it and link content
257     if os.path.exists(module_sharedoc_tui_dir):
258         mkdir(sharedoc_tui_dir)
259         for fn in os.listdir(module_sharedoc_tui_dir):
260             symlink(os.path.join(module_sharedoc_tui_dir, fn), os.path.join(sharedoc_tui_dir, fn))
261             pass
262         pass
263
264     #directory share/salome : create it and link content
265     if os.path.exists(module_share_dir):
266         mkdir(share_dir)
267         for fn in os.listdir(module_share_dir):
268           if fn=="resources":
269             #resources : create it and link content
270             mkdir(os.path.join(share_dir,"resources"))
271             for ffn in os.listdir(os.path.join(module_share_dir,"resources")):
272               symlink(os.path.join(module_share_dir, "resources", ffn), os.path.join(share_dir,"resources", ffn))
273           else:
274             #other directories (not resources)
275             symlink(os.path.join(module_share_dir, fn), os.path.join(share_dir, fn))
276     else:
277         print "share/salome directory %s does not exist" % module_share_dir
278         pass
279
280     #html files in doc/salome directory
281     if os.path.exists(module_doc_dir):
282         mkdir(doc_dir)
283         for fn in os.listdir(module_doc_dir):
284             if fn == 'gui':continue
285             if fn == 'tui':continue
286             symlink(os.path.join(module_doc_dir, fn), os.path.join(doc_dir, fn))
287             pass
288         pass
289
290     #directory doc/salome/gui : create it and link content
291     if os.path.exists(module_doc_gui_dir):
292         mkdir(doc_gui_dir)
293         for fn in os.listdir(module_doc_gui_dir):
294             symlink(os.path.join(module_doc_gui_dir, fn), os.path.join(doc_gui_dir, fn))
295             pass
296         pass
297
298     #directory doc/salome/tui : create it and link content
299     if os.path.exists(module_doc_tui_dir):
300         mkdir(doc_tui_dir)
301         for fn in os.listdir(module_doc_tui_dir):
302             symlink(os.path.join(module_doc_tui_dir, fn), os.path.join(doc_tui_dir, fn))
303             pass
304         pass
305
306 # -----------------------------------------------------------------------------
307
308 def main():
309     usage="""usage: %prog [options]
310 Typical use is:
311   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
312 """
313     parser = optparse.OptionParser(usage=usage)
314
315     parser.add_option('-v', '--verbose', action='count', dest='verbose',
316                       default=0, help="Increase verbosity")
317
318     parser.add_option('--prefix', dest="prefix", default='.',
319                       help="The base directory to install to (default .)")
320
321     parser.add_option('--module', dest="module",
322                       help="The module directory to install in (mandatory)")
323
324     parser.add_option('--clear', dest='clear', action='store_true',
325         help="Clear out the install and start from scratch")
326
327     options, args = parser.parse_args()
328     link_module(options)
329     pass
330
331 # -----------------------------------------------------------------------------
332
333 if __name__ == '__main__':
334     main()
335     pass