"""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 # The options of docker, docker buildx and docker compose that take a value, so the value is not read as a subcommand. DOCKER_VALUED = frozenset({"--ansi", "--builder", "--config", "--context", "--env-file", "--file", "--host", "--log-level", "--parallel", "--profile", "--progress", "--project-directory", "--project-name", "--tlscacert", "--tlscert", "--tlskey", "-c", "-f", "-H", "-l", "-p"}) # The subcommands that make a container, whose memory limits docker reads from the same options. 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]?" # 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]: """The subcommand docker's arguments name, past its options: such as ["build"], ["compose", "run"] or ["buildx", "build"].""" positional: list[str] = [] skip = False for word in arguments: if skip: skip = False elif word.startswith("-"): skip = word in DOCKER_VALUED else: positional.append(word) if positional[0] not in ("buildx", "compose", "container") or len(positional) == 2: break return positional def unlimited(arguments: list[str]) -> bool: """Whether docker's arguments make a container, with docker run or docker create, and leave its memory or its swap without a limit. 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) 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 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 lacks a memory limit or a swap limit.""" docker = launched(words, {"docker"}) return docker is not None and unlimited(docker.arguments) def helped(arguments: list[str], subcommand: list[str]) -> bool: """Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes.""" after = arguments.index(subcommand[-1]) + 1 return "--help" in arguments[:after + 1]