1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

changed so regexes and shell globs work transparently

This commit is contained in:
Brian Coca 2015-10-19 17:42:36 -04:00 committed by Matt Clay
parent 956fe4c709
commit 3e5dc1fd74

View file

@ -50,9 +50,9 @@ options:
required: false
default: '*'
description:
- One or more (shell type) patterns, which restrict the list of files to be returned to
- One or more (shell or regex) patterns, which restrict the list of files to be returned to
those whose basenames match at least one of the patterns specified. Multiple patterns can be
specified using a list. The patterns can be simple shell globs or a python regex prefixed by a '~'.
specified using a list.
aliases: ['pattern']
contains:
required: false
@ -123,7 +123,7 @@ EXAMPLES = '''
- find: paths="/var/tmp" age="3600" age_stamp=atime recurse=yes
# find /var/log files equal or greater than 10 megabytes ending with .old or .log.gz via regex
- find: paths="/var/tmp" patterns="~.*\.(?:old|log\.gz)$" size="10m"
- find: paths="/var/tmp" patterns="^.*?\.(?:old|log\.gz)$" size="10m"
'''
@ -156,14 +156,25 @@ examined:
def pfilter(f, patterns=None):
'''filter using glob patterns'''
if patterns is None:
return True
match = False
for p in patterns:
if p.startswith('~'):
r = re.compile(p[1:])
return r.match(f)
else:
return fnmatch.fnmatch(f, p)
try:
r = re.compile(p)
match = r.match(f)
except:
pass
if not match:
match = fnmatch.fnmatch(f, p)
if match:
break
return match
def agefilter(st, now, age, timestamp):