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 regels
803 B

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import os
  5. from time import time
  6. from webnotes.utils import get_site_path, touch_file
  7. class LockTimeoutError(Exception):
  8. pass
  9. def create_lock(name):
  10. lock_path = get_lock_path(name)
  11. if not check_lock(lock_path):
  12. return touch_file(lock_path)
  13. else:
  14. return False
  15. def check_lock(path):
  16. if not os.path.exists(path):
  17. return False
  18. if time() - os.path.getmtime(path) > 600:
  19. raise LockTimeoutError(path)
  20. return True
  21. def delete_lock(name):
  22. lock_path = get_lock_path(name)
  23. try:
  24. os.remove(lock_path)
  25. except OSError:
  26. pass
  27. return True
  28. def get_lock_path(name):
  29. name = name.lower()
  30. lock_path = get_site_path(name + '.lock')
  31. return lock_path