Add retry to get_version script (#268)

This commit is contained in:
Jure Bajic 2021-10-14 16:07:40 +02:00 committed by GitHub
parent 3fc5e57fef
commit 519a204424
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import os
import re import re
import subprocess import subprocess
import sys import sys
import os import time
from functools import wraps
# This script is used to determine the current version of Memgraph. The script # This script is used to determine the current version of Memgraph. The script
# determines the current version using `git` automatically. The user can also # determines the current version using `git` automatically. The user can also
@ -93,16 +94,29 @@ import os
# https://fedoraproject.org/wiki/Package_Versioning_Examples # https://fedoraproject.org/wiki/Package_Versioning_Examples
def retry(retry_limit, timeout=100):
def inner_func(func):
@wraps(func)
def wrapper(*args, **kwargs):
for _ in range(retry_limit):
try:
return func(*args, **kwargs)
except Exception:
time.sleep(timeout)
return func(*args, **kwargs)
return wrapper
return inner_func
@retry(3)
def get_output(*cmd, multiple=False): def get_output(*cmd, multiple=False):
ret = subprocess.run(cmd, stdout=subprocess.PIPE, check=True) ret = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
if multiple: if multiple:
return list(map(lambda x: x.strip(), return list(map(lambda x: x.strip(), ret.stdout.decode("utf-8").strip().split("\n")))
ret.stdout.decode("utf-8").strip().split("\n")))
return ret.stdout.decode("utf-8").strip() return ret.stdout.decode("utf-8").strip()
def format_version(variant, version, offering, distance=None, shorthash=None, def format_version(variant, version, offering, distance=None, shorthash=None, suffix=None):
suffix=None):
if not distance: if not distance:
# This is a release version. # This is a release version.
if variant == "deb": if variant == "deb":
@ -135,8 +149,7 @@ def format_version(variant, version, offering, distance=None, shorthash=None,
return ret return ret
elif variant == "rpm": elif variant == "rpm":
# <VERSION>_0.<DISTANCE>.<SHORTHASH>.<OFFERING>[.<SUFFIX>] # <VERSION>_0.<DISTANCE>.<SHORTHASH>.<OFFERING>[.<SUFFIX>]
ret = "{}_0.{}.{}{}".format( ret = "{}_0.{}.{}{}".format(version, distance, shorthash, "." + offering if offering else "")
version, distance, shorthash, "." + offering if offering else "")
if suffix: if suffix:
ret += "." + suffix ret += "." + suffix
return ret return ret
@ -149,27 +162,23 @@ def format_version(variant, version, offering, distance=None, shorthash=None,
# Parse arguments. # Parse arguments.
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(description="Get the current version of Memgraph.")
description="Get the current version of Memgraph.") parser.add_argument("--open-source", action="store_true", help="set the current offering to 'open-source'")
parser.add_argument("version", help="manual version override, if supplied the version isn't " "determined using git")
parser.add_argument("suffix", help="custom suffix for the current version being built")
parser.add_argument( parser.add_argument(
"--open-source", action="store_true", "--variant",
help="set the current offering to 'open-source'") choices=("binary", "deb", "rpm"),
default="binary",
help="which variant of the version string should be generated",
)
parser.add_argument( parser.add_argument(
"version", help="manual version override, if supplied the version isn't " "--memgraph-root-dir", help="The root directory of the checked out " "Memgraph repository.", default="."
"determined using git") )
parser.add_argument(
"suffix", help="custom suffix for the current version being built")
parser.add_argument(
"--variant", choices=("binary", "deb", "rpm"), default="binary",
help="which variant of the version string should be generated")
parser.add_argument(
"--memgraph-root-dir", help="The root directory of the checked out "
"Memgraph repository.", default=".")
args = parser.parse_args() args = parser.parse_args()
if not os.path.isdir(args.memgraph_root_dir): if not os.path.isdir(args.memgraph_root_dir):
raise Exception("The root directory ({}) is not a valid directory".format( raise Exception("The root directory ({}) is not a valid directory".format(args.memgraph_root_dir))
args.memgraph_root_dir))
os.chdir(args.memgraph_root_dir) os.chdir(args.memgraph_root_dir)
@ -179,8 +188,7 @@ offering = "open-source" if args.open_source else None
if args.version: if args.version:
if not re.match(r"^[0-9]+\.[0-9]+\.[0-9]+$", args.version): if not re.match(r"^[0-9]+\.[0-9]+\.[0-9]+$", args.version):
raise Exception("Invalid version supplied '{}'!".format(args.version)) raise Exception("Invalid version supplied '{}'!".format(args.version))
print(format_version(args.variant, args.version, offering, print(format_version(args.variant, args.version, offering, suffix=args.suffix), end="")
suffix=args.suffix), end="")
sys.exit(0) sys.exit(0)
# Within CI, after the regular checkout, master is sometimes (e.g. in the case # Within CI, after the regular checkout, master is sometimes (e.g. in the case
@ -234,33 +242,28 @@ versions.sort(reverse=True)
current_version = None current_version = None
for version in versions: for version in versions:
version_tuple, branch, master_branch_merge = version version_tuple, branch, master_branch_merge = version
current_branch_merge = get_output( current_branch_merge = get_output("git", "merge-base", current_hash, branch)
"git", "merge-base", current_hash, branch) master_current_merge = get_output("git", "merge-base", current_hash, "master")
master_current_merge = get_output(
"git", "merge-base", current_hash, "master")
# The first check checks whether this commit is a child of `master` and # The first check checks whether this commit is a child of `master` and
# the version branch was created before us. # the version branch was created before us.
# The second check checks whether this commit is a child of the version # The second check checks whether this commit is a child of the version
# branch. # branch.
if master_branch_merge == current_branch_merge or \ if master_branch_merge == current_branch_merge or master_branch_merge == master_current_merge:
master_branch_merge == master_current_merge:
current_version = version current_version = version
break break
# Determine current version. # Determine current version.
if current_version is None: if current_version is None:
raise Exception("You are attempting to determine the version for a very " raise Exception("You are attempting to determine the version for a very " "old version of Memgraph!")
"old version of Memgraph!")
version, branch, master_branch_merge = current_version version, branch, master_branch_merge = current_version
distance = int(get_output("git", "rev-list", "--count", "--first-parent", distance = int(get_output("git", "rev-list", "--count", "--first-parent", master_branch_merge + ".." + current_hash))
master_branch_merge + ".." + current_hash))
version_str = ".".join(map(str, version)) + ".0" version_str = ".".join(map(str, version)) + ".0"
if distance == 0: if distance == 0:
print(format_version(args.variant, version_str, offering, print(format_version(args.variant, version_str, offering, suffix=args.suffix), end="")
suffix=args.suffix),
end="")
else: else:
print(format_version(args.variant, version_str, offering, print(
distance=distance, shorthash=current_hash_short, format_version(
suffix=args.suffix), args.variant, version_str, offering, distance=distance, shorthash=current_hash_short, suffix=args.suffix
end="") ),
end="",
)