Salome HOME
Improve SALOME logger: customize log file name (add port, user and host ids) to avoid...
[modules/kernel.git] / bin / virtual_salome.py
1 # Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
2 #           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either 
6 # version 2.1 of the License.
7
8 # This library is distributed in the hope that it will be useful 
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 # Lesser General Public License for more details.
12
13 # You should have received a copy of the GNU Lesser General Public  
14 # License along with this library; if not, write to the Free Software 
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 """Create a virtual Salome installation 
20
21 Based on a script created by Ian Bicking.
22
23 Typical use::
24
25   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
26
27 install module KERNEL in the current directory
28 """
29
30 import sys, os, optparse, shutil,glob,fnmatch
31 py_version = 'python%s.%s' % (sys.version_info[0], sys.version_info[1])
32
33 verbose=0
34
35 # -----------------------------------------------------------------------------
36
37 def mkdir(path):
38     """Create a directory and all the intermediate directories if path does not exist"""
39     if not os.path.exists(path):
40         print 'Creating %s' % path
41         os.makedirs(path)
42     else:
43         if verbose:
44             print 'Directory %s already exists' % path
45             pass
46         pass
47
48 # -----------------------------------------------------------------------------
49
50 def symlink(src, dest):
51     """Create a link if it does not exist"""
52     if not os.path.exists(dest):
53         if verbose:
54             print 'Creating symlink %s' % dest
55             pass
56         os.symlink(src, dest)
57     else:
58         print 'Symlink %s already exists' % dest
59         pass
60     pass
61
62 # -----------------------------------------------------------------------------
63
64 def rmtree(dir):
65     """Remove (recursive) a directory if it exists"""
66     if os.path.exists(dir):
67         print 'Deleting tree %s' % dir
68         shutil.rmtree(dir)
69     else:
70         if verbose:
71             print 'Do not need to delete %s; already gone' % dir
72             pass
73         pass
74     pass
75
76 # -----------------------------------------------------------------------------
77
78 __lib__dir__ = None
79 def get_lib_dir():
80     global __lib__dir__
81     if __lib__dir__: return __lib__dir__
82     import platform
83     __lib__dir__ = "lib"
84     return __lib__dir__
85
86 # -----------------------------------------------------------------------------
87
88 def link_module(options):
89
90     if not options.module:
91         print "Option module is mandatory"
92         return 
93    
94     module_dir=os.path.abspath(options.module)
95     if not os.path.exists(module_dir):
96         print "Module %s does not exist" % module_dir
97         return
98
99     home_dir = os.path.expanduser(options.prefix)
100
101     module_bin_dir=os.path.join(module_dir,'bin','salome')
102     module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome')
103     module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),py_version,'site-packages','salome')
104     module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),py_version,
105                                           'site-packages','salome','shared_modules')
106     module_share_dir=os.path.join(module_dir,'share','salome','resources')
107     module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui')
108     module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui')
109     module_doc_dir=os.path.join(module_dir,'doc','salome')
110     module_sharedoc_dir=os.path.join(module_dir,'share','doc','salome')
111     module_sharedoc_gui_dir=os.path.join(module_dir,'share','doc','salome','gui')
112     module_sharedoc_tui_dir=os.path.join(module_dir,'share','doc','salome','tui')
113
114     bin_dir=os.path.join(home_dir,'bin','salome')
115     lib_dir=os.path.join(home_dir,'lib','salome')
116     lib_py_dir=os.path.join(home_dir,'lib',py_version,'site-packages','salome')
117     lib_py_shared_dir=os.path.join(home_dir,'lib',py_version,
118                                    'site-packages','salome','shared_modules')
119     share_dir=os.path.join(home_dir,'share','salome','resources')
120     doc_gui_dir=os.path.join(home_dir,'doc','salome','gui')
121     doc_tui_dir=os.path.join(home_dir,'doc','salome','tui')
122     doc_dir=os.path.join(home_dir,'doc','salome')
123     sharedoc_dir=os.path.join(home_dir,'share','doc','salome')
124     sharedoc_gui_dir=os.path.join(home_dir,'share','doc','salome','gui')
125     sharedoc_tui_dir=os.path.join(home_dir,'share','doc','salome','tui')
126
127     verbose = options.verbose
128
129     if options.clear:
130         rmtree(bin_dir)
131         rmtree(lib_dir)
132         rmtree(lib_py_dir)
133         rmtree(share_dir)
134         rmtree(doc_dir)
135         rmtree(sharedoc_dir)
136         pass
137     
138     #directory bin/salome : create it and link content
139     if os.path.exists(module_bin_dir):
140         mkdir(bin_dir)
141         for fn in os.listdir(module_bin_dir):
142             symlink(os.path.join(module_bin_dir, fn), os.path.join(bin_dir, fn))
143             pass
144         pass
145     else:
146         print module_bin_dir, " doesn't exist"
147         pass    
148     
149     #directory lib/salome : create it and link content
150     if os.path.exists(module_lib_dir):
151         mkdir(lib_dir)
152         for fn in os.listdir(module_lib_dir):
153             symlink(os.path.join(module_lib_dir, fn), os.path.join(lib_dir, fn))
154             pass
155         pass
156     else:
157         print module_lib_dir, " doesn't exist"
158         pass    
159     
160     #directory lib/py_version/site-packages/salome : create it and link content
161     if not os.path.exists(module_lib_py_dir):
162         print "Python directory %s does not exist" % module_lib_py_dir
163     else:
164         mkdir(lib_py_shared_dir)
165         for fn in os.listdir(module_lib_py_dir):
166             if fn == "shared_modules": continue
167             symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn))
168             pass    
169         if os.path.exists(module_lib_py_shared_dir):
170             for fn in os.listdir(module_lib_py_shared_dir):
171                 symlink(os.path.join(module_lib_py_shared_dir, fn), os.path.join(lib_py_shared_dir, fn))
172                 pass
173             pass
174         else:
175             print module_lib_py_shared_dir, " doesn't exist"
176             pass    
177
178     #directory share/doc/salome (KERNEL doc) : create it and link content
179     if os.path.exists(module_sharedoc_dir):
180         mkdir(sharedoc_dir)
181         for fn in os.listdir(module_sharedoc_dir):
182             if fn == 'gui':continue
183             if fn == 'tui':continue
184             symlink(os.path.join(module_sharedoc_dir, fn), os.path.join(sharedoc_dir, fn))
185             pass
186         pass
187
188     #directory share/doc/salome/gui : create it and link content
189     if os.path.exists(module_sharedoc_gui_dir):
190         mkdir(sharedoc_gui_dir)
191         for fn in os.listdir(module_sharedoc_gui_dir):
192             symlink(os.path.join(module_sharedoc_gui_dir, fn), os.path.join(sharedoc_gui_dir, fn))
193             pass
194         pass
195     
196     #directory share/doc/salome/tui : create it and link content
197     if os.path.exists(module_sharedoc_tui_dir):
198         mkdir(sharedoc_tui_dir)
199         for fn in os.listdir(module_sharedoc_tui_dir):
200             symlink(os.path.join(module_sharedoc_tui_dir, fn), os.path.join(sharedoc_tui_dir, fn))
201             pass
202         pass
203
204     #directory share/salome/resources : create it and link content
205     if os.path.exists(module_share_dir):
206         mkdir(share_dir)
207         for fn in os.listdir(module_share_dir):
208             symlink(os.path.join(module_share_dir, fn), os.path.join(share_dir, fn))
209             pass
210         pass
211     else:
212         print "resources directory %s does not exist" % module_share_dir
213         pass
214
215     #html files in doc/salome directory
216     if os.path.exists(module_doc_dir):
217         mkdir(doc_dir)
218         for fn in os.listdir(module_doc_dir):
219             if fn == 'gui':continue
220             if fn == 'tui':continue
221             symlink(os.path.join(module_doc_dir, fn), os.path.join(doc_dir, fn))
222             pass
223         pass
224     
225     #directory doc/salome/gui : create it and link content
226     if os.path.exists(module_doc_gui_dir):
227         mkdir(doc_gui_dir)
228         for fn in os.listdir(module_doc_gui_dir):
229             symlink(os.path.join(module_doc_gui_dir, fn), os.path.join(doc_gui_dir, fn))
230             pass
231         pass
232     
233     #directory doc/salome/tui : create it and link content
234     if os.path.exists(module_doc_tui_dir):
235         mkdir(doc_tui_dir)
236         for fn in os.listdir(module_doc_tui_dir):
237             symlink(os.path.join(module_doc_tui_dir, fn), os.path.join(doc_tui_dir, fn))
238             pass
239         pass
240
241 # -----------------------------------------------------------------------------
242
243 def main():
244     usage="""usage: %prog [options]
245 Typical use is:
246   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
247 """
248     parser = optparse.OptionParser(usage=usage)
249
250     parser.add_option('-v', '--verbose', action='count', dest='verbose',
251                       default=0, help="Increase verbosity")
252
253     parser.add_option('--prefix', dest="prefix", default='.',
254                       help="The base directory to install to (default .)")
255
256     parser.add_option('--module', dest="module", 
257                       help="The module directory to install in (mandatory)")
258
259     parser.add_option('--clear', dest='clear', action='store_true',
260         help="Clear out the install and start from scratch")
261
262     options, args = parser.parse_args()
263     link_module(options)
264     pass
265     
266 # -----------------------------------------------------------------------------
267
268 if __name__ == '__main__':
269     main()
270     pass