Skip to content

Commit

Permalink
Merge branch 'release/v1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Jan 14, 2017
2 parents f419004 + 26998fa commit 19692d8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion portalocker/__about__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__package_name__ = 'portalocker'
__author__ = 'Rick van Hattem'
__email__ = '[email protected]'
__version__ = '1.0.0'
__version__ = '1.0.1'
__description__ = '''Wraps the portalocker recipe for easy usage'''
__url__ = 'https://github.com/WoLpH/portalocker'

1 change: 1 addition & 0 deletions portalocker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#: Locking utility class to automatically handle opening with timeouts and
#: context wrappers
Lock = utils.Lock
TemporaryFileLock = utils.TemporaryFileLock
open_atomic = utils.open_atomic

__all__ = [
Expand Down
23 changes: 22 additions & 1 deletion portalocker/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
import atexit
import tempfile
import contextlib
from . import exceptions
Expand Down Expand Up @@ -64,7 +65,7 @@ class Lock(object):

def __init__(
self, filename, mode='a', timeout=DEFAULT_TIMEOUT,
check_interval=DEFAULT_CHECK_INTERVAL, fail_when_locked=True,
check_interval=DEFAULT_CHECK_INTERVAL, fail_when_locked=False,
flags=LOCK_METHOD):
'''Lock manager with build-in timeout
Expand Down Expand Up @@ -193,3 +194,23 @@ def __enter__(self):

def __exit__(self, type_, value, tb):
self.release()

def __delete__(self, instance): # pragma: no cover
instance.release()


class TemporaryFileLock(Lock):

def __init__(self, filename='.lock', timeout=DEFAULT_TIMEOUT,
check_interval=DEFAULT_CHECK_INTERVAL, fail_when_locked=True,
flags=LOCK_METHOD):

Lock.__init__(self, filename=filename, mode='w', timeout=timeout,
check_interval=check_interval,
fail_when_locked=fail_when_locked, flags=flags)
atexit.register(self.release)

def release(self):
Lock.release(self)
if os.path.isfile(self.filename): # pragma: no branch
os.unlink(self.filename)
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import py
import pytest


@pytest.fixture
def tmpfile(tmpdir_factory):
tmpdir = tmpdir_factory.mktemp('temp')
filename = tmpdir.join('tmpfile')
yield str(filename)
try:
filename.remove(ignore_errors=True)
except (py.error.EBUSY, py.error.ENOENT):
pass

14 changes: 14 additions & 0 deletions tests/temporary_file_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import portalocker


def test_temporary_file_lock(tmpfile):
with portalocker.TemporaryFileLock(tmpfile):
pass

assert not os.path.isfile(tmpfile)

lock = portalocker.TemporaryFileLock(tmpfile)
lock.acquire()
del lock

12 changes: 0 additions & 12 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
from __future__ import print_function
from __future__ import with_statement

import py
import pytest
import portalocker


@pytest.fixture
def tmpfile(tmpdir_factory):
tmpdir = tmpdir_factory.mktemp('temp')
filename = tmpdir.join('tmpfile')
yield str(filename)
try:
filename.remove(ignore_errors=True)
except py.error.EBUSY:
pass


def test_exceptions(tmpfile):
# Open the file 2 times
a = open(tmpfile, 'a')
Expand Down

0 comments on commit 19692d8

Please sign in to comment.