template.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. # pylint:disable=bad-option-value,import-outside-toplevel
  15. import os
  16. import shutil
  17. import sys
  18. import tempfile
  19. from base64 import b64decode
  20. DEPENDENCIES = b"""
  21. {zipfile_content}
  22. """
  23. def create_temp_dir():
  24. try:
  25. cur_dir = os.path.dirname(os.path.realpath(__file__))
  26. tmp_dir = tempfile.mkdtemp(dir=cur_dir, prefix=".piocore-installer-")
  27. testscript_path = os.path.join(tmp_dir, "test.py")
  28. with open(testscript_path, "w") as fp:
  29. fp.write("print(1)")
  30. assert os.path.isfile(testscript_path)
  31. os.remove(testscript_path)
  32. return tmp_dir
  33. except (AssertionError, NameError):
  34. pass
  35. return tempfile.mkdtemp()
  36. def bootstrap():
  37. import pioinstaller.__main__
  38. pioinstaller.__main__.main()
  39. def main():
  40. runtime_tmp_dir = create_temp_dir()
  41. os.environ["TMPDIR"] = runtime_tmp_dir
  42. tmp_dir = tempfile.mkdtemp(dir=runtime_tmp_dir)
  43. try:
  44. pioinstaller_zip = os.path.join(tmp_dir, "pioinstaller.zip")
  45. with open(pioinstaller_zip, "wb") as fp:
  46. fp.write(b64decode(DEPENDENCIES))
  47. sys.path.insert(0, pioinstaller_zip)
  48. bootstrap()
  49. finally:
  50. for d in (runtime_tmp_dir, tmp_dir):
  51. if d and os.path.isdir(d):
  52. shutil.rmtree(d, ignore_errors=True)
  53. if __name__ == "__main__":
  54. main()