From cd318c9926c1d63b960f97b10b83214e0e38aca2 Mon Sep 17 00:00:00 2001 From: Caspar van Leeuwen Date: Tue, 24 Sep 2024 10:58:07 +0200 Subject: [PATCH] Don't set fallback version in __init__.py anymore. We should have always gotten one with one of the three methods. If not, it's a bug --- eessi/testsuite/__init__.py | 60 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/eessi/testsuite/__init__.py b/eessi/testsuite/__init__.py index afb16eab..3e0f5725 100644 --- a/eessi/testsuite/__init__.py +++ b/eessi/testsuite/__init__.py @@ -1,9 +1,6 @@ # WARNING: this file is imported in setup.py # To make sure this works, we should avoid using imports other than from the Python standard library -# Set a fallback, so that failing to get the version from elsewhere doesn't cause a breaking error -__version__ = "0.0.0" - try: # If this is an installed package, setuptools_scm will have written the _version.py file in the current directory from ._version import __version__ @@ -36,6 +33,33 @@ file = None try: file = open(pyproject_toml, 'r') + # Open the file and parse it manually + fallback_version = None + with file: + for line in file: + stripped_line = line.strip() + + # Check if we're entering the [tool.setuptools_scm] section + if stripped_line == "[tool.setuptools_scm]": + in_setuptools_scm_section = True + elif stripped_line.startswith("[") and in_setuptools_scm_section: + # We've reached a new section, so stop searching + break + + # If we're in the right section, look for the fallback_version key + if in_setuptools_scm_section and stripped_line.startswith("fallback_version"): + # Extract the value after the '=' sign and strip any surrounding quotes or whitespace + fallback_version = stripped_line.split('=', 1)[1].strip().strip('"').strip("'") + break + # Account for the possibility that we failed to extract the fallback_version field from pyproject.toml + if fallback_version: + __version__ = fallback_version + else: + msg = "fallback_version not found in file %s" % pyproject_toml + msg += " when trying the get the EESSI test suite version. This should never happen." + msg += " Please report an issue on Github, including information on how you installed" + msg += " the EESSI test suite. Defaulting to version %s." % __version__ + print(msg) except FileNotFoundError: msg = "File %s not found when trying to extract the EESSI test suite version from" % pyproject_toml msg += " pyproject.toml. This should never happen. Please report an issue on GitHub, including information on how you" @@ -44,30 +68,6 @@ except Exception as e: print("When trying to open file %s, an exception was raised: %s." % (pyproject_toml, e)) - # Open the file and parse it manually - fallback_version = None - with file: - for line in file: - stripped_line = line.strip() - - # Check if we're entering the [tool.setuptools_scm] section - if stripped_line == "[tool.setuptools_scm]": - in_setuptools_scm_section = True - elif stripped_line.startswith("[") and in_setuptools_scm_section: - # We've reached a new section, so stop searching - break - - # If we're in the right section, look for the fallback_version key - if in_setuptools_scm_section and stripped_line.startswith("fallback_version"): - # Extract the value after the '=' sign and strip any surrounding quotes or whitespace - fallback_version = stripped_line.split('=', 1)[1].strip().strip('"').strip("'") - break - # Account for the possibility that we failed to extract the fallback_version field from pyproject.toml - if fallback_version: - __version__ = fallback_version - else: - msg = "fallback_version not found in file %s" % pyproject_toml - msg += " when trying the get the EESSI test suite version. This should never happen." - msg += " Please report an issue on Github, including information on how you installed" - msg += " the EESSI test suite. Defaulting to version %s." % __version__ - print(msg) + # One of the above three methods SHOULD work in any situation. If it doesn't, one will at this point + # get an error when trying to import __version__ from this file. That's ok, it's considered a bug + # if you reach this point without having a __version__ set