From 6e57042c2a76176ce30552797e501e9a997d9cbd Mon Sep 17 00:00:00 2001 From: Sandro Bonazzola Date: Fri, 9 Feb 2024 15:30:16 +0100 Subject: [PATCH] Drop usage of distutils Replace `distutils.util.strtobool` with a local copy of the function following the [Migration advice](https://peps.python.org/pep-0632/#migration-advice) due to `distutils` module deprecation. Signed-off-by: Sandro Bonazzola --- MIT_LICENSE | 17 +++++++++++++++++ did/plugins/confluence.py | 5 ++--- did/plugins/gitlab.py | 5 ++--- did/plugins/jira.py | 7 +++---- did/utils.py | 24 ++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 MIT_LICENSE diff --git a/MIT_LICENSE b/MIT_LICENSE new file mode 100644 index 00000000..1bb5a443 --- /dev/null +++ b/MIT_LICENSE @@ -0,0 +1,17 @@ +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/did/plugins/confluence.py b/did/plugins/confluence.py index 34c48a3f..c3c36a54 100644 --- a/did/plugins/confluence.py +++ b/did/plugins/confluence.py @@ -32,7 +32,6 @@ higher priority. """ -import distutils.util import os import re import urllib.parse @@ -43,7 +42,7 @@ from did.base import Config, ReportError from did.stats import Stats, StatsGroup -from did.utils import listed, log, pretty +from did.utils import listed, log, pretty, strtobool # Maximum number of results fetched at once MAX_RESULTS = 100 @@ -214,7 +213,7 @@ def __init__(self, option, name=None, parent=None, user=None): # SSL verification if "ssl_verify" in config: try: - self.ssl_verify = distutils.util.strtobool( + self.ssl_verify = strtobool( config["ssl_verify"]) except Exception as error: raise ReportError( diff --git a/did/plugins/gitlab.py b/did/plugins/gitlab.py index a4832308..2ba4f6ff 100644 --- a/did/plugins/gitlab.py +++ b/did/plugins/gitlab.py @@ -24,7 +24,6 @@ """ -import distutils.util from time import sleep import dateutil @@ -33,7 +32,7 @@ from did.base import Config, ReportError, get_token from did.stats import Stats, StatsGroup -from did.utils import listed, log, pretty +from did.utils import listed, log, pretty, strtobool GITLAB_SSL_VERIFY = True GITLAB_API = 4 @@ -382,7 +381,7 @@ def __init__(self, option, name=None, parent=None, user=None): "No GitLab token set in the [{0}] section".format(option)) # Check SSL verification try: - self.ssl_verify = bool(distutils.util.strtobool( + self.ssl_verify = bool(strtobool( config["ssl_verify"])) except KeyError: self.ssl_verify = GITLAB_SSL_VERIFY diff --git a/did/plugins/jira.py b/did/plugins/jira.py index c9098864..e37f1ec6 100644 --- a/did/plugins/jira.py +++ b/did/plugins/jira.py @@ -78,7 +78,6 @@ Other values are ``basic`` and ``token``. """ -import distutils.util import os import re import urllib.parse @@ -90,7 +89,7 @@ from did.base import Config, ReportError, get_token from did.stats import Stats, StatsGroup -from did.utils import listed, log, pretty +from did.utils import listed, log, pretty, strtobool # Maximum number of results fetched at once MAX_RESULTS = 1000 @@ -345,7 +344,7 @@ def __init__(self, option, name=None, parent=None, user=None): # SSL verification if "ssl_verify" in config: try: - self.ssl_verify = distutils.util.strtobool( + self.ssl_verify = strtobool( config["ssl_verify"]) except Exception as error: raise ReportError( @@ -356,7 +355,7 @@ def __init__(self, option, name=None, parent=None, user=None): # Make sure we have project set self.project = config.get("project", None) if "use_scriptrunner" in config: - self.use_scriptrunner = distutils.util.strtobool( + self.use_scriptrunner = strtobool( config["use_scriptrunner"]) else: self.use_scriptrunner = True diff --git a/did/utils.py b/did/utils.py index 977e1f52..e719e2f3 100644 --- a/did/utils.py +++ b/did/utils.py @@ -187,6 +187,30 @@ def shorted(text, width=MAX_WIDTH): return "\n".join(lines) +def strtobool(val): + """ + Convert a string representation of truth to true (1) or false (0). + Reimplemented following instructions at + https://peps.python.org/pep-0632/#migration-advice + This is a copy of + https://github.com/pypa/distutils/blob/ee021a1c58b43607ccc75447159bd90f502c6bea/distutils/util.py#L340 + Which is under MIT license. + + Returns: + 1 if val is within 'y', 'yes', 't', 'true', 'on', and '1'. + 0 if val is within 'n', 'no', 'f', 'false', 'off', and '0'. + Raises: + ValueError if 'val' is anything else. + """ + val = val.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}".format(val)) + + def item(text, level=0, options=None): """ Print indented item. """ # Extra line before in each section (unless brief)