mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
assemble module: fix insertion of newlines when not needed.
This builds on GH-6359 and changes the logic so that a newline is only inserted between fragments if the previous fragment does not end with a newline.
This commit is contained in:
parent
3ac731087c
commit
dc93b31d22
1 changed files with 13 additions and 5 deletions
|
@ -102,17 +102,22 @@ def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None):
|
||||||
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
|
||||||
|
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 = "%s/%s" % (src_path, f)
|
||||||
|
if not os.path.isfile(fragment):
|
||||||
|
continue
|
||||||
|
fragment_content = file(fragment).read()
|
||||||
|
|
||||||
|
# always put a newline between fragments if the previous fragment didn't end with a newline.
|
||||||
|
if add_newline:
|
||||||
|
tmp.write('\n')
|
||||||
|
|
||||||
# delimiters should only appear between fragments
|
# delimiters should only appear between fragments
|
||||||
if delimit_me:
|
if delimit_me:
|
||||||
# always put a newline between fragments
|
|
||||||
tmp.write('\n')
|
|
||||||
|
|
||||||
if delimiter:
|
if delimiter:
|
||||||
# un-escape anything like newlines
|
# un-escape anything like newlines
|
||||||
delimiter = delimiter.decode('unicode-escape')
|
delimiter = delimiter.decode('unicode-escape')
|
||||||
|
@ -122,9 +127,12 @@ def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None):
|
||||||
if delimiter[-1] != '\n':
|
if delimiter[-1] != '\n':
|
||||||
tmp.write('\n')
|
tmp.write('\n')
|
||||||
|
|
||||||
if os.path.isfile(fragment):
|
tmp.write(fragment_content)
|
||||||
tmp.write(file(fragment).read())
|
|
||||||
delimit_me = True
|
delimit_me = True
|
||||||
|
if fragment_content.endswith('\n'):
|
||||||
|
add_newline = False
|
||||||
|
else:
|
||||||
|
add_newline = True
|
||||||
|
|
||||||
tmp.close()
|
tmp.close()
|
||||||
return temp_path
|
return temp_path
|
||||||
|
|
Loading…
Reference in a new issue