deploy_util.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2023 The Qt Company Ltd.
  2. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. from __future__ import annotations
  4. import logging
  5. import shutil
  6. import sys
  7. from pathlib import Path
  8. from . import EXE_FORMAT
  9. from .config import Config, DesktopConfig
  10. def config_option_exists():
  11. for argument in sys.argv:
  12. if any(item in argument for item in ["--config-file", "-c"]):
  13. return True
  14. return False
  15. def cleanup(config: Config, is_android: bool = False):
  16. """
  17. Cleanup the generated build folders/files.
  18. Parameters:
  19. config (Config): The configuration object containing paths and settings.
  20. is_android (bool): Flag indicating if the cleanup is for an Android project. Default is False.
  21. """
  22. if config.generated_files_path.exists():
  23. try:
  24. shutil.rmtree(config.generated_files_path)
  25. logging.info("[DEPLOY] Deployment directory purged")
  26. except PermissionError as e:
  27. print(f"{type(e).__name__}: {e}")
  28. logging.warning(f"[DEPLOY] Could not delete {config.generated_files_path}")
  29. if is_android:
  30. buildozer_spec: Path = config.project_dir / "buildozer.spec"
  31. if buildozer_spec.exists():
  32. try:
  33. buildozer_spec.unlink()
  34. logging.info(f"[DEPLOY] {str(buildozer_spec)} removed")
  35. except PermissionError as e:
  36. print(f"{type(e).__name__}: {e}")
  37. logging.warning(f"[DEPLOY] Could not delete {buildozer_spec}")
  38. buildozer_build: Path = config.project_dir / ".buildozer"
  39. if buildozer_build.exists():
  40. try:
  41. shutil.rmtree(buildozer_build)
  42. logging.info(f"[DEPLOY] {str(buildozer_build)} removed")
  43. except PermissionError as e:
  44. print(f"{type(e).__name__}: {e}")
  45. logging.warning(f"[DEPLOY] Could not delete {buildozer_build}")
  46. def create_config_file(main_file: Path, dry_run: bool = False):
  47. """
  48. Creates a new pysidedeploy.spec
  49. """
  50. config_file = main_file.parent / "pysidedeploy.spec"
  51. logging.info(f"[DEPLOY] Creating config file {config_file}")
  52. default_config_file = Path(__file__).parent / "default.spec"
  53. # the config parser needs a reference to parse. So, in the case of --dry-run
  54. # use the default.spec file.
  55. if dry_run:
  56. return default_config_file
  57. shutil.copy(default_config_file, config_file)
  58. return config_file
  59. def finalize(config: DesktopConfig):
  60. """
  61. Copy the executable into the final location
  62. For Android deployment, this is done through buildozer
  63. """
  64. exe_format = EXE_FORMAT
  65. if config.mode == DesktopConfig.NuitkaMode.STANDALONE and sys.platform != "darwin":
  66. exe_format = ".dist"
  67. generated_exec_path = config.generated_files_path / (config.source_file.stem + exe_format)
  68. if not generated_exec_path.exists():
  69. logging.error(f"[DEPLOY] Executable not found at {generated_exec_path.absolute()}")
  70. return
  71. logging.info(f"[DEPLOY] executable generated at {generated_exec_path.absolute()}")
  72. if not config.exe_dir:
  73. logging.info("[DEPLOY] Not copying output executable because no output directory specified")
  74. return
  75. output_path = config.exe_dir / (config.title + exe_format)
  76. if sys.platform == "darwin" or config.mode == DesktopConfig.NuitkaMode.STANDALONE:
  77. # Copy the folder that contains the executable
  78. logging.info(f"[DEPLOY] copying generated folder to {output_path.absolute()}")
  79. shutil.copytree(generated_exec_path, output_path, dirs_exist_ok=True)
  80. else:
  81. # Copy a single file
  82. logging.info(f"[DEPLOY] copying generated file to {output_path.absolute()}")
  83. shutil.copy(generated_exec_path, output_path)
  84. print(f"[DEPLOY] Executed file created in {output_path.absolute()}")