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.

108 lines
3.8 KiB

  1. """
  2. Adafruit Raspberry Pi Blinka Setup Script
  3. (C) Adafruit Industries, Creative Commons 3.0 - Attribution Share Alike
  4. """
  5. try:
  6. from adafruit_shell import Shell
  7. except ImportError:
  8. raise RuntimeError("The library 'adafruit_shell' was not found. To install, try typing: sudo pip3 install adafruit-python-shell")
  9. import os
  10. shell = Shell()
  11. shell.group="Blinka"
  12. default_python = 3
  13. def default_python_version(numeric=True):
  14. version = shell.run_command("python -c 'import platform; print(platform.python_version())'", suppress_message=True, return_output=True)
  15. if numeric:
  16. return float(version[0:version.rfind(".")])
  17. return version
  18. def sys_update():
  19. print("Updating System Packages")
  20. if not shell.run_command('sudo apt update', True):
  21. shell.bail("Apt failed to update indexes!")
  22. if not shell.run_command('sudo apt-get update', True):
  23. shell.bail("Apt failed to update indexes!")
  24. print("Upgrading packages...")
  25. if not shell.run_command("sudo apt-get upgrade"):
  26. shell.bail("Apt failed to install software!")
  27. def set_raspiconfig():
  28. """
  29. Enable various Raspberry Pi interfaces
  30. """
  31. print("Enabling I2C")
  32. shell.run_command("sudo raspi-config nonint do_i2c 0")
  33. print("Enabling SPI")
  34. shell.run_command("sudo raspi-config nonint do_spi 0")
  35. print("Enabling Serial")
  36. shell.run_command("sudo raspi-config nonint do_serial 0")
  37. print("Enabling SSH")
  38. shell.run_command("sudo raspi-config nonint do_ssh 0")
  39. print("Enabling Camera")
  40. shell.run_command("sudo raspi-config nonint do_camera 0")
  41. print("Disable raspi-config at Boot")
  42. shell.run_command("sudo raspi-config nonint disable_raspi_config_at_boot 0")
  43. def update_python():
  44. print("Making sure Python 3 is the default")
  45. if default_python < 3:
  46. shell.run_command("sudo apt-get install -y python3 git python3-pip")
  47. shell.run_command("sudo update-alternatives --install /usr/bin/python python $(which python2) 1")
  48. shell.run_command("sudo update-alternatives --install /usr/bin/python python $(which python3) 2")
  49. shell.run_command("sudo update-alternatives --skip-auto --config python")
  50. def update_pip():
  51. print("Making sure PIP is installed")
  52. shell.run_command("sudo apt-get install -y python3-pip")
  53. shell.run_command("sudo pip3 install --upgrade setuptools")
  54. def install_blinka():
  55. print("Installing latest version of Blinka locally")
  56. shell.run_command("pip3 install --upgrade adafruit-blinka")
  57. def main():
  58. global default_python
  59. shell.clear()
  60. # Check Raspberry Pi and Bail
  61. pi_model = shell.get_board_model()
  62. print("""This script configures your
  63. Raspberry Pi and installs Blinka
  64. """)
  65. print("{} detected.\n".format(pi_model))
  66. if not shell.is_raspberry_pi():
  67. shell.bail("Non-Raspberry Pi board detected. This must be run on a Raspberry Pi")
  68. if shell.get_os() != "Raspbian":
  69. shell.bail("Sorry. This script currently only runs on Raspberry Pi OS.")
  70. if not shell.is_python3():
  71. shell.bail("You must be running Python 3. Older versions have now been deprecated.")
  72. if default_python_version() < 3:
  73. shell.warn("WARNING Default System python version is {}. It will be updated to Version 3.".format(default_python_version(False)))
  74. default_python = 2
  75. if not shell.prompt("Continue?"):
  76. shell.exit()
  77. sys_update()
  78. set_raspiconfig()
  79. update_python()
  80. update_pip()
  81. install_blinka()
  82. # Done
  83. print("""DONE.
  84. Settings take effect on next boot.
  85. """)
  86. if not shell.prompt("REBOOT NOW?", default="y"):
  87. print("Exiting without reboot.")
  88. shell.exit()
  89. print("Reboot started...")
  90. os.sync()
  91. shell.reboot()
  92. shell.exit()
  93. # Main function
  94. if __name__ == "__main__":
  95. shell.require_root()
  96. main()