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