-
Notifications
You must be signed in to change notification settings - Fork 217
fixing bug that could cause to silently overwrite an existing easyconfig when using --try-* #2635
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
Conversation
|
Tagging @ocaisa |
| print_warning("Overwriting existing file at %s with tweaked easyconfig file (due to --force)", tweaked_spec) | ||
| else: | ||
| raise EasyBuildError("A file already exists at %s where tweaked easyconfig file would be written", | ||
| tweaked_spec) |
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.
@mboisson Shouldn't we do this in dump itself instead? We may be using dump is other places in the future...
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.
We could. I went to the simplest and least intrusive. Note that there are two different code-paths that can write files, in tweak.py. The first one uses write_file, the other one uses dump. The test was already around the write_file call, but not around the dump part. I am happy with whatever fix you or @ocaisa think is best.
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.
In this case I really think doing it in dump itself makes more sense...
I'm not sure why there are two code paths where one uses write_file and another uses dump, maybe @ocaisa knows?
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.
There are two paths because one uses regex substitution and the other modifies the EasyConfig object and dumps the modified object.
Ultimately the dump method does use write_file so perhaps the question is whether we want to pass down options, the choices currently are:
def write_file(path, txt, append=False, forced=False, backup=False):
"""
Write given contents to file at given path;
overwrites current file contents without backup by default!
:param path: location of file
:param txt: contents to write to file
:param append: append to existing file rather than overwrite
:param forced: force actually writing file in (extended) dry run mode
:param backup: back up existing file before overwriting or modifying 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.
Perhaps doing a backup is sufficient? That way nothing ever gets removed and no need for the force option
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 think write_file should at the very least issue a warning if not fail if it is going to be over-writing an existing 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.
If a backup is created, it will print a warning message for that.
But I agree with @mboisson here, it shouldn't overwrite existing files without --force (and it should still create a backup if --force is used).
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.
@mboisson W.r.t. making write_file fail hard rather than overwriting an existing file: we can't do that without breaking backward compatibility, changing that would likely break a lot of easyblocks. Also, always having to use force to make write_file actually overwrite existing file would be a bit much, that's not standard semantics in Linux either...
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.
So, how do we proceed? Are you saying that the original check that @mboisson had is appropriate then and maybe couple that with adding the backup kw in the dump?
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 took a look at it, the best way is to enhance write_file a bit to optionally not blindly overwrite existing files, and then use write_file correctly in both places.
Done in ComputeCanada#8
|
@mboisson Also, we should get this covered in the tests, i.e. have a test that verifies that |
…s --force is used, and to be verbose about creating backups
different approach to avoid blind overwriting of existing files with --try-*
fix tweak_one test + remove duplicate check for forced overwriting of existing file
|
@ocaisa Thoughts on current approach? |
| tweaked_ec) | ||
|
|
||
| write_file(tweaked_ec, ectxt) | ||
| write_file(tweaked_ec, ectxt, backup=True, overwrite=False, verbose=True) |
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's the point of this? If you're not going to overwrite then why backup? Shouldn't backup imply overwrite?
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.
Ah ok, I understand it now but the kw name is not very helpful, how about unforced_overwrite?
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.
overwrite=False means "don't overwrite unless --force is used"
Without also adding backup=True, you may overwrite when using --force without backing up the file.
Whether you want a backup or not depends on in what context you're calling write_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.
Ok, but the kw name needs tweaking, it's not unreasonable to expect overwrite=False not to overwrite.
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 had overwrite_without_force before, which I found too long, but would that be better?
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.
how about always_overwrite ?
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.
overwrite_without_force is long but since the option is so connected to --force I think it is worth the keystrokes, it will save people having to read the docstring
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.
@mboisson always_overwrite makes sense to me. Can you make that change?
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 kind of lost in all of the changes you PR'd to us now :/
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 think I did it.
| tweaked_spec = os.path.join(targetdir or tempfile.gettempdir(), ec_filename) | ||
| parsed_ec['ec'].dump(tweaked_spec) | ||
|
|
||
| parsed_ec['ec'].dump(tweaked_spec, overwrite=False, backup=True) |
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.
Ditto
fix write_file tests after 'overwrite' argument was renamed to 'always_overwrite'
No description provided.