You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 satır
2.4KB

  1. import multiprocessing
  2. import shutil
  3. from pathlib import Path
  4. from typing import Optional
  5. from . import paths, proc
  6. class DDSWrapper:
  7. """
  8. Wraps a 'dds' executable with some convenience APIs that invoke various
  9. 'dds' subcommands.
  10. """
  11. def __init__(self, path: Path) -> None:
  12. self.path = path
  13. self.repo_dir = paths.PREBUILT_DIR / 'ci-repo'
  14. self.catalog_path = paths.PREBUILT_DIR / 'ci-catalog.db'
  15. @property
  16. def catalog_path_arg(self) -> str:
  17. """The arguments for --catalog"""
  18. return f'--catalog={self.catalog_path}'
  19. @property
  20. def repo_dir_arg(self) -> str:
  21. """The arguments for --repo-dir"""
  22. return f'--repo-dir={self.repo_dir}'
  23. def clean(self, *, build_dir: Optional[Path] = None, repo: bool = True, catalog: bool = True) -> None:
  24. """
  25. Clean out prior executable output, including repos, catalog, and
  26. the build results at 'build_dir', if given.
  27. """
  28. if build_dir and build_dir.exists():
  29. shutil.rmtree(build_dir)
  30. if repo and self.repo_dir.exists():
  31. shutil.rmtree(self.repo_dir)
  32. if catalog and self.catalog_path.exists():
  33. self.catalog_path.unlink()
  34. def run(self, args: proc.CommandLine) -> None:
  35. """Execute the 'dds' executable with the given arguments"""
  36. proc.check_run([self.path, args])
  37. def catalog_json_import(self, path: Path) -> None:
  38. """Run 'catalog import' to import the given JSON. Only applicable to older 'dds'"""
  39. self.run(['catalog', 'import', self.catalog_path_arg, f'--json={path}'])
  40. def build(self,
  41. *,
  42. toolchain: Path,
  43. root: Path,
  44. build_root: Optional[Path] = None,
  45. jobs: Optional[int] = None) -> None:
  46. """
  47. Run 'dds build' with the given arguments.
  48. :param toolchain: The toolchain to use for the build.
  49. :param root: The root project directory.
  50. :param build_root: The root directory where the output will be written.
  51. :param jobs: The number of jobs to use. Default is CPU-count + 2
  52. """
  53. jobs = jobs or multiprocessing.cpu_count() + 2
  54. self.run([
  55. 'build',
  56. f'--toolchain={toolchain}',
  57. self.repo_dir_arg,
  58. self.catalog_path_arg,
  59. f'--jobs={jobs}',
  60. f'--project-dir={root}',
  61. f'--out={build_root}',
  62. ])