mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix file.append state
This commit is contained in:
parent
91e095bb41
commit
0b7821c8db
2 changed files with 26 additions and 42 deletions
|
@ -2456,27 +2456,10 @@ def append(path, *args, **kwargs):
|
|||
else:
|
||||
args = [kwargs['args']]
|
||||
|
||||
# Make sure we have a newline at the end of the file. Do this in binary
|
||||
# mode so SEEK_END with nonzero offset will work.
|
||||
with salt.utils.fopen(path, 'rb+') as ofile:
|
||||
linesep = salt.utils.to_bytes(os.linesep)
|
||||
try:
|
||||
ofile.seek(-len(linesep), os.SEEK_END)
|
||||
except IOError as exc:
|
||||
if exc.errno in (errno.EINVAL, errno.ESPIPE):
|
||||
# Empty file, simply append lines at the beginning of the file
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
if ofile.read(len(linesep)) != linesep:
|
||||
ofile.seek(0, os.SEEK_END)
|
||||
ofile.write(linesep)
|
||||
# Append lines in text mode
|
||||
with salt.utils.fopen(path, 'r+') as ofile:
|
||||
ofile.seek(0, os.SEEK_END)
|
||||
with salt.utils.fopen(path, 'a') as ofile:
|
||||
for line in args:
|
||||
ofile.write('{0}\n'.format(line))
|
||||
ofile.write('{0}{1}'.format(line, os.linesep))
|
||||
|
||||
return 'Wrote {0} lines to "{1}"'.format(len(args), path)
|
||||
|
||||
|
|
|
@ -3459,12 +3459,16 @@ def append(name,
|
|||
|
||||
.. versionadded:: 0.9.5
|
||||
'''
|
||||
name = os.path.expanduser(name)
|
||||
|
||||
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}
|
||||
if not name:
|
||||
return _error(ret, 'Must provide name to file.append')
|
||||
|
||||
name = os.path.expanduser(name)
|
||||
|
||||
ret = {'name': name,
|
||||
'changes': {},
|
||||
'result': False,
|
||||
'comment': ''}
|
||||
|
||||
if sources is None:
|
||||
sources = []
|
||||
|
||||
|
@ -3516,16 +3520,14 @@ def append(name,
|
|||
with salt.utils.fopen(name, 'rb') as fp_:
|
||||
slines = fp_.readlines()
|
||||
|
||||
count = 0
|
||||
test_lines = []
|
||||
|
||||
append_lines = []
|
||||
try:
|
||||
for chunk in text:
|
||||
if ignore_whitespace:
|
||||
if __salt__['file.search'](
|
||||
name,
|
||||
salt.utils.build_whitespace_split_regex(chunk),
|
||||
multiline=True):
|
||||
name,
|
||||
salt.utils.build_whitespace_split_regex(chunk),
|
||||
multiline=True):
|
||||
continue
|
||||
elif __salt__['file.search'](
|
||||
name,
|
||||
|
@ -3533,22 +3535,17 @@ def append(name,
|
|||
multiline=True):
|
||||
continue
|
||||
|
||||
lines = chunk.splitlines()
|
||||
for line_item in chunk.splitlines():
|
||||
append_lines.append('{0}\n'.format(line_item))
|
||||
|
||||
for line in lines:
|
||||
if __opts__['test']:
|
||||
ret['comment'] = 'File {0} is set to be updated'.format(name)
|
||||
ret['result'] = None
|
||||
test_lines.append('{0}\n'.format(line))
|
||||
else:
|
||||
__salt__['file.append'](name, line)
|
||||
count += 1
|
||||
except TypeError:
|
||||
return _error(ret, 'No text found to append. Nothing appended')
|
||||
|
||||
if __opts__['test']:
|
||||
nlines = slines + test_lines
|
||||
ret['comment'] = 'File {0} is set to be updated'.format(name)
|
||||
ret['result'] = None
|
||||
nlines = list(slines)
|
||||
nlines.extend(append_lines)
|
||||
if slines != nlines:
|
||||
if not salt.utils.istextfile(name):
|
||||
ret['changes']['diff'] = 'Replace binary file'
|
||||
|
@ -3562,6 +3559,13 @@ def append(name,
|
|||
ret['result'] = True
|
||||
return ret
|
||||
|
||||
if len(append_lines):
|
||||
append_lines = [item.strip() for item in append_lines]
|
||||
__salt__['file.append'](name, args=append_lines)
|
||||
ret['comment'] = 'Appended {0} lines'.format(len(append_lines))
|
||||
else:
|
||||
ret['comment'] = 'File {0} is in correct state'.format(name)
|
||||
|
||||
with salt.utils.fopen(name, 'rb') as fp_:
|
||||
nlines = fp_.readlines()
|
||||
|
||||
|
@ -3574,11 +3578,8 @@ def append(name,
|
|||
''.join(difflib.unified_diff(slines, nlines))
|
||||
)
|
||||
|
||||
if count:
|
||||
ret['comment'] = 'Appended {0} lines'.format(count)
|
||||
else:
|
||||
ret['comment'] = 'File {0} is in correct state'.format(name)
|
||||
ret['result'] = True
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue