gha.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. tasks.gha
  4. ~~~~~~~~~
  5. GitHub Actions Tasks
  6. """
  7. # pylint: disable=resource-leakage
  8. import pathlib
  9. from invoke import task # pylint: disable=3rd-party-module-not-gated
  10. CODE_DIR = pathlib.Path(__file__).resolve().parent.parent
  11. GHA_WORKFLOWS_DIR = CODE_DIR / ".github" / "workflows"
  12. GHA_TEMPLATES_DIR = GHA_WORKFLOWS_DIR / "templates"
  13. LINUX_DISTROS = {
  14. "amazon": {"display_name": "Amazon Linux", "versions": ["2"]},
  15. "arch": {"display_name": "Arch Linux", "versions": ["lts"]},
  16. "centos": {"display_name": "CentOS", "versions": ["7", "8"]},
  17. "debian": {"display_name": "Debian", "versions": ["9", "10"]},
  18. "fedora": {"display_name": "Fedora", "versions": ["31", "32"]},
  19. "opensuse": {"display_name": "Opensuse", "versions": ["15"]},
  20. "ubuntu": {"display_name": "Ubuntu", "versions": {"16.04", "18.04", "20.04"}},
  21. }
  22. DEFAULT_NOX_ENV = "runtests-zeromq"
  23. DEFAULT_NOX_PASSTHROUGH_OPTS = "--ssh-tests"
  24. LINUX_CONFIGS = {
  25. "": {
  26. "": {
  27. "env": DEFAULT_NOX_ENV,
  28. "opts": DEFAULT_NOX_PASSTHROUGH_OPTS,
  29. "display_name": "",
  30. }
  31. },
  32. "centos-7": {
  33. "m2crypto": {
  34. "env": "{}-m2crypto".format(DEFAULT_NOX_ENV),
  35. "opts": DEFAULT_NOX_PASSTHROUGH_OPTS,
  36. "display_name": "M2Crypto",
  37. },
  38. "proxy": {"env": DEFAULT_NOX_ENV, "opts": "--proxy", "display_name": "Proxy"},
  39. "pycryptodome": {
  40. "env": "{}-pycryptodome".format(DEFAULT_NOX_ENV),
  41. "opts": DEFAULT_NOX_PASSTHROUGH_OPTS,
  42. "display_name": "PyCryptodome",
  43. },
  44. "tcp": {
  45. "env": "runtests-tcp",
  46. "opts": DEFAULT_NOX_PASSTHROUGH_OPTS,
  47. "display_name": "TCP",
  48. },
  49. },
  50. "ubuntu-1604": {
  51. "m2crypto": {
  52. "env": "{}-m2crypto".format(DEFAULT_NOX_ENV),
  53. "opts": DEFAULT_NOX_PASSTHROUGH_OPTS,
  54. "display_name": "M2Crypto",
  55. },
  56. "proxy": {"env": DEFAULT_NOX_ENV, "opts": "--proxy", "display_name": "Proxy"},
  57. "pycryptodome": {
  58. "env": "{}-pycryptodome".format(DEFAULT_NOX_ENV),
  59. "opts": DEFAULT_NOX_PASSTHROUGH_OPTS,
  60. "display_name": "PyCryptodome",
  61. },
  62. "tcp": {
  63. "env": "runtests-tcp",
  64. "opts": DEFAULT_NOX_PASSTHROUGH_OPTS,
  65. "display_name": "TCP",
  66. },
  67. },
  68. }
  69. @task
  70. def generate(ctx, output="ci", skip_windows=False, skip_macos=False):
  71. output_file = str(GHA_WORKFLOWS_DIR / "{}.yml".format(output))
  72. with open(str(GHA_TEMPLATES_DIR / "upload.yml")) as rfh:
  73. upload_step = rfh.read()
  74. jobs = ""
  75. for template in ("pre-commit.yml", "lint.yml", "docs.yml"):
  76. with open(str(GHA_TEMPLATES_DIR / template)) as rfh:
  77. jobs += rfh.read()
  78. for distro, details in sorted(LINUX_DISTROS.items()):
  79. for version in sorted(details["versions"]):
  80. name = slug = "{}-{}".format(distro, version.replace(".", ""))
  81. display_name = "{} {}".format(details["display_name"], version.upper())
  82. c_details = LINUX_CONFIGS.get(slug)
  83. if c_details is None:
  84. c_details = LINUX_CONFIGS[""].copy()
  85. else:
  86. c_details.update(LINUX_CONFIGS[""].copy())
  87. for config, c_details in sorted(c_details.items()):
  88. job_name = name
  89. job_display_name = display_name
  90. if config:
  91. job_name = "{}-{}".format(job_name, config)
  92. if c_details["display_name"]:
  93. job_display_name = "{} {}".format(
  94. job_display_name, c_details["display_name"]
  95. )
  96. nox_env_name = c_details["env"]
  97. nox_passthrough_opts = c_details["opts"]
  98. with open(str(GHA_TEMPLATES_DIR / "linux.yml")) as rfh:
  99. jobs += rfh.read().format(
  100. name=job_name,
  101. slug=slug,
  102. display_name=job_display_name,
  103. nox_env_name=nox_env_name,
  104. nox_passthrough_opts=nox_passthrough_opts,
  105. )
  106. jobs += upload_step.format(display_name=job_display_name)
  107. if skip_macos is False:
  108. with open(str(GHA_TEMPLATES_DIR / "macos.yml")) as rfh:
  109. jobs += rfh.read()
  110. jobs += upload_step.format(display_name="macOS Catalina 10.15")
  111. if skip_windows is False:
  112. with open(str(GHA_TEMPLATES_DIR / "windows.yml")) as rfh:
  113. jobs += rfh.read()
  114. jobs += upload_step.format(display_name="Windows Server 2019")
  115. with open(output_file, "w") as wfh:
  116. with open(str(GHA_TEMPLATES_DIR / "header.yml")) as rfh:
  117. wfh.write(rfh.read().format(workflow=output, jobs=jobs))