doccaz / scc-tools

A set of simple tools to interact with SUSE Customer Center (SCC)
MIT License
12 stars 1 forks source link

Deprecation warning on recent Python versions #36

Open doccaz opened 2 months ago

doccaz commented 2 months ago

Users are getting this warning on newer Python versions:

$ ./vercheck.py -l
./vercheck.py:13: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.version import LooseVersion

This is due to PEP 632, which will deprecate the Distutils package in the near future. We need to re-implement the same features with setuptools.

doccaz commented 2 months ago

PEP632 states that you must use the packaging.version.Version() class to substitute the distutils one. I made a quick script to read one of vercheck's CSV reports and try out a simple comparison with two versions to validate this:

#!/usr/bin/python3

import sys
from distutils.version import LooseVersion

from packaging.version import Version

def main():

    args = sys.argv

    filename = args[1]
    print(f'reading {filename}')
    with open(filename, 'r') as f:
        lines=f.readlines()

    versions=[]

    for line in lines:
        versions.append([line.split(',')[1], line.split(',')[2]])

    try:
        for old,new in versions:
            print(f"comparing if version [{old}] < [{new}])")
            print(f"[distutils]: {LooseVersion(old) < LooseVersion(new)}")
            print(f"[packaging]: {Version(old) < Version(new)}")
    except Exception as e:
        print(f'----> comparison failed: {e}')
    return

if __name__ == '__main__':
    main()

Running it yields not so good results...

erico@suselab-erico:~/Projetos/scc-tools> ./testversions.py vercheck-different-scc_rmt_231027_1743.csv 
reading vercheck-different-scc_rmt_231027_1743.csv
comparing if version [84.87+git20180409.04c9dae-150300.10.3.1] < [84.87+git20180409.04c9dae-150300.10.20.1])
/home/erico/Projetos/scc-tools/./testversions.py:26: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  print(f"[distutils]: {LooseVersion(old) < LooseVersion(new)}")
[distutils]: True
[packaging]: True
comparing if version [84.87+git20180409.04c9dae-150300.10.3.1] < [84.87+git20180409.04c9dae-150300.10.20.1])
[distutils]: True
[packaging]: True
comparing if version [3.0.4-150500.11.3.1] < [3.0.4-150500.11.9.1])
[distutils]: True
----> comparison failed: Invalid version: '3.0.4-150500.11.3.1'

Apparently packaging.version uses a fixed regex that doesn't recognize the "150500" that SLE uses. According to this, you can just override the regex by assigning a new value to packaging.version.VERSION_PATTERN first.

doccaz commented 2 months ago

As expected, the default regex is awfully complex: https://regex101.com/r/ryw6wU/1

doccaz commented 1 month ago

Reference URLs: