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