Re: [A-a-p-user] Wildcards, ant/zsh style
Brought to you by:
vimboss
From: Tim H. <ti...@n2...> - 2004-09-10 21:46:25
|
On Wed, Sep 01, 2004 at 04:18:28PM +0200, Bram Moolenaar wrote: > > Stefan Arentz wrote: > > As you mentioned it's already possible with the ":tree" command. I > don't plan to change the wildcard expansion, at least not soon. > > Is there a Python function for this kind of wildcard expansion? This is what I wrote to overcome the problem: :python def rlist(dir, pattern = r'.*', exclude = r'^$'): """recursively list all files in dir, matching the regexp pattern. Dirs that match the regexp exclude will not be traversed. The resulting filenames are relative to dir, i.e. dir will have to be prefixed.""" l = [] for root, dirs, files in os.walk(dir): # remove the first dir r = os.path.split(root) if r[0] == '': root = '' else: root = r[1] l.extend([os.path.join(root,f) for f in files if re.search(pattern,os.path.join(root,f)) ]) rs = [ d for d in dirs if re.search(exclude,os.path.join(root,d)) ] # we cannot assign to dirs, since it aliases to what # os.walk() uses, so we have to use dirs.remove() for r in rs: try: dirs.remove(r) except ValueError: pass return l You can use it like this: WebFiles = `rlist('website', '.*', '^(.*/)?CVS$')` This will get all files that match anything (.*) but it will exclude CVS directories. Syntax for the patterns is regexp. Then I use the following horrible install command to copy my web files: @for f in var2list(_no.WebFiles): :do installdata $f { var_DATADIR = web } { var_PREFIX = .} { var_PKGNAME= } { keepdir } The name rlist is not the most appropriate, but feel free to change it. I would like to see such a function in the AAP python library. Also a function to expand wildcards, but exclude certain patterns (such as: *, but exclude anything that ends with a ~ (certain backup files)). Perhaps the two can even be combined. Tim -- Zen Microsystems: we're the om in .commmmmmmmm... |