New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for mixed line ending in file #45498
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume when you say "mixed line ending" you mean windows and unix line endings? If so, this is GTM.
@terminalmage You assume right. What stands for GTM? |
salt/modules/file.py
Outdated
def _split(content): | ||
split_content = [] | ||
for linesep_line in content.split(os.linesep): | ||
for content_line in linesep_line.split('\n'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code makes a difference only being executed on platforms with os.linesep != '\n'... Why not use str.splitlines() instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm aware of that.
That specific part of code was tested in file.blockreplace. I do not know how str.splitlines() will behave
salt/modules/file.py
Outdated
@@ -1728,11 +1735,11 @@ def line(path, content=None, match=None, mode=None, location=None, | |||
log.warning('Cannot find text to {0}. File \'{1}\' is empty.'.format(mode, path)) | |||
body = '' | |||
elif mode == 'delete': | |||
body = os.linesep.join([line for line in body.split(os.linesep) if line.find(match) < 0]) | |||
body = os.linesep.join([line for line in _split(body) if line.find(match) < 0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Joining with os.linesep and splitting by something else will lead to all the line breaks replaced in the file...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems legit to me, to output file with line endings proper to OS.
Converting '\n' to os.linesep is default python behavior for not binary writes.
On Fri, Jan 19, 2018 at 10:49:01PM +0000, Jerzy Drozdz wrote:
t0fik commented on this pull request.
> @@ -1728,11 +1735,11 @@ def line(path, content=None, match=None, mode=None, location=None,
log.warning('Cannot find text to {0}. File \'{1}\' is empty.'.format(mode, path))
body = ''
elif mode == 'delete':
- body = os.linesep.join([line for line in body.split(os.linesep) if line.find(match) < 0])
+ body = os.linesep.join([line for line in _split(body) if line.find(match) < 0])
Seems legit to me, to output file with line endings proper to OS.
Might be proper for OS, but confusing for software consuming that
particular file. Function that modifies a line shouldn't silently change
all the file :)
… Converting '\n' to os.linesep is default python behavior for not binary writes.
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#45498 (comment)
|
@sergeizv What is your proposition, instead converting line endings? |
On Fri, Jan 19, 2018 at 11:44:16PM +0000, Jerzy Drozdz wrote:
@sergeizv What is your proposition, instead converting line endings?
Maybe detect line break first and then split and join by it instead of
os.linesep?
For now that particular function cannot handle line endings, which are not proper for the OS. For example: LF instead CRLF on Windows
The same is true for blockreplace() as well at least
…
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#45498 (comment)
|
I will rewrite line function to detect line endings. I hadn't time to do it. |
salt/modules/file.py
Outdated
@@ -1589,6 +1590,13 @@ def _get_line_indent(src, line, indent): | |||
|
|||
return ''.join(idt) + line.strip() | |||
|
|||
def _get_line_ending(src, line): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to add line ending, rather than get_line_ending consider a different name for the function.
e.g. _add_src_eol(src, line).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@damon-atkins I wanted to be consistent with name scheme of _get_line_indent, which adds indentation to line.
IMHO better names to both functions would be _set_line_eol and _set_line_indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, change it
@t0fik There's a lint error: https://jenkins.saltstack.com/job/PR/job/salt-pr-lint-n/19000/violations/ |
There's also some related test failures: https://jenkins.saltstack.com/job/PR/job/salt-pr-linode-cent7-py3/2006/ |
salt/modules/file.py
Outdated
@@ -1589,6 +1590,13 @@ def _get_line_indent(src, line, indent): | |||
|
|||
return ''.join(idt) + line.strip() | |||
|
|||
def _get_line_ending(src, line): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, change it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not comfortable with something that changes this much to be included in a bugfix release, especially with all the changes to file I/O made for Oxygen. In my opinion this should be done in develop.
salt/modules/win_file.py
Outdated
lstat, path_exists_glob, write, pardir, join, HASHES, HASHES_REVMAP, | ||
comment, uncomment, _add_flags, comment_line, _regex_to_static, | ||
_set_line_indent, apply_template_on_contents, dirname, basename, | ||
list_backups_dir, _assert_occurrence, _starts_till, _set_line_eol) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why you felt the need to indent the entire block here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was more readable for me, when I was checking imports. I've reverted indentation to original
salt/modules/file.py
Outdated
Add line ending | ||
''' | ||
regex = re.compile('((?<!\r)\n|\r(?!\n)|\r\n)$') | ||
line_ending = regex.search(src).group() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens on the final line of the file? Not all files end in a newline, and this regex wouldn't match a line without one:
>>> regex = re.compile('((?<!\r)\n|\r(?!\n)|\r\n)$')
>>> regex.search('foo').group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add guard statement.
except Exception as ex: | ||
raise CommandExecutionError("{0}: '{1}'".format(_get_error_message(ex), regex)) | ||
|
||
return src and src.group() or regex | ||
return src and src[0] or regex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this being done? I don't see this function being referenced in any of the changed code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function is referenced in code which was not changed, line 1734 (orig 1723). I had to change how file is read (to list instead to string) so I had to modify this function
@t0fik I am making the rounds on PRs today :) Did you get a chance to review the comments above? Those will need to be addressed and this change needs to be moved to develop before we can move forward here. |
@rallytime sorry, I was busy. I've reviewed comments and fixed issues. |
5816a85
to
bbbb8f1
Compare
This still needs to be rebased/resubmitted on top of develop. |
I'll resubmit PR to develop on weekend. |
PR resubmited to develop #46291 |
Adds support for mixed line endings in file.line method.
Test written: No
Not signed