From b5801dc63d689b7560688f67dd2b2415653d5af8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 18 Nov 2015 07:40:31 +0530 Subject: [PATCH] ... --- src/duktape/__init__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/duktape/__init__.py b/src/duktape/__init__.py index fc4f7bcc2f..1388bc8778 100644 --- a/src/duktape/__init__.py +++ b/src/duktape/__init__.py @@ -9,10 +9,12 @@ __docformat__ = 'restructuredtext en' __all__ = ['dukpy', 'Context', 'undefined', 'JSError', 'to_python'] -import errno, os, sys, numbers, hashlib, json, tempfile, stat +import errno, os, sys, numbers, hashlib, json from functools import partial -from calibre.constants import plugins +from calibre.constants import plugins, iswindows +from calibre.utils.filenames import atomic_rename + dukpy, err = plugins['dukpy'] if err: raise RuntimeError('Failed to load dukpy with error: %s' % err) @@ -120,10 +122,11 @@ def readfile(path, enc='utf-8'): return [None, errno.errorcode[e.errno], 'Failed to read from file: %s with error: %s' % (path, e.message or e)] def atomic_write(name, raw): - with tempfile.NamedTemporaryFile(dir=os.getcwdu(), delete=False) as f: + bdir, bname = os.path.dirname(os.path.abspath(name)), os.path.basename(name) + tname = ('_' if iswindows else '.') + bname + with open(os.path.join(bdir, tname), 'wb') as f: f.write(raw) - os.fchmod(f.fileno(), stat.S_IREAD|stat.S_IWRITE|stat.S_IRGRP|stat.S_IROTH) - os.rename(f.name, name) + atomic_rename(tname, name) def writefile(path, data, enc='utf-8'): if enc == undefined: