mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
More determined temp file removal on program exit in windows
This commit is contained in:
parent
e1bf5349b0
commit
e7a3db383b
@ -29,6 +29,27 @@ def remove_dir(x):
|
||||
except:
|
||||
pass
|
||||
|
||||
def determined_remove_dir(x):
|
||||
for i in range(10):
|
||||
try:
|
||||
import shutil
|
||||
shutil.rmtree(x)
|
||||
return
|
||||
except:
|
||||
import os # noqa
|
||||
if os.path.exists(x):
|
||||
# In case some other program has one of the temp files open.
|
||||
import time
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
return
|
||||
try:
|
||||
import shutil
|
||||
shutil.rmtree(x, ignore_errors=True)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def app_prefix(prefix):
|
||||
if iswindows:
|
||||
return '%s_'%__appname__
|
||||
@ -78,7 +99,7 @@ def base_dir():
|
||||
base = get_windows_temp_path()
|
||||
|
||||
_base_dir = tempfile.mkdtemp(prefix=prefix, dir=base)
|
||||
atexit.register(remove_dir, _base_dir)
|
||||
atexit.register(determined_remove_dir if iswindows else remove_dir, _base_dir)
|
||||
|
||||
try:
|
||||
tempfile.gettempdir()
|
||||
@ -110,6 +131,7 @@ def _make_dir(suffix, prefix, base):
|
||||
return tempfile.mkdtemp(suffix, prefix, base)
|
||||
|
||||
class PersistentTemporaryFile(object):
|
||||
|
||||
"""
|
||||
A file-like object that is a temporary file that is available even after being closed on
|
||||
all platforms. It is automatically deleted on normal program termination.
|
||||
@ -117,7 +139,7 @@ class PersistentTemporaryFile(object):
|
||||
_file = None
|
||||
|
||||
def __init__(self, suffix="", prefix="", dir=None, mode='w+b'):
|
||||
if prefix == None:
|
||||
if prefix is None:
|
||||
prefix = ""
|
||||
if dir is None:
|
||||
dir = base_dir()
|
||||
@ -158,6 +180,7 @@ def PersistentTemporaryDirectory(suffix='', prefix='', dir=None):
|
||||
return tdir
|
||||
|
||||
class TemporaryDirectory(object):
|
||||
|
||||
'''
|
||||
A temporary directory to be used in a with statement.
|
||||
'''
|
||||
@ -181,7 +204,7 @@ class TemporaryDirectory(object):
|
||||
class TemporaryFile(object):
|
||||
|
||||
def __init__(self, suffix="", prefix="", dir=None, mode='w+b'):
|
||||
if prefix == None:
|
||||
if prefix is None:
|
||||
prefix = ''
|
||||
if suffix is None:
|
||||
suffix = ''
|
||||
@ -201,12 +224,11 @@ class TemporaryFile(object):
|
||||
cleanup(self._name)
|
||||
|
||||
|
||||
|
||||
class SpooledTemporaryFile(tempfile.SpooledTemporaryFile):
|
||||
|
||||
def __init__(self, max_size=0, suffix="", prefix="", dir=None, mode='w+b',
|
||||
bufsize=-1):
|
||||
if prefix == None:
|
||||
if prefix is None:
|
||||
prefix = ''
|
||||
if suffix is None:
|
||||
suffix = ''
|
||||
|
Loading…
x
Reference in New Issue
Block a user