__main__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 os
  16. import platform
  17. import sys
  18. import click
  19. from pioinstaller import __title__, __version__, core, exception, util
  20. from pioinstaller.pack import packer
  21. from pioinstaller.python import check as python_check
  22. log = logging.getLogger(__name__)
  23. @click.group(name="main", invoke_without_command=True)
  24. @click.version_option(__version__, prog_name=__title__)
  25. @click.option("--verbose", is_flag=True, default=False, help="Verbose output")
  26. @click.option("--shutdown-piohome/--no-shutdown-piohome", is_flag=True, default=True)
  27. @click.option("--dev", is_flag=True, default=False)
  28. @click.option(
  29. "--ignore-python",
  30. multiple=True,
  31. help="A path to Python to be ignored (multiple options and unix wildcards are allowed)",
  32. )
  33. @click.pass_context
  34. def cli(
  35. ctx, verbose, shutdown_piohome, dev, ignore_python
  36. ): # pylint:disable=too-many-arguments
  37. if verbose:
  38. logging.getLogger("pioinstaller").setLevel(logging.DEBUG)
  39. ctx.obj["dev"] = dev
  40. if not ctx.invoked_subcommand:
  41. click.echo("Installer version: %s" % __version__)
  42. click.echo("Platform: %s" % platform.platform(terse=True))
  43. click.echo("Python version: %s" % sys.version)
  44. click.echo("Python path: %s" % sys.executable)
  45. try:
  46. core.install_platformio_core(shutdown_piohome, dev, ignore_python)
  47. except exception.PIOInstallerException as e:
  48. raise click.ClickException(str(e))
  49. @cli.command()
  50. @click.argument(
  51. "target",
  52. default=os.getcwd,
  53. required=False,
  54. type=click.Path(
  55. exists=False, file_okay=True, dir_okay=True, writable=True, resolve_path=True
  56. ),
  57. )
  58. def pack(target):
  59. return packer.pack(target)
  60. @cli.group()
  61. def check():
  62. pass
  63. @check.command()
  64. def python():
  65. try:
  66. python_check()
  67. click.secho(
  68. "The Python %s (%s) interpreter is compatible."
  69. % (platform.python_version(), util.get_pythonexe_path()),
  70. fg="green",
  71. )
  72. except (exception.IncompatiblePythonError, exception.DistutilsNotFound) as e:
  73. raise click.ClickException(
  74. "The Python %s (%s) interpreter is not compatible.\nReason: %s"
  75. % (platform.python_version(), util.get_pythonexe_path(), str(e))
  76. )
  77. @check.command("core")
  78. @click.option("--auto-upgrade/--no-auto-upgrade", is_flag=True, default=True)
  79. @click.option("--version-spec", default=None)
  80. @click.option(
  81. "--dump-state",
  82. type=click.Path(
  83. exists=False, file_okay=True, dir_okay=True, writable=True, resolve_path=True
  84. ),
  85. )
  86. @click.pass_context
  87. def core_check(ctx, auto_upgrade, version_spec, dump_state):
  88. try:
  89. state = core.check(
  90. dev=ctx.obj.get("dev", False),
  91. auto_upgrade=auto_upgrade,
  92. version_spec=version_spec,
  93. )
  94. if dump_state:
  95. core.dump_state(target=str(dump_state), state=state)
  96. click.secho(
  97. "Found compatible PlatformIO Core %s -> %s"
  98. % (state.get("core_version"), state.get("platformio_exe")),
  99. fg="green",
  100. )
  101. except (exception.InvalidPlatformIOCore) as e:
  102. raise click.ClickException(
  103. "Compatible PlatformIO Core not found.\nReason: %s" % str(e)
  104. )
  105. def main():
  106. return cli(obj={}) # pylint: disable=no-value-for-parameter, unexpected-keyword-arg
  107. if __name__ == "__main__":
  108. sys.exit(main())