mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-18 23:10:14 +01:00
fasteners' InterProcessLock doesn't work within one process, thus it is not suitable for our dvc library. Signed-off-by: Ruslan Kuprieiev <[email protected]>
48 lines
984 B
Python
48 lines
984 B
Python
import os
|
|
import time
|
|
import zc.lockfile
|
|
|
|
from dvc.exceptions import DvcException
|
|
|
|
|
|
class LockError(DvcException):
|
|
pass
|
|
|
|
|
|
class Lock(object):
|
|
LOCK_FILE = 'lock'
|
|
TIMEOUT = 5
|
|
|
|
def __init__(self, dvc_dir):
|
|
self.lock_file = os.path.join(dvc_dir, self.LOCK_FILE)
|
|
self._lock = None
|
|
|
|
@staticmethod
|
|
def init(dvc_dir):
|
|
return Lock(dvc_dir)
|
|
|
|
def _do_lock(self):
|
|
try:
|
|
self._lock = zc.lockfile.LockFile(self.lock_file)
|
|
except zc.lockfile.LockError:
|
|
raise LockError('Cannot perform the cmd since DVC is busy and locked. Please retry the cmd later.')
|
|
|
|
def lock(self):
|
|
try:
|
|
self._do_lock()
|
|
return
|
|
except LockError:
|
|
time.sleep(self.TIMEOUT)
|
|
|
|
self._do_lock()
|
|
|
|
def unlock(self):
|
|
self._lock.close()
|
|
self._lock = None
|
|
|
|
def __enter__(self):
|
|
self.lock()
|
|
|
|
def __exit__(self, type, value, tb):
|
|
self.unlock()
|