From 95569f69c6ec32b7a1469bc2d21945085d991d30 Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Fri, 6 Dec 2024 12:32:57 +1100 Subject: [PATCH] Use legacycrypt where crypt isn't available Since Python 3.13 has removed the crypt module, add legacycrypt to the optional requirements, attempting to import it if the crypt module isn't found. Fixes #594 --- netutils/password.py | 9 +++++++-- pyproject.toml | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/netutils/password.py b/netutils/password.py index 5a9026a7..58ad1305 100644 --- a/netutils/password.py +++ b/netutils/password.py @@ -1,7 +1,5 @@ """Functions for working with Passwords.""" -# TODO: Swap out crypt prior to py3.13 -import crypt # pylint: disable=deprecated-module import random import secrets import string @@ -247,6 +245,13 @@ def encrypt_cisco_type5(unencrypted_password: str, salt: t.Optional[str] = None, '$1$MHkb$v2MFmDkQX66TTxLkFF50K/' >>> """ + try: + import crypt # pylint: disable=deprecated-module + except ModuleNotFoundError: + try: + import legacycrypt as crypt + except ModuleNotFoundError: + raise ValueError("Crypt module not available") if not salt: salt = "".join(secrets.choice(ALPHABET) for _ in range(salt_len)) elif not set(salt) <= set(ALPHABET): diff --git a/pyproject.toml b/pyproject.toml index be2b2746..68956e6c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ napalm = {version = "^4.0.0", optional = true} jsonschema = {version = "^4.17.3", optional = true} [tool.poetry.extras] -optionals = ["jsonschema", "napalm"] +optionals = ["jsonschema", "napalm", "legacycrypt"] [tool.poetry.group.dev.dependencies] bandit = "*"