-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindInFiles.py
executable file
·38 lines (29 loc) · 1.3 KB
/
findInFiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env python
__doc__ = 'looks into the directory and sub-dirs performing a regex search in the files.\n\nUsage: findInFiles regex path [options]'
import re
from optparse import OptionParser
import os
parser = OptionParser(description=__doc__)
parser.add_option('--followSymLinks','-f',action="store_true", metavar='',help='follows symbolic links',dest='flinks',default = False)
parser.add_option('--maxDepth','-D',metavar='number', type=int,help='Sets the maximum depth to look at',dest='Mdepth',default = 20)
(options,pars) = parser.parse_args()
path = pars[1]
regex = re.compile(pars[0])
def MapDirStructure( directory, objectList, followLinks, depth=0 ):
dirContent = os.listdir(directory)
for entry in dirContent:
fpath = os.path.join(directory,entry)
if os.path.islink( fpath ):
if followLinks:
fpath = os.path.realpath( fpath )
else:
return
if os.path.isdir( fpath ) and depth <= options.Mdepth:
MapDirStructure(fpath, objectList, followLinks,depth+1)
elif os.path.isfile( fpath ):
objectList.append(fpath)
thisPath = os.path.join( os.getcwd() , path)
thisRealPath = os.path.realpath( thisPath )
files = []
MapDirStructure( thisRealPath, files, options.flinks)
for f in files: