Re: Wildcards, ant/zsh style

Tim Hemel <[email protected]> Fri, 10 Sep 2004 11:06:43 +0200
Newsgroups gmane.comp.tools.aap.user
Message-ID <[email protected]>
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...


-------------------------------------------------------
This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
Project Admins to receive an Apple iPod Mini FREE for your judgement on
who ports your project to Linux PPC the best. Sponsored by IBM. 
Deadline: Sept. 13. Go here: http://sf.net/ppc_contest.php