Salome HOME
Calcium and Palm ok
[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 # -----------------------------------------------------------------------------
34
35 def mkdir(path):
36     """Create a directory and all the intermediate directories if path does not exist"""
37     if not os.path.exists(path):
38         print 'Creating %s' % path
39         os.makedirs(path)
40     else:
41         if verbose:
42             print 'Directory %s already exists' % path
43             pass
44         pass
45
46 # -----------------------------------------------------------------------------
47
48 def symlink(src, dest):
49     """Create a link if it does not exist"""
50     if not os.path.exists(dest):
51         if verbose:
52             print 'Creating symlink %s' % dest
53             pass
54         os.symlink(src, dest)
55     else:
56         print 'Symlink %s already exists' % dest
57         pass
58     pass
59
60 # -----------------------------------------------------------------------------
61
62 def rmtree(dir):
63     """Remove (recursive) a directory if it exists"""
64     if os.path.exists(dir):
65         print 'Deleting tree %s' % dir
66         shutil.rmtree(dir)
67     else:
68         if verbose:
69             print 'Do not need to delete %s; already gone' % dir
70             pass
71         pass
72     pass
73
74 # -----------------------------------------------------------------------------
75
76 __lib__dir__ = None
77 def get_lib_dir():
78     global __lib__dir__
79     if __lib__dir__: return __lib__dir__
80     import platform
81     if platform.architecture()[0] == "64bit":
82         __lib__dir__ = "lib64"
83     else:
84         __lib__dir__ = "lib"
85     return get_lib_dir()
86
87 # -----------------------------------------------------------------------------
88
89 def link_module(options):
90     global verbose
91
92     if not options.module:
93         print "Option module is mandatory"
94         return 
95    
96     module_dir=options.module
97     if not os.path.exists(module_dir):
98         print "Module %s does not exist" % module_dir
99         return
100
101     home_dir = os.path.expanduser(options.prefix)
102
103     module_bin_dir=os.path.join(module_dir,'bin','salome')
104     module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome')
105     module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),py_version,'site-packages','salome')
106     module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),py_version,
107                                           'site-packages','salome','shared_modules')
108     module_share_dir=os.path.join(module_dir,'share','salome','resources')
109     module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui')
110     module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui')
111     module_doc_dir=os.path.join(module_dir,'doc','salome')
112     module_sharedoc_dir=os.path.join(module_dir,'share','doc','salome')
113
114     if not os.path.exists(module_lib_py_dir):
115         print "Python directory %s does not exist" % module_lib_py_dir
116         return
117
118     bin_dir=os.path.join(home_dir,'bin','salome')
119     lib_dir=os.path.join(home_dir,'lib','salome')
120     lib_py_dir=os.path.join(home_dir,'lib',py_version,'site-packages','salome')
121     lib_py_shared_dir=os.path.join(home_dir,'lib',py_version,
122                                    'site-packages','salome','shared_modules')
123     share_dir=os.path.join(home_dir,'share','salome','resources')
124     doc_gui_dir=os.path.join(home_dir,'doc','salome','gui')
125     doc_tui_dir=os.path.join(home_dir,'doc','salome','tui')
126     doc_dir=os.path.join(home_dir,'doc','salome')
127     sharedoc_dir=os.path.join(home_dir,'share','doc','salome')
128
129     verbose = options.verbose
130
131     if options.clear:
132         rmtree(bin_dir)
133         rmtree(lib_dir)
134         rmtree(lib_py_dir)
135         rmtree(share_dir)
136         rmtree(doc_dir)
137         rmtree(sharedoc_dir)
138         pass
139     
140     #directory bin/salome : create it and link content
141     if os.path.exists(module_bin_dir):
142         mkdir(bin_dir)
143         for fn in os.listdir(module_bin_dir):
144             symlink(os.path.join(module_bin_dir, fn), os.path.join(bin_dir, fn))
145             pass
146         pass
147     else:
148         print module_bin_dir, " doesn't exist"
149         pass    
150     
151     #directory lib/salome : create it and link content
152     if os.path.exists(module_lib_dir):
153         mkdir(lib_dir)
154         for fn in os.listdir(module_lib_dir):
155             symlink(os.path.join(module_lib_dir, fn), os.path.join(lib_dir, fn))
156             pass
157         pass
158     else:
159         print module_lib_dir, " doesn't exist"
160         pass    
161     
162     #directory lib/py_version/site-packages/salome : create it and link content
163     mkdir(lib_py_shared_dir)
164     for fn in os.listdir(module_lib_py_dir):
165         if fn == "shared_modules": continue
166         symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn))
167         pass    
168     if os.path.exists(module_lib_py_shared_dir):
169         for fn in os.listdir(module_lib_py_shared_dir):
170             symlink(os.path.join(module_lib_py_shared_dir, fn), os.path.join(lib_py_shared_dir, fn))
171             pass
172         pass
173     else:
174         print module_lib_py_shared_dir, " doesn't exist"
175         pass    
176
177     #directory share/doc/salome (KERNEL doc) : create it and link content
178     if os.path.exists(module_sharedoc_dir):
179         mkdir(sharedoc_dir)
180         for fn in os.listdir(module_sharedoc_dir):
181             symlink(os.path.join(module_sharedoc_dir, fn), os.path.join(sharedoc_dir, fn))
182             pass
183         pass
184     pass
185
186
187     #directory share/salome/resources : create it and link content
188     mkdir(share_dir)
189     for fn in os.listdir(module_share_dir):
190         symlink(os.path.join(module_share_dir, fn), os.path.join(share_dir, fn))
191
192     #html files in doc/salome directory
193     if os.path.exists(module_doc_dir):
194         mkdir(doc_dir)
195         for fn in os.listdir(module_doc_dir):
196             if fn == 'gui':continue
197             if fn == 'tui':continue
198             symlink(os.path.join(module_doc_dir, fn), os.path.join(doc_dir, fn))
199             pass
200         pass
201     
202     #directory doc/salome/gui : create it and link content
203     if os.path.exists(module_doc_gui_dir):
204         mkdir(doc_gui_dir)
205         for fn in os.listdir(module_doc_gui_dir):
206             symlink(os.path.join(module_doc_gui_dir, fn), os.path.join(doc_gui_dir, fn))
207             pass
208         pass
209     
210     #directory doc/salome/tui : create it and link content
211     if os.path.exists(module_doc_tui_dir):
212         mkdir(doc_tui_dir)
213         for fn in os.listdir(module_doc_tui_dir):
214             symlink(os.path.join(module_doc_tui_dir, fn), os.path.join(doc_tui_dir, fn))
215             pass
216         pass
217
218 # -----------------------------------------------------------------------------
219
220 def main():
221     usage="""usage: %prog [options]
222 Typical use is:
223   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
224 """
225     parser = optparse.OptionParser(usage=usage)
226
227     parser.add_option('-v', '--verbose', action='count', dest='verbose',
228                       default=0, help="Increase verbosity")
229
230     parser.add_option('--prefix', dest="prefix", default='.',
231                       help="The base directory to install to (default .)")
232
233     parser.add_option('--module', dest="module", 
234                       help="The module directory to install in (mandatory)")
235
236     parser.add_option('--clear', dest='clear', action='store_true',
237         help="Clear out the install and start from scratch")
238
239     options, args = parser.parse_args()
240     link_module(options)
241     pass
242     
243 # -----------------------------------------------------------------------------
244
245 if __name__ == '__main__':
246     main()
247     pass