Fix assemble module for python3
This commit is contained in:
parent
9c664f424e
commit
007b5f4c32
1 changed files with 24 additions and 21 deletions
|
@ -2,6 +2,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# (c) 2012, Stephen Fromm <sfromm@gmail.com>
|
# (c) 2012, Stephen Fromm <sfromm@gmail.com>
|
||||||
|
# (c) 2016, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||||
#
|
#
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
#
|
#
|
||||||
|
@ -18,11 +19,6 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import os
|
|
||||||
import os.path
|
|
||||||
import tempfile
|
|
||||||
import re
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: assemble
|
module: assemble
|
||||||
|
@ -106,42 +102,53 @@ EXAMPLES = '''
|
||||||
- assemble: src=/etc/ssh/conf.d/ dest=/etc/ssh/sshd_config validate='/usr/sbin/sshd -t -f %s'
|
- assemble: src=/etc/ssh/conf.d/ dest=/etc/ssh/sshd_config validate='/usr/sbin/sshd -t -f %s'
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import codecs
|
||||||
|
import os
|
||||||
|
import os.path
|
||||||
|
import re
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.pycompat24 import get_exception
|
||||||
|
from ansible.module_utils.six import b
|
||||||
|
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# Support method
|
# Support method
|
||||||
|
|
||||||
def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False):
|
def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False):
|
||||||
''' 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, 'wb')
|
||||||
delimit_me = False
|
delimit_me = False
|
||||||
add_newline = False
|
add_newline = 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):
|
if compiled_regexp and not compiled_regexp.search(f):
|
||||||
continue
|
continue
|
||||||
fragment = "%s/%s" % (src_path, f)
|
fragment = u"%s/%s" % (src_path, f)
|
||||||
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
|
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
|
||||||
continue
|
continue
|
||||||
fragment_content = file(fragment).read()
|
fragment_content = open(fragment, 'rb').read()
|
||||||
|
|
||||||
# always put a newline between fragments if the previous fragment didn't end with a newline.
|
# always put a newline between fragments if the previous fragment didn't end with a newline.
|
||||||
if add_newline:
|
if add_newline:
|
||||||
tmp.write('\n')
|
tmp.write(b('\n'))
|
||||||
|
|
||||||
# delimiters should only appear between fragments
|
# delimiters should only appear between fragments
|
||||||
if delimit_me:
|
if delimit_me:
|
||||||
if delimiter:
|
if delimiter:
|
||||||
# un-escape anything like newlines
|
# un-escape anything like newlines
|
||||||
delimiter = delimiter.decode('unicode-escape')
|
delimiter = codecs.escape_decode(delimiter)[0]
|
||||||
tmp.write(delimiter)
|
tmp.write(delimiter)
|
||||||
# always make sure there's a newline after the
|
# always make sure there's a newline after the
|
||||||
# delimiter, so lines don't run together
|
# delimiter, so lines don't run together
|
||||||
if delimiter[-1] != '\n':
|
if delimiter[-1] != b('\n'):
|
||||||
tmp.write('\n')
|
tmp.write(b('\n'))
|
||||||
|
|
||||||
tmp.write(fragment_content)
|
tmp.write(fragment_content)
|
||||||
delimit_me = True
|
delimit_me = True
|
||||||
if fragment_content.endswith('\n'):
|
if fragment_content.endswith(b('\n')):
|
||||||
add_newline = False
|
add_newline = False
|
||||||
else:
|
else:
|
||||||
add_newline = True
|
add_newline = True
|
||||||
|
@ -149,6 +156,7 @@ def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, igno
|
||||||
tmp.close()
|
tmp.close()
|
||||||
return temp_path
|
return temp_path
|
||||||
|
|
||||||
|
|
||||||
def cleanup(path, result=None):
|
def cleanup(path, result=None):
|
||||||
# cleanup just in case
|
# cleanup just in case
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
|
@ -160,8 +168,6 @@ def cleanup(path, result=None):
|
||||||
if result is not None:
|
if result is not None:
|
||||||
result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, str(e))]
|
result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, str(e))]
|
||||||
|
|
||||||
# ==============================================================
|
|
||||||
# main
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
|
@ -199,7 +205,7 @@ 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)
|
||||||
|
|
||||||
if regexp != None:
|
if regexp is not None:
|
||||||
try:
|
try:
|
||||||
compiled_regexp = re.compile(regexp)
|
compiled_regexp = re.compile(regexp)
|
||||||
except re.error:
|
except re.error:
|
||||||
|
@ -246,8 +252,5 @@ def main():
|
||||||
result['msg'] = "OK"
|
result['msg'] = "OK"
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
# import module snippets
|
if __name__ == '__main__':
|
||||||
from ansible.module_utils.basic import *
|
main()
|
||||||
|
|
||||||
main()
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue