reuse_latest_release_binaries.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 argparse
  7. import json
  8. import os
  9. import shlex
  10. import subprocess
  11. import sys
  12. from urllib.error import HTTPError, URLError
  13. import urllib.request
  14. def get_last_commit(target_path, cwd):
  15. last_commit_cmd = f"git log -n 1 --pretty=format:%H -- {target_path}"
  16. p = subprocess.run(
  17. shlex.split(last_commit_cmd), capture_output=True, check=True, cwd=cwd
  18. )
  19. return p.stdout.decode().strip()
  20. def fetch_git_tags():
  21. list_tag_cmd = (
  22. 'git tag --list WAMR-*.*.* --sort=committerdate --format="%(refname:short)"'
  23. )
  24. p = subprocess.run(shlex.split(list_tag_cmd), capture_output=True, check=True)
  25. all_tags = p.stdout.decode().strip()
  26. return all_tags.split("\n")
  27. def download_binaries(binary_name_stem, cwd):
  28. """
  29. 1. find the latest release name
  30. 2. form assets download url:
  31. """
  32. try:
  33. all_tags = fetch_git_tags()
  34. # *release_process.yml* will create a tag and release at first
  35. second_last_tag = all_tags[-2]
  36. latest_tag = all_tags[-1]
  37. latest_url = "https://api.github.com/repos/bytecodealliance/wasm-micro-runtime/releases/latest"
  38. print(f"::notice::query the latest release with {latest_url}...")
  39. with urllib.request.urlopen(latest_url) as response:
  40. body = response.read()
  41. release_name = json.loads(body)["name"]
  42. # WAMR-X.Y.Z -> X.Y.Z
  43. second_last_sem_ver = second_last_tag[5:]
  44. latest_sem_ver = latest_tag[5:]
  45. assert latest_sem_ver in binary_name_stem
  46. name_stem_in_release = binary_name_stem.replace(
  47. latest_sem_ver, second_last_sem_ver
  48. )
  49. # download and rename
  50. for file_ext in (".zip", ".tar.gz"):
  51. assets_url = f"https://github.com/bytecodealliance/wasm-micro-runtime/releases/download/{release_name}/{name_stem_in_release}{file_ext}"
  52. local_path = f"{binary_name_stem}{file_ext}"
  53. print(f"::notice::download from {assets_url} and save as {local_path}...")
  54. urllib.request.urlretrieve(assets_url, local_path)
  55. return True
  56. except HTTPError as error:
  57. print(error.status, error.reason)
  58. except URLError as error:
  59. print(error.reason)
  60. except TimeoutError:
  61. print("Request timeout")
  62. return False
  63. def main():
  64. parser = argparse.ArgumentParser(
  65. description="Reuse binaries of the latest release if no more modification on the_path since last_commit"
  66. )
  67. parser.add_argument("working_directory", type=str)
  68. parser.add_argument("--binary_name_stem", type=str)
  69. parser.add_argument("--last_commit", type=str)
  70. parser.add_argument("--the_path", type=str)
  71. args = parser.parse_args()
  72. last_commit = get_last_commit(args.the_path, args.working_directory)
  73. if last_commit == args.last_commit:
  74. return download_binaries(args.binary_name_stem, args.working_directory)
  75. else:
  76. return False
  77. if __name__ == "__main__":
  78. # use output to indicate results
  79. # echo "result=${result}" >> "$GITHUB_OUTPUT"
  80. with open(os.environ.get("GITHUB_OUTPUT"), 'a') as output_file:
  81. output_file.write("result=hit\n" if main() else "result=not-hit\n")
  82. # always return 0
  83. sys.exit(0)