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

Feature to only update atomic file when changed #1378

Open
altendky opened this issue Aug 25, 2019 · 1 comment
Open

Feature to only update atomic file when changed #1378

altendky opened this issue Aug 25, 2019 · 1 comment

Comments

@altendky
Copy link
Contributor

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.

click/click/_compat.py

Lines 536 to 546 in 38a2712

def close(self, delete=False):
if self.closed:
return
self._f.close()
if not _can_replace:
try:
os.remove(self._real_filename)
except OSError:
pass
_replace(self._tmp_filename, self._real_filename)
self.closed = True

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.

@ljluestc
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants