home.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2014-present PlatformIO <contact@platformio.org>
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import multiprocessing
  16. import subprocess
  17. import requests
  18. from pioinstaller import exception
  19. HTTP_HOST = "127.0.0.1"
  20. HTTP_PORT_BEGIN = 8008
  21. HTTP_PORT_END = 8050
  22. log = logging.getLogger(__name__)
  23. def _shutdown():
  24. for port in range(HTTP_PORT_BEGIN, HTTP_PORT_END):
  25. try:
  26. requests.get(
  27. "http://%s:%d?__shutdown__=1" % (HTTP_HOST, port), timeout=(0.5, 2)
  28. )
  29. log.debug("The server %s:%d is stopped", HTTP_HOST, port)
  30. except: # pylint:disable=bare-except
  31. pass
  32. def shutdown_pio_home_servers():
  33. proc = multiprocessing.Process(target=_shutdown)
  34. proc.start()
  35. proc.join(10)
  36. proc.terminate()
  37. return True
  38. def install_pio_home(platformio_exe):
  39. try:
  40. subprocess.check_output(
  41. [platformio_exe, "home", "--host", "__do_not_start__"],
  42. stderr=subprocess.PIPE,
  43. )
  44. return True
  45. except Exception as e: # pylint:disable=broad-except
  46. raise exception.PIOInstallerException("Could not install PIO Home: %s" % str(e))