<commit_before>from __future__ import division, absolute_import, print_function

__all__ = ['punic_cli', 'main']

import logging
import os
import punic.shshutil as shutil
import sys
import click
from click_didyoumean import DYMGroup
import punic
from .copy_frameworks import *
from .errors import *
from .logger import *
from .model import *
from .runner import *
from .semantic_version import *
from .utilities import *
from .version_check import *


@click.group(cls=DYMGroup)
@click.option('--echo', default=False, is_flag=True, help="""Echo all commands to terminal.""")
@click.option('--verbose', default=False, is_flag=True, help="""Verbose logging.""")
@click.option('--color/--no-color', default=True, is_flag=True, help="""TECHNICOLOR.""")
# @click.option('--timing', default=False, is_flag=True) # TODO
@click.pass_context
def punic_cli(context, echo, verbose, color):

    context.token_normalize_func = lambda x:x if not x else x.lower()

    level = logging.DEBUG if verbose else logging.INFO
    logging.basicConfig(format='%(message)s', level=level)
    runner.echo = echo
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.WARNING)
    requests_log.propagate = True

    logger.color = color

    punic = Punic()
    context.obj = punic

    # Dont do this here. use `punic version`
    # version_check()


@punic_cli.command()
@click.pass_context
def fetch(context):
    """Fetch the project's dependencies.."""
    with timeit('fetch'):
        with error_handling():
            logger.info("<cmd>fetch</cmd>")
            punic = context.obj
            punic.config.can_fetch = True # obviously

            punic.fetch()



@punic_cli.command()
@click.pass_context
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
def resolve(context, fetch):
    """Resolve dependencies and output `Carthage.resolved` file.

    This sub-command does not build dependencies. Use this sub-command when a dependency has changed and you just want to update `Cartfile.resolved`.
    """
    with timeit('resolve'):
        with error_handling():
            logger.info("<cmd>Resolve</cmd>")
            punic = context.obj
            punic.config.can_fetch = fetch

            punic.resolve()


@punic_cli.command()
@click.pass_context
@click.option('--configuration', default=None,
    help="""Dependency configurations to build. Usually 'Release' or 'Debug'.""")
@click.option('--platform', default=None, help="""Platform to build. Comma separated list.""")
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
@click.argument('deps', nargs=-1)
def build(context, configuration, platform, fetch, deps):
    """Fetch and build the project's dependencies."""
    with timeit('build'):
        with error_handling():
            logger.info("<cmd>Build</cmd>")
            punic = context.obj
            punic.config.update(configuration=configuration, platform=platform)
            punic.config.can_fetch = fetch

            punic.build(dependencies=deps)


@punic_cli.command()
@click.pass_context
@click.option('--configuration', default=None,
    help="""Dependency configurations to build. Usually 'Release' or 'Debug'.""")
@click.option('--platform', default=None, help="""Platform to build. Comma separated list.""")
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
@click.argument('deps', nargs=-1)
def update(context, configuration, platform, fetch, deps):
    """Update and rebuild the project's dependencies."""
    with timeit('update'):
        with error_handling():
            logger.info("<cmd>Update</cmd>")
            punic = context.obj
            punic.config.update(configuration=configuration, platform=platform)
            punic.config.can_fetch = fetch

            punic.resolve()
            punic.build(dependencies=deps)



@punic_cli.command()
@click.pass_context
@click.option('--derived-data', default=False, is_flag=True, help="""Clean the punic derived data directory.""")
@click.option('--caches', default=False, is_flag=True, help="""Clean the global punic carthage files.""")
@click.option('--all', default=False, is_flag=True, help="""Clean all.""")
def clean(context, derived_data, caches, all):
    """Clean project & punic environment."""
    logger.info("<cmd>Clean</cmd>")
    punic = context.obj

    if derived_data or all:
        logger.info('Erasing derived data directory'.format(**punic.__dict__))
        if punic.config.derived_data_path.exists():
            shutil.rmtree(punic.config.derived_data_path)

    if caches or all:
        if punic.config.repo_cache_directory.exists():
            logger.info('Cleaning {}'.format(punic.config.repo_cache_directory))
            shutil.rmtree(punic.config.repo_cache_directory )
        logger.info('Cleaning run cache')
        runner.reset()


@punic_cli.command()
@click.pass_context
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
@click.option('--open', default=False, is_flag=True, help="""Open the graph image file.""")
def graph(context, fetch, open):
    """Output resolved dependency graph."""
    with timeit('graph'):
        with error_handling():
            logger.info("<cmd>Graph</cmd>")
            punic = context.obj
            punic.config.can_fetch = fetch
            graph = punic.graph()

            import networkx as nx

            logger.info('Writing graph file to "{}".'.format(os.getcwd()))
            nx.drawing.nx_pydot.write_dot(graph, 'graph.dot')

            command = 'dot graph.dot -ograph.png -Tpng'
            if runner.can_run(command):
                logger.info('Rendering dot file to png file.')
                runner.check_run(command)
                if open:
                    runner.run('open graph.png')<commit_msg>Fix clean.<commit_after>from __future__ import division, absolute_import, print_function

__all__ = ['punic_cli', 'main']

import logging
import os
import punic.shshutil as shutil
import sys
import click
from click_didyoumean import DYMGroup
import punic
from .copy_frameworks import *
from .errors import *
from .logger import *
from .model import *
from .runner import *
from .semantic_version import *
from .utilities import *
from .version_check import *


@click.group(cls=DYMGroup)
@click.option('--echo', default=False, is_flag=True, help="""Echo all commands to terminal.""")
@click.option('--verbose', default=False, is_flag=True, help="""Verbose logging.""")
@click.option('--color/--no-color', default=True, is_flag=True, help="""TECHNICOLOR.""")
# @click.option('--timing', default=False, is_flag=True) # TODO
@click.pass_context
def punic_cli(context, echo, verbose, color):

    context.token_normalize_func = lambda x:x if not x else x.lower()

    level = logging.DEBUG if verbose else logging.INFO
    logging.basicConfig(format='%(message)s', level=level)
    runner.echo = echo
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.WARNING)
    requests_log.propagate = True

    logger.color = color

    punic = Punic()
    context.obj = punic

    # Dont do this here. use `punic version`
    # version_check()


@punic_cli.command()
@click.pass_context
def fetch(context):
    """Fetch the project's dependencies.."""
    with timeit('fetch'):
        with error_handling():
            logger.info("<cmd>fetch</cmd>")
            punic = context.obj
            punic.config.can_fetch = True # obviously

            punic.fetch()



@punic_cli.command()
@click.pass_context
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
def resolve(context, fetch):
    """Resolve dependencies and output `Carthage.resolved` file.

    This sub-command does not build dependencies. Use this sub-command when a dependency has changed and you just want to update `Cartfile.resolved`.
    """
    with timeit('resolve'):
        with error_handling():
            logger.info("<cmd>Resolve</cmd>")
            punic = context.obj
            punic.config.can_fetch = fetch

            punic.resolve()


@punic_cli.command()
@click.pass_context
@click.option('--configuration', default=None,
    help="""Dependency configurations to build. Usually 'Release' or 'Debug'.""")
@click.option('--platform', default=None, help="""Platform to build. Comma separated list.""")
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
@click.argument('deps', nargs=-1)
def build(context, configuration, platform, fetch, deps):
    """Fetch and build the project's dependencies."""
    with timeit('build'):
        with error_handling():
            logger.info("<cmd>Build</cmd>")
            punic = context.obj
            punic.config.update(configuration=configuration, platform=platform)
            punic.config.can_fetch = fetch

            punic.build(dependencies=deps)


@punic_cli.command()
@click.pass_context
@click.option('--configuration', default=None,
    help="""Dependency configurations to build. Usually 'Release' or 'Debug'.""")
@click.option('--platform', default=None, help="""Platform to build. Comma separated list.""")
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
@click.argument('deps', nargs=-1)
def update(context, configuration, platform, fetch, deps):
    """Update and rebuild the project's dependencies."""
    with timeit('update'):
        with error_handling():
            logger.info("<cmd>Update</cmd>")
            punic = context.obj
            punic.config.update(configuration=configuration, platform=platform)
            punic.config.can_fetch = fetch

            punic.resolve()
            punic.build(dependencies=deps)



@punic_cli.command()
@click.pass_context
@click.option('--derived-data', default=False, is_flag=True, help="""Clean the punic derived data directory.""")
@click.option('--caches', default=False, is_flag=True, help="""Clean the global punic carthage files.""")
@click.option('--all', default=False, is_flag=True, help="""Clean all.""")
def clean(context, derived_data, caches, all):
    """Clean project & punic environment."""
    logger.info("<cmd>Clean</cmd>")
    punic = context.obj

    if derived_data or all:
        logger.info('Erasing derived data directory')
        if punic.config.derived_data_path.exists():
            shutil.rmtree(punic.config.derived_data_path)

    if caches or all:
        if punic.config.repo_cache_directory.exists():
            logger.info('Erasing {}'.format(punic.config.repo_cache_directory))
            shutil.rmtree(punic.config.repo_cache_directory )
        logger.info('Erasing run cache')
        runner.reset()


@punic_cli.command()
@click.pass_context
@click.option('--fetch/--no-fetch', default=True, is_flag=True, help="""Controls whether to fetch dependencies.""")
@click.option('--open', default=False, is_flag=True, help="""Open the graph image file.""")
def graph(context, fetch, open):
    """Output resolved dependency graph."""
    with timeit('graph'):
        with error_handling():
            logger.info("<cmd>Graph</cmd>")
            punic = context.obj
            punic.config.can_fetch = fetch
            graph = punic.graph()

            import networkx as nx

            logger.info('Writing graph file to "{}".'.format(os.getcwd()))
            nx.drawing.nx_pydot.write_dot(graph, 'graph.dot')

            command = 'dot graph.dot -ograph.png -Tpng'
            if runner.can_run(command):
                logger.info('Rendering dot file to png file.')
                runner.check_run(command)
                if open:
                    runner.run('open graph.png')
{'fraction_non_alphanumeric': 0.14332274778539195, 'fraction_numerical': 0.0005014206919605549, 'mean_word_length': 3.414238288454445, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '6094202', 'n_tokens_mistral': 3575, 'n_tokens_neox': 3455, 'n_words': 842}