checkout.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from dulwich import porcelain
  2. from dulwich import repo
  3. import os
  4. def is_file_locked(file_path):
  5. try:
  6. with open(file_path, 'r'):
  7. return False
  8. except IOError:
  9. return True
  10. def close_file(file_object):
  11. file_object.close()
  12. def release_file_resource(file_path):
  13. file_descriptor = os.open(file_path, os.O_RDONLY)
  14. os.close(file_descriptor)
  15. def unlock_file(file_path):
  16. if is_file_locked(file_path):
  17. with open(file_path, 'r') as file:
  18. close_file(file)
  19. release_file_resource(file_path)
  20. def ro():
  21. os.system("/etc/init.d/S01mount_ro start")
  22. def rw():
  23. os.system("/etc/init.d/S01mount_ro stop")
  24. def checkout(rep, branch):
  25. r = repo.Repo(rep)
  26. try:
  27. porcelain.checkout_branch(r, 'origin/' + branch)
  28. except porcelain.CheckoutError:
  29. print("Error")
  30. porcelain.reset(r, 'hard')
  31. porcelain.checkout_branch(r, 'origin/' + branch)
  32. localrep = "/home/"
  33. unlockfile = localrep + "cclb_launch"
  34. rw()
  35. unlock_file(unlockfile)
  36. if porcelain.active_branch(localrep) != b'master':
  37. checkout(localrep, 'master')
  38. else:
  39. checkout(localrep, 'en')
  40. ro()