From 46af42a02d0ab046bbbaf523c836d5f20c6cf036 Mon Sep 17 00:00:00 2001 From: Gert Van Gool Date: Wed, 31 May 2023 00:04:07 +0000 Subject: [PATCH] Remove usage of distutils distutils is scheduled to be removed in Python 3.12. It's used to create the full directory tree for storing the files, we can replace that with [pathlib.Path.mkdir](https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir). Since we already have a `Path` object now, we can also replace calls to `os.path.join`, `os.stat` with calls on `Path`. --- src/django_mermaid/apps.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/django_mermaid/apps.py b/src/django_mermaid/apps.py index f223e1d..a2d3a0b 100644 --- a/src/django_mermaid/apps.py +++ b/src/django_mermaid/apps.py @@ -1,8 +1,4 @@ -from distutils import dir_util -from os import stat -from os.path import dirname -from os.path import exists -from os.path import join +import pathlib from urllib.request import urlretrieve from django.apps import AppConfig @@ -18,9 +14,11 @@ def ready(self): """Download mermaid.js from CDN if not already present""" version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION) cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % version - static_dir = join(dirname(__file__), "static") - mermaid_dir = join(static_dir, "mermaid", version) - if not exists(join(mermaid_dir, "mermaid.js")) or \ - stat(join(mermaid_dir, "mermaid.js")).st_size == 0: - dir_util.create_tree(mermaid_dir, ["mermaid.js"]) - urlretrieve(cdn, join(mermaid_dir, "mermaid.js")) + current_dir = pathlib.Path(__file__).parent + static_dir = current_dir / "static" + mermaid_dir = static_dir / "mermaid" / version + mermaid_js = mermaid_dir / "mermaid.js" + if not mermaid_js.exists() or \ + mermaid_js.stat().st_size == 0: + mermaid_dir.mkdir(parents=True, exist_ok=True) + urlretrieve(cdn, str(mermaid_js))