diff --git a/python/mach/mach/util.py b/python/mach/mach/util.py index 203f08f92b8d..b6bf1727fa94 100644 --- a/python/mach/mach/util.py +++ b/python/mach/mach/util.py @@ -115,3 +115,18 @@ def to_optional_str(path: Optional[Path]): return str(path) else: return None + + +def strtobool(value: str): + # Reimplementation of distutils.util.strtobool + # https://docs.python.org/3.9/distutils/apiref.html#distutils.util.strtobool + true_vals = ("y", "yes", "t", "true", "on", "1") + false_vals = ("n", "no", "f", "false", "off", "0") + + value = value.lower() + if value in true_vals: + return 1 + if value in false_vals: + return 0 + + raise ValueError(f'Expected one of: {", ".join(true_vals + false_vals)}') diff --git a/python/mozbuild/mozbuild/backend/mach_commands.py b/python/mozbuild/mozbuild/backend/mach_commands.py index 3feedefc4203..8b0cdd50678d 100644 --- a/python/mozbuild/mozbuild/backend/mach_commands.py +++ b/python/mozbuild/mozbuild/backend/mach_commands.py @@ -409,7 +409,7 @@ def get_clang_tools(command_context, clang_tools_path): def prompt_bool(prompt, limit=5): """Prompts the user with prompt and requires a boolean value.""" - from distutils.util import strtobool + from mach.util import strtobool for _ in range(limit): try: diff --git a/python/mozbuild/mozbuild/code_analysis/mach_commands.py b/python/mozbuild/mozbuild/code_analysis/mach_commands.py index a65d35c3cfed..b0416679bb80 100644 --- a/python/mozbuild/mozbuild/code_analysis/mach_commands.py +++ b/python/mozbuild/mozbuild/code_analysis/mach_commands.py @@ -51,7 +51,7 @@ def build_repo_relative_path(abs_path, repo_path): def prompt_bool(prompt, limit=5): """Prompts the user with prompt and requires a boolean value.""" - from distutils.util import strtobool + from mach.util import strtobool for _ in range(limit): try: diff --git a/taskcluster/docker/funsize-update-generator/scripts/funsize.py b/taskcluster/docker/funsize-update-generator/scripts/funsize.py index 84fd2fbd0bfc..f43a8a6d7712 100644 --- a/taskcluster/docker/funsize-update-generator/scripts/funsize.py +++ b/taskcluster/docker/funsize-update-generator/scripts/funsize.py @@ -13,10 +13,10 @@ import shutil import tempfile import time from contextlib import AsyncExitStack -from distutils.util import strtobool from pathlib import Path import aiohttp +from mach.util import strtobool from mardor.reader import MarReader from mardor.signing import get_keysize from scriptworker.utils import get_hash, retry_async diff --git a/taskcluster/mach_commands.py b/taskcluster/mach_commands.py index 78099f6eea13..73e77fce6606 100644 --- a/taskcluster/mach_commands.py +++ b/taskcluster/mach_commands.py @@ -17,23 +17,11 @@ from functools import partial import gecko_taskgraph.main from gecko_taskgraph.main import commands as taskgraph_commands from mach.decorators import Command, CommandArgument, SubCommand +from mach.util import strtobool logger = logging.getLogger("taskcluster") -def strtobool(value): - """Convert string to boolean. - - Wraps "distutils.util.strtobool", deferring the import of the package - in case it's not installed. Otherwise, we have a "chicken and egg problem" where - |mach bootstrap| would install the required package to enable "distutils.util", but - it can't because mach fails to interpret this file. - """ - from distutils.util import strtobool - - return bool(strtobool(value)) - - def get_taskgraph_command_parser(name): """Given a command name, obtain its argument parser. @@ -59,7 +47,7 @@ def get_taskgraph_decision_parser(): ( ["--optimize-target-tasks"], { - "type": lambda flag: strtobool(flag), + "type": lambda flag: bool(strtobool(flag)), "nargs": "?", "const": "true", "help": "If specified, this indicates whether the target "