click icon indicating copy to clipboard operation
click copied to clipboard

Feature to only update atomic file when changed

Open altendky opened this issue 5 years ago • 1 comments

I have various programs that are generating source files for use in a C project build. Often these files get generated the same each time but since they are written regardless they trigger cascading rebuilds of that file and those that depend on it.

https://github.com/pallets/click/blob/38a2712d596a5df9288c1817748b71197d8c7c57/click/_compat.py#L536-L546

I didn't implement it yet but it seems like it wouldn't be particularly difficult to add a changed check right after self._f.close(). Perhaps right before to avoid reopening the new content? I would not expect this changed check to be atomic with the replace. Maybe there's a way. It not being atomic isn't an issue for me. If two things are trying to dump into the file 'at the same time'... so it goes.

The 'only if changed' option would then be cascaded back up at least to the click.File() interface. Though having only_write_if_changed be always present but only usable with atomic=True isn't great so probably more to talk about there. A separate callable, something else, I haven't thought this through yet.

Does this sound like a feature which would be of interest and is worth me developing a PR to discuss? Or, should I just go implement this for myself on the side without bothering to have so much click integration.

altendky avatar Aug 25 '19 16:08 altendky

import click

class FileIfChanged(click.File):
    def write(self, value):
        # Check if the file exists and read its current content
        try:
            with open(self.get_filename(), 'r') as f:
                existing_content = f.read()
        except FileNotFoundError:
            existing_content = ""

        # Compare the existing content with the new content
        if existing_content != value:
            # Only write if the content is different
            super().write(value)

@click.command()
@click.option('--output', type=FileIfChanged('w'), default='output.txt')
def main(output):
    # Your program logic here
    output.write('Hello, World!')

if __name__ == '__main__':
    main()

ljluestc avatar Sep 16 '23 16:09 ljluestc