Example #1
0
def install():
    """Ensure that the mercurial hooks are installed.

    This searches for the ``.hg/hgrc`` configuration file and will add commit
    and qrefresh hooks to it, if they do not already exist.

    It will also print a message to stdout about how to configure the hook.

    :returns:
        True if successful, False if the ``.hg/hgrc`` file doesn't exist.
    :rtype:
        bool
    :raises:
        flake8.exceptions.MercurialCommitHookAlreadyExists
    :raises:
        flake8.exceptions.MercurialQRefreshHookAlreadyExists
    """
    hgrc = find_hgrc(create_if_missing=True)
    if hgrc is None:
        return False

    hgconfig = configparser_for(hgrc)

    if not hgconfig.has_section('hooks'):
        hgconfig.add_section('hooks')

    if hgconfig.has_option('hooks', 'commit'):
        raise exc.MercurialCommitHookAlreadyExists(
            path=hgrc,
            value=hgconfig.get('hooks', 'commit'),
        )

    if hgconfig.has_option('hooks', 'qrefresh'):
        raise exc.MercurialQRefreshHookAlreadyExists(
            path=hgrc,
            value=hgconfig.get('hooks', 'qrefresh'),
        )

    hgconfig.set('hooks', 'commit', 'python:flake8.main.mercurial.hook')
    hgconfig.set('hooks', 'qrefresh', 'python:flake8.main.mercurial.hook')

    if not hgconfig.has_section('flake8'):
        hgconfig.add_section('flake8')

    if not hgconfig.has_option('flake8', 'strict'):
        hgconfig.set('flake8', 'strict', False)

    with open(hgrc, 'w') as fd:
        hgconfig.write(fd)

    print('mercurial hooks installed, for configuration options see')
    print('http://flake8.pycqa.org/en/latest/user/using-hooks.html')

    return True
Example #2
0
def install():
    """Ensure that the mercurial hooks are installed."""
    hgrc = find_hgrc(create_if_missing=True)
    if hgrc is None:
        return False

    hgconfig = configparser_for(hgrc)

    if not hgconfig.has_section('hooks'):
        hgconfig.add_section('hooks')

    if hgconfig.has_option('hooks', 'commit'):
        raise exc.MercurialCommitHookAlreadyExists(
            path=hgrc,
            value=hgconfig.get('hooks', 'commit'),
        )

    if hgconfig.has_option('hooks', 'qrefresh'):
        raise exc.MercurialQRefreshHookAlreadyExists(
            path=hgrc,
            value=hgconfig.get('hooks', 'qrefresh'),
        )

    hgconfig.set('hooks', 'commit', 'python:flake8.main.mercurial.hook')
    hgconfig.set('hooks', 'qrefresh', 'python:flake8.main.mercurial.hook')

    if not hgconfig.has_section('flake8'):
        hgconfig.add_section('flake8')

    if not hgconfig.has_option('flake8', 'strict'):
        hgconfig.set('flake8', 'strict', False)

    with open(hgrc, 'w') as fd:
        hgconfig.write(fd)

    return True