Answer principles round 2: 0 🔴, 1 🟡 (0 misread); a swap limit is a size other than 0, so --memory-swap -1 is no limit, read like the memory limit and not from any word; the docstrings and changelog name both limits
This commit is contained in:
+10
-10
@@ -1,4 +1,4 @@
|
||||
"""What a docker command asks of docker: its subcommand, and whether a container it starts has a memory limit."""
|
||||
"""What a docker command asks of docker: its subcommand, and whether a container it starts has a memory limit and a swap limit."""
|
||||
import re
|
||||
|
||||
from shell_command import launched
|
||||
@@ -9,10 +9,8 @@ DOCKER_VALUED = frozenset({"--ansi", "--builder", "--config", "--context", "--en
|
||||
STARTS_CONTAINER = (["container", "create"], ["container", "run"], ["create"], ["run"])
|
||||
# A size as docker reads one, such as 4g, 512M or 1.5GiB: the pattern go-units' RAMInBytes parses.
|
||||
SIZE = r"\d+(?:\.\d+)*[kKmMgGtTpP]?[iI]?[bB]?"
|
||||
# -m4g is -m with its value attached.
|
||||
ATTACHED_LIMIT = re.compile(rf"(?:-m|--memory=){SIZE}")
|
||||
# --memory-swap takes a size, or -1 for unlimited swap.
|
||||
SWAP_LIMIT = re.compile(r"--memory-swap(=.*)?")
|
||||
# A swap limit is a size other than 0: docker reads --memory-swap -1 as unlimited swap, and 0 as unset.
|
||||
SWAP = rf"(?!0+(?:\.0+)*[kKmMgGtTpP]?[iI]?[bB]?$){SIZE}"
|
||||
|
||||
|
||||
def docker_subcommand(arguments: list[str]) -> list[str]:
|
||||
@@ -37,16 +35,18 @@ def unlimited(arguments: list[str]) -> bool:
|
||||
The container's own arguments, past its image, are read too: telling them apart needs every value-taking option of docker run. So a -m 4g there reads as a limit, while python -m pytest does not, since pytest is no size.
|
||||
"""
|
||||
subcommand = docker_subcommand(arguments)
|
||||
return subcommand in STARTS_CONTAINER and not (limited(arguments) and any(SWAP_LIMIT.fullmatch(word) for word in arguments)) and not helped(arguments, subcommand)
|
||||
limited = given(arguments, ("-m", "--memory"), SIZE) and given(arguments, ("--memory-swap",), SWAP)
|
||||
return subcommand in STARTS_CONTAINER and not limited and not helped(arguments, subcommand)
|
||||
|
||||
|
||||
def limited(arguments: list[str]) -> bool:
|
||||
"""Whether the arguments carry a memory limit: -m or --memory with a size, as its next word or attached."""
|
||||
return any(ATTACHED_LIMIT.fullmatch(word) or (word in ("-m", "--memory") and re.fullmatch(SIZE, value)) for word, value in zip(arguments, [*arguments[1:], ""]))
|
||||
def given(arguments: list[str], options: tuple[str, ...], value: str) -> bool:
|
||||
"""Whether the arguments give one of the options a value value matches, as its next word or attached, as in -m4g or --memory=4g."""
|
||||
attached = re.compile("|".join(re.escape(option) + ("" if len(option) == 2 else "=") + f"(?:{value})" for option in options))
|
||||
return any(attached.fullmatch(word) or (word in options and re.fullmatch(value, following)) for word, following in zip(arguments, [*arguments[1:], ""]))
|
||||
|
||||
|
||||
def unlimited_container(words: list[str]) -> bool:
|
||||
"""Whether the simple command, directly or through a launcher, starts a container that has no memory limit."""
|
||||
"""Whether the simple command, directly or through a launcher, starts a container that lacks a memory limit or a swap limit."""
|
||||
docker = launched(words, {"docker"})
|
||||
return docker is not None and unlimited(docker.arguments)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user