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.

149 lines
4.0KB

  1. import argparse
  2. import os
  3. import sys
  4. import pytest
  5. from pathlib import Path
  6. from typing import Sequence, NamedTuple
  7. import subprocess
  8. import urllib.request
  9. import shutil
  10. from self_build import self_build
  11. from dds_ci import paths, proc
  12. class CIOptions(NamedTuple):
  13. toolchain: str
  14. def _do_bootstrap_build(opts: CIOptions) -> None:
  15. print('Bootstrapping by a local build of prior versions...')
  16. subprocess.check_call([
  17. sys.executable,
  18. '-u',
  19. str(paths.TOOLS_DIR / 'bootstrap.py'),
  20. ])
  21. def _do_bootstrap_download() -> None:
  22. filename = {
  23. 'win32': 'dds-win-x64.exe',
  24. 'linux': 'dds-linux-x64',
  25. 'darwin': 'dds-macos-x64',
  26. 'freebsd11': 'dds-freebsd-x64',
  27. 'freebsd12': 'dds-freebsd-x64',
  28. }.get(sys.platform)
  29. if filename is None:
  30. raise RuntimeError(f'We do not have a prebuilt DDS binary for '
  31. f'the "{sys.platform}" platform')
  32. url = f'https://github.com/vector-of-bool/dds/releases/download/0.1.0-alpha.3/{filename}'
  33. print(f'Downloading prebuilt DDS executable: {url}')
  34. stream = urllib.request.urlopen(url)
  35. paths.PREBUILT_DDS.parent.mkdir(exist_ok=True, parents=True)
  36. with paths.PREBUILT_DDS.open('wb') as fd:
  37. while True:
  38. buf = stream.read(1024 * 4)
  39. if not buf:
  40. break
  41. fd.write(buf)
  42. if os.name != 'nt':
  43. # Mark the binary executable. By default it won't be
  44. mode = paths.PREBUILT_DDS.stat().st_mode
  45. mode |= 0b001_001_001
  46. paths.PREBUILT_DDS.chmod(mode)
  47. def main(argv: Sequence[str]) -> int:
  48. parser = argparse.ArgumentParser()
  49. parser.add_argument(
  50. '-B',
  51. '--bootstrap-with',
  52. help='How are we to obtain a bootstrapped DDS executable?',
  53. choices=('download', 'build', 'skip'),
  54. required=True,
  55. )
  56. parser.add_argument(
  57. '--toolchain',
  58. '-T',
  59. help='The toolchain to use for the CI process',
  60. required=True,
  61. )
  62. parser.add_argument(
  63. '--build-only',
  64. action='store_true',
  65. help='Only build the `dds` executable. Skip second-phase and tests.')
  66. args = parser.parse_args(argv)
  67. opts = CIOptions(toolchain=args.toolchain)
  68. if args.bootstrap_with == 'build':
  69. _do_bootstrap_build(opts)
  70. elif args.bootstrap_with == 'download':
  71. _do_bootstrap_download()
  72. elif args.bootstrap_with == 'skip':
  73. pass
  74. else:
  75. assert False, 'impossible'
  76. cat_path = paths.BUILD_DIR / 'catalog.db'
  77. if cat_path.is_file():
  78. cat_path.unlink()
  79. ci_repo_dir = paths.BUILD_DIR / '_ci-repo'
  80. if ci_repo_dir.exists():
  81. shutil.rmtree(ci_repo_dir)
  82. proc.check_run([
  83. paths.PREBUILT_DDS,
  84. 'catalog',
  85. 'import',
  86. ('--catalog', cat_path),
  87. ('--json', paths.PROJECT_ROOT / 'catalog.json'),
  88. ])
  89. self_build(
  90. paths.PREBUILT_DDS,
  91. toolchain=opts.toolchain,
  92. dds_flags=[
  93. ('--catalog', cat_path),
  94. ('--repo-dir', ci_repo_dir),
  95. ])
  96. print('Main build PASSED!')
  97. print(f'A `dds` executable has been generated: {paths.CUR_BUILT_DDS}')
  98. if args.build_only:
  99. print(
  100. f'`--build-only` was given, so second phase and tests will not execute'
  101. )
  102. return 0
  103. # Delete the catalog database, since there may be schema changes since the
  104. # bootstrap executable was built
  105. cat_path.unlink()
  106. proc.check_run([
  107. paths.CUR_BUILT_DDS,
  108. 'catalog',
  109. 'import',
  110. ('--catalog', cat_path),
  111. ('--json', paths.PROJECT_ROOT / 'catalog.json'),
  112. ])
  113. self_build(
  114. paths.CUR_BUILT_DDS,
  115. toolchain=opts.toolchain,
  116. dds_flags=[f'--repo-dir={ci_repo_dir}', f'--catalog={cat_path}'])
  117. print('Bootstrap test PASSED!')
  118. return pytest.main([
  119. '-v',
  120. '--durations=10',
  121. f'--basetemp={paths.BUILD_DIR / "_tmp"}',
  122. '-n4',
  123. 'tests/',
  124. ])
  125. if __name__ == "__main__":
  126. sys.exit(main(sys.argv[1:]))