startup_check.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import subprocess
  2. import sys
  3. import os
  4. def check_git_exists():
  5. try:
  6. # Check if Git is installed
  7. subprocess.call(["git", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  8. except OSError:
  9. return False
  10. return True
  11. def install_git():
  12. if sys.version_info[0] == 2:
  13. version_cmd = subprocess.call
  14. else:
  15. version_cmd = subprocess.run
  16. # Install Git based on the operating system type
  17. system = sys.platform.lower()
  18. if "linux" in system:
  19. version_cmd(["sudo", "apt-get", "install", "git"])
  20. elif "darwin" in system:
  21. version_cmd(["brew", "install", "git"])
  22. elif "win" in system:
  23. print("Please manually install Git and ensure it is added to the system PATH.")
  24. sys.exit(1)
  25. def check_file_changes(filepath):
  26. # Use Git to check the file status
  27. result = subprocess.Popen(["git", "status", "--porcelain", filepath], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  28. out, _ = result.communicate()
  29. # Return True if the file has changes
  30. return bool(out.decode('utf-8'))
  31. def revert_to_original(filepath):
  32. # Use Git to revert the file to its original state
  33. subprocess.call(["git", "checkout", filepath])
  34. def startup_check():
  35. file_path = os.getcwd() + "/ra/fsp/src/bsp/cmsis/Device/RENESAS/Source/startup.c"
  36. python_executable = 'python' if sys.version_info[0] == 2 else 'python3'
  37. # Check if Git is installed, if not, try to install it
  38. if not check_git_exists():
  39. print("Git not detected, attempting to install...")
  40. install_git()
  41. # Check if Git is installed after the installation attempt
  42. if not check_git_exists():
  43. print("Git installation failed. Please manually install Git and add it to the system PATH.")
  44. sys.exit(1)
  45. # Check if the file has changes
  46. if check_file_changes(file_path):
  47. # If changes are detected, revert the file to its original state
  48. revert_to_original(file_path)
  49. # else:
  50. # print "File {file_path} is unchanged."
  51. if __name__ == "__main__":
  52. startup_check()