Back to index

salome-kernel  6.5.0
syshelper.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 __author__="gboulant"
00025 __date__ ="$21 mai 2010 18:00:23$"
00026 
00027 
00028 def findFiles(rootpath, excludes=[]):
00029     """
00030     This looks after files recursively from the specified rootpath,
00031     but without visiting directories whose basename is in the list
00032     @param excludes.
00033     """
00034     if not os.path.exists(rootpath):
00035         raise RuntimeError("the path %s does not exists"%rootpath)
00036 
00037     exclude_options=""
00038     for excludepath in excludes:
00039         exclude_options+="-e %s "%excludepath
00040 
00041     listfiles=[]        
00042     stream=os.popen("find %s -type f | grep -v -e '\.svn' %s 2>/dev/null"%(rootpath,exclude_options))
00043     for line in stream.readlines():
00044         listfiles.append(line.split('\n')[0])
00045         
00046     return listfiles
00047 
00048 import sys
00049 from stat import ST_MODE, S_ISDIR, S_ISREG
00050 def walktree(rootpath, callback, **kwargs):
00051     '''
00052     This recursively descends the directory tree rooted at rootpath,
00053     calling the callback function for each regular file
00054     '''
00055     for f in os.listdir(rootpath):
00056         pathname = os.path.join(rootpath, f)
00057         try:
00058             mode = os.stat(pathname)[ST_MODE]
00059         except OSError, e:
00060             # It probably means that the file is a broken inode
00061             mode = -1
00062         if S_ISDIR(mode):
00063             # It's a directory, recurse into it
00064             walktree(pathname, callback, **kwargs)
00065         elif S_ISREG(mode):
00066             # It's a file, call the callback function
00067             callback(pathname, **kwargs)
00068         else:
00069             # Unknown file type, print a message
00070             print 'Skipping %s' % pathname
00071 
00072 
00073 #
00074 # =============================================================
00075 # Use cases and unit test functions
00076 # =============================================================
00077 #
00078 import os
00079 try:
00080     TESTDOCDIR=os.path.join(os.environ["KERNEL_ROOT_DIR"],"share")
00081 except KeyError:
00082     TESTDOCDIR="/tmp"
00083     
00084 def TEST_findFiles():
00085     print "########## find 1"
00086     rootpath=TESTDOCDIR
00087     listfiles=findFiles(rootpath)
00088     for filename in listfiles:
00089         print filename
00090 
00091     print "########## find 2"
00092     excludes=[os.path.join(TESTDOCDIR,"doc")]
00093     listfiles=findFiles(rootpath,excludes)
00094     for filename in listfiles:
00095         print filename
00096 
00097     return True
00098 
00099 # This is the test callback function
00100 def visitfile_withargs(file, rootid):
00101     print 'visiting file %s (rootid=%s)'%(file,str(rootid))
00102 
00103 def visitfile_withoutargs(file):
00104     print 'visiting file %s'%(file)
00105 
00106 def TEST_walktree():
00107     #walktree(TESTDOCDIR, visitfile_withargs, rootid=2)
00108     walktree(TESTDOCDIR, visitfile_withoutargs)
00109     return True
00110 
00111 if __name__ == "__main__":
00112     import unittester
00113     unittester.run("syshelper", "TEST_findFiles")
00114     unittester.run("syshelper", "TEST_walktree")