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

check conditions for .signac_shell_history permission. #279

Merged
merged 14 commits into from
Feb 19, 2020

Conversation

ac-optimus
Copy link
Contributor

@ac-optimus ac-optimus commented Jan 28, 2020

Description

Motivation and Context

This will fix - #262

Types of Changes

  • Documentation update
  • Bug fix
  • New feature
  • Breaking change1

1The change breaks (or has the potential to break) existing functionality.

Checklist:

If necessary:

  • I have updated the API documentation as part of the package doc-strings.
  • I have created a separate pull request to update the framework documentation on signac-docs and linked it here.
  • I have updated the changelog and added all related issue and pull request numbers for future reference (if applicable). See example below.

Example for a changelog entry: Fix issue with launching rockets to the moon (#101, #212).

@ac-optimus ac-optimus requested review from a team as code owners January 28, 2020 22:05
@ac-optimus ac-optimus requested review from atravitz and removed request for a team January 28, 2020 22:05
@csadorf csadorf self-requested a review January 29, 2020 08:16
Copy link
Contributor

@csadorf csadorf left a comment

Choose a reason for hiding this comment

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

@ac-optimus Thank you for the contribution!

In addition to my code comments, could you make sure that your code follows our code style guidelines? The easiest way to ensure that is by using flake8, ideally in combination with a git hook, see here for instructions on how to set that up.

signac/__main__.py Show resolved Hide resolved
@ac-optimus ac-optimus removed their assignment Jan 29, 2020
atexit.register(readline.write_history_file, fn_hist)
except PermissionError:
print("Warning: .signac_shell_history does not have read/write permission.\
Copy link
Member

Choose a reason for hiding this comment

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

Try breaking the line like this. Python will combine the two strings into one message. Notice there is a space at the end of the first line.

print("Warning: .signac_shell_history does not have read/write permission. "
      "The history of this shell will not be saved.")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bdice done!

@ac-optimus ac-optimus requested a review from bdice January 29, 2020 21:15
Copy link
Contributor

@csadorf csadorf left a comment

Choose a reason for hiding this comment

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

Thank you for making that change! I have a few more questions.

raise
atexit.register(readline.write_history_file, fn_hist)
with open(fn_hist, 'w') as f: pass
atexit.register(readline.write_history_file, fn_hist)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we maybe just open it in append (a) mode so that in case that we fail to write the history from memory at program exit it is at least not lost?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@csadorf yes seems like a good idea. done that!

Copy link
Member

Choose a reason for hiding this comment

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

@csadorf I'm not sure if I understand. How does using a fix the problem if we don't have write access? I thought the primary difference between w and a was whether it overwrote the existing file or appended to the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bdice actually the main reason for line-1048 is to check weather the file has a write permission. If it doesn't then we get an exception that is used to throw a warning.

Copy link
Member

Choose a reason for hiding this comment

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

@ac-optimus @csadorf I would prefer to use the approach of permissions-checking for this specific instance, instead of attempting to write to the file and catching an exception. I believe that opening the file (using with open(...): pass) requires more system calls and may have unintended side-effects such as memory allocation (for the file buffer) and updating the "modified" timestamp of the file with no actual modifications. My preferred solution would be to use os.access as before.

Copy link
Contributor Author

@ac-optimus ac-optimus Jan 31, 2020

Choose a reason for hiding this comment

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

or we can throw common warning for both read and write permission error like this-

                    try:
                        readline.read_history_file(fn_hist)
                        readline.set_history_length(1000)
                        with open(fn_hist, "a"):
                            pass
                    except FileNotFoundError:
                        pass
                    except PermissionError:
                        print("Warning: Shell history from "
                              "{}".format(os.path.relpath(fn_hist)),
                              "would be lost. Permission error.")
                    atexit.register(readline.write_history_file, fn_hist)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd just go with the last alternative, since I believe that it solves the original issue. @bdice Do you agree?

Copy link
Member

Choose a reason for hiding this comment

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

I recommend that we adopt the snippet I proposed above: #279 (comment)
@ac-optimus, can you apply that snippet and test it out manually? (no need to write a unit test)

Copy link
Contributor

Choose a reason for hiding this comment

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

@bdice Sounds good to me!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bdice I already tried it manually and it was the reason why I proposed slight modification (1st code spinet above). readline.write_history_file(fn_hist) does not throw error when we do not have write permission and hence the exception is never called. And the 3rd one is more general. Please have a look and correct me if I am wrong.

signac/__main__.py Outdated Show resolved Hide resolved
signac/__main__.py Outdated Show resolved Hide resolved
raise
atexit.register(readline.write_history_file, fn_hist)
with open(fn_hist, 'w') as f: pass
atexit.register(readline.write_history_file, fn_hist)
Copy link
Member

Choose a reason for hiding this comment

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

@csadorf I'm not sure if I understand. How does using a fix the problem if we don't have write access? I thought the primary difference between w and a was whether it overwrote the existing file or appended to the end.

@ac-optimus ac-optimus requested a review from bdice January 30, 2020 19:44
signac/__main__.py Outdated Show resolved Hide resolved
@ac-optimus ac-optimus requested a review from bdice January 30, 2020 20:06
@csadorf
Copy link
Contributor

csadorf commented Jan 31, 2020

@ac-optimus I recommend that you execute the linter locally so that your code follows the code style guidelines.

@ac-optimus
Copy link
Contributor Author

@ac-optimus I recommend that you execute the linter locally so that your code follows the code style guidelines.

okay sure.

@bdice
Copy link
Member

bdice commented Jan 31, 2020

Edit: This was in response to "what to do now?" -- sorry, I thought that was a general question. I will reply to the comment where you specifically wrote that question.

@ac-optimus Your code still isn't passing the linter (flake8). Try installing the flake8 hook as described here, which prevents you from committing code that fails the linter. You can see the errors in the CI output.

signac/__main__.py:1048:52: F841 local variable 'f' is assigned to but never used
                        with open(fn_hist, 'w') as f: pass
                                                   ^
signac/__main__.py:1048:53: E701 multiple statements on one line (colon)
                        with open(fn_hist, 'w') as f: pass
                                                    ^
signac/__main__.py:1054:101: E501 line too long (112 > 100 characters)
                               "The history of this shell will not be saved.").format(os.path.relpath(fn_hist)))

@ac-optimus
Copy link
Contributor Author

Edit: This was in response to "what to do now?" -- sorry, I thought that was a general question. I will reply to the comment where you specifically wrote that question.

@ac-optimus Your code still isn't passing the linter (flake8). Try installing the flake8 hook as described here, which prevents you from committing code that fails the linter. You can see the errors in the CI output.

signac/__main__.py:1048:52: F841 local variable 'f' is assigned to but never used
                        with open(fn_hist, 'w') as f: pass
                                                   ^
signac/__main__.py:1048:53: E701 multiple statements on one line (colon)
                        with open(fn_hist, 'w') as f: pass
                                                    ^
signac/__main__.py:1054:101: E501 line too long (112 > 100 characters)
                               "The history of this shell will not be saved.").format(os.path.relpath(fn_hist)))

@bdice @csadorf corrected the code style(using linter). Need to learn a lot. Thanks!
Should I merge all the commits, seems like lot mess happened due to me?

@bdice
Copy link
Member

bdice commented Jan 31, 2020

@ac-optimus We will squash all the commits on this branch into one when the pull request is merged so don't worry about the history being unclean. We can wait until the end to merge in master. Take a look at the code block I suggested on that comment thread and let me know what you think. I think it resolves the questions that @csadorf and I both had about EAFP.

@codecov
Copy link

codecov bot commented Jan 31, 2020

Codecov Report

Merging #279 into master will decrease coverage by 0.06%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #279      +/-   ##
==========================================
- Coverage   64.88%   64.81%   -0.07%     
==========================================
  Files          40       40              
  Lines        5624     5630       +6     
==========================================
  Hits         3649     3649              
- Misses       1975     1981       +6     

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e55e1a...ce499d7. Read the comment docs.

Copy link
Contributor

@csadorf csadorf left a comment

Choose a reason for hiding this comment

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

I did not know that the permissions would be automatically changed/ the file is replaced. Let's focus on the original issue (issue #262), that is that the shell fails to launch when there is a permissions error. Instead a warning should be printed and the shell is opened anyways. Whether we can also save the history is a different problem. I agree with @bdice and suggest we don't preemptively add any checks concerning that.

@csadorf
Copy link
Contributor

csadorf commented Feb 13, 2020

@bdice It looks like this PR is waiting on your input. @ac-optimus Is that correct?

@ac-optimus
Copy link
Contributor Author

@bdice It looks like this PR is waiting on your input. @ac-optimus Is that correct?

@csadorf yes I guess.

Copy link
Contributor

@csadorf csadorf left a comment

Choose a reason for hiding this comment

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

Please adopt the code snippet that @bdice proposed. The original issue will be addressed by that. See also my in-code comment.

@csadorf csadorf removed the request for review from bdice February 17, 2020 15:00
@csadorf csadorf added the bug Something isn't working label Feb 17, 2020
Copy link
Contributor

@csadorf csadorf left a comment

Choose a reason for hiding this comment

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

I need to revise my previous review comment, I'm going to push the needed modifications to this PR.

Copy link
Contributor

@csadorf csadorf left a comment

Choose a reason for hiding this comment

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

After more testing, I found that the write_history_file function will throw if the file can actually not be overridden by the user. In conclusion, @ac-optimus please adopt @bdice 's proposed solution exactly. Thank you.

@ac-optimus
Copy link
Contributor Author

ac-optimus commented Feb 17, 2020

After more testing, I found that the write_history_file function will throw if the file can actually not be overridden by the user. In conclusion, @ac-optimus please adopt @bdice 's proposed solution exactly. Thank you.

okay sure!

@csadorf
Copy link
Contributor

csadorf commented Feb 17, 2020

@ac-optimus It looks like there is some issue with our CI configuration. This sometimes happens when libraries that we depend on are updated in a non-backwards-compatible way. So please ignore the failing CI for now.

@csadorf
Copy link
Contributor

csadorf commented Feb 17, 2020

@ac-optimus Please merge master, this might also fix the CI issues.

@ac-optimus
Copy link
Contributor Author

@ac-optimus Please merge master, this might also fix the CI issues.

@csadorf merged it to master of my fork.

@csadorf
Copy link
Contributor

csadorf commented Feb 17, 2020

@ac-optimus Please merge master, this might also fix the CI issues.

@csadorf merged it to master of my fork.

Looks like the master branch of your fork is out of date.

@ac-optimus
Copy link
Contributor Author

@ac-optimus Please merge master, this might also fix the CI issues.

@csadorf merged it to master of my fork.

Looks like the master branch of your fork is out of date.

corrected that! the master is updated and merged with the permission_issue branch.

Copy link
Contributor

@csadorf csadorf left a comment

Choose a reason for hiding this comment

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

LGTM! Thx!

@csadorf
Copy link
Contributor

csadorf commented Feb 18, 2020

@atravitz I fixed the CI issues upstream and merged master here. Please merge this PR at your discretion.

@atravitz atravitz merged commit 9f212d8 into glotzerlab:master Feb 19, 2020
@bdice bdice added this to the v1.4.0 milestone Feb 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants