<commit_before>"""
"""
from __future__ import absolute_import

from subprocess import check_call
try:
    from subprocess import check_output
except ImportError:
    from ..util import check_output

from six import iteritems

from ..io import info
from . import ExecutionContext


class DockerExecutionContext(ExecutionContext):
    def __init__(self, image, docker_config=None, **kwargs):
        self.image = image
        self.docker_config = docker_config
        self.use_sudo = docker_config.get('use_sudo', False)
        self.share_args = []
        self.env = {}
        self.container_ids = []
        self.image_ids = []

    def start(self, share=None, env=None, **kwargs):
        if share is not None:
            for host, guest, read in share:
                self.share_args.append(
                    '--volume={host}:{guest}:{read}'.format(host=host,
                                                            guest=guest,
                                                            read=read))
        if env is not None:
            self.env = env

    def run(self, cmd, capture_output=False, **kwargs):
        if self.container_ids:
            image = ':'.join(image.split(':')[0], self.container_ids[-1])
            check_call(['docker', 'commit', self.container_ids[-1], image])
            self.image_ids.append(image)
        else:
            image = self.image.image
        cmd = self.normalize_cmd(cmd)
        run_cmd = 'docker run --cidfile __cid.txt'.split()
        run_cmd.extend(self.share_args)
        for (k, v) in iteritems(self.env):
            run_cmd.append('--env={k}={v}'.format(k=k, v=v))
        run_cmd.append(image)
        run_cmd.extend(cmd)
        if self.use_sudo:
            run_cmd = ['sudo'] + run_cmd
        info('Running docker: %s', ' '.join(run_cmd))
        output = None
        if capture_output:
            output = check_output(run_cmd)
        else:
            check_call(run_cmd)
        self.container_ids.append(open('__cid.txt').read().strip())
        return output

    def destroy(self, **kwargs):
        for i in self.container_ids:
            check_call(['docker', 'rm', '-v', i])
        for i in self.image_ids:
            check_call(['docker', 'rmi', i])
        self.share_args = []
        self.container_ids = []
        self.image_ids = []<commit_msg>Remove __cid.txt between docker executions<commit_after>"""
"""
from __future__ import absolute_import

from os import unlink
from subprocess import check_call
try:
    from subprocess import check_output
except ImportError:
    from ..util import check_output

from six import iteritems

from ..io import info
from . import ExecutionContext


class DockerExecutionContext(ExecutionContext):
    def __init__(self, image, docker_config=None, **kwargs):
        self.image = image
        self.docker_config = docker_config
        self.use_sudo = docker_config.get('use_sudo', False)
        self.share_args = []
        self.env = {}
        self.container_ids = []
        self.image_ids = []

    def start(self, share=None, env=None, **kwargs):
        if share is not None:
            for host, guest, read in share:
                self.share_args.append(
                    '--volume={host}:{guest}:{read}'.format(host=host,
                                                            guest=guest,
                                                            read=read))
        if env is not None:
            self.env = env

    def run(self, cmd, capture_output=False, **kwargs):
        if self.container_ids:
            image = ':'.join(image.split(':')[0], self.container_ids[-1])
            check_call(['docker', 'commit', self.container_ids[-1], image])
            self.image_ids.append(image)
        else:
            image = self.image.image
        cmd = self.normalize_cmd(cmd)
        run_cmd = 'docker run --cidfile __cid.txt'.split()
        run_cmd.extend(self.share_args)
        for (k, v) in iteritems(self.env):
            run_cmd.append('--env={k}={v}'.format(k=k, v=v))
        run_cmd.append(image)
        run_cmd.extend(cmd)
        if self.use_sudo:
            run_cmd = ['sudo'] + run_cmd
        info('Running docker: %s', ' '.join(run_cmd))
        output = None
        if capture_output:
            output = check_output(run_cmd)
        else:
            check_call(run_cmd)
        self.container_ids.append(open('__cid.txt').read().strip())
        unlink('__cid.txt')
        return output

    def destroy(self, **kwargs):
        for i in self.container_ids:
            check_call(['docker', 'rm', '-v', i])
        for i in self.image_ids:
            check_call(['docker', 'rmi', i])
        self.share_args = []
        self.container_ids = []
        self.image_ids = []
{'fraction_non_alphanumeric': 0.12272251308900524, 'fraction_numerical': 0.001256544502617801, 'mean_word_length': 1.9445129469790383, 'pattern_counts': {'":': 0, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '751149', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1374, 'n_words': 313}