Fork of the espurna firmware for `mhsw` switches
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.

157 lines
4.8 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import os
  18. import argparse
  19. import re
  20. import shlex
  21. import configparser
  22. import collections
  23. CI = any([os.environ.get("TRAVIS"), os.environ.get("CI")])
  24. Build = collections.namedtuple("Build", "env extends build_flags src_build_flags")
  25. def expand_variables(cfg, value):
  26. RE_VARS = re.compile("\$\{.*?\}")
  27. for var in RE_VARS.findall(value):
  28. section, option = var.replace("${", "").replace("}", "").split(".", 1)
  29. value = value.replace(var, expand_variables(cfg, cfg.get(section, option)))
  30. return value
  31. def get_builds(cfg):
  32. RE_NEWLINE = re.compile("\r\n|\n")
  33. BASE_BUILD_FLAGS = set(
  34. shlex.split(expand_variables(cfg, cfg.get("env", "build_flags")))
  35. )
  36. for section in cfg.sections():
  37. if (not section.startswith("env:")) or (
  38. section.startswith("env:esp8266-") and section.endswith("-base")
  39. ):
  40. continue
  41. build_flags = None
  42. src_build_flags = None
  43. try:
  44. build_flags = cfg.get(section, "build_flags")
  45. build_flags = RE_NEWLINE.sub(" ", build_flags).strip()
  46. build_flags = " ".join(
  47. BASE_BUILD_FLAGS ^ set(shlex.split(expand_variables(cfg, build_flags)))
  48. )
  49. except configparser.NoOptionError:
  50. pass
  51. try:
  52. src_build_flags = cfg.get(section, "src_build_flags")
  53. src_build_flags = RE_NEWLINE.sub(" ", src_build_flags).strip()
  54. src_build_flags = expand_variables(cfg, src_build_flags)
  55. except configparser.NoOptionError:
  56. pass
  57. yield Build(
  58. section.replace("env:", ""),
  59. cfg.get(section, "extends").replace("env:", ""),
  60. build_flags,
  61. src_build_flags,
  62. )
  63. def find_any(string, values):
  64. for value in values:
  65. if value in string:
  66. return True
  67. return False
  68. def generate_lines(builds, ignore):
  69. cores = []
  70. generic = []
  71. for build in builds:
  72. if find_any(build.env, ignore):
  73. continue
  74. flags = []
  75. if build.build_flags:
  76. flags.append('PLATFORMIO_BUILD_FLAGS="{}"'.format(build.build_flags))
  77. if build.src_build_flags:
  78. flags.append('ESPURNA_FLAGS="{}"'.format(build.src_build_flags))
  79. flags.append('ESPURNA_RELEASE_NAME="{env}"'.format(env=build.env))
  80. cmd = ["env"]
  81. cmd.extend(flags)
  82. cmd.extend(["pio", "run", "-e", build.extends, "-s", "-t", "release"])
  83. line = " ".join(cmd)
  84. # push core variants to the front as they definetly include global build_flags
  85. output = generic
  86. if "ESPURNA_CORE" in build.src_build_flags:
  87. output = cores
  88. output.append(line)
  89. return cores + generic
  90. def every(seq, nth, total):
  91. index = 0
  92. for value in seq:
  93. if index == nth:
  94. yield value
  95. index = (index + 1) % total
  96. if __name__ == "__main__":
  97. if not CI:
  98. raise ValueError("* Not in CI *")
  99. parser = argparse.ArgumentParser()
  100. parser.add_argument("--version", required=True)
  101. parser.add_argument("--destination", required=True)
  102. parser.add_argument("--ignore", action="append")
  103. args = parser.parse_args()
  104. Config = configparser.ConfigParser()
  105. with open("platformio.ini", "r") as f:
  106. Config.read_file(f)
  107. builder_total_threads = int(os.environ["BUILDER_TOTAL_THREADS"])
  108. builder_thread = int(os.environ["BUILDER_THREAD"])
  109. if builder_thread >= builder_total_threads:
  110. raise ValueError("* Builder thread index out of range *")
  111. builds = every(get_builds(Config), builder_thread, builder_total_threads)
  112. print("#!/bin/bash")
  113. print("set -e -x")
  114. print('export ESPURNA_RELEASE_VERSION="{}"'.format(args.version))
  115. print('export ESPURNA_RELEASE_DESTINATION="{}"'.format(args.destination))
  116. print('trap "ls -l ${ESPURNA_RELEASE_DESTINATION}" EXIT')
  117. print(
  118. 'echo "Selected thread #{} out of {}"'.format(
  119. builder_thread + 1, builder_total_threads
  120. )
  121. )
  122. for line in generate_lines(builds, args.ignore or ()):
  123. print(line)