-
Notifications
You must be signed in to change notification settings - Fork 881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tools/read-version: fix the tool so that it can handle version parsing errors #4234
Conversation
The test is failing because version_long is None since: version_long": "23.2-50-ge2a7178a", does not conform to X.Y.Z-xxx-gHASH format. |
53313a3
to
9efc1e4
Compare
@ani-sinha , can you give me a few examples of the version numbers that you're trying to use and how/where they are failing? I just want to be aware of the use cases and testing appropriately. |
For example, in our RHEL repo, we are seeing this: $ python3 tools/read-version Its because the tags start with "cloud-init-" prefix. $ git describe HEAD etc. |
tags can be arbitrary. The tool should not fail. It should still find something reasonable to deal with or leave versions empty. |
@ani-sinha , thanks for the info. What about something like this? diff --git a/tools/read-version b/tools/read-version
index 1e4842efb..5d6112dc4 100755
--- a/tools/read-version
+++ b/tools/read-version
@@ -51,6 +51,37 @@ def is_gitdir(path):
return False
+def get_extra_details(version, version_long):
+ release = None
+ extra = None
+ commit = None
+ distance = None
+
+ # Should match upstream version number. E.g., 23.1 or 23.1.2
+ short_regex = r"(\d+\.\d+\.?\d*)"
+ # Should match version including upstream version, distance, and commit
+ # E.g., 23.1.2-10-g12ab34cd
+ long_regex = r"(\d+\.\d+\.?\d*){1}.*-(\d+)+-g([a-f0-9]{8}){1}.*"
+
+ short_match = re.search(short_regex, version)
+ long_match = re.search(long_regex, version_long)
+ if long_match:
+ release, distance, commit = long_match.groups()
+ extra = f"-{distance}-g{commit}"
+ elif short_match:
+ release = short_match.groups()[0]
+
+ return {
+ "release": release,
+ "version": version,
+ "version_long": version_long,
+ "extra": extra,
+ "commit": commit,
+ "distance": distance,
+ "is_release_branch_ci": is_release_branch_ci,
+ }
+
+
use_long = "--long" in sys.argv or os.environ.get("CI_RV_LONG")
use_tags = "--tags" in sys.argv or os.environ.get("CI_RV_TAGS")
output_json = "--json" in sys.argv
@@ -106,40 +137,10 @@ else:
version = src_version
version_long = ""
-# version is X.Y.Z[+xxx.gHASH]
-# version_long is "" or X.Y[.Z]-xxx-gHASH
-# validate the version formats
-if not re.match('^\d+\.\d+\.\d+', version):
- version = src_version
-
-if version_long and \
- not re.match('^\d+\.\d+(\.\d+)*-\d+-g*', version_long):
- version_long = ""
-
-release = version.partition("-")[0]
-extra = None
-commit = None
-distance = None
-
-if version_long:
- info = version_long.partition("-")[2]
- extra = f"-{info}"
- distance, commit = info.split("-")
- # remove the 'g' from gHASH
- commit = commit[1:]
-
-data = {
- "release": release,
- "version": version,
- "version_long": version_long,
- "extra": extra,
- "commit": commit,
- "distance": distance,
- "is_release_branch_ci": is_release_branch_ci,
-}
if output_json:
- sys.stdout.write(json.dumps(data, indent=1) + "\n")
+ details = get_extra_details(version, version_long)
+ sys.stdout.write(json.dumps(details, indent=1) + "\n")
else:
sys.stdout.write(version + "\n")
Changes:
One added benefit is the If it works for you, feel free to |
Sadly this won't work. I am getting the following when I run my unit tests:
|
@TheRealFalcon I made some small changes on top and this seems to work diff --git a/tools/read-version b/tools/read-version
index ebc46462..1990fa5b 100755
--- a/tools/read-version
+++ b/tools/read-version
@@ -58,10 +58,10 @@ def get_extra_details(version, version_long):
distance = None
# Should match upstream version number. E.g., 23.1 or 23.1.2
- short_regex = r"(\d+\.\d+\.?\d*)"
+ short_regex = r"^(\d+\.\d+\.?\d*)"
# Should match version including upstream version, distance, and commit
# E.g., 23.1.2-10-g12ab34cd
- long_regex = r"(\d+\.\d+\.?\d*){1}.*-(\d+)+-g([a-f0-9]{8}){1}.*"
+ long_regex = r"^(\d+\.\d+\.?\d*){1}.*-(\d+)+-g([a-f0-9]{8}){1}.*"
short_match = re.search(short_regex, version)
long_match = re.search(long_regex, version_long)
@@ -70,6 +70,8 @@ def get_extra_details(version, version_long):
extra = f"-{distance}-g{commit}"
elif short_match:
release = short_match.groups()[0]
+ else:
+ version = src_version
return {
"release": release,
@@ -137,10 +139,11 @@ else:
version_long = ""
+details = get_extra_details(version, version_long)
+
if output_json:
- details = get_extra_details(version, version_long)
sys.stdout.write(json.dumps(details, indent=1) + "\n")
else:
- sys.stdout.write(version + "\n")
+ sys.stdout.write(details["version"] + "\n")
sys.exit(0) |
I see, thanks. I forgot about the PEP 440 aspect. One more attempt if you don't mind. This is against my previous patch, not the patch you responded with, but it takes into account what you provided in yours: diff --git a/tools/read-version b/tools/read-version
index 5d6112dc4..e2efd9e58 100755
--- a/tools/read-version
+++ b/tools/read-version
@@ -51,7 +51,7 @@ def is_gitdir(path):
return False
-def get_extra_details(version, version_long):
+def get_version_details(version, version_long):
release = None
extra = None
commit = None
@@ -138,10 +138,18 @@ else:
version_long = ""
+details = get_version_details(version, version_long)
+
if output_json:
- details = get_extra_details(version, version_long)
sys.stdout.write(json.dumps(details, indent=1) + "\n")
else:
- sys.stdout.write(version + "\n")
+ output = ""
+ if details["release"]:
+ output += details["release"]
+ if details["extra"]:
+ output += details["extra"]
+ if not output:
+ output = src_version
+ sys.stdout.write(output + "\n")
sys.exit(0)
I'd like to not use |
…g errors git describe may not return version/tags in the format that the read-version tool expects. Make the tool robust so that it can gracefully handle version strings that are not in the regular format. We use regex to capture the details we care about, but if we cannot find them, we won't traceback and will continue to use version and version_long as expected. Signed-off-by: Ani Sinha <[email protected]>
@TheRealFalcon yes this is perfect! Works for us. |
9efc1e4
to
657f193
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR and for working through this with me!
…g errors (canonical#4234) git describe may not return version/tags in the format that the read-version tool expects. Make the tool robust so that it can gracefully handle version strings that are not in the regular format. We use regex to capture the details we care about, but if we cannot find them, we won't traceback and will continue to use version and version_long as expected. Signed-off-by: Ani Sinha <[email protected]>
git describe may not return version/tags in the format that the read-version tool expects. Make the tool robust so that it can gracefully handle version strings that are not in the regular format.