sts-sponsors team mailing list archive
-
sts-sponsors team
-
Mailing list archive
-
Message #06558
[Merge] ~ack/maas:drop-unused-provisioningserver-utils into maas:master
Alberto Donato has proposed merging ~ack/maas:drop-unused-provisioningserver-utils into maas:master.
Commit message:
drop some unused utilities from provisioningserver.utils
Requested reviews:
MAAS Maintainers (maas-maintainers)
For more details, see:
https://code.launchpad.net/~ack/maas/+git/maas/+merge/439896
--
Your team MAAS Maintainers is requested to review the proposed merge of ~ack/maas:drop-unused-provisioningserver-utils into maas:master.
diff --git a/src/provisioningserver/utils/__init__.py b/src/provisioningserver/utils/__init__.py
index f2c73f2..a0800e2 100644
--- a/src/provisioningserver/utils/__init__.py
+++ b/src/provisioningserver/utils/__init__.py
@@ -7,7 +7,6 @@ from collections.abc import Iterable
from functools import lru_cache, reduce
from itertools import chain
import os
-from pipes import quote
from typing import Tuple
import tempita
@@ -57,53 +56,6 @@ def dict_depth(d, depth=0):
return max(dict_depth(v, depth + 1) for _, v in d.items())
-def split_lines(input, separator):
- """Split each item from `input` into a key/value pair."""
- return (line.split(separator, 1) for line in input if line.strip() != "")
-
-
-def strip_pairs(input):
- """Strip whitespace of each key/value pair in input."""
- return ((key.strip(), value.strip()) for (key, value) in input)
-
-
-class Safe:
- """An object that is safe to render as-is."""
-
- __slots__ = ("value",)
-
- def __init__(self, value):
- self.value = value
-
- def __repr__(self):
- return f"<{self.__class__.__name__} {self.value!r}>"
-
-
-class ShellTemplate(tempita.Template):
- """A Tempita template specialised for writing shell scripts.
-
- By default, substitutions will be escaped using `pipes.quote`, unless
- they're marked as safe. This can be done using Tempita's filter syntax::
-
- {{foobar|safe}}
-
- or as a plain Python expression::
-
- {{safe(foobar)}}
-
- """
-
- default_namespace = dict(tempita.Template.default_namespace, safe=Safe)
-
- def _repr(self, value, pos):
- """Shell-quote the value by default."""
- rep = super()._repr
- if isinstance(value, Safe):
- return rep(value.value, pos)
- else:
- return quote(rep(value, pos))
-
-
def classify(func, subjects):
"""Classify `subjects` according to `func`.
@@ -154,12 +106,6 @@ def flatten(*things):
return _flatten(things)
-def is_true(value):
- if value is None:
- return False
- return value.lower() in ("yes", "true", "t", "1")
-
-
def sudo(command_args):
"""Wrap the command arguments in a sudo command, if not in debug mode."""
if snap.running_in_snap():
@@ -221,52 +167,6 @@ def is_instance_or_subclass(test, *query):
return False
-# Capacity units supported by convert_size_to_bytes() function.
-CAPACITY_UNITS = {
- "KiB": 2**10,
- "MiB": 2**20,
- "GiB": 2**30,
- "TiB": 2**40,
- "PiB": 2**50,
- "EiB": 2**60,
- "ZiB": 2**70,
- "YiB": 2**80,
-}
-
-
-class UnknownCapacityUnitError(Exception):
- """Unknown capacity unit used."""
-
-
-def convert_size_to_bytes(value):
- """
- Converts storage size values with units (GiB, TiB...) to bytes.
-
- :param value: A string containing a number and unit separated by at least
- one space character. If unit is not specified, defaults to bytes.
- :return: An integer indicating the number of bytes for the given value in
- any other size unit.
- :raises UnknownCapacityUnitError: unsupported capacity unit.
- """
- # Split value on the first space.
- capacity_def = value.split(" ", 1)
- if len(capacity_def) == 1:
- # No unit specified, default to bytes.
- return int(capacity_def[0])
-
- capacity_value, capacity_unit = capacity_def
- capacity_value = float(capacity_value)
- capacity_unit = capacity_unit.strip()
- if capacity_unit in CAPACITY_UNITS:
- multiplier = CAPACITY_UNITS[capacity_unit]
- else:
- raise UnknownCapacityUnitError(
- "Unknown capacity unit '%s'" % capacity_unit
- )
- # Convert value to bytes.
- return int(capacity_value * multiplier)
-
-
# Architectures as defined by:
# https://github.com/lxc/lxd/blob/master/shared/osarch/architectures.go
# https://www.debian.org/releases/oldstable/i386/ch02s01.html.en
diff --git a/src/provisioningserver/utils/tests/test_utils.py b/src/provisioningserver/utils/tests/test_utils.py
index 0b750d4..df16d05 100644
--- a/src/provisioningserver/utils/tests/test_utils.py
+++ b/src/provisioningserver/utils/tests/test_utils.py
@@ -20,18 +20,14 @@ import provisioningserver.utils
from provisioningserver.utils import (
CircularDependency,
classify,
- convert_size_to_bytes,
debian_to_kernel_architecture,
flatten,
is_instance_or_subclass,
kernel_to_debian_architecture,
locate_config,
locate_template,
- Safe,
- ShellTemplate,
sorttop,
sudo,
- UnknownCapacityUnitError,
)
@@ -93,45 +89,6 @@ class TestLocateTemplate(MAASTestCase):
)
-class TestSafe(MAASTestCase):
- """Test `Safe`."""
-
- def test_value(self):
- something = object()
- safe = Safe(something)
- self.assertIs(something, safe.value)
-
- def test_repr(self):
- string = factory.make_string()
- safe = Safe(string)
- self.assertEqual("<Safe %r>" % string, repr(safe))
-
-
-class TestShellTemplate(MAASTestCase):
- """Test `ShellTemplate`."""
-
- def test_substitute_escapes(self):
- # Substitutions are shell-escaped.
- template = ShellTemplate("{{a}}")
- expected = "'1 2 3'"
- observed = template.substitute(a="1 2 3")
- self.assertEqual(expected, observed)
-
- def test_substitute_does_not_escape_safe(self):
- # Substitutions will not be escaped if they're marked with `safe`.
- template = ShellTemplate("{{a|safe}}")
- expected = "$ ! ()"
- observed = template.substitute(a="$ ! ()")
- self.assertEqual(expected, observed)
-
- def test_substitute_does_not_escape_safe_objects(self):
- # Substitutions will not be escaped if they're `safe` objects.
- template = ShellTemplate("{{safe(a)}}")
- expected = "$ ! ()"
- observed = template.substitute(a="$ ! ()")
- self.assertEqual(expected, observed)
-
-
class TestClassify(MAASTestCase):
def test_no_subjects(self):
self.assertSequenceEqual(([], []), classify(sentinel.func, []))
@@ -333,46 +290,6 @@ class TestIsInstanceOrSubclass(MAASTestCase):
)
-class TestConvertSizeToBytes(MAASTestCase):
- """Tests for `convert_size_to_bytes`."""
-
- scenarios = (
- ("bytes", {"value": "24111", "expected": 24111}),
- ("KiB", {"value": "2.21 KiB", "expected": int(2.21 * 2**10)}),
- ("MiB", {"value": "2.21 MiB", "expected": int(2.21 * 2**20)}),
- ("GiB", {"value": "2.21 GiB", "expected": int(2.21 * 2**30)}),
- ("TiB", {"value": "2.21 TiB", "expected": int(2.21 * 2**40)}),
- ("PiB", {"value": "2.21 PiB", "expected": int(2.21 * 2**50)}),
- ("EiB", {"value": "2.21 EiB", "expected": int(2.21 * 2**60)}),
- ("ZiB", {"value": "2.21 ZiB", "expected": int(2.21 * 2**70)}),
- ("YiB", {"value": "2.21 YiB", "expected": int(2.21 * 2**80)}),
- (
- "whitespace",
- {"value": "2.21 GiB", "expected": int(2.21 * 2**30)},
- ),
- ("zero", {"value": "0 TiB", "expected": 0}),
- )
-
- def test_convert_size_to_bytes(self):
- self.assertEqual(self.expected, convert_size_to_bytes(self.value))
-
-
-class TestConvertSizeToBytesErrors(MAASTestCase):
- """Error handling tests for `convert_size_to_bytes`."""
-
- def test_unknown_capacity_unit(self):
- error = self.assertRaises(
- UnknownCapacityUnitError, convert_size_to_bytes, "200 superbytes"
- )
- self.assertEqual("Unknown capacity unit 'superbytes'", str(error))
-
- def test_empty_string(self):
- self.assertRaises(ValueError, convert_size_to_bytes, "")
-
- def test_empty_value(self):
- self.assertRaises(ValueError, convert_size_to_bytes, " KiB")
-
-
class TestKernelToDebianArchitecture(MAASTestCase):
"""Tests for `kernel_to_debian_architecture`."""
Follow ups