Make calibre worker processes use the same temp directory as the calibre GUI

This commit is contained in:
Kovid Goyal 2010-10-09 06:58:49 -06:00
parent 6a57e799af
commit f9bddf4f67
2 changed files with 17 additions and 6 deletions

View File

@ -5,7 +5,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
Provides platform independent temporary files that persist even after
being closed.
"""
import tempfile, os, atexit
import tempfile, os, atexit, binascii, cPickle
from calibre import __version__, __appname__
@ -30,9 +30,18 @@ def remove_dir(x):
def base_dir():
global _base_dir
if _base_dir is None:
_base_dir = tempfile.mkdtemp(prefix='%s_%s_tmp_'%(__appname__,
__version__))
atexit.register(remove_dir, _base_dir)
td = os.environ.get('CALIBRE_WORKER_TEMP_DIR', None)
if td is not None:
try:
td = cPickle.loads(binascii.unhexlify(td))
except:
td = None
if td and os.path.exists(td):
_base_dir = td
else:
_base_dir = tempfile.mkdtemp(prefix='%s_%s_tmp_'%(__appname__,
__version__))
atexit.register(remove_dir, _base_dir)
return _base_dir
class PersistentTemporaryFile(object):

View File

@ -6,11 +6,11 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import subprocess, os, sys, time
import subprocess, os, sys, time, binascii, cPickle
from calibre.constants import iswindows, isosx, isfrozen, isnewosx
from calibre.utils.config import prefs
from calibre.ptempfile import PersistentTemporaryFile
from calibre.ptempfile import PersistentTemporaryFile, base_dir
if iswindows:
import win32process
@ -81,6 +81,8 @@ class Worker(object):
def env(self):
env = dict(os.environ)
env['CALIBRE_WORKER'] = '1'
td = binascii.hexlify(cPickle.dumps(base_dir()))
env['CALIBRE_WORKER_TEMP_DIR'] = td
env.update(self._env)
return env