download_mks_assets.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #
  2. # download_mks_assets.py
  3. # Added by HAS_TFT_LVGL_UI to download assets from Makerbase repo
  4. #
  5. import pioutil
  6. if pioutil.is_pio_build():
  7. import requests, zipfile, tempfile, shutil
  8. from pathlib import Path
  9. env = pioutil.env
  10. url = "https://github.com/makerbase-mks/Mks-Robin-Nano-Marlin2.0-Firmware/archive/0263cdaccf.zip"
  11. deps_path = Path(env.Dictionary("PROJECT_LIBDEPS_DIR"))
  12. zip_path = deps_path / "mks-assets.zip"
  13. assets_path = Path(env.Dictionary("PROJECT_BUILD_DIR"), env.Dictionary("PIOENV"), "assets")
  14. def download_mks_assets():
  15. print("Downloading MKS Assets for TFT_LVGL_UI")
  16. r = requests.get(url, stream=True)
  17. # the user may have a very clean workspace,
  18. # so create the PROJECT_LIBDEPS_DIR directory if not exits
  19. if not deps_path.exists():
  20. deps_path.mkdir()
  21. with zip_path.open('wb') as fd:
  22. for chunk in r.iter_content(chunk_size=128):
  23. fd.write(chunk)
  24. def copy_mks_assets():
  25. print("Copying MKS Assets for TFT_LVGL_UI")
  26. output_path = Path(tempfile.mkdtemp())
  27. zip_obj = zipfile.ZipFile(zip_path, 'r')
  28. zip_obj.extractall(output_path)
  29. zip_obj.close()
  30. if assets_path.exists() and not assets_path.is_dir():
  31. assets_path.unlink()
  32. if not assets_path.exists():
  33. assets_path.mkdir()
  34. base_path = ''
  35. for filename in output_path.iterdir():
  36. base_path = filename
  37. fw_path = (output_path / base_path / 'Firmware')
  38. font_path = fw_path / 'mks_font'
  39. for filename in font_path.iterdir():
  40. shutil.copy(font_path / filename, assets_path)
  41. pic_path = fw_path / 'mks_pic'
  42. for filename in pic_path.iterdir():
  43. shutil.copy(pic_path / filename, assets_path)
  44. shutil.rmtree(output_path, ignore_errors=True)
  45. if not zip_path.exists():
  46. download_mks_assets()
  47. if not assets_path.exists():
  48. copy_mks_assets()