commands.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2022 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 json
  5. import subprocess
  6. import sys
  7. from pathlib import Path
  8. from functools import lru_cache
  9. from . import DEFAULT_IGNORE_DIRS
  10. """
  11. All utility functions for deployment
  12. """
  13. def run_command(command, dry_run: bool, fetch_output: bool = False):
  14. command_str = " ".join([str(cmd) for cmd in command])
  15. output = None
  16. is_windows = (sys.platform == "win32")
  17. try:
  18. if not dry_run:
  19. if fetch_output:
  20. output = subprocess.check_output(command, shell=is_windows)
  21. else:
  22. subprocess.check_call(command, shell=is_windows)
  23. else:
  24. print(command_str + "\n")
  25. except FileNotFoundError as error:
  26. raise FileNotFoundError(f"[DEPLOY] {error.filename} not found")
  27. except subprocess.CalledProcessError as error:
  28. raise RuntimeError(
  29. f"[DEPLOY] Command {command_str} failed with error {error} and return_code"
  30. f"{error.returncode}"
  31. )
  32. except Exception as error:
  33. raise RuntimeError(f"[DEPLOY] Command {command_str} failed with error {error}")
  34. return command_str, output
  35. @lru_cache
  36. def run_qmlimportscanner(project_dir: Path, dry_run: bool):
  37. """
  38. Runs pyside6-qmlimportscanner to find all the imported qml modules in project_dir
  39. """
  40. qml_modules = []
  41. cmd = ["pyside6-qmlimportscanner", "-rootPath", str(project_dir)]
  42. for ignore_dir in DEFAULT_IGNORE_DIRS:
  43. cmd.extend(["-exclude", ignore_dir])
  44. if dry_run:
  45. run_command(command=cmd, dry_run=True)
  46. # Run qmlimportscanner during dry_run as well to complete the command being run by nuitka
  47. _, json_string = run_command(command=cmd, dry_run=False, fetch_output=True)
  48. json_string = json_string.decode("utf-8")
  49. json_array = json.loads(json_string)
  50. qml_modules = [item['name'] for item in json_array if item['type'] == "module"]
  51. return qml_modules