startup_check.py 2.0 KB

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