Skip to content

Commit

Permalink
Removed usage of Python's deprecated distutils.version package.
Browse files Browse the repository at this point in the history
The distutils package was formally deprecated in Python 3.10 and
removed in Python 3.12.
  • Loading branch information
felixxm authored Jan 3, 2024
1 parent 69e8325 commit 7596a1f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 2 additions & 3 deletions docs/templatetags/docs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from distutils.version import StrictVersion

from django import template
from django.utils.safestring import mark_safe
from django.utils.version import get_version_tuple
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name
Expand Down Expand Up @@ -46,7 +45,7 @@ def get_all_doc_versions(context, url=None):
versions.append(release.version)

# Save the versions into the context
versions = sorted(StrictVersion(x) for x in versions if x != "dev")
versions = sorted(get_version_tuple(x) for x in versions if x != "dev")

This comment has been minimized.

Copy link
@trojjer

trojjer Jan 3, 2024

Hi. Could this change in the templatetag have broken the rendering of the version picker? Not seen this before; all the href strings are the same and text is the representation of a tuple.

Screenshot 2024-01-03 at 17 52 36

This comment has been minimized.

Copy link
@trojjer

trojjer Jan 3, 2024

I see that before, the items in versions passed to the context here were strings via StrictVersion(x):

>>> str(StrictVersion("5.0")) '5.0'

return [str(x) for x in versions] + ["dev"]


Expand Down
5 changes: 3 additions & 2 deletions releases/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import datetime
from distutils.version import LooseVersion

from django.conf import settings
from django.core.cache import cache
from django.db import models
from django.utils.functional import cached_property
from django.utils.version import get_complete_version, get_main_version

from .utils import get_loose_version_tuple


# A version of django.utils.version.get_version() which maps "rc" to "rc"
# (packages generated using setuptools 8+) instead of "c" (older versions of
Expand Down Expand Up @@ -198,7 +199,7 @@ def __str__(self):
def version_tuple(self):
"""Return a tuple in the format of django.VERSION."""
version = self.version.replace("-", "").replace("_", "")
version = LooseVersion(version).version
version = list(get_loose_version_tuple(version))
if len(version) == 2:
version.append(0)
if not isinstance(version[2], int):
Expand Down
5 changes: 2 additions & 3 deletions releases/templatetags/release_notes.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from distutils.version import LooseVersion

from django import template
from django.utils.html import format_html
from django.utils.translation import gettext as _
from django_hosts.resolvers import reverse

from ..models import Release
from ..utils import get_loose_version_tuple

register = template.Library()


@register.simple_tag()
def release_notes(version, show_version=False):
version_x_dot_y = ".".join(str(x) for x in LooseVersion(version).version[:2])
version_x_dot_y = ".".join(str(x) for x in get_loose_version_tuple(version)[:2])
is_pre_release = any(c in version for c in ("a", "b", "c"))
# links for prereleases don't have their own release notes
display_version = version_x_dot_y if is_pre_release else version
Expand Down
19 changes: 19 additions & 0 deletions releases/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.utils.regex_helper import _lazy_re_compile

version_component_re = _lazy_re_compile(r"(\d+|[a-z]+|\.)")


def get_loose_version_tuple(version):
"""
Return a tuple of version numbers (e.g. (1, 2, 3, 'b', 2)) from the version
string (e.g. '1.2.3b2').
"""
version_numbers = []
for item in version_component_re.split(version):
if item and item != ".":
try:
component = int(item)
except ValueError:
component = item
version_numbers.append(component)
return tuple(version_numbers)

0 comments on commit 7596a1f

Please sign in to comment.