utils.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import os
  4. import re
  5. from .const import REPO_ROOT
  6. from compose import const as compose_const
  7. section_header_re = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+ \([0-9]{4}-[01][0-9]-[0-3][0-9]\)$')
  8. class ScriptError(Exception):
  9. pass
  10. def branch_name(version):
  11. return 'bump-{}'.format(version)
  12. def read_release_notes_from_changelog():
  13. with open(os.path.join(REPO_ROOT, 'CHANGELOG.md'), 'r') as f:
  14. lines = f.readlines()
  15. i = 0
  16. while i < len(lines):
  17. if section_header_re.match(lines[i]):
  18. break
  19. i += 1
  20. j = i + 1
  21. while j < len(lines):
  22. if section_header_re.match(lines[j]):
  23. break
  24. j += 1
  25. return ''.join(lines[i + 2:j - 1])
  26. def update_init_py_version(version):
  27. path = os.path.join(REPO_ROOT, 'compose', '__init__.py')
  28. with open(path, 'r') as f:
  29. contents = f.read()
  30. contents = re.sub(r"__version__ = '[0-9a-z.-]+'", "__version__ = '{}'".format(version), contents)
  31. with open(path, 'w') as f:
  32. f.write(contents)
  33. def update_run_sh_version(version):
  34. path = os.path.join(REPO_ROOT, 'script', 'run', 'run.sh')
  35. with open(path, 'r') as f:
  36. contents = f.read()
  37. contents = re.sub(r'VERSION="[0-9a-z.-]+"', 'VERSION="{}"'.format(version), contents)
  38. with open(path, 'w') as f:
  39. f.write(contents)
  40. def compatibility_matrix():
  41. result = {}
  42. for engine_version in compose_const.API_VERSION_TO_ENGINE_VERSION.values():
  43. result[engine_version] = []
  44. for fmt, api_version in compose_const.API_VERSIONS.items():
  45. result[compose_const.API_VERSION_TO_ENGINE_VERSION[api_version]].append(fmt.vstring)
  46. return result
  47. def yesno(prompt, default=None):
  48. """
  49. Prompt the user for a yes or no.
  50. Can optionally specify a default value, which will only be
  51. used if they enter a blank line.
  52. Unrecognised input (anything other than "y", "n", "yes",
  53. "no" or "") will return None.
  54. """
  55. answer = input(prompt).strip().lower()
  56. if answer == "y" or answer == "yes":
  57. return True
  58. elif answer == "n" or answer == "no":
  59. return False
  60. elif answer == "":
  61. return default
  62. else:
  63. return None