From f9bddf4f67746ae682ffd886c0c913940b543512 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 9 Oct 2010 06:58:49 -0600 Subject: [PATCH] Make calibre worker processes use the same temp directory as the calibre GUI --- src/calibre/ptempfile.py | 17 +++++++++++++---- src/calibre/utils/ipc/launch.py | 6 ++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/calibre/ptempfile.py b/src/calibre/ptempfile.py index ca7343ee9b..16a1ef4ce4 100644 --- a/src/calibre/ptempfile.py +++ b/src/calibre/ptempfile.py @@ -5,7 +5,7 @@ __copyright__ = '2008, Kovid Goyal ' 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): diff --git a/src/calibre/utils/ipc/launch.py b/src/calibre/utils/ipc/launch.py index e13c9e0cb6..8d3628d69a 100644 --- a/src/calibre/utils/ipc/launch.py +++ b/src/calibre/utils/ipc/launch.py @@ -6,11 +6,11 @@ __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' __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