Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): common method write_if_changed
- Loading branch information
1 parent
ff04ac8
commit e19e7c3
Showing
4 changed files
with
29 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
|
||
|
||
def write_if_changed(path, data): | ||
|
||
if isinstance(data, str): | ||
data = data.encode() | ||
|
||
changed = False | ||
|
||
with open(os.open(path, os.O_CREAT | os.O_RDWR), 'wb+') as f: | ||
f.seek(0) | ||
current = f.read() | ||
if current != data: | ||
changed = True | ||
f.seek(0) | ||
f.write(data) | ||
f.truncate() | ||
os.fsync(f) | ||
|
||
return changed |