-
Notifications
You must be signed in to change notification settings - Fork 36
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
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.
@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
Outdated
atexit.register(readline.write_history_file, fn_hist) | ||
except PermissionError: | ||
print("Warning: .signac_shell_history does not have read/write permission.\ |
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.
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.")
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.
@bdice done!
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.
Thank you for making that change! I have a few more questions.
signac/__main__.py
Outdated
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) |
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.
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?
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.
@csadorf yes seems like a good idea. done that!
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.
@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.
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.
@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.
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.
@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.
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.
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)
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'd just go with the last alternative, since I believe that it solves the original issue. @bdice Do you agree?
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 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)
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.
@bdice Sounds good to me!
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.
@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.
This reverts commit a308fd5.
signac/__main__.py
Outdated
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) |
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.
@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.
Co-Authored-By: Bradley Dice <[email protected]>
@ac-optimus I recommend that you execute the linter locally so that your code follows the code style guidelines. |
okay sure. |
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! |
@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 |
Codecov Report
@@ 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.
|
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 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.
@bdice It looks like this PR is waiting on your input. @ac-optimus Is that correct? |
@csadorf yes I guess. |
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.
Please adopt the code snippet that @bdice proposed. The original issue will be addressed by that. See also my in-code comment.
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 need to revise my previous review comment, I'm going to push the needed modifications to this PR.
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.
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! |
@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. |
@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. |
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.
LGTM! Thx!
@atravitz I fixed the CI issues upstream and merged master here. Please merge this PR at your discretion. |
Description
Motivation and Context
This will fix - #262
Types of Changes
1The change breaks (or has the potential to break) existing functionality.
Checklist:
If necessary:
Example for a changelog entry:
Fix issue with launching rockets to the moon (#101, #212).