fetch_and_compare_version.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. import re
  7. import shlex
  8. import subprocess
  9. import sys
  10. def fetch_version_from_code():
  11. """
  12. search the semantic version definition in core/version.h
  13. """
  14. major, minor, patch = "", "", ""
  15. with open("core/version.h", encoding="utf-8") as f:
  16. for line in f:
  17. if "WAMR_VERSION" not in line:
  18. continue
  19. major_match = re.search(r"WAMR_VERSION_MAJOR (\d+)", line)
  20. if major_match is not None:
  21. major = major_match.groups()[0]
  22. continue
  23. minor_match = re.search(r"WAMR_VERSION_MINOR (\d+)", line)
  24. if minor_match is not None:
  25. minor = minor_match.groups()[0]
  26. continue
  27. patch_match = re.search(r"WAMR_VERSION_PATCH (\d+)", line)
  28. if patch_match is not None:
  29. patch = patch_match.groups()[0]
  30. if len(major) == 0 or len(minor) == 0 or len(patch) == 0:
  31. raise Exception(
  32. "can't find the semantic version definition likes WAMR_VERSION_*"
  33. )
  34. return f"WAMR-{major}.{minor}.{patch}"
  35. def fetch_latest_git_tag():
  36. """
  37. Get the most recent tag from the HEAD,
  38. if it's main branch, it should be the latest release tag.
  39. if it's release/x.x.x branch, it should be the latest release tag of the branch.
  40. """
  41. list_tag_cmd = "git describe --tags --abbrev=0 HEAD"
  42. p = subprocess.run(shlex.split(list_tag_cmd), capture_output=True, check=True)
  43. all_tags = p.stdout.decode().strip()
  44. latest_tag = all_tags.split("\n")[-1]
  45. return latest_tag
  46. def match_version_pattern(v):
  47. pattern = r"WAMR-\d+\.\d+\.\d+"
  48. m = re.match(pattern, v)
  49. return m is not None
  50. def split_version_string(v):
  51. """
  52. return the semantic version as an integer list
  53. """
  54. pattern = r"WAMR-(\d+)\.(\d+)\.(\d+)"
  55. m = re.match(pattern, v)
  56. return [int(x) for x in m.groups()]
  57. def compare_version_string(v1, v2):
  58. """
  59. return value:
  60. - 1. if v1 > v2
  61. - -1. if v1 < v2
  62. - 0. if v1 == v2
  63. """
  64. if not match_version_pattern(v1):
  65. raise Exception(f"{v1} doesn't match the version pattern")
  66. if not match_version_pattern(v2):
  67. raise Exception(f"{v2} doesn't match the version pattern")
  68. v1_sem_ver = split_version_string(v1)
  69. v2_sem_ver = split_version_string(v2)
  70. return 0 if v1_sem_ver == v2_sem_ver else (1 if v1_sem_ver > v2_sem_ver else -1)
  71. def is_major_or_minor_changed(v1, v2):
  72. """
  73. return true if change either major of v2 or minor of v2
  74. return false or else
  75. """
  76. if not match_version_pattern(v1):
  77. raise Exception(f"{v1} doesn't match the version pattern")
  78. if not match_version_pattern(v2):
  79. raise Exception(f"{v2} doesn't match the version pattern")
  80. v1_major, v1_minor, _ = split_version_string(v1)
  81. v2_major, v2_minor, _ = split_version_string(v2)
  82. return v2_major != v1_major or v2_minor != v1_minor
  83. def next_version():
  84. definition = fetch_version_from_code()
  85. tag = fetch_latest_git_tag()
  86. new_version = ""
  87. minor_changed = False
  88. if compare_version_string(definition, tag) == 1:
  89. new_version = definition.split("-")[-1]
  90. if is_major_or_minor_changed(tag, definition):
  91. minor_changed = True
  92. return new_version, "major_minor_change" if minor_changed else "patch_change"
  93. if __name__ == "__main__":
  94. print(f"{next_version()[0]},{next_version()[1]}")
  95. sys.exit(0)