Skip to content

Conversation

@mboisson
Copy link
Contributor

No description provided.

@mboisson
Copy link
Contributor Author

Tagging @ocaisa

@boegel boegel added the bug fix label Oct 23, 2018
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)
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Member

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

Copy link
Member

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

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

Copy link
Member

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

Copy link
Member

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

Copy link
Member

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?

Copy link
Member

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

@boegel
Copy link
Member

boegel commented Oct 23, 2018

@mboisson Also, we should get this covered in the tests, i.e. have a test that verifies that dump will not blindly overwrite an existing easyconfig file, and that you can use --force to convince it to do so...

@boegel boegel added this to the 3.8.0 milestone Oct 23, 2018
mboisson and others added 2 commits November 15, 2018 07:55
different approach to avoid blind overwriting of existing files with --try-*
fix tweak_one test + remove duplicate check for forced overwriting of existing file
boegel
boegel previously approved these changes Nov 15, 2018
@boegel
Copy link
Member

boegel commented Nov 15, 2018

@ocaisa Thoughts on current approach?

tweaked_ec)

write_file(tweaked_ec, ectxt)
write_file(tweaked_ec, ectxt, backup=True, overwrite=False, verbose=True)
Copy link
Member

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?

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how about always_overwrite ?

Copy link
Member

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

Copy link
Member

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?

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'm kind of lost in all of the changes you PR'd to us now :/

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

Choose a reason for hiding this comment

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

Ditto

boegel and others added 2 commits November 16, 2018 10:07
fix write_file tests after 'overwrite' argument was renamed to 'always_overwrite'
@easybuilders easybuilders deleted a comment from boegelbot Nov 16, 2018
@boegel
Copy link
Member

boegel commented Nov 16, 2018

Good to go, thanks a lot for looking into this @mboisson, and to @ocaisa for the feedback!

@boegel boegel merged commit 3c9fa52 into easybuilders:develop Nov 16, 2018
@boegel boegel changed the title fixing bug that could cause to silently overwrite an existing easyconfig fixing bug that could cause to silently overwrite an existing easyconfig when using --try-* Nov 28, 2018
@mboisson mboisson deleted the fix-bug-tweak branch May 6, 2020 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants