From bc4b40d8e788ca362b597c5e92670ca7ad9005c4 Mon Sep 17 00:00:00 2001 From: Brian Coca <brian.coca+git@gmail.com> Date: Tue, 13 Oct 2015 10:04:50 -0400 Subject: [PATCH] added regex support to find, also added 'singular' aliasess to patterns and paths --- files/find.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/files/find.py b/files/find.py index 659ec16026e..d7042f0027f 100644 --- a/files/find.py +++ b/files/find.py @@ -50,17 +50,18 @@ options: required: false default: '*' description: - - One or more (shell type) file glob patterns, which restrict the list of files to be returned to + - One or more (shell type) 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. + specified using a list. The patterns can be simple shell globs or a python regex prefixed by a '~'. + aliases: ['pattern'] contains: required: false default: null description: - - One or more re patterns which should be matched against the file content + - One or more re patterns which should be matched against the file content paths: required: true - aliases: [ "name" ] + aliases: [ "name", "path" ] description: - List of paths to the file or directory to search. All paths must be fully qualified. file_type: @@ -121,8 +122,9 @@ EXAMPLES = ''' # Recursively find /var/tmp files with last access time greater than 3600 seconds - find: paths="/var/tmp" age="3600" age_stamp=atime recurse=yes -# find /var/log files equal or greater than 10 megabytes ending with .log or .log.gz -- find: paths="/var/tmp" patterns="*.log","*.log.gz" size="10m" +# 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" + ''' RETURN = ''' @@ -157,9 +159,11 @@ def pfilter(f, patterns=None): if patterns is None: return True for p in patterns: - if fnmatch.fnmatch(f, p): - return True - return False + if p.startswith('~'): + r = re.compile(p[1:]) + return r.match(f) + else: + return fnmatch.fnmatch(f, p) def agefilter(st, now, age, timestamp): @@ -236,8 +240,8 @@ def statinfo(st): def main(): module = AnsibleModule( argument_spec = dict( - paths = dict(required=True, aliases=['name'], type='list'), - patterns = dict(default=['*'], type='list'), + paths = dict(required=True, aliases=['name','path'], type='list'), + patterns = dict(default=['*'], type='list', aliases['pattern']), contains = dict(default=None, type='str'), file_type = dict(default="file", choices=['file', 'directory'], type='str'), age = dict(default=None, type='str'),