I have a C++ code generator in Python that generates many source files. Most of the time, only one file changes, but because the generators regenerates all of the files, they are all rebuilt. Is there a way to either get Python to not overwrite the files, or else to get cmak to use a checksum to see what needs to be rebuilt rather than just using the file date?
I was thinking something like this would be easy in Python: If I could replace
with open('blah', 'w') as f:
with this:
with open_but_only_overwrite_if_total_output_is_different('blah', 'w') as f:
What’s a nice way of accomplishing that?
Combining the code and ideas of Neil G, Petr Viktorin, gecco, and joel3000:
Some little additions (hopefully improvements):
shutil.copyfileonly copies the contents oftempnameintofilename, while preserving metadata like file permissions and fileownership.
filecmp.cmpchecks the size of the filesand returns
Falseif the sizes don’t match. That could be a nicespeedup if the files are large and one has stuff appended to the
end. It also reads and compares bufsize = 8*1024 bytes at a time,
instead of lines at a time.
bufsizewill generally be bigger than aline, which would result in fewer reads.