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