2005-07-05 19:48:00 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2005, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Defines a Transaction class for multi-file transactions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
import error
|
|
|
|
import python
|
|
|
|
import file as File
|
|
|
|
|
|
|
|
# 'txn' is used as an abbreviation for 'transaction' in the following source.
|
|
|
|
|
|
|
|
class FailedAcquisition(error.Error):
|
|
|
|
def __init__(self, txnDir, e=None):
|
|
|
|
self.txnDir = txnDir
|
|
|
|
msg = 'Could not acquire transaction directory: %s.' % self.txnDir
|
|
|
|
error.Error.__init__(self, msg, e)
|
|
|
|
|
|
|
|
class InProgress(error.Error):
|
|
|
|
def __init__(self, inProgress, e=None):
|
|
|
|
self.inProgress = inProgress
|
|
|
|
msg = 'Transaction appears to be in progress already: %s exists.' % \
|
|
|
|
self.inProgress
|
|
|
|
error.Error.__init__(self, msg, e)
|
|
|
|
|
|
|
|
|
|
|
|
class TransactionMixin(python.Object):
|
|
|
|
JOURNAL = 'journal'
|
|
|
|
ORIGINALS = 'originals'
|
|
|
|
INPROGRESS = '.inProgress'
|
|
|
|
REPLACEMENTS = 'replacements'
|
|
|
|
# expects a self.dir. used by Transaction and Rollback.
|
|
|
|
def __init__(self, txnDir):
|
|
|
|
self.txnDir = txnDir
|
|
|
|
self.dir = self.txnDir + self.INPROGRESS
|
|
|
|
self._journalName = self.dirize(self.JOURNAL)
|
|
|
|
|
|
|
|
def escape(self, filename):
|
|
|
|
return os.path.abspath(filename)[1:]
|
|
|
|
|
|
|
|
def dirize(self, *args):
|
|
|
|
return os.path.join(self.dir, *args)
|
|
|
|
|
|
|
|
def _original(self, filename):
|
|
|
|
return self.dirize(self.ORIGINALS, self.escape(filename))
|
|
|
|
|
|
|
|
def _replacement(self, filename):
|
|
|
|
return self.dirize(self.REPLACEMENTS, self.escape(filename))
|
|
|
|
|
2005-07-06 05:59:16 +02:00
|
|
|
def _checkCwd(self):
|
2005-07-05 19:48:00 +02:00
|
|
|
expected = File.contents(self.dirize('cwd'))
|
|
|
|
if os.getcwd() != expected:
|
|
|
|
raise InvalidCwd(expected)
|
|
|
|
|
2005-07-06 05:59:16 +02:00
|
|
|
def _journalCommands(self):
|
2005-07-05 19:48:00 +02:00
|
|
|
journal = file(self._journalName)
|
|
|
|
for line in journal:
|
|
|
|
line = line.rstrip('\n')
|
|
|
|
(command, rest) = line.split(None, 1)
|
|
|
|
args = rest.split()
|
|
|
|
yield (command, args)
|
|
|
|
|
|
|
|
|
|
|
|
class Transaction(TransactionMixin):
|
2005-07-18 06:46:41 +02:00
|
|
|
# XXX Transaction needs to be made threadsafe.
|
2005-07-05 19:48:00 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2005-07-18 06:55:12 +02:00
|
|
|
"""Transaction(txnDir) -> None
|
2005-07-05 19:48:00 +02:00
|
|
|
|
|
|
|
txnDir is the directory that will hold the transaction's working files
|
|
|
|
and such. If it can't be renamed, there is probably an active
|
|
|
|
transaction.
|
|
|
|
"""
|
|
|
|
TransactionMixin.__init__(self, *args, **kwargs)
|
|
|
|
if os.path.exists(self.dir):
|
|
|
|
raise InProgress(self.dir)
|
|
|
|
if not os.path.exists(self.txnDir):
|
|
|
|
raise FailedAcquisition(self.txnDir)
|
|
|
|
try:
|
|
|
|
os.rename(self.txnDir, self.dir)
|
|
|
|
except EnvironmentError, e:
|
2005-07-06 05:59:46 +02:00
|
|
|
raise FailedAcquisition(self.txnDir, e)
|
2005-07-05 19:48:00 +02:00
|
|
|
os.mkdir(self.dirize(self.ORIGINALS))
|
|
|
|
os.mkdir(self.dirize(self.REPLACEMENTS))
|
|
|
|
self._journal = file(self._journalName, 'a')
|
|
|
|
cwd = file(self.dirize('cwd'), 'w')
|
|
|
|
cwd.write(os.getcwd())
|
|
|
|
cwd.close()
|
|
|
|
|
2005-07-06 05:59:16 +02:00
|
|
|
def _journalCommand(self, command, *args):
|
2005-07-05 19:48:00 +02:00
|
|
|
File.writeLine(self._journal,
|
|
|
|
'%s %s' % (command, ' '.join(map(str, args))))
|
|
|
|
self._journal.flush()
|
|
|
|
|
|
|
|
def _makeOriginal(self, filename):
|
|
|
|
File.copy(filename, self._original(filename))
|
|
|
|
|
2005-07-18 06:55:12 +02:00
|
|
|
# XXX There needs to be a way, given a transaction, to get a
|
|
|
|
# "sub-transaction", which:
|
|
|
|
#
|
|
|
|
# 1. Doesn't try to grab the txnDir and move it, but instead is just
|
|
|
|
# given the actual directory being used and uses that.
|
|
|
|
# 2. Acquires the lock of the original transaction, only releasing it
|
|
|
|
# when its .commit method is called (assuming Transaction is
|
|
|
|
# threadsafe).
|
|
|
|
# 3. Has a no-op .commit method (i.e., doesn't commit).
|
|
|
|
#
|
|
|
|
# This is so that, for instance, an object with an active Transaction
|
|
|
|
# can give other objects a Transaction-ish object without worrying that
|
|
|
|
# the transaction will be committed, while still allowing those objects
|
|
|
|
# to work properly with real transactions (i.e., they still call
|
|
|
|
# as they would on a normal Transaction, it just has no effect with a
|
|
|
|
# sub-transaction).
|
|
|
|
# The method that returns a subtransaction should be called "child."
|
|
|
|
def child(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2005-07-18 06:37:32 +02:00
|
|
|
# XXX create, replace, etc. return file objects. This class should keep a
|
|
|
|
# list of such file descriptors and only allow a commit if all of them
|
|
|
|
# are closed. Trying to commit with open file objects should raise an
|
|
|
|
# exception.
|
|
|
|
def create(self, filename):
|
|
|
|
"""
|
|
|
|
Returns a file object for a filename that should be created (with
|
|
|
|
the contents as they were written to the filename) when the transaction
|
|
|
|
is committed.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError # XXX.
|
|
|
|
|
|
|
|
def mkdir(self, filename):
|
|
|
|
raise NotImplementedError # XXX
|
|
|
|
|
|
|
|
def delete(self, filename):
|
|
|
|
raise NotImplementedError # XXX
|
|
|
|
|
2005-07-05 19:48:00 +02:00
|
|
|
def replace(self, filename):
|
2005-07-18 06:37:32 +02:00
|
|
|
"""
|
|
|
|
Returns a file object for a filename that should be replaced by the
|
|
|
|
contents written to the file object when the transaction is committed.
|
|
|
|
"""
|
2005-07-06 05:59:16 +02:00
|
|
|
self._checkCwd()
|
2005-07-05 19:48:00 +02:00
|
|
|
self._makeOriginal(filename)
|
2005-07-06 05:59:16 +02:00
|
|
|
self._journalCommand('replace', filename)
|
2005-07-05 19:48:00 +02:00
|
|
|
return File.open(self._replacement(filename))
|
|
|
|
|
|
|
|
def append(self, filename):
|
2005-07-06 05:59:16 +02:00
|
|
|
self._checkCwd()
|
2005-07-05 19:48:00 +02:00
|
|
|
length = os.stat(filename).st_size
|
2005-07-06 05:59:16 +02:00
|
|
|
self._journalCommand('append', filename, length)
|
2005-07-05 19:48:00 +02:00
|
|
|
replacement = self._replacement(filename)
|
|
|
|
File.copy(filename, replacement)
|
|
|
|
return file(replacement, 'a')
|
|
|
|
|
|
|
|
def commit(self, removeWhenComplete=True):
|
|
|
|
self._journal.close()
|
2005-07-06 05:59:16 +02:00
|
|
|
self._checkCwd()
|
2005-07-05 19:48:00 +02:00
|
|
|
File.touch(self.dirize('commit'))
|
2005-07-06 05:59:16 +02:00
|
|
|
for (command, args) in self._journalCommands():
|
2005-07-05 19:48:00 +02:00
|
|
|
methodName = 'commit%s' % command.capitalize()
|
|
|
|
getattr(self, methodName)(*args)
|
|
|
|
File.touch(self.dirize('committed'))
|
|
|
|
if removeWhenComplete:
|
|
|
|
shutil.rmtree(self.dir)
|
|
|
|
|
|
|
|
def commitReplace(self, filename):
|
|
|
|
shutil.copy(self._replacement(filename), filename)
|
|
|
|
|
|
|
|
def commitAppend(self, filename, length):
|
|
|
|
shutil.copy(self._replacement(filename), filename)
|
|
|
|
|
2005-07-16 07:09:49 +02:00
|
|
|
# XXX need to be able to rename files transactionally. (hard; especially
|
|
|
|
# with renames that depend on one another. It might be easier to do
|
|
|
|
# rename separate from relocate.)
|
|
|
|
|
2005-07-05 19:48:00 +02:00
|
|
|
|
|
|
|
class Rollback(TransactionMixin):
|
|
|
|
def rollback(self, removeWhenComplete=True):
|
2005-07-06 05:59:16 +02:00
|
|
|
self._checkCwd()
|
2005-07-05 19:48:00 +02:00
|
|
|
if not os.path.exists(self.dirize('commit')):
|
|
|
|
return # No action taken; commit hadn't begun.
|
2005-07-06 05:59:16 +02:00
|
|
|
for (command, args) in self._journalCommands():
|
2005-07-05 19:48:00 +02:00
|
|
|
methodName = 'rollback%s' % command.capitalize()
|
|
|
|
getattr(self, methodName)(*args)
|
|
|
|
if removeWhenComplete:
|
|
|
|
shutil.rmtree(self.dir)
|
|
|
|
|
|
|
|
def rollbackReplace(self, filename):
|
|
|
|
shutil.copy(self._original(filename), filename)
|
|
|
|
|
|
|
|
def rollbackAppend(self, filename, length):
|
|
|
|
fd = file(filename, 'a')
|
|
|
|
fd.truncate(int(length))
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=8 expandtab textwidth=78:
|