<commit_before>import logging
import os
import re

from . import errors
from .. import config
from .. import parallel
from ..config.environment import Environment
from ..const import LABEL_CONFIG_FILES
from ..const import LABEL_ENVIRONMENT_FILE
from ..const import LABEL_WORKING_DIR
from ..project import Project
from .docker_client import get_client
from .docker_client import load_context
from .docker_client import make_context
from .errors import UserError

log = logging.getLogger(__name__)

SILENT_COMMANDS = {
    'events',
    'exec',
    'kill',
    'logs',
    'pause',
    'ps',
    'restart',
    'rm',
    'start',
    'stop',
    'top',
    'unpause',
}


def project_from_options(project_dir, options, additional_options=None):
    additional_options = additional_options or {}
    override_dir = get_project_dir(options)
    environment_file = options.get('--env-file')
    environment = Environment.from_env_file(override_dir or project_dir, environment_file)
    environment.silent = options.get('COMMAND', None) in SILENT_COMMANDS
    set_parallel_limit(environment)

    # get the context for the run
    context = None
    context_name = options.get('--context', None)
    if context_name:
        context = load_context(context_name)
        if not context:
            raise UserError("Context '{}' not found".format(context_name))

    host = options.get('--host', None)
    if host is not None:
        if context:
            raise UserError(
                "-H, --host and -c, --context are mutually exclusive. Only one should be set.")
        host = host.lstrip('=')
        context = make_context(host, options, environment)

    return get_project(
        project_dir,
        get_config_path_from_options(options, environment),
        project_name=options.get('--project-name'),
        verbose=options.get('--verbose'),
        context=context,
        environment=environment,
        override_dir=override_dir,
        interpolate=(not additional_options.get('--no-interpolate')),
        environment_file=environment_file,
        enabled_profiles=get_profiles_from_options(options, environment)
    )


def set_parallel_limit(environment):
    parallel_limit = environment.get('COMPOSE_PARALLEL_LIMIT')
    if parallel_limit:
        try:
            parallel_limit = int(parallel_limit)
        except ValueError:
            raise errors.UserError(
                'COMPOSE_PARALLEL_LIMIT must be an integer (found: "{}")'.format(
                    environment.get('COMPOSE_PARALLEL_LIMIT')
                )
            )
        if parallel_limit <= 1:
            raise errors.UserError('COMPOSE_PARALLEL_LIMIT can not be less than 2')
        parallel.GlobalLimit.set_global_limit(parallel_limit)


def get_project_dir(options):
    override_dir = None
    files = get_config_path_from_options(options, os.environ)
    if files:
        if files[0] == '-':
            return '.'
        override_dir = os.path.dirname(files[0])
    return options.get('--project-directory') or override_dir


def get_config_from_options(base_dir, options, additional_options=None):
    additional_options = additional_options or {}
    override_dir = get_project_dir(options)
    environment_file = options.get('--env-file')
    environment = Environment.from_env_file(override_dir or base_dir, environment_file)
    config_path = get_config_path_from_options(options, environment)
    return config.load(
        config.find(base_dir, config_path, environment, override_dir),
        not additional_options.get('--no-interpolate')
    )


def get_config_path_from_options(options, environment):
    def unicode_paths(paths):
        return [p.decode('utf-8') if isinstance(p, bytes) else p for p in paths]

    file_option = options.get('--file')
    if file_option:
        return unicode_paths(file_option)

    config_files = environment.get('COMPOSE_FILE')
    if config_files:
        pathsep = environment.get('COMPOSE_PATH_SEPARATOR', os.pathsep)
        return unicode_paths(config_files.split(pathsep))
    return None


def get_profiles_from_options(options, environment):
    profile_option = options.get('--profile')
    if profile_option:
        return profile_option

    profiles = environment.get('COMPOSE_PROFILE')
    if profiles:
        return profiles.split(',')

    return []


def get_project(project_dir, config_path=None, project_name=None, verbose=False,
                context=None, environment=None, override_dir=None,<commit_msg>Fix incorrect CLI variable name for service profiles<commit_after>import logging
import os
import re

from . import errors
from .. import config
from .. import parallel
from ..config.environment import Environment
from ..const import LABEL_CONFIG_FILES
from ..const import LABEL_ENVIRONMENT_FILE
from ..const import LABEL_WORKING_DIR
from ..project import Project
from .docker_client import get_client
from .docker_client import load_context
from .docker_client import make_context
from .errors import UserError

log = logging.getLogger(__name__)

SILENT_COMMANDS = {
    'events',
    'exec',
    'kill',
    'logs',
    'pause',
    'ps',
    'restart',
    'rm',
    'start',
    'stop',
    'top',
    'unpause',
}


def project_from_options(project_dir, options, additional_options=None):
    additional_options = additional_options or {}
    override_dir = get_project_dir(options)
    environment_file = options.get('--env-file')
    environment = Environment.from_env_file(override_dir or project_dir, environment_file)
    environment.silent = options.get('COMMAND', None) in SILENT_COMMANDS
    set_parallel_limit(environment)

    # get the context for the run
    context = None
    context_name = options.get('--context', None)
    if context_name:
        context = load_context(context_name)
        if not context:
            raise UserError("Context '{}' not found".format(context_name))

    host = options.get('--host', None)
    if host is not None:
        if context:
            raise UserError(
                "-H, --host and -c, --context are mutually exclusive. Only one should be set.")
        host = host.lstrip('=')
        context = make_context(host, options, environment)

    return get_project(
        project_dir,
        get_config_path_from_options(options, environment),
        project_name=options.get('--project-name'),
        verbose=options.get('--verbose'),
        context=context,
        environment=environment,
        override_dir=override_dir,
        interpolate=(not additional_options.get('--no-interpolate')),
        environment_file=environment_file,
        enabled_profiles=get_profiles_from_options(options, environment)
    )


def set_parallel_limit(environment):
    parallel_limit = environment.get('COMPOSE_PARALLEL_LIMIT')
    if parallel_limit:
        try:
            parallel_limit = int(parallel_limit)
        except ValueError:
            raise errors.UserError(
                'COMPOSE_PARALLEL_LIMIT must be an integer (found: "{}")'.format(
                    environment.get('COMPOSE_PARALLEL_LIMIT')
                )
            )
        if parallel_limit <= 1:
            raise errors.UserError('COMPOSE_PARALLEL_LIMIT can not be less than 2')
        parallel.GlobalLimit.set_global_limit(parallel_limit)


def get_project_dir(options):
    override_dir = None
    files = get_config_path_from_options(options, os.environ)
    if files:
        if files[0] == '-':
            return '.'
        override_dir = os.path.dirname(files[0])
    return options.get('--project-directory') or override_dir


def get_config_from_options(base_dir, options, additional_options=None):
    additional_options = additional_options or {}
    override_dir = get_project_dir(options)
    environment_file = options.get('--env-file')
    environment = Environment.from_env_file(override_dir or base_dir, environment_file)
    config_path = get_config_path_from_options(options, environment)
    return config.load(
        config.find(base_dir, config_path, environment, override_dir),
        not additional_options.get('--no-interpolate')
    )


def get_config_path_from_options(options, environment):
    def unicode_paths(paths):
        return [p.decode('utf-8') if isinstance(p, bytes) else p for p in paths]

    file_option = options.get('--file')
    if file_option:
        return unicode_paths(file_option)

    config_files = environment.get('COMPOSE_FILE')
    if config_files:
        pathsep = environment.get('COMPOSE_PATH_SEPARATOR', os.pathsep)
        return unicode_paths(config_files.split(pathsep))
    return None


def get_profiles_from_options(options, environment):
    profile_option = options.get('--profile')
    if profile_option:
        return profile_option

    profiles = environment.get('COMPOSE_PROFILES')
    if profiles:
        return profiles.split(',')

    return []


def get_project(project_dir, config_path=None, project_name=None, verbose=False,
                context=None, environment=None, override_dir=None,
{'fraction_non_alphanumeric': 0.09703145768719539, 'fraction_numerical': 0.0011076650420912715, 'mean_word_length': 3.2589622641509433, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7621814', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2585, 'n_words': 588}