Commit 9012188f authored by SVN-Git Migration's avatar SVN-Git Migration

Imported Upstream version 0.4~ds0

parent 8159a6ee
from portalocker import *
from utils import *
from portalocker import lock, unlock, LOCK_EX, LOCK_SH, LOCK_NB, LockException
from utils import Lock, AlreadyLocked
__all__ = [
'lock',
'unlock',
'LOCK_EX',
'LOCK_SH',
'LOCK_NB',
'LockException',
'Lock',
'AlreadyLocked',
]
......@@ -61,11 +61,12 @@ __all__ = [
import os
class LockException(Exception):
# Error codes:
LOCK_FAILED = 1
if os.name == 'nt':
if os.name == 'nt': # pragma: no cover
import win32con
import win32file
import pywintypes
......@@ -79,37 +80,43 @@ elif os.name == 'posix':
LOCK_EX = fcntl.LOCK_EX
LOCK_SH = fcntl.LOCK_SH
LOCK_NB = fcntl.LOCK_NB
else:
raise RuntimeError, 'PortaLocker only defined for nt and posix platforms'
else: # pragma: no cover
raise RuntimeError('PortaLocker only defined for nt and posix platforms')
if os.name == 'nt':
def lock(file, flags):
def nt_lock(file, flags): # pragma: no cover
hfile = win32file._get_osfhandle(file.fileno())
try:
win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped)
except pywintypes.error, exc_value:
# error: (33, 'LockFileEx', 'The process cannot access the file because another process has locked a portion of the file.')
# error: (33, 'LockFileEx', 'The process cannot access the file
# because another process has locked a portion of the file.')
if exc_value[0] == 33:
raise LockException(LockException.LOCK_FAILED, exc_value[2])
else:
# Q: Are there exceptions/codes we should be dealing with here?
# Q: Are there exceptions/codes we should be dealing with
# here?
raise
def unlock(file):
def nt_unlock(file): # pragma: no cover
hfile = win32file._get_osfhandle(file.fileno())
try:
win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped)
except pywintypes.error, exc_value:
if exc_value[0] == 158:
# error: (158, 'UnlockFileEx', 'The segment is already unlocked.')
# To match the 'posix' implementation, silently ignore this error
# error: (158, 'UnlockFileEx', 'The segment is already '
# 'unlocked.')
# To match the 'posix' implementation, silently ignore this
# error
pass
else:
# Q: Are there exceptions/codes we should be dealing with here?
# Q: Are there exceptions/codes we should be dealing with
# here?
raise
elif os.name == 'posix':
def lock(file, flags):
def posix_lock(file, flags):
try:
fcntl.flock(file.fileno(), flags)
except IOError, exc_value:
......@@ -117,12 +124,20 @@ elif os.name == 'posix':
# every IO error
raise LockException(*exc_value)
def unlock(file):
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
def posix_unlock(file):
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
if os.name == 'nt': # pragma: no cover
lock = nt_lock
unlock = nt_unlock
elif os.name == 'posix':
lock = posix_lock
unlock = posix_unlock
else: # pragma: no cover
raise RuntimeError('Your os %r is unsupported.' % os.name)
if __name__ == '__main__':
if __name__ == '__main__': # pragma: no cover
from time import time, strftime, localtime
import sys
import portalocker
......@@ -131,7 +146,7 @@ if __name__ == '__main__':
portalocker.lock(log, portalocker.LOCK_EX)
timestamp = strftime('%m/%d/%Y %H:%M:%S\n', localtime(time()))
log.write( timestamp )
log.write(timestamp)
print 'Wrote lines. Hit enter to release lock.'
dummy = sys.stdin.readline()
......
......@@ -11,19 +11,16 @@ __all__ = [
'AlreadyLocked',
]
class AlreadyLocked(Exception):
pass
class Lock(object):
def __init__(
self,
filename,
mode='a',
truncate=0,
timeout=DEFAULT_TIMEOUT,
check_interval=DEFAULT_CHECK_INTERVAL,
fail_when_locked=True,
):
self, filename, mode='a', truncate=0, timeout=DEFAULT_TIMEOUT,
check_interval=DEFAULT_CHECK_INTERVAL, fail_when_locked=True):
'''Lock manager with build-in timeout
filename -- filename
......@@ -53,7 +50,8 @@ class Lock(object):
assert 'w' not in mode, 'Mode "w" clears the file before locking'
def acquire(self, timeout=None, check_interval=None, fail_when_locked=None):
def acquire(
self, timeout=None, check_interval=None, fail_when_locked=None):
'''Acquire the locked filehandle'''
if timeout is None:
timeout = self.timeout
......@@ -83,15 +81,17 @@ class Lock(object):
# Try again
try:
fh = self._get_lock(fh)
# We've got the lock, now return an error if
# fail_when_locked is True or break if not
# We already tried to the get the lock
# If fail_when_locked is true, then stop trying
if fail_when_locked:
self._release_lock()
raise AlreadyLocked(*exception)
else:
else: # pragma: no cover
# We've got the lock
fh = self._get_lock(fh)
break
except portalocker.LockException:
pass
......@@ -140,4 +140,3 @@ class Lock(object):
def __exit__(self, type, value, tb):
if self.fh:
self.fh.close()
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand
import sys
author = 'Rick van Hattem'
email = 'Rick.van.Hattem@Fawo.nl'
version = '0.4'
desc = '''Wraps the portalocker recipe for easy usage'''
extra = {}
if sys.version_info >= (3, 0):
extra.update(use_2to3=True)
try:
from setuptools import setup, find_packages
except ImportError:
from distutils.core import setup, find_packages
author = 'Rick van Hattem'
email = 'Rick.van.Hattem@Fawo.nl'
version = '0.3'
desc = '''Wraps the portalocker recipe for easy usage'''
class PyTest(TestCommand):
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = ['tests']
self.test_suite = True
def run_tests(self):
#import here, cause outside the eggs aren't loaded
import pytest
errno = pytest.main(self.test_args)
sys.exit(errno)
setup(name='portalocker',
setup(
name='portalocker',
version=version,
description=desc,
long_description=open('README.rest').read(),
......@@ -35,7 +47,6 @@ setup(name='portalocker',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
zip_safe=False,
platforms=['any'],
test_suite='nose.collector',
cmdclass={'test': PyTest},
**extra
)
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