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:
|
except:
|
||||||
pass
|
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):
|
def app_prefix(prefix):
|
||||||
if iswindows:
|
if iswindows:
|
||||||
return '%s_'%__appname__
|
return '%s_'%__appname__
|
||||||
@ -78,7 +99,7 @@ def base_dir():
|
|||||||
base = get_windows_temp_path()
|
base = get_windows_temp_path()
|
||||||
|
|
||||||
_base_dir = tempfile.mkdtemp(prefix=prefix, dir=base)
|
_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:
|
try:
|
||||||
tempfile.gettempdir()
|
tempfile.gettempdir()
|
||||||
@ -110,6 +131,7 @@ def _make_dir(suffix, prefix, base):
|
|||||||
return tempfile.mkdtemp(suffix, prefix, base)
|
return tempfile.mkdtemp(suffix, prefix, base)
|
||||||
|
|
||||||
class PersistentTemporaryFile(object):
|
class PersistentTemporaryFile(object):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
A file-like object that is a temporary file that is available even after being closed on
|
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.
|
all platforms. It is automatically deleted on normal program termination.
|
||||||
@ -117,7 +139,7 @@ class PersistentTemporaryFile(object):
|
|||||||
_file = None
|
_file = None
|
||||||
|
|
||||||
def __init__(self, suffix="", prefix="", dir=None, mode='w+b'):
|
def __init__(self, suffix="", prefix="", dir=None, mode='w+b'):
|
||||||
if prefix == None:
|
if prefix is None:
|
||||||
prefix = ""
|
prefix = ""
|
||||||
if dir is None:
|
if dir is None:
|
||||||
dir = base_dir()
|
dir = base_dir()
|
||||||
@ -158,6 +180,7 @@ def PersistentTemporaryDirectory(suffix='', prefix='', dir=None):
|
|||||||
return tdir
|
return tdir
|
||||||
|
|
||||||
class TemporaryDirectory(object):
|
class TemporaryDirectory(object):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
A temporary directory to be used in a with statement.
|
A temporary directory to be used in a with statement.
|
||||||
'''
|
'''
|
||||||
@ -181,7 +204,7 @@ class TemporaryDirectory(object):
|
|||||||
class TemporaryFile(object):
|
class TemporaryFile(object):
|
||||||
|
|
||||||
def __init__(self, suffix="", prefix="", dir=None, mode='w+b'):
|
def __init__(self, suffix="", prefix="", dir=None, mode='w+b'):
|
||||||
if prefix == None:
|
if prefix is None:
|
||||||
prefix = ''
|
prefix = ''
|
||||||
if suffix is None:
|
if suffix is None:
|
||||||
suffix = ''
|
suffix = ''
|
||||||
@ -201,12 +224,11 @@ class TemporaryFile(object):
|
|||||||
cleanup(self._name)
|
cleanup(self._name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SpooledTemporaryFile(tempfile.SpooledTemporaryFile):
|
class SpooledTemporaryFile(tempfile.SpooledTemporaryFile):
|
||||||
|
|
||||||
def __init__(self, max_size=0, suffix="", prefix="", dir=None, mode='w+b',
|
def __init__(self, max_size=0, suffix="", prefix="", dir=None, mode='w+b',
|
||||||
bufsize=-1):
|
bufsize=-1):
|
||||||
if prefix == None:
|
if prefix is None:
|
||||||
prefix = ''
|
prefix = ''
|
||||||
if suffix is None:
|
if suffix is None:
|
||||||
suffix = ''
|
suffix = ''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user