Here are the examples of the python api twisted.python.lockfile.FilesystemLock taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
13 Examples
3
Example 1
Project: mythbox Source File: test_lockfile.py
def test_noKillCall(self):
"""
Verify that when L{lockfile.kill} does end up as None (e.g. on Windows
without pywin32), it doesn't end up being called and raising a
L{TypeError}.
"""
self.patch(lockfile, "kill", None)
fl = lockfile.FilesystemLock(self.mktemp())
fl.lock()
self.assertFalse(fl.lock())
3
Example 2
Project: mythbox Source File: test_lockfile.py
def _symlinkErrorTest(self, errno):
def fakeSymlink(source, dest):
raise OSError(errno, None)
self.patch(lockfile, 'symlink', fakeSymlink)
lockf = self.mktemp()
lock = lockfile.FilesystemLock(lockf)
exc = self.assertRaises(OSError, lock.lock)
self.assertEqual(exc.errno, errno)
3
Example 3
Project: mythbox Source File: test_lockfile.py
def test_cleanlyAcquire(self):
"""
If the lock has never been held, it can be acquired and the C{clean}
and C{locked} attributes are set to C{True}.
"""
lockf = self.mktemp()
lock = lockfile.FilesystemLock(lockf)
self.assertTrue(lock.lock())
self.assertTrue(lock.clean)
self.assertTrue(lock.locked)
3
Example 4
Project: mythbox Source File: test_lockfile.py
def test_cleanlyRelease(self):
"""
If a lock is released cleanly, it can be re-acquired and the C{clean}
and C{locked} attributes are set to C{True}.
"""
lockf = self.mktemp()
lock = lockfile.FilesystemLock(lockf)
self.assertTrue(lock.lock())
lock.unlock()
self.assertFalse(lock.locked)
lock = lockfile.FilesystemLock(lockf)
self.assertTrue(lock.lock())
self.assertTrue(lock.clean)
self.assertTrue(lock.locked)
3
Example 5
Project: mythbox Source File: test_lockfile.py
def test_cannotLockLocked(self):
"""
If a lock is currently locked, it cannot be locked again.
"""
lockf = self.mktemp()
firstLock = lockfile.FilesystemLock(lockf)
self.assertTrue(firstLock.lock())
secondLock = lockfile.FilesystemLock(lockf)
self.assertFalse(secondLock.lock())
self.assertFalse(secondLock.locked)
3
Example 6
Project: mythbox Source File: test_lockfile.py
def test_lockReleasedDuringAcquireReadlink(self):
"""
If the lock is initially held but is released while an attempt
is made to acquire it, the lock attempt fails and
L{FilesystemLock.lock} returns C{False}.
"""
def fakeReadlink(name):
# While another process is doing os.rmdir which the
# Windows implementation of rmlink does, a readlink call
# will fail with EACCES.
raise IOError(errno.EACCES, None)
readlinkPatch = self.patch(lockfile, 'readlink', fakeReadlink)
lockf = self.mktemp()
lock = lockfile.FilesystemLock(lockf)
lockfile.symlink(str(43125), lockf)
self.assertFalse(lock.lock())
self.assertFalse(lock.locked)
3
Example 7
Project: Comet Source File: eventwriter.py
@contextmanager
def event_file(ivorn, dirname=None):
# Return a file object into which we can write an event.
# If a directory is specified, write into that; otherwise, use the cwd.
# We use a lock to ensure we don't clobber other files with the same name.
if not dirname:
dirname=os.getcwd()
fname = os.path.join(dirname, string_to_filename(ivorn))
lock = lockfile.FilesystemLock(string_to_filename(ivorn) + "-lock")
lock.lock()
try:
while os.path.exists(fname):
fname += "."
with open(fname, 'w') as f:
yield f
finally:
lock.unlock()
0
Example 8
Project: mythbox Source File: unix.py
def startListening(self):
"""Create and bind my socket, and begin listening on it.
This is called on unserialization, and must be called after creating a
server to begin listening on the specified port.
"""
log.msg("%s starting on %r" % (self.factory.__class__, repr(self.port)))
if self.wantPID:
self.lockFile = lockfile.FilesystemLock(self.port + ".lock")
if not self.lockFile.lock():
raise CannotListenError, (None, self.port, "Cannot acquire lock")
else:
if not self.lockFile.clean:
try:
# This is a best-attempt at cleaning up
# left-over unix sockets on the filesystem.
# If it fails, there's not much else we can
# do. The bind() below will fail with an
# exception that actually propegates.
if stat.S_ISSOCK(os.stat(self.port).st_mode):
os.remove(self.port)
except:
pass
self.factory.doStart()
try:
skt = self.createInternetSocket()
skt.bind(self.port)
except socket.error, le:
raise CannotListenError, (None, self.port, le)
else:
if _inFilesystemNamespace(self.port):
# Make the socket readable and writable to the world.
os.chmod(self.port, self.mode)
skt.listen(self.backlog)
self.connected = True
self.socket = skt
self.fileno = self.socket.fileno
self.numberAccepts = 100
self.startReading()
0
Example 9
Project: mythbox Source File: test_lockfile.py
def test_lockReleasedDuringAcquireSymlink(self):
"""
If the lock is released while an attempt is made to acquire
it, the lock attempt fails and C{FilesystemLock.lock} returns
C{False}. This can happen on Windows when L{lockfile.symlink}
fails with L{IOError} of C{EIO} because another process is in
the middle of a call to L{os.rmdir} (implemented in terms of
RemoveDirectory) which is not atomic.
"""
def fakeSymlink(src, dst):
# While another process id doing os.rmdir which the Windows
# implementation of rmlink does, a rename call will fail with EIO.
raise OSError(errno.EIO, None)
self.patch(lockfile, 'symlink', fakeSymlink)
lockf = self.mktemp()
lock = lockfile.FilesystemLock(lockf)
self.assertFalse(lock.lock())
self.assertFalse(lock.locked)
0
Example 10
def _setUpTestdir(self):
self._tearDownLogFile()
currentDir = os.getcwd()
base = filepath.FilePath(self.workingDirectory)
counter = 0
while True:
if counter:
testdir = base.sibling('%s-%d' % (base.basename(), counter))
else:
testdir = base
self._testDirLock = FilesystemLock(testdir.path + '.lock')
if self._testDirLock.lock():
# It is not in use
if testdir.exists():
# It exists though - delete it
self._removeSafely(testdir)
break
else:
# It is in use
if self.workingDirectory == '_trial_temp':
counter += 1
else:
raise _WorkingDirectoryBusy()
testdir.makedirs()
os.chdir(testdir.path)
file('_trial_marker', 'w').close()
return currentDir
0
Example 11
@staticmethod
def copyPackage(title):
"""
Copy package directory to db path using a file lock to avoid potential
concurrency race conditions.
@param title: string to use in log entry
@type title: C{str}
"""
dbpath = FilePath(TimezoneCache.getDBPath())
pkgpath = TimezoneCache.FilteredFilePath(TimezoneCache._getPackageDBPath())
lockfile = FilesystemLock(dbpath.path + ".lock")
result = lockfile.lock()
try:
if result and not dbpath.exists():
log.info(
"{title} timezones from {pkg} to {to}",
title=title,
pkg=pkgpath.path,
to=dbpath.path
)
# Copy over the entire package
pkgpath.copyFilteredDirectoryTo(dbpath)
finally:
if result:
lockfile.unlock()
0
Example 12
Project: SubliminalCollaborator Source File: unix.py
def startListening(self):
"""
Create and bind my socket, and begin listening on it.
This is called on unserialization, and must be called after creating a
server to begin listening on the specified port.
"""
log.msg("%s starting on %r" % (
self._getLogPrefix(self.factory), self.port))
if self.wantPID:
self.lockFile = lockfile.FilesystemLock(self.port + ".lock")
if not self.lockFile.lock():
raise CannotListenError, (None, self.port, "Cannot acquire lock")
else:
if not self.lockFile.clean:
try:
# This is a best-attempt at cleaning up
# left-over unix sockets on the filesystem.
# If it fails, there's not much else we can
# do. The bind() below will fail with an
# exception that actually propagates.
if stat.S_ISSOCK(os.stat(self.port).st_mode):
os.remove(self.port)
except:
pass
self.factory.doStart()
try:
skt = self.createInternetSocket()
skt.bind(self.port)
except socket.error, le:
raise CannotListenError, (None, self.port, le)
else:
if _inFilesystemNamespace(self.port):
# Make the socket readable and writable to the world.
os.chmod(self.port, self.mode)
skt.listen(self.backlog)
self.connected = True
self.socket = skt
self.fileno = self.socket.fileno
self.numberAccepts = 100
self.startReading()
0
Example 13
Project: SubliminalCollaborator Source File: util.py
def _unusedTestDirectory(base):
"""
Find an unused directory named similarly to C{base}.
Once a directory is found, it will be locked and a marker dropped into it to
identify it as a trial temporary directory.
@param base: A template path for the discovery process. If this path
exactly cannot be used, a path which varies only in a suffix of the
basename will be used instead.
@type base: L{FilePath}
@return: A two-tuple. The first element is a L{FilePath} representing the
directory which was found and created. The second element is a locked
L{FilesystemLock}. Another call to C{_unusedTestDirectory} will not be
able to reused the the same name until the lock is released, either
explicitly or by this process exiting.
"""
counter = 0
while True:
if counter:
testdir = base.sibling('%s-%d' % (base.basename(), counter))
else:
testdir = base
testDirLock = FilesystemLock(testdir.path + '.lock')
if testDirLock.lock():
# It is not in use
if testdir.exists():
# It exists though - delete it
_removeSafely(testdir)
# Create it anew and mark it as ours so the next _removeSafely on it
# succeeds.
testdir.makedirs()
testdir.child('_trial_marker').setContent('')
return testdir, testDirLock
else:
# It is in use
if base.basename() == '_trial_temp':
counter += 1
else:
raise _WorkingDirectoryBusy()