* Remove six for PY2 compatability since our dependencies are not, PY2 is legacy. * Removed usages of utils from future/past libraries since they are deprecated. This includes 'from __future__ ...' and 'from past...' statements. * Removed compatibility imports for PY2, switched from six imports to standard library imports. * Removed utils code blocks that handle operations depending on PY2/3 versions. * Removed 'from __future__ ...' lines from templates/code generators * Used PY3 syntaxes in place of PY2 compatible blocks. eg: metaclass
46 lines
1,023 B
Python
46 lines
1,023 B
Python
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
|
# MIT License. See license.txt
|
|
|
|
'''
|
|
File based locking utility
|
|
'''
|
|
|
|
import os
|
|
from time import time
|
|
from frappe.utils import get_site_path, touch_file
|
|
|
|
class LockTimeoutError(Exception):
|
|
pass
|
|
|
|
def create_lock(name):
|
|
'''Creates a file in the /locks folder by the given name'''
|
|
lock_path = get_lock_path(name)
|
|
if not check_lock(lock_path):
|
|
return touch_file(lock_path)
|
|
else:
|
|
return False
|
|
|
|
def lock_exists(name):
|
|
'''Returns True if lock of the given name exists'''
|
|
return os.path.exists(get_lock_path(name))
|
|
|
|
def check_lock(path, timeout=600):
|
|
if not os.path.exists(path):
|
|
return False
|
|
if time() - os.path.getmtime(path) > timeout:
|
|
raise LockTimeoutError(path)
|
|
return True
|
|
|
|
def delete_lock(name):
|
|
lock_path = get_lock_path(name)
|
|
try:
|
|
os.remove(lock_path)
|
|
except OSError:
|
|
pass
|
|
return True
|
|
|
|
def get_lock_path(name):
|
|
name = name.lower()
|
|
locks_dir = 'locks'
|
|
lock_path = get_site_path(locks_dir, name + '.lock')
|
|
return lock_path
|