Skip to content

Commit

Permalink
qubes: use LooseVersion for parsing Heads version
Browse files Browse the repository at this point in the history
Heads may present its version as `git describe` output, which is the
case for both development builds, as well as automatic CI builds.
Standard Version() class from packaging doesn't handle this format, and
LegacyVersion() (which works) is removed from packaging release 22.0.
Vendor in LooseVersion from distutils.version (which is also
deprecated), which has explicitly meant to parse arbitrary version
format.

Signed-off-by: Marek Marczykowski-Górecki <[email protected]>
  • Loading branch information
marmarek committed Feb 25, 2023
1 parent e5a7713 commit ba62421
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
104 changes: 104 additions & 0 deletions contrib/qubes/src/qubes_fwupd_common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import grp
import re
import os

EXIT_CODES = {"ERROR": 1, "SUCCESS": 0, "NOTHING_TO_DO": 2}
Expand Down Expand Up @@ -26,3 +27,106 @@ def create_dirs(*args):
f"the personal data!!{WARNING_COLOR}"
)
os.umask(old_umask)

class LooseVersion:

"""Version numbering for anarchists and software realists.
Implements the standard interface for version number classes as
described above. A version number consists of a series of numbers,
separated by either periods or strings of letters. When comparing
version numbers, the numeric components will be compared
numerically, and the alphabetic components lexically. The following
are all valid version numbers, in no particular order:
1.5.1
1.5.2b2
161
3.10a
8.02
3.4j
1996.07.12
3.2.pl0
3.1.1.6
2g6
11g
0.960923
2.2beta29
1.13++
5.5.kw
2.0b1pl0
In fact, there is no such thing as an invalid version number under
this scheme; the rules for comparison are simple and predictable,
but may not always give the results you want (for some definition
of "want").
"""

component_re = re.compile(r'(\d+ | [a-z]+ | \.)', re.VERBOSE)

def __init__ (self, vstring=None):
if vstring:
self.parse(vstring)

def parse (self, vstring):
# I've given up on thinking I can reconstruct the version string
# from the parsed tuple -- so I just store the string here for
# use by __str__
self.vstring = vstring
components = [x for x in self.component_re.split(vstring)
if x and x != '.']
for i, obj in enumerate(components):
try:
components[i] = int(obj)
except ValueError:
pass

self.version = components

def __str__ (self):
return self.vstring

def __repr__ (self):
return "LooseVersion ('%s')" % str(self)

def _cmp (self, other):
if isinstance(other, str):
other = LooseVersion(other)
elif not isinstance(other, LooseVersion):
return NotImplemented

if self.version == other.version:
return 0
if self.version < other.version:
return -1
if self.version > other.version:
return 1

def __eq__(self, other):
c = self._cmp(other)
if c is NotImplemented:
return c
return c == 0

def __lt__(self, other):
c = self._cmp(other)
if c is NotImplemented:
return c
return c < 0

def __le__(self, other):
c = self._cmp(other)
if c is NotImplemented:
return c
return c <= 0

def __gt__(self, other):
c = self._cmp(other)
if c is NotImplemented:
return c
return c > 0

def __ge__(self, other):
c = self._cmp(other)
if c is NotImplemented:
return c
return c >= 0
6 changes: 3 additions & 3 deletions contrib/qubes/src/qubes_fwupd_heads.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import shutil
import xml.etree.ElementTree as ET
from packaging.version import Version
from qubes_fwupd_common import EXIT_CODES, create_dirs
from qubes_fwupd_common import EXIT_CODES, create_dirs, LooseVersion

FWUPDTOOL = "/bin/fwupdtool"

Expand Down Expand Up @@ -88,10 +88,10 @@ def _parse_heads_updates(self, device):
return EXIT_CODES["NOTHING_TO_DO"]
for release in heads_metadata_info.find("releases").findall("release"):
release_ver = release.get("version")
if self.heads_version == "heads" or Version(release_ver) > Version(
if self.heads_version == "heads" or LooseVersion(release_ver) > LooseVersion(
self.heads_version
):
if not self.heads_update_version or Version(release_ver) > Version(
if not self.heads_update_version or LooseVersion(release_ver) > LooseVersion(
self.heads_update_version
):
self.heads_update_url = release.find("location").text
Expand Down

0 comments on commit ba62421

Please sign in to comment.