summaryrefslogtreecommitdiff
path: root/files
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2016-10-02 21:41:21 -0700
committerToshio Kuratomi <a.badger@gmail.com>2016-10-02 21:51:00 -0700
commite4c5a13a7ad6355f779a88456780bdcd86baee31 (patch)
tree33f7934fb2a3d1211c61fec89587f086dbbee3f9 /files
parent857e1d374bb09a59f09f3edb8500baa9a5c12d6a (diff)
downloadansible-modules-core-e4c5a13a7ad6355f779a88456780bdcd86baee31.tar.gz
Fix assemble module for python3
Diffstat (limited to 'files')
-rw-r--r--files/assemble.py45
1 files changed, 24 insertions, 21 deletions
diff --git a/files/assemble.py b/files/assemble.py
index 6eea02e5..39edbdd3 100644
--- a/files/assemble.py
+++ b/files/assemble.py
@@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
# (c) 2012, Stephen Fromm <sfromm@gmail.com>
+# (c) 2016, Toshio Kuratomi <tkuratomi@ansible.com>
#
# This file is part of Ansible
#
@@ -18,11 +19,6 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
-import os
-import os.path
-import tempfile
-import re
-
DOCUMENTATION = '''
---
module: assemble
@@ -108,42 +104,53 @@ EXAMPLES = '''
- 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
def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False):
''' assemble a file from a directory of fragments '''
tmpfd, temp_path = tempfile.mkstemp()
- tmp = os.fdopen(tmpfd,'w')
+ tmp = os.fdopen(tmpfd, 'wb')
delimit_me = False
add_newline = False
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 = u"%s/%s" % (src_path, f)
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
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.
if add_newline:
- tmp.write('\n')
+ tmp.write(b('\n'))
# delimiters should only appear between fragments
if delimit_me:
if delimiter:
# un-escape anything like newlines
- delimiter = delimiter.decode('unicode-escape')
+ delimiter = codecs.escape_decode(delimiter)[0]
tmp.write(delimiter)
# always make sure there's a newline after the
# delimiter, so lines don't run together
- if delimiter[-1] != '\n':
- tmp.write('\n')
+ if delimiter[-1] != b('\n'):
+ tmp.write(b('\n'))
tmp.write(fragment_content)
delimit_me = True
- if fragment_content.endswith('\n'):
+ if fragment_content.endswith(b('\n')):
add_newline = False
else:
add_newline = True
@@ -151,6 +158,7 @@ def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, igno
tmp.close()
return temp_path
+
def cleanup(path, result=None):
# cleanup just in case
if os.path.exists(path):
@@ -162,8 +170,6 @@ def cleanup(path, result=None):
if result is not None:
result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, str(e))]
-# ==============================================================
-# main
def main():
@@ -201,7 +207,7 @@ def main():
if not os.path.isdir(src):
module.fail_json(msg="Source (%s) is not a directory" % src)
- if regexp != None:
+ if regexp is not None:
try:
compiled_regexp = re.compile(regexp)
except re.error:
@@ -248,8 +254,5 @@ def main():
result['msg'] = "OK"
module.exit_json(**result)
-# import module snippets
-from ansible.module_utils.basic import *
-
-main()
-
+if __name__ == '__main__':
+ main()