From 497190fe2dbdd544940762477a780b5b96e586f1 Mon Sep 17 00:00:00 2001 From: John Turner <7strbass@gmail.com> Date: Wed, 13 Sep 2023 10:10:50 -0400 Subject: [PATCH] --remove deprecated distutils from setup --packaging --- examples/ab_test.py | 22 +++++++++++++++++++--- setup.py | 24 ++++++++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/examples/ab_test.py b/examples/ab_test.py index bd484be47d..a580ba11fa 100755 --- a/examples/ab_test.py +++ b/examples/ab_test.py @@ -7,7 +7,6 @@ import argparse import csv -import distutils import demo_runner as dr @@ -173,6 +172,23 @@ def get_csv_data( return rows, fields +def strtobool(input_str: str) -> bool: + """Convert a string representation of truth to 1(true) or 0(false) + + True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values + are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if + 'val' is anything else. + (This code was taken directly from pre-deprecated distutils.utils.strtobool) + """ + val = input_str.lower() + if val in ("y", "yes", "t", "true", "on", "1"): + return 1 + elif val in ("n", "no", "f", "false", "off", "0"): + return 0 + else: + raise ValueError("invalid truth value %r" % (val,)) + + args = parser.parse_args() control_val = None @@ -185,9 +201,9 @@ def get_csv_data( if "control_value" in args: control_val = float(args.control_value) elif args.boolean: - test_val = distutils.util.strtobool(args.test_value) + test_val = strtobool(args.test_value) if "control_value" in args: - control_val = distutils.util.strtobool(args.control_value) + control_val = strtobool(args.control_value) elif args.string: test_val = args.test_value if "control_value" in args: diff --git a/setup.py b/setup.py index f1434405ab..795c82aa8c 100644 --- a/setup.py +++ b/setup.py @@ -19,9 +19,9 @@ import shutil import subprocess import sys -from distutils.util import strtobool -from distutils.version import StrictVersion +# from https://github.com/pypa/packaging/issues/520#issuecomment-1067119795 +from packaging.version import Version from setuptools import Extension, find_packages, setup from setuptools.command.build_ext import build_ext @@ -39,7 +39,19 @@ def str2bool(input_str: str) -> bool: - return bool(strtobool(input_str.lower())) + """Convert a string representation of truth to True or False + + True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values + are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if + 'val' is anything else. + """ + val = input_str.lower() + if val in ("y", "yes", "t", "true", "on", "1"): + return True + elif val in ("n", "no", "f", "false", "off", "0"): + return False + else: + raise ValueError("invalid truth value %r" % (val,)) def is_pip() -> bool: @@ -438,9 +450,9 @@ def load(filename): if __name__ == "__main__": - assert StrictVersion( - "{}.{}".format(sys.version_info[0], sys.version_info[1]) - ) >= StrictVersion("3.9"), "Must use python 3.9 or newer" + assert Version("{}.{}".format(sys.version_info[0], sys.version_info[1])) >= Version( + "3.9" + ), "Must use python 3.9 or newer" with open("./requirements.txt", "r") as f: requirements = [l.strip() for l in f.readlines() if len(l.strip()) > 0]