Back to index

salome-kernel  6.5.0
getAppliPath.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 import os
00026 
00027 def relpath(target, base):
00028     """ Find relative path from base to target
00029         if target== "/local/chris/appli" and base== "/local/chris" the result is appli
00030         if target== /tmp/appli and base /local/chris the result is ../../tmp/appli
00031     """
00032     target=target.split(os.path.sep)
00033     base=base.split(os.path.sep)
00034     for i in xrange(len(base)):
00035       if base[i] != target[i]:
00036         i=i-1
00037         #not in base
00038         break
00039     p=['..']*(len(base)-i-1)+target[i+1:]
00040     if p == []:
00041       return '.'
00042     return os.path.join( *p )
00043 
00044 def set_var(VAR, strpath):
00045     """Set VAR environment variable """
00046     value = "%r" % strpath
00047     shell = os.getenv('SHELL')
00048     if shell and shell.endswith('csh'):
00049         return "setenv %s %s" % (VAR, value)
00050     else:
00051         return "export %s=%s" % (VAR, value)
00052 
00053 if __name__ == "__main__":
00054   applipath=relpath(os.path.realpath(os.path.dirname(__file__)),os.path.realpath(os.getenv('HOME')))
00055   print applipath