|
| 1 | +"""Support Singularity{,-CE} {2,3}.x or Apptainer 1.x.""" |
| 2 | +import re |
| 3 | +from subprocess import check_output # nosec |
| 4 | +from typing import List, Optional, Tuple |
| 5 | + |
| 6 | +from .loghandler import _logger |
| 7 | + |
| 8 | +# Cached version number of singularity |
| 9 | +# This is a list containing major and minor versions as integer. |
| 10 | +# (The number of minor version digits can vary among different distributions, |
| 11 | +# therefore we need a list here.) |
| 12 | +_SINGULARITY_VERSION: Optional[List[int]] = None |
| 13 | +# Cached flavor / distribution of singularity |
| 14 | +# Can be singularity, singularity-ce or apptainer |
| 15 | +_SINGULARITY_FLAVOR: str = "" |
| 16 | + |
| 17 | + |
| 18 | +def get_version() -> Tuple[List[int], str]: |
| 19 | + """ |
| 20 | + Parse the output of 'singularity --version' to determine the flavor and version. |
| 21 | +
|
| 22 | + Both pieces of information will be cached. |
| 23 | +
|
| 24 | + :returns: A tuple containing: |
| 25 | + - A tuple with major and minor version numbers as integer. |
| 26 | + - A string with the name of the singularity flavor. |
| 27 | + """ |
| 28 | + global _SINGULARITY_VERSION # pylint: disable=global-statement |
| 29 | + global _SINGULARITY_FLAVOR # pylint: disable=global-statement |
| 30 | + if _SINGULARITY_VERSION is None: |
| 31 | + version_output = check_output( # nosec |
| 32 | + ["singularity", "--version"], universal_newlines=True |
| 33 | + ).strip() |
| 34 | + |
| 35 | + version_match = re.match(r"(.+) version ([0-9\.]+)", version_output) |
| 36 | + if version_match is None: |
| 37 | + raise RuntimeError("Output of 'singularity --version' not recognized.") |
| 38 | + |
| 39 | + version_string = version_match.group(2) |
| 40 | + _SINGULARITY_VERSION = [int(i) for i in version_string.split(".")] |
| 41 | + _SINGULARITY_FLAVOR = version_match.group(1) |
| 42 | + |
| 43 | + _logger.debug( |
| 44 | + f"Singularity version: {version_string}" " ({_SINGULARITY_FLAVOR}." |
| 45 | + ) |
| 46 | + return (_SINGULARITY_VERSION, _SINGULARITY_FLAVOR) |
| 47 | + |
| 48 | + |
| 49 | +def is_apptainer_1_or_newer() -> bool: |
| 50 | + """ |
| 51 | + Check if apptainer singularity distribution is version 1.0 or higher. |
| 52 | +
|
| 53 | + Apptainer v1.0.0 is compatible with SingularityCE 3.9.5. |
| 54 | + See: https://github.com/apptainer/apptainer/releases |
| 55 | + """ |
| 56 | + v = get_version() |
| 57 | + if v[1] != "apptainer": |
| 58 | + return False |
| 59 | + return v[0][0] >= 1 |
| 60 | + |
| 61 | + |
| 62 | +def is_version_2_6() -> bool: |
| 63 | + """ |
| 64 | + Check if this singularity version is exactly version 2.6. |
| 65 | +
|
| 66 | + Also returns False if the flavor is not singularity or singularity-ce. |
| 67 | + """ |
| 68 | + v = get_version() |
| 69 | + if v[1] != "singularity" and v[1] != "singularity-ce": |
| 70 | + return False |
| 71 | + return v[0][0] == 2 and v[0][1] == 6 |
| 72 | + |
| 73 | + |
| 74 | +def is_version_3_or_newer() -> bool: |
| 75 | + """Check if this version is singularity version 3 or newer or equivalent.""" |
| 76 | + if is_apptainer_1_or_newer(): |
| 77 | + return True # this is equivalent to singularity-ce > 3.9.5 |
| 78 | + v = get_version() |
| 79 | + return v[0][0] >= 3 |
| 80 | + |
| 81 | + |
| 82 | +def is_version_3_1_or_newer() -> bool: |
| 83 | + """Check if this version is singularity version 3.1 or newer or equivalent.""" |
| 84 | + if is_apptainer_1_or_newer(): |
| 85 | + return True # this is equivalent to singularity-ce > 3.9.5 |
| 86 | + v = get_version() |
| 87 | + return v[0][0] >= 4 or (v[0][0] == 3 and v[0][1] >= 1) |
| 88 | + |
| 89 | + |
| 90 | +def is_version_3_4_or_newer() -> bool: |
| 91 | + """Detect if Singularity v3.4+ is available.""" |
| 92 | + if is_apptainer_1_or_newer(): |
| 93 | + return True # this is equivalent to singularity-ce > 3.9.5 |
| 94 | + v = get_version() |
| 95 | + return v[0][0] >= 4 or (v[0][0] == 3 and v[0][1] >= 4) |
0 commit comments