##// END OF EJS Templates
add dir skipping (on recursive walk) to mglob
vivainio -
Show More
@@ -1,6 +1,6 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 """ mglob - enhanced file list expansion module
3 r""" mglob - enhanced file list expansion module
4
4
5 Use as stand-alone utility (for xargs, `backticks` etc.),
5 Use as stand-alone utility (for xargs, `backticks` etc.),
6 or a globbing library for own python programs. Globbing the sys.argv is something
6 or a globbing library for own python programs. Globbing the sys.argv is something
@@ -51,6 +51,9 b' Supported syntax in globs (wilcard matching patterns)::'
51 - File foo, or all files in dir foo
51 - File foo, or all files in dir foo
52 !*.bak readme*
52 !*.bak readme*
53 - readme*, exclude files ending with .bak
53 - readme*, exclude files ending with .bak
54 !.svn/ !.hg/ !*_Data/ rec:.
55 - Skip .svn, .hg, foo_Data dirs (and their subdirs) in recurse.
56 Trailing / is the key, \ does not work!
54 dir:foo
57 dir:foo
55 - the directory foo (not files in foo)
58 - the directory foo (not files in foo)
56 dir:*
59 dir:*
@@ -69,16 +72,6 b' __version__ = "0.2"'
69 import os,glob,fnmatch,sys
72 import os,glob,fnmatch,sys
70 from sets import Set as set
73 from sets import Set as set
71
74
72 def recfind(p, pats = ["*"]):
73 for (dp,dnames,fnames) in os.walk(p):
74 for f in fnames:
75 matched = False
76 for p in pats:
77 if fnmatch.fnmatch(f,p):
78 matched = True
79 break
80 if matched:
81 yield os.path.join(dp,f)
82
75
83 def expand(flist):
76 def expand(flist):
84 """ Expand the glob(s) in flist.
77 """ Expand the glob(s) in flist.
@@ -91,6 +84,32 b' def expand(flist):'
91 flist = flist.split()
84 flist = flist.split()
92 done_set = set()
85 done_set = set()
93 denied_set = set()
86 denied_set = set()
87
88 def recfind(p, pats = ["*"]):
89 denied_dirs = ["*" + d+"*" for d in denied_set if d.endswith("/")]
90 #print "de", denied_dirs
91 for (dp,dnames,fnames) in os.walk(p):
92 # see if we should ignore the whole directory
93 dp_norm = dp.replace("\\","/") + "/"
94 deny = False
95 #print "dp",dp
96 for deny_pat in denied_dirs:
97 if fnmatch.fnmatch( dp_norm, deny_pat):
98 deny = True
99 break
100 if deny:
101 continue
102
103
104 for f in fnames:
105 matched = False
106 for p in pats:
107 if fnmatch.fnmatch(f,p):
108 matched = True
109 break
110 if matched:
111 yield os.path.join(dp,f)
112
94 def once_filter(seq):
113 def once_filter(seq):
95 for it in seq:
114 for it in seq:
96 p = os.path.abspath(it)
115 p = os.path.abspath(it)
@@ -164,8 +183,11 b' def main():'
164
183
165 print "\n".join(expand(sys.argv[1:])),
184 print "\n".join(expand(sys.argv[1:])),
166
185
167 def mglob_f(self, arg):
186 def mglob_f(self, arg):
168 return expand(arg)
187 if arg.strip():
188 return expand(arg)
189 print "Please specify pattern!"
190 print globsyntax
169
191
170 def ipython_install():
192 def ipython_install():
171 """ register %mglob for IPython """
193 """ register %mglob for IPython """
General Comments 0
You need to be logged in to leave comments. Login now