silentbugs / hon-client-scraper

Messy python script to quickly fetch hon client versions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SemverUtil next() function to get the next version

silentbugs opened this issue · comments

HoN client semver

HoN uses 3 and 4 digit semantic versioning:

  • major.minor.patch (e.g. 1.0.21)
  • major.minor.patch.hotfix (e.g. 1.0.21.1)

Hotfix versions don't always exist for a specific patch version.

We need to define some basic functionality for fetching next version for a given one. I just hacked together a very messy way that I believe works for the next() function on SemverUtil. It takes as a parameter an exists boolean, because the next version depends on if the current version exists. For example: 1.0.1 next() is 1.0.1.0 if it exists, 1.1.0 if it doesn't. 1.0.1.0 next() is 1.0.1.1 if it exists, or 1.0.2 if it doesn't. Feel free to clean up the messy function:

    def next(self, exists=True):
        _items = self.items[:]

        if exists:
            if len(_items) == 4:
                # for 4 digit semvers that exist, the next hotfix version
                _items[-1] = str(int(_items[-1]) + 1)

            if len(_items) == 3:
                # for 3 digit semvers that exist, the first hotfix version
                _items.append('0')

            return self._create_semver(_items)
        else:
            if len(_items) == 3:
                if _items[1] == '0' and _items[2] == '0':
                    # we ran out of versions
                    raise SemverException('There is no next version')
                elif _items[2] == '0':
                    # for zero 3rd digit semvers, we probably ran out of minor
                    # versionsincrement major
                    _items[0] = str(int(_items[0]) + 1)
                    _items[1] = '0'
                    _items[2] = '0'
                else:
                    # for non-zero 3rd digit semvers, increment 2nd digit,
                    # set 3rd to 0
                    _items[1] = str(int(_items[1]) + 1)
                    _items[2] = '0'

            if len(_items) == 4:
                # for 4 digit semvers that don't exist, remove 4th digit
                # increment 3rd by 1
                _items = _items[:3]
                _items[-1] = str(int(_items[-1]) + 1)

            return self._create_semver(_items)

Some tests:

>>> from utils import SemverUtil
>>> version = SemverUtil('1.2.0')

>>> version.next()
'1.2.0.0'

>>> version.next(exists=False)
'1.2.1'

>>> version = SemverUtil(version.next(exists=False))
>>> print(version)
'1.3.0'

>>> version.next()
'1.3.0.0'

>>> version.next(exists=False)
'2.0.0'

>>> version = SemverUtil(version.next(exists=False)
>>> version.next()
'2.0.0.0'

>>> version.next(exists=False)
utils.SemverException: There is no next version

Even though the code is really messy I think I hacked together semver.next().

Rewrote the next() function a bit more cleanly so that the versions are readable. The spaghetti logic unfortunately still exists:

    def next(self, exists=True):
        sv = Semver(self.__str__())

        if exists:
            if sv.hotfix is not None:
                # for 4 digit semvers that exist, the next hotfix version
                sv.hotfix += 1

            if sv.hotfix is None:
                # for 3 digit semvers that exist, the first hotfix version
                sv.hotfix = 0

        else:
            if sv.hotfix is None:
                if sv.minor == 0 and sv.patch == 0:
                    # we ran out of versions
                    raise SemverException('There is no next version')
                elif sv.patch == 0:
                    # for zero 3rd digit semvers, we probably ran out of minor
                    # versionsincrement major
                    sv.major += 1
                    sv.minor = 0
                    sv.patch = 0
                else:
                    # for non-zero 3rd digit semvers, increment 2nd digit,
                    # set 3rd to 0
                    sv.minor += 1
                    sv.patch = 0

            if sv.hotfix is not None:
                # for 4 digit semvers that don't exist, remove 4th digit
                # increment 3rd by 1
                sv.hotfix = None
                sv.patch += 1

        return sv