Salome HOME
[PYTHON 3] 1st draft
[modules/kernel.git] / src / KERNEL_PY / kernel / syshelper.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 __author__="gboulant"
25 __date__ ="$21 mai 2010 18:00:23$"
26
27
28 def findFiles(rootpath, excludes=None):
29     """
30     This looks after files recursively from the specified rootpath,
31     but without visiting directories whose basename is in the list
32     @param excludes.
33     """
34     if not os.path.exists(rootpath):
35         raise RuntimeError("the path %s does not exists"%rootpath)
36
37     if excludes is None:
38         excludes = []
39     exclude_options=""
40     for excludepath in excludes:
41         exclude_options+="-e %s "%excludepath
42
43     listfiles=[]        
44     stream=os.popen("find %s -type f | grep -v -e '\.svn' %s 2>/dev/null"%(rootpath,exclude_options))
45     for line in stream.readlines():
46         listfiles.append(line.split('\n')[0])
47         
48     return listfiles
49
50 import sys
51 from stat import ST_MODE, S_ISDIR, S_ISREG
52 def walktree(rootpath, callback, **kwargs):
53     '''
54     This recursively descends the directory tree rooted at rootpath,
55     calling the callback function for each regular file
56     '''
57     for f in os.listdir(rootpath):
58         print(f)
59         pathname = os.path.join(rootpath, f)
60         print(pathname)
61         try:
62             mode = os.stat(pathname)[ST_MODE]
63         except OSError as e:
64             # It probably means that the file is a broken inode
65             mode = -1
66         if S_ISDIR(mode):
67             # It's a directory, recurse into it
68             walktree(pathname, callback, **kwargs)
69         elif S_ISREG(mode):
70             # It's a file, call the callback function
71             callback(pathname, **kwargs)
72         else:
73             # Unknown file type, print a message
74             print('Skipping %s' % pathname)
75
76
77 #
78 # =============================================================
79 # Use cases and unit test functions
80 # =============================================================
81 #
82 import os
83 try:
84     TESTDOCDIR=os.path.join(os.environ["KERNEL_ROOT_DIR"],"share")
85 except KeyError:
86     TESTDOCDIR="/tmp"
87     
88 def TEST_findFiles():
89     print("########## find 1")
90     rootpath=TESTDOCDIR
91     listfiles=findFiles(rootpath)
92     for filename in listfiles:
93         print(filename)
94
95     print("########## find 2")
96     excludes=[os.path.join(TESTDOCDIR,"doc")]
97     listfiles=findFiles(rootpath,excludes)
98     for filename in listfiles:
99         print(filename)
100
101     return True
102
103 # This is the test callback function
104 def visitfile_withargs(file, rootid):
105     print('visiting file %s (rootid=%s)'%(file,str(rootid)))
106
107 def visitfile_withoutargs(file):
108     print('visiting file %s'%(file))
109
110 def TEST_walktree():
111     #walktree(TESTDOCDIR, visitfile_withargs, rootid=2)
112     walktree(TESTDOCDIR, visitfile_withoutargs)
113     return True
114
115 if __name__ == "__main__":
116     from . import unittester
117     unittester.run("syshelper", "TEST_findFiles")
118     unittester.run("syshelper", "TEST_walktree")