<commit_before>import os
import shutil
import sys
import tempfile
from collections.abc import Mapping
from grp import getgrgid
from io import StringIO
from pwd import getpwuid

from .config import find_config, ScubaConfig, OverrideMixin
from .config import ConfigError, ConfigNotFoundError
from .dockerutil import get_image_command
from .dockerutil import get_image_entrypoint
from .dockerutil import make_vol_opt
from .utils import shell_quote_cmd, flatten_list


def verbose_msg(fmt, *args):
    # TODO: remove
    pass


class ScubaError(Exception):
    pass

class ScubaDive:
    def __init__(self, user_command, docker_args=None, env=None, as_root=False, verbose=False,
            image_override=None, entrypoint=None, shell_override=None, keep_tempfiles=False):

        env = env or {}
        if not isinstance(env, Mapping):
            raise ValueError('Argument env must be dict-like')

        self.user_command = user_command
        self.as_root = as_root
        self.verbose = verbose
        self.image_override = image_override
        self.entrypoint_override = entrypoint
        self.shell_override = shell_override
        self.keep_tempfiles = keep_tempfiles

        # These will be added to docker run cmdline
        self.env_vars = env
        self.volumes = []
        self.options = []
        self.docker_args = docker_args or []
        self.workdir = None

        self.__locate_scubainit()

        # .scuba.yml is allowed to be missing if --image was given.
        self.__load_config(allow_missing=bool(image_override))

        # Process any aliases
        self.context = ScubaContext.process_command(
                                  cfg = self.config,
                                  command = self.user_command,
                                  image = self.image_override,
                                  shell = self.shell_override,
                                  )



    def prepare(self):
        '''Prepare to run the docker command'''
        self.__make_scubadir()

        if self.is_remote_docker:
            '''
            Docker is running remotely (e.g. boot2docker on OSX).
            We don't need to do any user setup whatsoever.

            TODO: For now, remote instances won't have any .scubainit

            See:
            https://github.com/JonathonReinhart/scuba/issues/17
            '''
            raise ScubaError('Remote docker not supported (DOCKER_HOST is set)')

        # Docker is running natively
        self.__setup_native_run()

        # Apply environment vars from .scuba.yml
        self.env_vars.update(self.context.environment)

    def __enter__(self):
        return self

    def __exit__(self, *exc_info):
        if not self.keep_tempfiles:
            shutil.rmtree(self.__scubadir_hostpath)

    def __str__(self):
        s = StringIO()

        indent = '  '
        level = 0

        def writelist(name, vals):
            writeln(s, '{}{}:'.format(indent*level, name))
            for val in vals or ():
                writeln(s, '{}{}'.format(indent*(level+1), val))

        def writescl(name, val=''):
            writeln(s, '{}{:<14s}{}'.format(indent*level, name+':', val))

        writeln(s, 'ScubaDive')
        level += 1

        writescl('verbose', self.verbose)
        writescl('as_root', self.as_root)
        writescl('workdir', self.workdir)

        writelist('options', self.options)
        writelist('docker_args', self.docker_args)
        writelist('env_vars', ('{}={}'.format(*e) for e in self.env_vars.items()))
        writelist('volumes', ('{} => {} {}'.format(hp, cp, opt)
                              for hp, cp, opt in self.__get_vol_opts()))

        writescl('user_command', self.user_command)
        writescl('context')
        level += 1
        writescl('script', self.context.script)
        writescl('image', self.context.image)
        writelist('docker_args', self.context.docker_args)

        return s.getvalue()




    @property
    def is_remote_docker(self):
        return 'DOCKER_HOST' in os.environ

    def add_env(self, name, val):
        '''Add an environment variable to the docker run invocation
        '''
        if name in self.env_vars:
            raise KeyError(name)<commit_msg>scuba: Remove unnecessary ScubaDive attributes<commit_after>import os
import shutil
import sys
import tempfile
from collections.abc import Mapping
from grp import getgrgid
from io import StringIO
from pwd import getpwuid

from .config import find_config, ScubaConfig, OverrideMixin
from .config import ConfigError, ConfigNotFoundError
from .dockerutil import get_image_command
from .dockerutil import get_image_entrypoint
from .dockerutil import make_vol_opt
from .utils import shell_quote_cmd, flatten_list


def verbose_msg(fmt, *args):
    # TODO: remove
    pass


class ScubaError(Exception):
    pass

class ScubaDive:
    def __init__(self, user_command, docker_args=None, env=None, as_root=False, verbose=False,
            image_override=None, entrypoint=None, shell_override=None, keep_tempfiles=False):

        env = env or {}
        if not isinstance(env, Mapping):
            raise ValueError('Argument env must be dict-like')

        self.as_root = as_root
        self.verbose = verbose
        self.entrypoint_override = entrypoint
        self.keep_tempfiles = keep_tempfiles

        # These will be added to docker run cmdline
        self.env_vars = env
        self.volumes = []
        self.options = []
        self.docker_args = docker_args or []
        self.workdir = None

        self.__locate_scubainit()

        # .scuba.yml is allowed to be missing if --image was given.
        self.__load_config(allow_missing=bool(image_override))

        # Process any aliases
        self.context = ScubaContext.process_command(
                                  cfg = self.config,
                                  command = user_command,
                                  image = image_override,
                                  shell = shell_override,
                                  )



    def prepare(self):
        '''Prepare to run the docker command'''
        self.__make_scubadir()

        if self.is_remote_docker:
            '''
            Docker is running remotely (e.g. boot2docker on OSX).
            We don't need to do any user setup whatsoever.

            TODO: For now, remote instances won't have any .scubainit

            See:
            https://github.com/JonathonReinhart/scuba/issues/17
            '''
            raise ScubaError('Remote docker not supported (DOCKER_HOST is set)')

        # Docker is running natively
        self.__setup_native_run()

        # Apply environment vars from .scuba.yml
        self.env_vars.update(self.context.environment)

    def __enter__(self):
        return self

    def __exit__(self, *exc_info):
        if not self.keep_tempfiles:
            shutil.rmtree(self.__scubadir_hostpath)

    def __str__(self):
        s = StringIO()

        indent = '  '
        level = 0

        def writelist(name, vals):
            writeln(s, '{}{}:'.format(indent*level, name))
            for val in vals or ():
                writeln(s, '{}{}'.format(indent*(level+1), val))

        def writescl(name, val=''):
            writeln(s, '{}{:<14s}{}'.format(indent*level, name+':', val))

        writeln(s, 'ScubaDive')
        level += 1

        writescl('verbose', self.verbose)
        writescl('as_root', self.as_root)
        writescl('workdir', self.workdir)

        writelist('options', self.options)
        writelist('docker_args', self.docker_args)
        writelist('env_vars', ('{}={}'.format(*e) for e in self.env_vars.items()))
        writelist('volumes', ('{} => {} {}'.format(hp, cp, opt)
                              for hp, cp, opt in self.__get_vol_opts()))

        writescl('context')
        level += 1
        writescl('script', self.context.script)
        writescl('image', self.context.image)
        writelist('docker_args', self.context.docker_args)

        return s.getvalue()




    @property
    def is_remote_docker(self):
        return 'DOCKER_HOST' in os.environ

    def add_env(self, name, val):
        '''Add an environment variable to the docker run invocation
        '''
        if name in self.env_vars:
            raise KeyError(name)
{'fraction_non_alphanumeric': 0.09833313346924091, 'fraction_numerical': 0.0021585321981052886, 'mean_word_length': 2.1650853889943074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7079673', 'n_tokens_mistral': 2482, 'n_tokens_neox': 2337, 'n_words': 613}