harlowja / fasteners

A python package that provides useful locks.
Apache License 2.0
248 stars 45 forks source link

Cannot use a relative path to a file in the current directory as a lock #12

Closed ubershmekel closed 8 years ago

ubershmekel commented 8 years ago

The problem is dirname here

    def _do_open(self):
        basedir = os.path.dirname(self.path)

Which causes this exception because basedir is empty.

Traceback (most recent call last):
  File "test_fastener.py", line 19, in <module>
    test()
  File "C:\Python26\lib\site-packages\fasteners\process_lock.py", line 250, in wrapper
    with lock:
  File "C:\Python26\lib\site-packages\fasteners\process_lock.py", line 173, in __enter__
    self.acquire()
  File "C:\Python26\lib\site-packages\fasteners\process_lock.py", line 150, in acquire
    self._do_open()
  File "C:\Python26\lib\site-packages\fasteners\process_lock.py", line 114, in _do_open
    made_basedir = _ensure_tree(basedir)
  File "C:\Python26\lib\site-packages\fasteners\process_lock.py", line 37, in _ensure_tree
    os.makedirs(path)
  File "C:\Python26\lib\os.py", line 157, in makedirs
    mkdir(name, mode)
WindowsError: [Error 3] The system cannot find the path specified: ''

Honestly I'm surprised this library is trying to create the file or directory. It should just fail if the lock is missing.

ubershmekel commented 8 years ago

For reference this was the test code

import time
import os

import fasteners

fpath = 'tmp_lock_file'
open(fpath, 'w').close()
#os.mkdir(fpath)

#@fasteners.process_lock.interprocess_locked(os.path.abspath(fpath))
@fasteners.process_lock.interprocess_locked(fpath)
def test():
    for i in range(5):
        print('I have the lock')
        time.sleep(1)

print('Waiting for lock')
test()
harlowja commented 8 years ago

Seems like we should just call os.path.abspath on all paths to ensure they are absolute, or add an option make_absolute=True to get past this...

ubershmekel commented 8 years ago

Or don't call _ensure_tree when basedir is an empty string.

harlowja commented 8 years ago

Btw https://pypi.python.org/pypi/fasteners/0.14.1 use that and the fix is in there :)

ubershmekel commented 8 years ago

Cool. Thank you :)