<commit_before>import contextlib
import subprocess
import time
import uuid

import aiostream
import pytest
import requests


def wait_for_container(url):
    """Wait for a container to initialise.

    Polls a URL every 100ms until the server responds.
    """
    # give the server 5 seconds to settle
    for _ in range(50):
        print(_)

        try:
            response = requests.get(url)
            response.raise_for_status()
        except requests.ConnectionError:
            pass
        else:
            return

        time.sleep(0.1)

    pytest.exit(
        "Server did not initialise in 5 seconds.\n"
        "WARNING: There may be a stale docker container still running."
    )


@contextlib.contextmanager
def dockerised_server(name, container_port, exposed_port):
    """Run a dockerised DAV server as a contenxt manager."""
    container_id = None
    url = f"http://127.0.0.1:{exposed_port}/"

    try:
        # Hint: This will block while the pull happends, and only return once
        # the container has actually started.
        output = subprocess.check_output(
            [
                "docker",
                "run",
                "--detach",
                "--publish",
                f"{exposed_port}:{container_port}",
                f"whynothugo/vdirsyncer-devkit-{name}",
            ]
        )

        container_id = output.decode().strip()
        wait_for_container(url)
<commit_msg>Auto-delete test containers<commit_after>import contextlib
import subprocess
import time
import uuid

import aiostream
import pytest
import requests


def wait_for_container(url):
    """Wait for a container to initialise.

    Polls a URL every 100ms until the server responds.
    """
    # give the server 5 seconds to settle
    for _ in range(50):
        print(_)

        try:
            response = requests.get(url)
            response.raise_for_status()
        except requests.ConnectionError:
            pass
        else:
            return

        time.sleep(0.1)

    pytest.exit(
        "Server did not initialise in 5 seconds.\n"
        "WARNING: There may be a stale docker container still running."
    )


@contextlib.contextmanager
def dockerised_server(name, container_port, exposed_port):
    """Run a dockerised DAV server as a contenxt manager."""
    container_id = None
    url = f"http://127.0.0.1:{exposed_port}/"

    try:
        # Hint: This will block while the pull happends, and only return once
        # the container has actually started.
        output = subprocess.check_output(
            [
                "docker",
                "run",
                "--rm",
                "--detach",
                "--publish",
                f"{exposed_port}:{container_port}",
                f"whynothugo/vdirsyncer-devkit-{name}",
            ]
        )

        container_id = output.decode().strip()
        wait_for_container(url)

{'fraction_non_alphanumeric': 0.08898015058179329, 'fraction_numerical': 0.01026694045174538, 'mean_word_length': 1.9259259259259258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '782922', 'n_tokens_mistral': 845, 'n_tokens_neox': 789, 'n_words': 237}