Skip to content

Commit 3fbf588

Browse files
committed
Rewrite file only if content will change
fixes #34
1 parent 32ce02b commit 3fbf588

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

fprettify/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,21 @@ def reformat_inplace(filename, stdout=False, **kwargs): # pragma: no cover
990990
if stdout:
991991
sys.stdout.write(newfile.getvalue())
992992
else:
993-
outfile = io.open(filename, 'w', encoding='utf-8')
994-
outfile.write(newfile.getvalue())
993+
outfile = io.open(filename, 'r', encoding='utf-8')
994+
995+
# write to outfile only if content has changed
996+
997+
import hashlib
998+
hash_new = hashlib.md5()
999+
hash_new.update(newfile.getvalue().encode('utf-8'))
1000+
hash_old = hashlib.md5()
1001+
hash_old.update(outfile.read().encode('utf-8'))
1002+
1003+
outfile.close()
1004+
1005+
if hash_new.digest() != hash_old.digest():
1006+
outfile = io.open(filename, 'w', encoding='utf-8')
1007+
outfile.write(newfile.getvalue())
9951008

9961009

9971010
def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_indent=False, impose_whitespace=True,

0 commit comments

Comments
 (0)