Skip to content
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

Closed
wants to merge 15 commits into from

Conversation

t0fik
Copy link
Contributor

@t0fik t0fik commented Jan 17, 2018

Adds support for mixed line endings in file.line method.

Test written: No

Not signed

Copy link
Contributor

@terminalmage terminalmage left a 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.

@t0fik
Copy link
Contributor Author

t0fik commented Jan 19, 2018

@terminalmage You assume right. What stands for GTM?

def _split(content):
split_content = []
for linesep_line in content.split(os.linesep):
for content_line in linesep_line.split('\n'):
Copy link
Contributor

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?

Copy link
Contributor Author

@t0fik t0fik Jan 19, 2018

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

@@ -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])
Copy link
Contributor

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...

Copy link
Contributor Author

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.

@sergeizv
Copy link
Contributor

sergeizv commented Jan 19, 2018 via email

@t0fik
Copy link
Contributor Author

t0fik commented Jan 19, 2018

@sergeizv What is your proposition, instead converting line endings?
For now that particular function cannot handle line endings, which are not proper for the OS. For example: LF instead CRLF on Windows

@sergeizv
Copy link
Contributor

sergeizv commented Jan 20, 2018 via email

@rallytime
Copy link
Contributor

@t0fik and @sergeizv Where did we land on this PR?

@t0fik
Copy link
Contributor Author

t0fik commented Feb 2, 2018

I will rewrite line function to detect line endings. I hadn't time to do it.

@@ -1589,6 +1590,13 @@ def _get_line_indent(src, line, indent):

return ''.join(idt) + line.strip()

def _get_line_ending(src, line):
Copy link
Contributor

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).

Copy link
Contributor Author

@t0fik t0fik Feb 4, 2018

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, change it

@rallytime
Copy link
Contributor

@rallytime
Copy link
Contributor

There's also some related test failures: https://jenkins.saltstack.com/job/PR/job/salt-pr-linode-cent7-py3/2006/

@@ -1589,6 +1590,13 @@ def _get_line_indent(src, line, indent):

return ''.join(idt) + line.strip()

def _get_line_ending(src, line):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, change it

Copy link
Contributor

@terminalmage terminalmage left a 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.

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)
Copy link
Contributor

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.

Copy link
Contributor Author

@t0fik t0fik Feb 28, 2018

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

Add line ending
'''
regex = re.compile('((?<!\r)\n|\r(?!\n)|\r\n)$')
line_ending = regex.search(src).group()
Copy link
Contributor

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'

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

@t0fik t0fik Feb 28, 2018

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

@rallytime
Copy link
Contributor

@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.

@t0fik
Copy link
Contributor Author

t0fik commented Feb 28, 2018

@rallytime sorry, I was busy. I've reviewed comments and fixed issues.

@t0fik t0fik force-pushed the 2017.7_line_mixed_line_ends branch from 5816a85 to bbbb8f1 Compare February 28, 2018 22:22
@terminalmage
Copy link
Contributor

This still needs to be rebased/resubmitted on top of develop.

@t0fik
Copy link
Contributor Author

t0fik commented Mar 1, 2018

I'll resubmit PR to develop on weekend.

@t0fik t0fik mentioned this pull request Mar 2, 2018
@t0fik
Copy link
Contributor Author

t0fik commented Mar 2, 2018

PR resubmited to develop #46291

@t0fik t0fik closed this Mar 2, 2018
@t0fik t0fik deleted the 2017.7_line_mixed_line_ends branch March 2, 2018 00:07
@t0fik t0fik restored the 2017.7_line_mixed_line_ends branch March 4, 2018 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants