From 748ad1855119ee3f014f2e7d36b53aeeff10809e Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Tue, 16 Jan 2024 14:16:57 +0100 Subject: [PATCH] Self-review --- scripts/version-patcher.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/scripts/version-patcher.py b/scripts/version-patcher.py index 1be65a59a..7088bb313 100644 --- a/scripts/version-patcher.py +++ b/scripts/version-patcher.py @@ -3,20 +3,25 @@ import subprocess import sys -def replace_version(filename, new_text): +def replace_version(filename, build_info): pattern = r'(Current : constant String := "[^+]+\+)([^"]*)(";)' - with open(filename, 'rb') as file: - content = file.read() # Depending on the context in which this is run, there may be mix-ups with # line terminators between the environment detected, github runner, etc... # So just keep them as they are and that should always work. - content = content.decode() - new_content = re.sub(pattern, r'\g<1>' + new_text + r'\3', content) + with open(filename, 'rb') as file: + content = file.read().decode() + + # The pattern captures the part between '+' and '";', replacing it with our + # new build information + + new_content = re.sub(pattern, r'\g<1>' + build_info + r'\3', content) + + # A few sanity checks and write if needed if new_content == content: - if new_text in content: + if build_info in content: print(f"Note: version in {filename} already up to date") else: print(f"WARNING: failed to update version in {filename}") @@ -32,11 +37,11 @@ def replace_version(filename, new_text): sys.exit(0) # If there is an argument to the script, retrieve it here and use it as the new -# dirty flag +# dirty flag after the commit if len(sys.argv) > 1: dirty = sys.argv[1] else: - # Detect the current directory contains changes + # Detect whether the current directory contains changes if subprocess.call(['git', 'diff-index', '--quiet', 'HEAD', '--']) != 0: dirty = "_dirty" else: @@ -45,6 +50,6 @@ def replace_version(filename, new_text): # Find the short git commit of the repository in the current directory commit = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip() -# Replace the build version part with the short commit hash +# Replace the build version part with the short commit hash plus any extra info print(f"Updating version in src/alire/alire-version.ads to commit {commit}{dirty}...") replace_version('src/alire/alire-version.ads', commit+dirty)