Skip to content

Commit

Permalink
Drop usage of distutils
Browse files Browse the repository at this point in the history
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 <sbonazzo@redhat.com>
  • Loading branch information
sandrobonazzola committed Feb 9, 2024
1 parent 0584a30 commit fa52009
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
5 changes: 2 additions & 3 deletions did/plugins/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
higher priority.
"""

import distutils.util
import os
import re
import urllib.parse
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions did/plugins/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"""

import distutils.util
from time import sleep

import dateutil
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions did/plugins/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
Other values are ``basic`` and ``token``.
"""

import distutils.util
import os
import re
import urllib.parse
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions did/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ 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 https://peps.python.org/pep-0632/#migration-advice
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)
Expand Down

0 comments on commit fa52009

Please sign in to comment.