[gnome-ostree] fileutil: New utility method to write JSON file



commit e95d3218f72e37c3810d280d1976b592008ca9f1
Author: Colin Walters <walters verbum org>
Date:   Sat Oct 20 20:28:22 2012 -0400

    fileutil: New utility method to write JSON file

 src/ostbuild/pyostbuild/fileutil.py |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
---
diff --git a/src/ostbuild/pyostbuild/fileutil.py b/src/ostbuild/pyostbuild/fileutil.py
index d9ae91f..30bf192 100644
--- a/src/ostbuild/pyostbuild/fileutil.py
+++ b/src/ostbuild/pyostbuild/fileutil.py
@@ -16,7 +16,7 @@
 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 # Boston, MA 02111-1307, USA.
 
-import os
+import os,json
 
 def ensure_dir(path):
     if not os.path.isdir(path):
@@ -24,3 +24,12 @@ def ensure_dir(path):
 
 def ensure_parent_dir(path):
     ensure_dir(os.path.dirname(path))
+
+def write_json_file_atomic(path, data):
+    path_tmp = path + '.tmp'
+    f = open(path_tmp, 'w')
+    json.dump(data, f, indent=4, sort_keys=True)
+    f.close()
+    os.chmod(path_tmp, 0644)
+    os.rename(path_tmp, path)
+    



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]