Skip to content

OS Version

netutils.os_version

Functions for working with OS Versions.

compare_version_loose(current_version, comparison, target_version)

Compares two version strings using the specified comparison operation, based on LooseVersion.

Parameters:

Name Type Description Default
current_version str

The current version string to compare.

required
comparison str

The comparison operation as a string (<, <=, ==, !=, >, >=).

required
target_version str

The target version string to compare against.

required

Returns:

Name Type Description
bool bool

The result of the comparison.

Raises:

Type Description
ValueError

If there is an invalid comparison.

TypeError

If not a valid version.

Example

from netutils.os_version import compare_version_loose compare_version_loose("3.3.3a", "==", "3.3.3a") True compare_version_loose("3.3.2", "<=", "3.3.3") True compare_version_loose("3.3.2", ">=", "3.3.3") False

Source code in netutils/os_version.py
def compare_version_loose(current_version: str, comparison: str, target_version: str) -> bool:
    """
    Compares two version strings using the specified comparison operation, based on LooseVersion.

    Args:
        current_version (str): The current version string to compare.
        comparison (str): The comparison operation as a string (<, <=, ==, !=, >, >=).
        target_version (str): The target version string to compare against.

    Returns:
        bool: The result of the comparison.

    Raises:
        ValueError: If there is an invalid comparison.
        TypeError: If not a valid version.

    Example:
        >>> from netutils.os_version import compare_version_loose
        >>> compare_version_loose("3.3.3a", "==", "3.3.3a")
        True
        >>> compare_version_loose("3.3.2", "<=", "3.3.3")
        True
        >>> compare_version_loose("3.3.2", ">=", "3.3.3")
        False
        >>>
    """
    return _compare_version(current_version, comparison, target_version, "loose")

compare_version_strict(current_version, comparison, target_version)

Compares two version strings using the specified comparison operation, based on LooseVersion.

Parameters:

Name Type Description Default
current_version str

The current version string to compare.

required
comparison str

The comparison operation as a string (<, <=, ==, !=, >, >=).

required
target_version str

The target version string to compare against.

required

Returns:

Name Type Description
bool bool

The result of the comparison.

Raises:

Type Description
ValueError

If there is an invalid comparison.

ValueError

If not a valid version.

Example

from netutils.os_version import compare_version_strict compare_version_strict("3.3.3", "==", "3.3.3") True compare_version_strict("3.3.2", "<=", "3.3.3") True compare_version_strict("3.3.2", ">=", "3.3.3") False

Source code in netutils/os_version.py
def compare_version_strict(current_version: str, comparison: str, target_version: str) -> bool:
    """
    Compares two version strings using the specified comparison operation, based on LooseVersion.

    Args:
        current_version (str): The current version string to compare.
        comparison (str): The comparison operation as a string (<, <=, ==, !=, >, >=).
        target_version (str): The target version string to compare against.

    Returns:
        bool: The result of the comparison.

    Raises:
        ValueError: If there is an invalid comparison.
        ValueError: If not a valid version.

    Example:
        >>> from netutils.os_version import compare_version_strict
        >>> compare_version_strict("3.3.3", "==", "3.3.3")
        True
        >>> compare_version_strict("3.3.2", "<=", "3.3.3")
        True
        >>> compare_version_strict("3.3.2", ">=", "3.3.3")
        False
        >>>
    """
    return _compare_version(current_version, comparison, target_version, "strict")

get_upgrade_path(current_version, target_version, firmware_list)

Utility to return the upgrade path from the current to target firmware version.

Returns:

Type Description
List[str]

List of firmware versions to upgrade from current to target.

Parameters:

Name Type Description Default
current_version str

Current firmware version.

required
target_version str

Target firmware version.

required
firmware_list List[str]

List of firmware versions to use as the upgrade path.

required

Raises:

Type Description
ValueError

If target version is older than current version.

ValueError

If target version equals current version.

Examples:

>>> from netutils.os_version import get_upgrade_path
>>> get_upgrade_path("9.1.6", "10.1.9", ["9.1.10", "9.1.15-h1", "10.0.0", "10.1.9"])
['9.1.10', '9.1.15-h1', '10.0.0', '10.1.9']
>>> from netutils.constants import UPGRADE_PATHS
>>> get_upgrade_path("9.1.6", "10.1.9", UPGRADE_PATHS["PANOS_OFFICIAL_V1"])
['9.1.15-h1', '10.0.0', '10.0.12', '10.1.0', '10.1.9']
Source code in netutils/os_version.py
def get_upgrade_path(current_version: str, target_version: str, firmware_list: t.List[str]) -> t.List[str]:
    """Utility to return the upgrade path from the current to target firmware version.

    Returns:
        List of firmware versions to upgrade from current to target.

    Args:
        current_version: Current firmware version.
        target_version: Target firmware version.
        firmware_list: List of firmware versions to use as the upgrade path.

    Raises:
        ValueError: If target version is older than current version.
        ValueError: If target version equals current version.

    Examples:
        >>> from netutils.os_version import get_upgrade_path
        >>> get_upgrade_path("9.1.6", "10.1.9", ["9.1.10", "9.1.15-h1", "10.0.0", "10.1.9"])
        ['9.1.10', '9.1.15-h1', '10.0.0', '10.1.9']
        >>> from netutils.constants import UPGRADE_PATHS
        >>> get_upgrade_path("9.1.6", "10.1.9", UPGRADE_PATHS["PANOS_OFFICIAL_V1"])
        ['9.1.15-h1', '10.0.0', '10.0.12', '10.1.0', '10.1.9']
    """
    if LooseVersion(current_version) > LooseVersion(target_version):
        raise ValueError("Target version must be newer than current version.")

    if LooseVersion(current_version) == LooseVersion(target_version):
        raise ValueError("Target version equals current version. No upgrade necessary.")

    upgrade_path = [
        version
        for version in firmware_list
        if LooseVersion(version) > LooseVersion(current_version)
        and LooseVersion(version) <= LooseVersion(target_version)
    ]

    if target_version not in upgrade_path:
        upgrade_path.append(target_version)

    return upgrade_path

version_metadata(vendor, os_type, version)

If a custom version parser is avaialable, use it.

Parameters:

Name Type Description Default
vendor str

Vendor name (Ex: "Juniper")

required
os_type str

OS Type (Ex: "JunOS")

required
version str

OS Version (Ex: "12.4R")

required

Returns:

Name Type Description
dict Dict[str, Any]

Dict of broken down version into vendor standards.

Examples:

>>> from netutils.os_version import version_metadata
>>> version_metadata("Cisco", "IOS", "15.5")
{'major': '15', 'minor': '5', 'vendor_metadata': False}
>>> version_metadata("juniper", "junos", "12.4R")
{'isservice': False, 'ismaintenance': False, 'isfrs': True, 'isspecial': False, 'service': None, 'service_build': None, 'service_respin': None, 'main': '12', 'minor': '4', 'type': 'R', 'build': None, 'major': '12', 'patch': None, 'vendor_metadata': True}
Source code in netutils/os_version.py
def version_metadata(vendor: str, os_type: str, version: str) -> t.Dict[str, t.Any]:
    """If a custom version parser is avaialable, use it.

    Args:
        vendor (str): Vendor name (Ex: "Juniper")
        os_type (str): OS Type (Ex: "JunOS")
        version (str): OS Version (Ex: "12.4R")

    Returns:
        dict: Dict of broken down version into vendor standards.

    Examples:
        >>> from netutils.os_version import version_metadata
        >>> version_metadata("Cisco", "IOS", "15.5")
        {'major': '15', 'minor': '5', 'vendor_metadata': False}
        >>> version_metadata("juniper", "junos", "12.4R")
        {'isservice': False, 'ismaintenance': False, 'isfrs': True, 'isspecial': False, 'service': None, 'service_build': None, 'service_respin': None, 'main': '12', 'minor': '4', 'type': 'R', 'build': None, 'major': '12', 'patch': None, 'vendor_metadata': True}
    """
    parsed_version = {}
    if vendor in version_metadata_parsers:
        try:
            parsed_version = version_metadata_parsers[vendor][os_type](version)  # type:ignore
            parsed_version.update({"vendor_metadata": True})
        except KeyError:
            parsed_version = version_metadata_parsers["default"](version)  # type:ignore
            parsed_version.update({"vendor_metadata": False})
    else:
        parsed_version = version_metadata_parsers["default"](version)  # type:ignore
        parsed_version.update({"vendor_metadata": False})

    return parsed_version  # type:ignore