Skip to content

Commit

Permalink
chore: drop uses of distutils
Browse files Browse the repository at this point in the history
The package is gone in Python 3.12. This affects unused code, which is now gone,
and some spread tests. The spread tests are still valid because they use older
bases, so just add a pyright exclusion for them.
  • Loading branch information
tigarmo committed Dec 9, 2024
1 parent 36c3ebf commit ee528bb
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 85 deletions.
33 changes: 0 additions & 33 deletions rockcraft/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import os
import pathlib
import shutil
import sys
from distutils.util import strtobool # pylint: disable=deprecated-module
from typing import NamedTuple

import rockcraft.errors
Expand All @@ -38,12 +36,6 @@ class OSPlatform(NamedTuple):
machine: str


def is_managed_mode() -> bool:
"""Check if rockcraft is running in a managed environment."""
managed_flag = os.getenv("CRAFT_MANAGED_MODE", "n")
return strtobool(managed_flag) == 1


def get_managed_environment_home_path() -> pathlib.Path:
"""Path for home when running in managed environment."""
return pathlib.Path("/root")
Expand All @@ -67,31 +59,6 @@ def get_managed_environment_snap_channel() -> str | None:
return os.getenv("ROCKCRAFT_INSTALL_SNAP_CHANNEL")


def confirm_with_user(
prompt: str, default: bool = False # noqa: FBT001,FBT002
) -> bool:
"""Query user for yes/no answer.
If stdin is not a tty, the default value is returned.
If user returns an empty answer, the default value is returned.
returns default value.
:returns: True if answer starts with [yY], False if answer starts with [nN],
otherwise the default.
"""
if is_managed_mode():
raise RuntimeError("confirmation not yet supported in managed-mode")

if not sys.stdin.isatty():
return default

choices = " [Y/n]: " if default else " [y/N]: "

reply = input(prompt + choices).lower().strip()
return reply[0] == "y" if reply else default


def _find_command_path_in_root(root: str, command_name: str) -> str | None:
"""Find the path of a command in a given root path."""
for bin_directory in (
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/plugins/python_source/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.core import setup
from setuptools import setup

setup(
name="hello",
Expand Down
2 changes: 1 addition & 1 deletion tests/spread/rockcraft/plugin-python-3.6/src/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.core import setup
from distutils.core import setup # pyright: ignore[reportMissingImports]

setup(
name="hello",
Expand Down
2 changes: 1 addition & 1 deletion tests/spread/rockcraft/plugin-python/src/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.core import setup
from distutils.core import setup # pyright: ignore[reportMissingImports]

setup(
name="hello",
Expand Down
49 changes: 0 additions & 49 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from pathlib import Path
from unittest.mock import call

import pytest
from rockcraft import utils
Expand Down Expand Up @@ -58,51 +57,3 @@ def test_get_managed_environment_snap_channel_none(monkeypatch):
monkeypatch.delenv("ROCKCRAFT_INSTALL_SNAP_CHANNEL", raising=False)

assert utils.get_managed_environment_snap_channel() is None


def test_confirm_with_user_defaults_with_tty(mock_input, mock_isatty):
mock_input.return_value = ""
mock_isatty.return_value = True

assert utils.confirm_with_user("prompt", default=True) is True
assert mock_input.mock_calls == [call("prompt [Y/n]: ")]
mock_input.reset_mock()

assert utils.confirm_with_user("prompt", default=False) is False
assert mock_input.mock_calls == [call("prompt [y/N]: ")]


def test_confirm_with_user_defaults_without_tty(mock_input, mock_isatty):
mock_isatty.return_value = False

assert utils.confirm_with_user("prompt", default=True) is True
assert utils.confirm_with_user("prompt", default=False) is False

assert mock_input.mock_calls == []


@pytest.mark.parametrize(
("user_input", "expected"),
[
("y", True),
("Y", True),
("yes", True),
("YES", True),
("n", False),
("N", False),
("no", False),
("NO", False),
],
)
def test_confirm_with_user(user_input, expected, mock_input, mock_isatty):
mock_input.return_value = user_input

assert utils.confirm_with_user("prompt") == expected
assert mock_input.mock_calls == [call("prompt [y/N]: ")]


def test_confirm_with_user_errors_in_managed_mode(mock_is_managed_mode):
mock_is_managed_mode.return_value = True

with pytest.raises(RuntimeError):
utils.confirm_with_user("prompt")

0 comments on commit ee528bb

Please sign in to comment.