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
import argparse
import os
import re
import subprocess
import sys
import os
import time
from functools import wraps
# This script is used to determine the current version of Memgraph. The script
# determines the current version using `git` automatically. The user can also
@ -93,16 +94,29 @@ import os
# 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):
ret = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
if multiple:
return list(map(lambda x: x.strip(),
ret.stdout.decode("utf-8").strip().split("\n")))
return list(map(lambda x: x.strip(), ret.stdout.decode("utf-8").strip().split("\n")))
return ret.stdout.decode("utf-8").strip()
def format_version(variant, version, offering, distance=None, shorthash=None,
suffix=None):
def format_version(variant, version, offering, distance=None, shorthash=None, suffix=None):
if not distance:
# This is a release version.
if variant == "deb":
@ -135,8 +149,7 @@ def format_version(variant, version, offering, distance=None, shorthash=None,
return ret
elif variant == "rpm":
# <VERSION>_0.<DISTANCE>.<SHORTHASH>.<OFFERING>[.<SUFFIX>]
ret = "{}_0.{}.{}{}".format(
version, distance, shorthash, "." + offering if offering else "")
ret = "{}_0.{}.{}{}".format(version, distance, shorthash, "." + offering if offering else "")
if suffix:
ret += "." + suffix
return ret
@ -149,27 +162,23 @@ def format_version(variant, version, offering, distance=None, shorthash=None,
# Parse arguments.
parser = argparse.ArgumentParser(
description="Get the current version of Memgraph.")
parser = argparse.ArgumentParser(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(
"--open-source", action="store_true",
help="set the current offering to 'open-source'")
"--variant",
choices=("binary", "deb", "rpm"),
default="binary",
help="which variant of the version string should be generated",
)
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(
"--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=".")
"--memgraph-root-dir", help="The root directory of the checked out " "Memgraph repository.", default="."
)
args = parser.parse_args()
if not os.path.isdir(args.memgraph_root_dir):
raise Exception("The root directory ({}) is not a valid directory".format(
args.memgraph_root_dir))
raise Exception("The root directory ({}) is not a valid directory".format(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 not re.match(r"^[0-9]+\.[0-9]+\.[0-9]+$", args.version):
raise Exception("Invalid version supplied '{}'!".format(args.version))
print(format_version(args.variant, args.version, offering,
suffix=args.suffix), end="")
print(format_version(args.variant, args.version, offering, suffix=args.suffix), end="")
sys.exit(0)
# 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
for version in versions:
version_tuple, branch, master_branch_merge = version
current_branch_merge = get_output(
"git", "merge-base", current_hash, branch)
master_current_merge = get_output(
"git", "merge-base", current_hash, "master")
current_branch_merge = get_output("git", "merge-base", current_hash, branch)
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 version branch was created before us.
# The second check checks whether this commit is a child of the version
# branch.
if master_branch_merge == current_branch_merge or \
master_branch_merge == master_current_merge:
if master_branch_merge == current_branch_merge or master_branch_merge == master_current_merge:
current_version = version
break
# Determine current version.
if current_version is None:
raise Exception("You are attempting to determine the version for a very "
"old version of Memgraph!")
raise Exception("You are attempting to determine the version for a very " "old version of Memgraph!")
version, branch, master_branch_merge = current_version
distance = int(get_output("git", "rev-list", "--count", "--first-parent",
master_branch_merge + ".." + current_hash))
distance = int(get_output("git", "rev-list", "--count", "--first-parent", master_branch_merge + ".." + current_hash))
version_str = ".".join(map(str, version)) + ".0"
if distance == 0:
print(format_version(args.variant, version_str, offering,
suffix=args.suffix),
end="")
print(format_version(args.variant, version_str, offering, suffix=args.suffix), end="")
else:
print(format_version(args.variant, version_str, offering,
distance=distance, shorthash=current_hash_short,
suffix=args.suffix),
end="")
print(
format_version(
args.variant, version_str, offering, distance=distance, shorthash=current_hash_short, suffix=args.suffix
),
end="",
)