fetch_and_compare_version.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 build-scripts/config_common.cmake
  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. list_tag_cmd = (
  37. 'git tag --list WAMR-*.*.* --sort=committerdate --format="%(refname:short)"'
  38. )
  39. p = subprocess.run(shlex.split(list_tag_cmd), capture_output=True, check=True)
  40. all_tags = p.stdout.decode().strip()
  41. latest_tag = all_tags.split("\n")[-1]
  42. return latest_tag
  43. def match_version_pattern(v):
  44. pattern = r"WAMR-\d+\.\d+\.\d+"
  45. m = re.match(pattern, v)
  46. return m is not None
  47. def split_version_string(v):
  48. """
  49. return the semantic version as an integer list
  50. """
  51. pattern = r"WAMR-(\d+)\.(\d+)\.(\d+)"
  52. m = re.match(pattern, v)
  53. return [int(x) for x in m.groups()]
  54. def compare_version_string(v1, v2):
  55. """
  56. return value:
  57. - 1. if v1 > v2
  58. - -1. if v1 < v2
  59. - 0. if v1 == v2
  60. """
  61. if not match_version_pattern(v1):
  62. raise Exception(f"{v1} doesn't match the version pattern")
  63. if not match_version_pattern(v2):
  64. raise Exception(f"{v2} doesn't match the version pattern")
  65. v1_sem_ver = split_version_string(v1)
  66. v2_sem_ver = split_version_string(v2)
  67. return 0 if v1_sem_ver == v2_sem_ver else (1 if v1_sem_ver > v2_sem_ver else -1)
  68. def is_major_or_minor_changed(v1, v2):
  69. """
  70. return true if change either major of v2 or minor of v2
  71. return false or else
  72. """
  73. if not match_version_pattern(v1):
  74. raise Exception(f"{v1} doesn't match the version pattern")
  75. if not match_version_pattern(v2):
  76. raise Exception(f"{v2} doesn't match the version pattern")
  77. v1_major, v1_minor, _ = split_version_string(v1)
  78. v2_major, v2_minor, _ = split_version_string(v2)
  79. return v2_major != v1_major or v2_minor != v1_minor
  80. def next_version():
  81. definition = fetch_version_from_code()
  82. tag = fetch_latest_git_tag()
  83. new_version = ""
  84. minor_changed = False
  85. if compare_version_string(definition, tag) == 1:
  86. new_version = definition.split("-")[-1]
  87. if is_major_or_minor_changed(tag, definition):
  88. minor_changed = True
  89. return new_version, "major_minor_change" if minor_changed else "patch_change"
  90. if __name__ == "__main__":
  91. print(f"{next_version()[0]},{next_version()[1]}")
  92. sys.exit(0)