You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

39 lines
727 B

  1. import os
  2. from time import time
  3. from webnotes.utils import get_site_path
  4. class LockTimeoutError(Exception):
  5. pass
  6. def create_lock(name):
  7. lock_path = get_lock_path(name)
  8. if not check_lock(lock_path):
  9. return touch_file(lock_path)
  10. else:
  11. return False
  12. def touch_file(path):
  13. with open(path, 'a'):
  14. os.utime(path, None)
  15. return True
  16. def check_lock(path):
  17. if not os.path.exists(path):
  18. return False
  19. if time() - os.path.getmtime(path) > 600:
  20. raise LockTimeoutError(path)
  21. return True
  22. def delete_lock(name):
  23. lock_path = get_lock_path(name)
  24. try:
  25. os.remove(lock_path)
  26. except OSError:
  27. pass
  28. return True
  29. def get_lock_path(name):
  30. name = name.lower()
  31. lock_path = get_site_path(name + '.lock')
  32. return lock_path