<commit_before>import json
import os
import subprocess

import boto3
import pytest
import yaml


@pytest.fixture
def moto_environment():
    overwrite_envs = {
        "FLASK_ENV": "development",
        "WERKZEUG_RUN_MAIN": "true",
        "AWS_ACCESS_KEY_ID": "1234",
        "AWS_SECRET_ACCESS_KEY": "5678",
    }
    recovery_values = {name: os.environ.get(name) for name in overwrite_envs.keys()}
    os.environ.update(overwrite_envs)
    yield
    for k, v in recovery_values.items():
        if v is None:
            del os.environ[k]
        else:
            os.environ[k] = v


@pytest.fixture
def moto_server_s3(moto_environment):
    s3 = subprocess.Popen(["moto_server", "s3", "-p", "5000"], stderr=subprocess.PIPE)
    s3.stderr.readline()
    with open("tests/aws/env.yml") as f:
        y = yaml.safe_load(f.read())
    boto_s3 = boto3.resource("s3", **y["s3_config"])
    try:
        boto_s3.create_bucket(
            Bucket="sample-bucket",
            CreateBucketConfiguration={
                "LocationConstraint": y["s3_config"]["region_name"]
            },
        )
    except Exception:
        pass
    yield
    s3.kill()
    s3.stderr.close()
    s3.wait()


@pytest.fixture
def moto_server_sqs(moto_environment):
    sqs = subprocess.Popen(["moto_server", "sqs", "-p", "5001"], stderr=subprocess.PIPE)
    sqs.stderr.readline()
    with open("tests/aws/env.yml") as f:
        y = yaml.safe_load(f.read())
    boto_sqs = boto3.resource("sqs", **y["sqs_config"])
    try:
        boto_sqs.create_queue(QueueName="sample-queue")
    except Exception:
        pass
    yield
    sqs.kill()
    sqs.stderr.close()
    sqs.wait()

<commit_msg>refactor: call subprocess.Popen with `with`<commit_after>import json
import os
import subprocess
from threading import Thread
import time

import boto3
import pytest
import yaml


@pytest.fixture
def moto_environment():
    overwrite_envs = {
        "FLASK_ENV": "development",
        "WERKZEUG_RUN_MAIN": "true",
        "AWS_ACCESS_KEY_ID": "1234",
        "AWS_SECRET_ACCESS_KEY": "5678",
    }
    recovery_values = {name: os.environ.get(name) for name in overwrite_envs.keys()}
    os.environ.update(overwrite_envs)
    yield
    for k, v in recovery_values.items():
        if v is None:
            del os.environ[k]
        else:
            os.environ[k] = v


def read_and_discard_in_background(file):
    Thread(target=lambda f: f.read(), args=(file,), daemon=True).start()


@pytest.fixture
def moto_server_s3(moto_environment):
    with subprocess.Popen(["moto_server", "s3", "-p", "5000"], stderr=subprocess.PIPE) as s3:
        read_and_discard_in_background(s3.stderr)
        time.sleep(.5)
        with open("tests/aws/env.yml") as f:
            y = yaml.safe_load(f.read())
        boto_s3 = boto3.resource("s3", **y["s3_config"])
        boto_s3.create_bucket(
            Bucket="sample-bucket",
            CreateBucketConfiguration={
                "LocationConstraint": y["s3_config"]["region_name"]
            },
        )
        yield
        s3.terminate()


@pytest.fixture
def moto_server_sqs(moto_environment):
    with subprocess.Popen(["moto_server", "sqs", "-p", "5001"], stderr=subprocess.PIPE) as sqs:
        read_and_discard_in_background(sqs.stderr)
        time.sleep(.5)
        with open("tests/aws/env.yml") as f:
            y = yaml.safe_load(f.read())
        boto_sqs = boto3.resource("sqs", **y["sqs_config"])
        boto_sqs.create_queue(QueueName="sample-queue")
        yield
        sqs.terminate()


{'fraction_non_alphanumeric': 0.1359773371104816, 'fraction_numerical': 0.017563739376770537, 'mean_word_length': 2.7365079365079366, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7111559', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1263, 'n_words': 225}