Salome HOME
Porting to Python 2.6 - add coding page specification for Python scripts
[modules/kernel.git] / bin / virtual_salome.py
1 #! /usr/bin/python
2 #  -*- coding: iso-8859-1 -*-
3 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 #  This library is free software; you can redistribute it and/or
9 #  modify it under the terms of the GNU Lesser General Public
10 #  License as published by the Free Software Foundation; either
11 #  version 2.1 of the License.
12 #
13 #  This library is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 #  Lesser General Public License for more details.
17 #
18 #  You should have received a copy of the GNU Lesser General Public
19 #  License along with this library; if not, write to the Free Software
20 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 verbose:
60             print 'Creating symlink %s' % dest
61             pass
62         os.symlink(src, dest)
63     else:
64         if verbose:
65             print 'Symlink %s already exists' % dest
66         pass
67     pass
68
69 # -----------------------------------------------------------------------------
70
71 def rmtree(dir):
72     """Remove (recursive) a directory if it exists"""
73     if os.path.exists(dir):
74         print 'Deleting tree %s' % dir
75         shutil.rmtree(dir)
76     else:
77         if verbose:
78             print 'Do not need to delete %s; already gone' % dir
79             pass
80         pass
81     pass
82
83 # -----------------------------------------------------------------------------
84
85 __lib__dir__ = None
86 def get_lib_dir():
87     global __lib__dir__
88     if __lib__dir__: return __lib__dir__
89     import platform
90     __lib__dir__ = "lib"
91     return __lib__dir__
92
93 # -----------------------------------------------------------------------------
94
95 def link_module(options):
96     global verbose
97
98     if not options.module:
99         print "Option module is mandatory"
100         return 
101    
102     module_dir=os.path.abspath(options.module)
103     if not os.path.exists(module_dir):
104         print "Module %s does not exist" % module_dir
105         return
106
107     verbose = options.verbose
108
109     home_dir = os.path.expanduser(options.prefix)
110     #try to find python version of salome application and put it in versio
111     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(home_dir,get_lib_dir(),"python*.*"))]
112     if not pys :
113       versio=None
114     else:
115       versio=pys[0]
116
117     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(module_dir,get_lib_dir(),"python*.*"))]
118     #check if the module has a python version compatible with application version
119     if not pys :
120       pyversio=versio or py_version
121     elif versio is None:
122       pyversio=pys[0]
123     elif versio in pys:
124       pyversio=versio
125     else:
126       #incompatible python versions
127       print "incompatible python versions : application has version %s and module %s has not" % (versio,module_dir)
128       return
129
130     module_bin_dir=os.path.join(module_dir,'bin','salome')
131     module_idl_dir=os.path.join(module_dir,'idl','salome')
132     module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome')
133     module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),pyversio,'site-packages','salome')
134     module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),pyversio,
135                                           'site-packages','salome','shared_modules')
136     module_share_dir=os.path.join(module_dir,'share','salome','resources')
137     module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui')
138     module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui')
139     module_doc_dir=os.path.join(module_dir,'doc','salome')
140     module_sharedoc_dir=os.path.join(module_dir,'share','doc','salome')
141     module_sharedoc_gui_dir=os.path.join(module_dir,'share','doc','salome','gui')
142     module_sharedoc_tui_dir=os.path.join(module_dir,'share','doc','salome','tui')
143
144     bin_dir=os.path.join(home_dir,'bin','salome')
145     idl_dir=os.path.join(home_dir,'idl','salome')
146     lib_dir=os.path.join(home_dir,'lib','salome')
147     lib_py_dir=os.path.join(home_dir,'lib',pyversio,'site-packages','salome')
148     lib_py_shared_dir=os.path.join(home_dir,'lib',pyversio,
149                                    'site-packages','salome','shared_modules')
150     share_dir=os.path.join(home_dir,'share','salome','resources')
151     doc_gui_dir=os.path.join(home_dir,'doc','salome','gui')
152     doc_tui_dir=os.path.join(home_dir,'doc','salome','tui')
153     doc_dir=os.path.join(home_dir,'doc','salome')
154     sharedoc_dir=os.path.join(home_dir,'share','doc','salome')
155     sharedoc_gui_dir=os.path.join(home_dir,'share','doc','salome','gui')
156     sharedoc_tui_dir=os.path.join(home_dir,'share','doc','salome','tui')
157
158     if options.clear:
159         rmtree(bin_dir)
160         rmtree(idl_dir)
161         rmtree(lib_dir)
162         rmtree(lib_py_dir)
163         rmtree(share_dir)
164         rmtree(doc_dir)
165         rmtree(sharedoc_dir)
166         pass
167     
168     #directory bin/salome : create it and link content
169     if os.path.exists(module_bin_dir):
170         mkdir(bin_dir)
171         for fn in os.listdir(module_bin_dir):
172             symlink(os.path.join(module_bin_dir, fn), os.path.join(bin_dir, fn))
173             pass
174         pass
175     else:
176         if verbose:
177             print module_bin_dir, " doesn't exist"
178         pass    
179     
180     #directory idl/salome : create it and link content
181     if os.path.exists(module_idl_dir):
182         mkdir(idl_dir)
183         for fn in os.listdir(module_idl_dir):
184             symlink(os.path.join(module_idl_dir, fn), os.path.join(idl_dir, fn))
185     else:
186         if verbose:
187             print module_idl_dir, " doesn't exist"
188
189     #directory lib/salome : create it and link content
190     if os.path.exists(module_lib_dir):
191         mkdir(lib_dir)
192         for fn in os.listdir(module_lib_dir):
193             symlink(os.path.join(module_lib_dir, fn), os.path.join(lib_dir, fn))
194             pass
195         pass
196     else:
197         if verbose:
198             print module_lib_dir, " doesn't exist"
199         pass    
200     
201     #directory lib/pyversio/site-packages/salome : create it and link content
202     if not os.path.exists(module_lib_py_dir):
203         print "Python directory %s does not exist" % module_lib_py_dir
204     else:
205         mkdir(lib_py_shared_dir)
206         for fn in os.listdir(module_lib_py_dir):
207             if fn == "shared_modules": continue
208             symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn))
209             pass    
210         if os.path.exists(module_lib_py_shared_dir):
211             for fn in os.listdir(module_lib_py_shared_dir):
212                 symlink(os.path.join(module_lib_py_shared_dir, fn), os.path.join(lib_py_shared_dir, fn))
213                 pass
214             pass
215         else:
216             if verbose:
217                 print module_lib_py_shared_dir, " doesn't exist"
218             pass    
219
220     #directory share/doc/salome (KERNEL doc) : create it and link content
221     if os.path.exists(module_sharedoc_dir):
222         mkdir(sharedoc_dir)
223         for fn in os.listdir(module_sharedoc_dir):
224             if fn == 'gui':continue
225             if fn == 'tui':continue
226             symlink(os.path.join(module_sharedoc_dir, fn), os.path.join(sharedoc_dir, fn))
227             pass
228         pass
229
230     #directory share/doc/salome/gui : create it and link content
231     if os.path.exists(module_sharedoc_gui_dir):
232         mkdir(sharedoc_gui_dir)
233         for fn in os.listdir(module_sharedoc_gui_dir):
234             symlink(os.path.join(module_sharedoc_gui_dir, fn), os.path.join(sharedoc_gui_dir, fn))
235             pass
236         pass
237     
238     #directory share/doc/salome/tui : create it and link content
239     if os.path.exists(module_sharedoc_tui_dir):
240         mkdir(sharedoc_tui_dir)
241         for fn in os.listdir(module_sharedoc_tui_dir):
242             symlink(os.path.join(module_sharedoc_tui_dir, fn), os.path.join(sharedoc_tui_dir, fn))
243             pass
244         pass
245
246     #directory share/salome/resources : create it and link content
247     if os.path.exists(module_share_dir):
248         mkdir(share_dir)
249         for fn in os.listdir(module_share_dir):
250             symlink(os.path.join(module_share_dir, fn), os.path.join(share_dir, fn))
251             pass
252         pass
253     else:
254         print "resources directory %s does not exist" % module_share_dir
255         pass
256
257     #html files in doc/salome directory
258     if os.path.exists(module_doc_dir):
259         mkdir(doc_dir)
260         for fn in os.listdir(module_doc_dir):
261             if fn == 'gui':continue
262             if fn == 'tui':continue
263             symlink(os.path.join(module_doc_dir, fn), os.path.join(doc_dir, fn))
264             pass
265         pass
266     
267     #directory doc/salome/gui : create it and link content
268     if os.path.exists(module_doc_gui_dir):
269         mkdir(doc_gui_dir)
270         for fn in os.listdir(module_doc_gui_dir):
271             symlink(os.path.join(module_doc_gui_dir, fn), os.path.join(doc_gui_dir, fn))
272             pass
273         pass
274     
275     #directory doc/salome/tui : create it and link content
276     if os.path.exists(module_doc_tui_dir):
277         mkdir(doc_tui_dir)
278         for fn in os.listdir(module_doc_tui_dir):
279             symlink(os.path.join(module_doc_tui_dir, fn), os.path.join(doc_tui_dir, fn))
280             pass
281         pass
282
283 # -----------------------------------------------------------------------------
284
285 def main():
286     usage="""usage: %prog [options]
287 Typical use is:
288   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
289 """
290     parser = optparse.OptionParser(usage=usage)
291
292     parser.add_option('-v', '--verbose', action='count', dest='verbose',
293                       default=0, help="Increase verbosity")
294
295     parser.add_option('--prefix', dest="prefix", default='.',
296                       help="The base directory to install to (default .)")
297
298     parser.add_option('--module', dest="module", 
299                       help="The module directory to install in (mandatory)")
300
301     parser.add_option('--clear', dest='clear', action='store_true',
302         help="Clear out the install and start from scratch")
303
304     options, args = parser.parse_args()
305     link_module(options)
306     pass
307     
308 # -----------------------------------------------------------------------------
309
310 if __name__ == '__main__':
311     main()
312     pass