25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

40 lines
724 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. pre
  8. lock_path = get_lock_path(name)
  9. if not check_lock(lock_path):
  10. return touch_file(lock_path)
  11. else:
  12. return False
  13. def touch_file(path):
  14. with open(path, 'a'):
  15. os.utime(path, None)
  16. return True
  17. def check_lock(path):
  18. if not os.path.exists(path):
  19. return False
  20. if time() - os.path.mtime(path) > 600:
  21. raise LockTimeoutError(path)
  22. return True
  23. def delete_lock(name):
  24. lock_path = get_lock_path(name)
  25. try:
  26. os.remove(path)
  27. except OSError:
  28. pass
  29. return True
  30. def get_lock_path(name):
  31. name = name.lower()
  32. lock_path = get_site_path(name + '.lock')
  33. return lock_path