]> SALOME platform Git repositories - modules/kernel.git/blob - bin/virtual_salome.py
Salome HOME
CCAR: add a new parameter (resources) to configuration of SALOME Application
[modules/kernel.git] / bin / virtual_salome.py
1 #  -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2007-2008  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 """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 py_version = 'python%s.%s' % (sys.version_info[0], sys.version_info[1])
36
37 verbose=0
38
39 # -----------------------------------------------------------------------------
40
41 def mkdir(path):
42     """Create a directory and all the intermediate directories if path does not exist"""
43     if not os.path.exists(path):
44         if verbose:
45             print 'Creating %s' % path
46         os.makedirs(path)
47     else:
48         if verbose:
49             print 'Directory %s already exists' % path
50             pass
51         pass
52
53 # -----------------------------------------------------------------------------
54
55 def symlink(src, dest):
56     """Create a link if it does not exist"""
57     if not os.path.exists(dest):
58         if os.path.lexists(dest):
59             print "Do not create symlink %s. It already exists but it's broken" % dest
60             return
61         if verbose:
62             print 'Creating symlink %s' % dest
63             pass
64         os.symlink(src, dest)
65     else:
66         if verbose:
67             print 'Symlink %s already exists' % dest
68         pass
69     pass
70
71 # -----------------------------------------------------------------------------
72
73 def rmtree(dir):
74     """Remove (recursive) a directory if it exists"""
75     if os.path.exists(dir):
76         print 'Deleting tree %s' % dir
77         shutil.rmtree(dir)
78     else:
79         if verbose:
80             print 'Do not need to delete %s; already gone' % dir
81             pass
82         pass
83     pass
84
85 # -----------------------------------------------------------------------------
86
87 __lib__dir__ = None
88 def get_lib_dir():
89     global __lib__dir__
90     if __lib__dir__: return __lib__dir__
91     import platform
92     __lib__dir__ = "lib"
93     return __lib__dir__
94
95 # -----------------------------------------------------------------------------
96
97 def link_module(options):
98     global verbose
99
100     if not options.module:
101         print "Option module is mandatory"
102         return 
103    
104     module_dir=os.path.abspath(options.module)
105     if not os.path.exists(module_dir):
106         print "Module %s does not exist" % module_dir
107         return
108
109     verbose = options.verbose
110
111     home_dir = os.path.expanduser(options.prefix)
112     #try to find python version of salome application and put it in versio
113     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(home_dir,get_lib_dir(),"python*.*"))]
114     if not pys :
115       versio=None
116     else:
117       versio=pys[0]
118
119     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(module_dir,get_lib_dir(),"python*.*"))]
120     #check if the module has a python version compatible with application version
121     if not pys :
122       pyversio=versio or py_version
123     elif versio is None:
124       pyversio=pys[0]
125     elif versio in pys:
126       pyversio=versio
127     else:
128       #incompatible python versions
129       print "incompatible python versions : application has version %s and module %s has not" % (versio,module_dir)
130       return
131
132     module_bin_dir=os.path.join(module_dir,'bin','salome')
133     module_idl_dir=os.path.join(module_dir,'idl','salome')
134     module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome')
135     module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),pyversio,'site-packages','salome')
136     module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),pyversio,
137                                           'site-packages','salome','shared_modules')
138     module_share_dir=os.path.join(module_dir,'share','salome')
139     module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui')
140     module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui')
141     module_doc_dir=os.path.join(module_dir,'doc','salome')
142     module_sharedoc_dir=os.path.join(module_dir,'share','doc','salome')
143     module_sharedoc_gui_dir=os.path.join(module_dir,'share','doc','salome','gui')
144     module_sharedoc_tui_dir=os.path.join(module_dir,'share','doc','salome','tui')
145
146     bin_dir=os.path.join(home_dir,'bin','salome')
147     idl_dir=os.path.join(home_dir,'idl','salome')
148     lib_dir=os.path.join(home_dir,'lib','salome')
149     lib_py_dir=os.path.join(home_dir,'lib',pyversio,'site-packages','salome')
150     lib_py_shared_dir=os.path.join(home_dir,'lib',pyversio,
151                                    'site-packages','salome','shared_modules')
152     share_dir=os.path.join(home_dir,'share','salome')
153     doc_gui_dir=os.path.join(home_dir,'doc','salome','gui')
154     doc_tui_dir=os.path.join(home_dir,'doc','salome','tui')
155     doc_dir=os.path.join(home_dir,'doc','salome')
156     sharedoc_dir=os.path.join(home_dir,'share','doc','salome')
157     sharedoc_gui_dir=os.path.join(home_dir,'share','doc','salome','gui')
158     sharedoc_tui_dir=os.path.join(home_dir,'share','doc','salome','tui')
159
160     if options.clear:
161         rmtree(bin_dir)
162         rmtree(idl_dir)
163         rmtree(lib_dir)
164         rmtree(lib_py_dir)
165         rmtree(share_dir)
166         rmtree(doc_dir)
167         rmtree(sharedoc_dir)
168         pass
169     
170     #directory bin/salome : create it and link content
171     if os.path.exists(module_bin_dir):
172         mkdir(bin_dir)
173         for fn in os.listdir(module_bin_dir):
174             symlink(os.path.join(module_bin_dir, fn), os.path.join(bin_dir, fn))
175             pass
176         pass
177     else:
178         if verbose:
179             print module_bin_dir, " doesn't exist"
180         pass    
181     
182     #directory idl/salome : create it and link content
183     if os.path.exists(module_idl_dir):
184         mkdir(idl_dir)
185         for fn in os.listdir(module_idl_dir):
186             symlink(os.path.join(module_idl_dir, fn), os.path.join(idl_dir, fn))
187     else:
188         if verbose:
189             print module_idl_dir, " doesn't exist"
190
191     #directory lib/salome : create it and link content
192     if os.path.exists(module_lib_dir):
193         mkdir(lib_dir)
194         for fn in os.listdir(module_lib_dir):
195             symlink(os.path.join(module_lib_dir, fn), os.path.join(lib_dir, fn))
196             pass
197         pass
198     else:
199         if verbose:
200             print module_lib_dir, " doesn't exist"
201         pass    
202     
203     #directory lib/pyversio/site-packages/salome : create it and link content
204     if not os.path.exists(module_lib_py_dir):
205         print "Python directory %s does not exist" % module_lib_py_dir
206     else:
207         mkdir(lib_py_shared_dir)
208         for fn in os.listdir(module_lib_py_dir):
209             if fn == "shared_modules": continue
210             symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn))
211             pass    
212         if os.path.exists(module_lib_py_shared_dir):
213             for fn in os.listdir(module_lib_py_shared_dir):
214                 symlink(os.path.join(module_lib_py_shared_dir, fn), os.path.join(lib_py_shared_dir, fn))
215                 pass
216             pass
217         else:
218             if verbose:
219                 print module_lib_py_shared_dir, " doesn't exist"
220             pass    
221
222     #directory share/doc/salome (KERNEL doc) : create it and link content
223     if os.path.exists(module_sharedoc_dir):
224         mkdir(sharedoc_dir)
225         for fn in os.listdir(module_sharedoc_dir):
226             if fn == 'gui':continue
227             if fn == 'tui':continue
228             symlink(os.path.join(module_sharedoc_dir, fn), os.path.join(sharedoc_dir, fn))
229             pass
230         pass
231
232     #directory share/doc/salome/gui : create it and link content
233     if os.path.exists(module_sharedoc_gui_dir):
234         mkdir(sharedoc_gui_dir)
235         for fn in os.listdir(module_sharedoc_gui_dir):
236             symlink(os.path.join(module_sharedoc_gui_dir, fn), os.path.join(sharedoc_gui_dir, fn))
237             pass
238         pass
239     
240     #directory share/doc/salome/tui : create it and link content
241     if os.path.exists(module_sharedoc_tui_dir):
242         mkdir(sharedoc_tui_dir)
243         for fn in os.listdir(module_sharedoc_tui_dir):
244             symlink(os.path.join(module_sharedoc_tui_dir, fn), os.path.join(sharedoc_tui_dir, fn))
245             pass
246         pass
247
248     #directory share/salome : create it and link content
249     if os.path.exists(module_share_dir):
250         mkdir(share_dir)
251         for fn in os.listdir(module_share_dir):
252           if fn=="resources":
253             #resources : create it and link content
254             mkdir(os.path.join(share_dir,"resources"))
255             for ffn in os.listdir(os.path.join(module_share_dir,"resources")):
256               symlink(os.path.join(module_share_dir, "resources", ffn), os.path.join(share_dir,"resources", ffn))
257           else:
258             #other directories (not resources)
259             symlink(os.path.join(module_share_dir, fn), os.path.join(share_dir, fn))
260     else:
261         print "share/salome directory %s does not exist" % module_share_dir
262         pass
263
264     #html files in doc/salome directory
265     if os.path.exists(module_doc_dir):
266         mkdir(doc_dir)
267         for fn in os.listdir(module_doc_dir):
268             if fn == 'gui':continue
269             if fn == 'tui':continue
270             symlink(os.path.join(module_doc_dir, fn), os.path.join(doc_dir, fn))
271             pass
272         pass
273     
274     #directory doc/salome/gui : create it and link content
275     if os.path.exists(module_doc_gui_dir):
276         mkdir(doc_gui_dir)
277         for fn in os.listdir(module_doc_gui_dir):
278             symlink(os.path.join(module_doc_gui_dir, fn), os.path.join(doc_gui_dir, fn))
279             pass
280         pass
281     
282     #directory doc/salome/tui : create it and link content
283     if os.path.exists(module_doc_tui_dir):
284         mkdir(doc_tui_dir)
285         for fn in os.listdir(module_doc_tui_dir):
286             symlink(os.path.join(module_doc_tui_dir, fn), os.path.join(doc_tui_dir, fn))
287             pass
288         pass
289
290 # -----------------------------------------------------------------------------
291
292 def main():
293     usage="""usage: %prog [options]
294 Typical use is:
295   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
296 """
297     parser = optparse.OptionParser(usage=usage)
298
299     parser.add_option('-v', '--verbose', action='count', dest='verbose',
300                       default=0, help="Increase verbosity")
301
302     parser.add_option('--prefix', dest="prefix", default='.',
303                       help="The base directory to install to (default .)")
304
305     parser.add_option('--module', dest="module", 
306                       help="The module directory to install in (mandatory)")
307
308     parser.add_option('--clear', dest='clear', action='store_true',
309         help="Clear out the install and start from scratch")
310
311     options, args = parser.parse_args()
312     link_module(options)
313     pass
314     
315 # -----------------------------------------------------------------------------
316
317 if __name__ == '__main__':
318     main()
319     pass