Commit d96f4695 by James Tanner

Merge pull request #4525 from ko-zu/ansible

Add a regexp parameter to assemble module to match or exclude filenames.
parent e4494be8
...@@ -22,6 +22,7 @@ import os ...@@ -22,6 +22,7 @@ import os
import os.path import os.path
import shutil import shutil
import tempfile import tempfile
import re
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
...@@ -70,6 +71,14 @@ options: ...@@ -70,6 +71,14 @@ options:
required: false required: false
default: "True" default: "True"
version_added: "1.4" version_added: "1.4"
regexp:
description:
- Assemble files only if C(regex) matches the filename. If not set,
all files are assembled. All "\" (backslash) must be escaped as
"\\\\" to comply yaml syntax. Uses Python regular expressions; see
U(http://docs.python.org/2/library/re.html).
required: false
default: null
others: others:
description: description:
- all arguments accepted by the M(file) module also work here - all arguments accepted by the M(file) module also work here
...@@ -88,12 +97,14 @@ EXAMPLES = ''' ...@@ -88,12 +97,14 @@ EXAMPLES = '''
# =========================================== # ===========================================
# Support method # Support method
def assemble_from_fragments(src_path, delimiter=None): def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None):
''' assemble a file from a directory of fragments ''' ''' assemble a file from a directory of fragments '''
tmpfd, temp_path = tempfile.mkstemp() tmpfd, temp_path = tempfile.mkstemp()
tmp = os.fdopen(tmpfd,'w') tmp = os.fdopen(tmpfd,'w')
delimit_me = False delimit_me = False
for f in sorted(os.listdir(src_path)): for f in sorted(os.listdir(src_path)):
if compiled_regexp and not compiled_regexp.search(f):
continue
fragment = "%s/%s" % (src_path, f) fragment = "%s/%s" % (src_path, f)
if delimit_me and delimiter: if delimit_me and delimiter:
tmp.write(delimiter) tmp.write(delimiter)
...@@ -116,6 +127,7 @@ def main(): ...@@ -116,6 +127,7 @@ def main():
dest = dict(required=True), dest = dict(required=True),
backup=dict(default=False, type='bool'), backup=dict(default=False, type='bool'),
remote_src=dict(default=False, type='bool'), remote_src=dict(default=False, type='bool'),
regexp = dict(required=False),
), ),
add_file_common_args=True add_file_common_args=True
) )
...@@ -127,6 +139,8 @@ def main(): ...@@ -127,6 +139,8 @@ def main():
dest = os.path.expanduser(module.params['dest']) dest = os.path.expanduser(module.params['dest'])
backup = module.params['backup'] backup = module.params['backup']
delimiter = module.params['delimiter'] delimiter = module.params['delimiter']
regexp = module.params['regexp']
compiled_regexp = None
if not os.path.exists(src): if not os.path.exists(src):
module.fail_json(msg="Source (%s) does not exist" % src) module.fail_json(msg="Source (%s) does not exist" % src)
...@@ -134,7 +148,13 @@ def main(): ...@@ -134,7 +148,13 @@ def main():
if not os.path.isdir(src): if not os.path.isdir(src):
module.fail_json(msg="Source (%s) is not a directory" % src) module.fail_json(msg="Source (%s) is not a directory" % src)
path = assemble_from_fragments(src, delimiter) if regexp != None:
try:
compiled_regexp = re.compile(regexp)
except re.error, e:
module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (e, regexp))
path = assemble_from_fragments(src, delimiter, compiled_regexp)
pathmd5 = module.md5(path) pathmd5 = module.md5(path)
if os.path.exists(dest): if os.path.exists(dest):
...@@ -157,3 +177,4 @@ def main(): ...@@ -157,3 +177,4 @@ def main():
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main() main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment