Back to index

salome-kernel  6.5.0
virtual_salome.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 """Create a virtual Salome installation
00025 
00026 Based on a script created by Ian Bicking.
00027 
00028 Typical use::
00029 
00030   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
00031 
00032 install module KERNEL in the current directory
00033 """
00034 
00035 import sys, os, optparse, shutil,glob,fnmatch
00036 py_version = 'python%s.%s' % (sys.version_info[0], sys.version_info[1])
00037 
00038 verbose=0
00039 
00040 # -----------------------------------------------------------------------------
00041 
00042 def mkdir(path):
00043     """Create a directory and all the intermediate directories if path does not exist"""
00044     if not os.path.exists(path):
00045         if verbose:
00046             print 'Creating %s' % path
00047         os.makedirs(path)
00048     else:
00049         if verbose:
00050             print 'Directory %s already exists' % path
00051             pass
00052         pass
00053 
00054 # -----------------------------------------------------------------------------
00055 
00056 def symlink(src, dest):
00057     """Create a link if it does not exist"""
00058     if not os.path.exists(dest):
00059         if os.path.lexists(dest):
00060             print "Do not create symlink %s. It already exists but it's broken" % dest
00061             return
00062         if verbose:
00063             print 'Creating symlink %s' % dest
00064             pass
00065         os.symlink(src, dest)
00066     else:
00067         if verbose:
00068             print 'Symlink %s already exists' % dest
00069         pass
00070     pass
00071 
00072 # -----------------------------------------------------------------------------
00073 
00074 def rmtree(dir):
00075     """Remove (recursive) a directory if it exists"""
00076     if os.path.exists(dir):
00077         print 'Deleting tree %s' % dir
00078         shutil.rmtree(dir)
00079     else:
00080         if verbose:
00081             print 'Do not need to delete %s; already gone' % dir
00082             pass
00083         pass
00084     pass
00085 
00086 # -----------------------------------------------------------------------------
00087 
00088 __lib__dir__ = None
00089 def get_lib_dir():
00090     global __lib__dir__
00091     if __lib__dir__: return __lib__dir__
00092     import platform
00093     __lib__dir__ = "lib"
00094     return __lib__dir__
00095 
00096 # -----------------------------------------------------------------------------
00097 
00098 def link_module(options):
00099     global verbose
00100 
00101     if not options.module:
00102         print "Option module is mandatory"
00103         return
00104 
00105     module_dir=os.path.abspath(options.module)
00106     if not os.path.exists(module_dir):
00107         print "Module %s does not exist" % module_dir
00108         return
00109 
00110     verbose = options.verbose
00111 
00112     home_dir = os.path.expanduser(options.prefix)
00113     #try to find python version of salome application and put it in versio
00114     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(home_dir,get_lib_dir(),"python*.*"))]
00115     if not pys :
00116       versio=None
00117     else:
00118       versio=pys[0]
00119 
00120     pys=[os.path.split(s)[1] for s in glob.glob(os.path.join(module_dir,get_lib_dir(),"python*.*"))]
00121     #check if the module has a python version compatible with application version
00122     if not pys :
00123       pyversio=versio or py_version
00124     elif versio is None:
00125       pyversio=pys[0]
00126     elif versio in pys:
00127       pyversio=versio
00128     else:
00129       #incompatible python versions
00130       print "incompatible python versions : application has version %s and module %s has not" % (versio,module_dir)
00131       return
00132 
00133     module_bin_dir=os.path.join(module_dir,'bin','salome')
00134     module_idl_dir=os.path.join(module_dir,'idl','salome')
00135     module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome')
00136     module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),pyversio,'site-packages','salome')
00137     module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),pyversio,
00138                                           'site-packages','salome','shared_modules')
00139     module_share_dir=os.path.join(module_dir,'share','salome')
00140     module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui')
00141     module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui')
00142     module_doc_dir=os.path.join(module_dir,'doc','salome')
00143     module_sharedoc_dir=os.path.join(module_dir,'share','doc','salome')
00144     module_sharedoc_gui_dir=os.path.join(module_dir,'share','doc','salome','gui')
00145     module_sharedoc_tui_dir=os.path.join(module_dir,'share','doc','salome','tui')
00146 
00147     bin_dir=os.path.join(home_dir,'bin','salome')
00148     idl_dir=os.path.join(home_dir,'idl','salome')
00149     lib_dir=os.path.join(home_dir,'lib','salome')
00150     lib_py_dir=os.path.join(home_dir,'lib',pyversio,'site-packages','salome')
00151     lib_py_shared_dir=os.path.join(home_dir,'lib',pyversio,
00152                                    'site-packages','salome','shared_modules')
00153     share_dir=os.path.join(home_dir,'share','salome')
00154     doc_gui_dir=os.path.join(home_dir,'doc','salome','gui')
00155     doc_tui_dir=os.path.join(home_dir,'doc','salome','tui')
00156     doc_dir=os.path.join(home_dir,'doc','salome')
00157     sharedoc_dir=os.path.join(home_dir,'share','doc','salome')
00158     sharedoc_gui_dir=os.path.join(home_dir,'share','doc','salome','gui')
00159     sharedoc_tui_dir=os.path.join(home_dir,'share','doc','salome','tui')
00160 
00161     if options.clear:
00162         rmtree(bin_dir)
00163         rmtree(idl_dir)
00164         rmtree(lib_dir)
00165         rmtree(lib_py_dir)
00166         rmtree(share_dir)
00167         rmtree(doc_dir)
00168         rmtree(sharedoc_dir)
00169         pass
00170 
00171     #directory bin/salome : create it and link content
00172     if os.path.exists(module_bin_dir):
00173         mkdir(bin_dir)
00174         for fn in os.listdir(module_bin_dir):
00175             symlink(os.path.join(module_bin_dir, fn), os.path.join(bin_dir, fn))
00176             pass
00177         pass
00178     else:
00179         if verbose:
00180             print module_bin_dir, " doesn't exist"
00181         pass
00182 
00183     #directory idl/salome : create it and link content
00184     if os.path.exists(module_idl_dir):
00185         mkdir(idl_dir)
00186         for fn in os.listdir(module_idl_dir):
00187             symlink(os.path.join(module_idl_dir, fn), os.path.join(idl_dir, fn))
00188     else:
00189         if verbose:
00190             print module_idl_dir, " doesn't exist"
00191 
00192     #directory lib/salome : create it and link content
00193     if os.path.exists(module_lib_dir):
00194         mkdir(lib_dir)
00195         for fn in os.listdir(module_lib_dir):
00196             symlink(os.path.join(module_lib_dir, fn), os.path.join(lib_dir, fn))
00197             pass
00198         pass
00199     else:
00200         if verbose:
00201             print module_lib_dir, " doesn't exist"
00202         pass
00203 
00204     #directory lib/pyversio/site-packages/salome : create it and link content
00205     if not os.path.exists(module_lib_py_dir):
00206         print "Python directory %s does not exist" % module_lib_py_dir
00207     else:
00208         # Specific action for the package salome
00209         module_lib_pypkg_dir=os.path.join(module_lib_py_dir,"salome")
00210         lib_pypkg_dir=os.path.join(lib_py_dir,"salome")
00211         mkdir(lib_pypkg_dir)
00212         mkdir(lib_py_shared_dir)
00213         for fn in os.listdir(module_lib_py_dir):
00214             if fn == "shared_modules": continue
00215             if fn == "salome": continue
00216             symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn))
00217             pass
00218         if os.path.exists(module_lib_py_shared_dir):
00219             for fn in os.listdir(module_lib_py_shared_dir):
00220                 symlink(os.path.join(module_lib_py_shared_dir, fn), os.path.join(lib_py_shared_dir, fn))
00221                 pass
00222             pass
00223         if os.path.exists(module_lib_pypkg_dir):
00224             for fn in os.listdir(module_lib_pypkg_dir):
00225                 symlink(os.path.join(module_lib_pypkg_dir, fn), os.path.join(lib_pypkg_dir, fn))
00226                 pass
00227             pass
00228         else:
00229             if verbose:
00230                 print module_lib_py_shared_dir, " doesn't exist"
00231             pass
00232 
00233     #directory share/doc/salome (KERNEL doc) : create it and link content
00234     if os.path.exists(module_sharedoc_dir):
00235         mkdir(sharedoc_dir)
00236         for fn in os.listdir(module_sharedoc_dir):
00237             if fn == 'gui':continue
00238             if fn == 'tui':continue
00239             symlink(os.path.join(module_sharedoc_dir, fn), os.path.join(sharedoc_dir, fn))
00240             pass
00241         pass
00242 
00243     #directory share/doc/salome/gui : create it and link content
00244     if os.path.exists(module_sharedoc_gui_dir):
00245         mkdir(sharedoc_gui_dir)
00246         for fn in os.listdir(module_sharedoc_gui_dir):
00247             symlink(os.path.join(module_sharedoc_gui_dir, fn), os.path.join(sharedoc_gui_dir, fn))
00248             pass
00249         pass
00250 
00251     #directory share/doc/salome/tui : create it and link content
00252     if os.path.exists(module_sharedoc_tui_dir):
00253         mkdir(sharedoc_tui_dir)
00254         for fn in os.listdir(module_sharedoc_tui_dir):
00255             symlink(os.path.join(module_sharedoc_tui_dir, fn), os.path.join(sharedoc_tui_dir, fn))
00256             pass
00257         pass
00258 
00259     #directory share/salome : create it and link content
00260     if os.path.exists(module_share_dir):
00261         mkdir(share_dir)
00262         for fn in os.listdir(module_share_dir):
00263             if fn in ["resources","plugins"] :
00264                 # Create the directory and then link the content
00265                 mkdir(os.path.join(share_dir,fn))
00266                 for ffn in os.listdir(os.path.join(module_share_dir,fn)):
00267                     symlink(os.path.join(module_share_dir, fn, ffn), os.path.join(share_dir,fn, ffn))
00268             else:
00269                 #other directories (not resources)
00270                 symlink(os.path.join(module_share_dir, fn), os.path.join(share_dir, fn))
00271     else:
00272         print "share/salome directory %s does not exist" % module_share_dir
00273         pass
00274 
00275     #html files in doc/salome directory
00276     if os.path.exists(module_doc_dir):
00277         mkdir(doc_dir)
00278         for fn in os.listdir(module_doc_dir):
00279             if fn == 'gui':continue
00280             if fn == 'tui':continue
00281             symlink(os.path.join(module_doc_dir, fn), os.path.join(doc_dir, fn))
00282             pass
00283         pass
00284 
00285     #directory doc/salome/gui : create it and link content
00286     if os.path.exists(module_doc_gui_dir):
00287         mkdir(doc_gui_dir)
00288         for fn in os.listdir(module_doc_gui_dir):
00289             symlink(os.path.join(module_doc_gui_dir, fn), os.path.join(doc_gui_dir, fn))
00290             pass
00291         pass
00292 
00293     #directory doc/salome/tui : create it and link content
00294     if os.path.exists(module_doc_tui_dir):
00295         mkdir(doc_tui_dir)
00296         for fn in os.listdir(module_doc_tui_dir):
00297             symlink(os.path.join(module_doc_tui_dir, fn), os.path.join(doc_tui_dir, fn))
00298             pass
00299         pass
00300 
00301 # -----------------------------------------------------------------------------
00302 
00303 def main():
00304     usage="""usage: %prog [options]
00305 Typical use is:
00306   python virtual_salome.py -v --prefix="." --module=/local/chris/SALOME2/RELEASES/Install/KERNEL_V3_1_0b1
00307 """
00308     parser = optparse.OptionParser(usage=usage)
00309 
00310     parser.add_option('-v', '--verbose', action='count', dest='verbose',
00311                       default=0, help="Increase verbosity")
00312 
00313     parser.add_option('--prefix', dest="prefix", default='.',
00314                       help="The base directory to install to (default .)")
00315 
00316     parser.add_option('--module', dest="module",
00317                       help="The module directory to install in (mandatory)")
00318 
00319     parser.add_option('--clear', dest='clear', action='store_true',
00320         help="Clear out the install and start from scratch")
00321 
00322     options, args = parser.parse_args()
00323     link_module(options)
00324     pass
00325 
00326 # -----------------------------------------------------------------------------
00327 
00328 if __name__ == '__main__':
00329     main()
00330     pass