<commit_before>import json
import shutil
import os
import subprocess
import sys
import configparser

import click
from babel.messages.pofile import read_po

from spynl.main.version import __version__ as spynl_version
from spynl.cli.utils import resolve_packages, check_ini, run_command, fail
from spynl.main.pkg_utils import (
    SPYNL_DISTRIBUTION,
    lookup_scm_commit,
    lookup_scm_commit_describe,
    get_spynl_packages,
)


@click.group()
@click.version_option(spynl_version)
def cli():
    """Entry point for all commands."""


@cli.group()
def dev():
    """entry point for dev commands."""


package_option = click.option(
    '-p',
    '--packages',
    multiple=True,
    callback=resolve_packages,
    help='Packages to operate on.',
)


language_option = click.option(
    '-l',
    '--languages',
    multiple=True,
    default=['nl', 'en', 'de', 'fr', 'es', 'it'],
    help=(
        'A language code such as "en" or "nl". '
        'Can be provided multiple times for multiple languages.'
    ),
)


ini_option = click.option(
    '-i',
    '--ini',
    help='Specify an ini file to use.',
    type=click.Path(exists=True),
    callback=check_ini,
)

task_option = click.option(
    '-t', '--task', type=click.Choice(['dev', 'latest']), default='dev'
)


@dev.command()
def versions():
    """Generate a human readable json file specifiying the versions in use."""
    versions = {'plugins': {}}
    for package in get_spynl_packages():
        info = {
            package.project_name: {
                'version': package.version,
                'commit': lookup_scm_commit(package.location),
                'scmVersion': lookup_scm_commit_describe(package.location),
            }
        }
        if package.project_name == SPYNL_DISTRIBUTION.project_name:
            versions.update(info)
        else:
            versions['plugins'].update(info)

    versions_path = os.path.join(sys.prefix, 'versions.json')
    with open(versions_path, 'w') as f:
        versions = json.dumps(versions, indent=4)
        print(versions, file=f)
    click.echo('Installed versions file successfully.')
    click.echo(versions)


@dev.command()
@package_option
@click.option('--reports', is_flag=True)
@click.option('--non-interactive', is_flag=True)
@click.option('--reports', is_flag=True)
@click.argument('pytest-options', nargs=-1)
def test(packages, reports, non_interactive, pytest_options):
    """Run tests."""
    cmd = sys.executable + ' -m pytest '

    if reports:
        cmd += ' --cov=spynl '

    for idx, pkg in enumerate(packages, start=1):
        cmd += ' '.join(pytest_options)

        os.chdir(pkg.location)
        try:
            run_command(cmd, check=True)
        except subprocess.CalledProcessError:
            failed_tests = True
            if non_interactive:
                fail("Tests for {} failed".format(pkg.project_name))
            else:
                msg = 'Do you wish to continue?'
                if idx != len(packages) and not click.confirm(msg):
                    fail('Aborting at user request.')
    if failed_tests:
        fail('Failed tests')


@dev.command()
@click.option(
    '--force', '-f', is_flag=True, default=False, help='Overwrite existing hooks'
)
@package_option
def install_git_hooks(force, packages):<commit_msg>hotfix cli command<commit_after>import json
import shutil
import os
import subprocess
import sys
import configparser

import click
from babel.messages.pofile import read_po

from spynl.main.version import __version__ as spynl_version
from spynl.cli.utils import resolve_packages, check_ini, run_command, fail
from spynl.main.pkg_utils import (
    SPYNL_DISTRIBUTION,
    lookup_scm_commit,
    lookup_scm_commit_describe,
    get_spynl_packages,
)


@click.group()
@click.version_option(spynl_version)
def cli():
    """Entry point for all commands."""


@cli.group()
def dev():
    """entry point for dev commands."""


package_option = click.option(
    '-p',
    '--packages',
    multiple=True,
    callback=resolve_packages,
    help='Packages to operate on.',
)


language_option = click.option(
    '-l',
    '--languages',
    multiple=True,
    default=['nl', 'en', 'de', 'fr', 'es', 'it'],
    help=(
        'A language code such as "en" or "nl". '
        'Can be provided multiple times for multiple languages.'
    ),
)


ini_option = click.option(
    '-i',
    '--ini',
    help='Specify an ini file to use.',
    type=click.Path(exists=True),
    callback=check_ini,
)

task_option = click.option(
    '-t', '--task', type=click.Choice(['dev', 'latest']), default='dev'
)


@dev.command()
def versions():
    """Generate a human readable json file specifiying the versions in use."""
    versions = {'plugins': {}}
    for package in get_spynl_packages():
        info = {
            package.project_name: {
                'version': package.version,
                'commit': lookup_scm_commit(package.location),
                'scmVersion': lookup_scm_commit_describe(package.location),
            }
        }
        if package.project_name == SPYNL_DISTRIBUTION.project_name:
            versions.update(info)
        else:
            versions['plugins'].update(info)

    versions_path = os.path.join(sys.prefix, 'versions.json')
    with open(versions_path, 'w') as f:
        versions = json.dumps(versions, indent=4)
        print(versions, file=f)
    click.echo('Installed versions file successfully.')
    click.echo(versions)


@dev.command()
@package_option
@click.option('--reports', is_flag=True)
@click.option('--non-interactive', is_flag=True)
@click.argument('pytest-options', nargs=-1)
def test(packages, reports, non_interactive, pytest_options):
    """Run tests."""
    cmd = sys.executable + ' -m pytest '

    if reports:
        cmd += ' --cov=spynl '

    failed_tests = False
    for idx, pkg in enumerate(packages, start=1):
        cmd += ' '.join(pytest_options)

        os.chdir(pkg.location)
        try:
            run_command(cmd, check=True)
        except subprocess.CalledProcessError:
            failed_tests = True
            if non_interactive:
                fail("Tests for {} failed".format(pkg.project_name))
            else:
                msg = 'Do you wish to continue?'
                if idx != len(packages) and not click.confirm(msg):
                    fail('Aborting at user request.')
    if failed_tests:
        fail('Failed tests')


@dev.command()
@click.option(
    '--force', '-f', is_flag=True, default=False, help='Overwrite existing hooks'
)
@package_option
def install_git_hooks(force, packages):
{'fraction_non_alphanumeric': 0.13241316547853785, 'fraction_numerical': 0.0009100561201274078, 'mean_word_length': 2.8448979591836734, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2059534', 'n_tokens_mistral': 2074, 'n_tokens_neox': 2030, 'n_words': 489}