Commit 8c7aa5c5 authored by Chris McDonough's avatar Chris McDonough

Allow users to specify the temporary directory for uncommitted blob data via

the ZODB_BLOB_TEMPDIR environment variable.  We don't use a value from ZConfig
because ZConfig use isn't mandated for standalone ZODB usage.
parent 9cfca891
...@@ -46,6 +46,8 @@ class Blob(Persistent): ...@@ -46,6 +46,8 @@ class Blob(Persistent):
results from this method call is unconditionally closed at results from this method call is unconditionally closed at
transaction boundaries and so may not be used across transaction boundaries and so may not be used across
transactions. """ transactions. """
tempdir = os.environ.get('ZODB_BLOB_TEMPDIR', tempfile.gettempdir())
result = None result = None
...@@ -64,7 +66,7 @@ class Blob(Persistent): ...@@ -64,7 +66,7 @@ class Blob(Persistent):
raise BlobError, "Already opened for reading." raise BlobError, "Already opened for reading."
if self._p_blob_uncommitted is None: if self._p_blob_uncommitted is None:
self._p_blob_uncommitted = utils.mktemp() self._p_blob_uncommitted = utils.mktemp(dir=tempdir)
self._p_blob_writers += 1 self._p_blob_writers += 1
result = BlobFile(self._p_blob_uncommitted, mode, self) result = BlobFile(self._p_blob_uncommitted, mode, self)
...@@ -75,7 +77,7 @@ class Blob(Persistent): ...@@ -75,7 +77,7 @@ class Blob(Persistent):
if self._p_blob_uncommitted is None: if self._p_blob_uncommitted is None:
# Create a new working copy # Create a new working copy
self._p_blob_uncommitted = utils.mktemp() self._p_blob_uncommitted = utils.mktemp(dir=tempdir)
uncommitted = BlobFile(self._p_blob_uncommitted, mode, self) uncommitted = BlobFile(self._p_blob_uncommitted, mode, self)
# NOTE: _p_blob data appears by virtue of Connection._setstate # NOTE: _p_blob data appears by virtue of Connection._setstate
utils.cp(file(self._p_blob_data), uncommitted) utils.cp(file(self._p_blob_data), uncommitted)
...@@ -103,7 +105,7 @@ class Blob(Persistent): ...@@ -103,7 +105,7 @@ class Blob(Persistent):
dm = BlobDataManager(self, result) dm = BlobDataManager(self, result)
# Blobs need to always participate in transactions. # Blobs need to always participate in transactions.
if self._p_jar: if self._p_jar is not None:
# If we are connected to a database, then we register # If we are connected to a database, then we register
# with the transaction manager for that. # with the transaction manager for that.
self._p_jar.transaction_manager.get().register(dm) self._p_jar.transaction_manager.get().register(dm)
......
...@@ -145,4 +145,17 @@ We can explicitly open Blobs in the different modified modes: ...@@ -145,4 +145,17 @@ We can explicitly open Blobs in the different modified modes:
'rb' 'rb'
>>> f9.close() >>> f9.close()
We can specify the tempdir that blobs use to keep uncommitted data by
modifying the ZODB_BLOB_TEMPDIR environment variable:
>>> import os, tempfile, shutil
>>> tempdir = tempfile.mkdtemp()
>>> os.environ['ZODB_BLOB_TEMPDIR'] = tempdir
>>> myblob = Blob()
>>> len(os.listdir(tempdir))
0
>>> f = myblob.open('w')
>>> len(os.listdir(tempdir))
1
>>> shutil.rmtree(tempdir)
>>> del os.environ['ZODB_BLOB_TEMPDIR']
...@@ -289,9 +289,9 @@ class WeakSet(object): ...@@ -289,9 +289,9 @@ class WeakSet(object):
return self.data.data.values() return self.data.data.values()
def mktemp(): def mktemp(dir=None):
"""Create a temp file, known by name, in a semi-secure manner.""" """Create a temp file, known by name, in a semi-secure manner."""
handle, filename = mkstemp() handle, filename = mkstemp(dir=dir)
os.close(handle) os.close(handle)
return filename return filename
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment