Skip to content
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

The package comparison option in the package module is not working as expected. #733

Open
700grm opened this issue Sep 2, 2023 · 1 comment

Comments

@700grm
Copy link
Contributor

700grm commented Sep 2, 2023

Software comparison does not work due to lexicographic order. Because of this, the Python statement assert 4.14.0 >= 10.14.0 is True, instead of False.

For example, let's test if the installed package dnf-4.14.0-5.el9_2.noarch is equal to or greater than the package version 10.14.0

The result will be True since version 4.14.0 is equal to version 4.14.0.

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version >= 4.14.0

The result will be True since the version of package 5.14.0 is greater than the version of package 4.14.0.

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version >= 5.14.0

However, if we compare it with version 10.14.0, the result will be True. The script will output that 5.14.0 is bigger than 10.14.0.

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version >= 10.14.0

In Python, when you use the >= operator to compare strings, it performs lexicographic (dictionary-style) comparison based on the ASCII values of the characters. In lexicographic comparison, characters are compared one by one from left to right.

In our specific case, 4.14.0 is indeed greater than 10.14.0 according to lexicographic comparison because the first character 4 is greater than 1. The comparison stops as soon as a character difference is found, so the rest of the characters don't matter in this context.

Here's a step-by-step comparison:

Compare the first characters: '4' vs. '1'. '4' is greater than '1', so '4.14.0' is considered greater.

As a result, the assertion assert '4.14.0' >= '10.14.0' is true because, in lexicographic order, 4.14.0 comes after 10.14.0.

I propose adding function to convert 4.14.0 to 004.014.000 for comparison purposes for example:

def pad_version(version_string):
    parts = version_string.split(".")
    compare_version = ".".join([part.zfill(3) for part in parts])
    return compare_version

# Example usage:
version_string = "4.14.0"
compare_version = pad_version(version_string)
print(compare_version)

# output
# 004.014.000

In this case, we will be able to execute a simple package comparison; however, we will have to write the version as 010.014.000 instead of 10.14.0

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version.compare >= 010.014.000

Maybe it doesn't look like the most beautiful solution; however, it will allow for easy-to-read package comparison code. And it will work with other distros and releases

I will appreciate your thought on this

@700grm 700grm changed the title Package comparison option in package module does not work as expected The package comparison option in the package module is not working as expected. Sep 2, 2023
@700grm
Copy link
Contributor Author

700grm commented Sep 2, 2023

I will be more than happy to take care of it; however, since I'm just starting my adventure with Python, I would appreciate more suggestions on how to implement the function into package module

Or split it into integers

# Split the strings into components
ver1 = '4.14.0'.split('.')
ver2 = '10.14.0'.split('.')

# Compare the first component as integers
print(int(ver1[0]) >= int(ver2[0]))  # Output: False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant