<commit_before>"""Fab tasks that execute things on a remote server."""
from django.conf import settings

from fabric.api import cd, env, local, run

from .local import drop_db, create_db, import_db, reset_passwords
from .utils import require_server, run_workon


@require_server
def run_collectstatic():
    """
    Runs `./manage.py collectstatic` on the given server.

    Usage::

        fab <server> run_collectstatic

    """
    with cd(settings.FAB_SETTING('SERVER_PROJECT_ROOT')):
        run_workon('python2.7 manage.py collectstatic --noinput')


@require_server
def run_compilemessages():
    """
    Executes ./manage.py compilemessages on the server.

    Usage::

        fab <server name> run_compilemessages

    """

    with cd(settings.FAB_SETTING('SERVER_PROJECT_ROOT')):
        run_workon('python2.7 manage.py compilemessages')


@require_server
def run_deploy_website(restart_apache=False, restart_uwsgi=False,
                       restart_nginx=False):
    """
    Executes all tasks necessary to deploy the website on the given server.

    Usage::

        fab <server> run_deploy_website

    """
    run_git_pull()
    run_pip_install()
    run_rsync_project()
    run_syncdb()
    run_collectstatic()
    if getattr(settings, 'MAKEMESSAGES_ON_DEPLOYMENT', False):
        run_makemessages()
    if getattr(settings, 'COMPILEMESSAGES_ON_DEPLOYMENT', False):
        run_compilemessages()
    if restart_apache:
        run_restart_apache()
    if restart_uwsgi:
        run_restart_uwsgi()
    if restart_nginx:
        run_restart_nginx()
    else:
        run_touch_wsgi()


@require_server
def run_download_db(filename=None):
    """
    Downloads the database from the server into your local machine.

    In order to import the downloaded database, run ``fab import_db``

    Usage::

        fab prod run_download_db
        fab prod run_download_db:filename=foobar.dump

    """
    if not filename:
        filename = settings.DB_DUMP_FILENAME
    local('scp {0}@{1}:{2}{3} .'.format(
        env.user, env.host_string,
        settings.FAB_SETTING('SERVER_DB_BACKUP_DIR'), filename))


@require_server
def run_download_media(filename=None):
    """
    Downloads the media dump from the server into your local machine.

    In order to import the downloaded media dump, run ``fab import_media``

    Usage::

        fab prod run_download_media
        fab prod run_download_media:filename=foobar.tar.gz

    """
    if not filename:
        filename = settings.MEDIA_DUMP_FILENAME

    local('scp {0}@{1}:{2}{3} .'.format(
        env.user, env.host_string,
        settings.FAB_SETTING('SERVER_MEDIA_BACKUP_DIR'), filename))


@require_server
def run_export_db(filename=None, db_role=None):
    """
    Exports the database on the server.

    Usage::

        fab prod run_export_db
        fab prod run_export_db:filename=foobar.dump
        fab prod run_export_db:db_role=foobar1

    """
    if not filename:
        filename = settings.DB_DUMP_FILENAME
    if not db_role:
        db_role = env.db_role
    run('pg_dump -c -Fc -O -U {0} -f {1}{2}'.format(
        db_role, settings.FAB_SETTING('SERVER_DB_BACKUP_DIR'),
        filename))


@require_server
def run_export_media(filename=None):
    """<commit_msg>Update remote.py<commit_after>"""Fab tasks that execute things on a remote server."""
from django.conf import settings

from fabric.api import cd, env, local, run

from .local import drop_db, create_db, import_db, reset_passwords
from .utils import require_server, run_workon


@require_server
def run_collectstatic():
    """
    Runs `./manage.py collectstatic` on the given server.

    Usage::

        fab <server> run_collectstatic

    """
    with cd(settings.FAB_SETTING('SERVER_PROJECT_ROOT')):
        run_workon('python2.7 manage.py collectstatic --noinput')


@require_server
def run_compilemessages():
    """
    Executes ./manage.py compilemessages on the server.

    Usage::

        fab <server name> run_compilemessages

    """

    with cd(settings.FAB_SETTING('SERVER_PROJECT_ROOT')):
        run_workon('python2.7 manage.py compilemessages')


@require_server
def run_deploy_website(restart_apache=False, restart_uwsgi=False,
                       restart_nginx=False):
    """
    Executes all tasks necessary to deploy the website on the given server.

    Usage::

        fab <server> run_deploy_website

    """
    run_git_pull()
    run_pip_install()
    run_rsync_project()
    run_syncdb()
    run_collectstatic()
    if getattr(settings, 'MAKEMESSAGES_ON_DEPLOYMENT', False):
        run_makemessages()
    if getattr(settings, 'COMPILEMESSAGES_ON_DEPLOYMENT', False):
        run_compilemessages()
    if restart_apache:
        run_restart_apache()
    if restart_uwsgi:
        run_restart_uwsgi()
    if restart_nginx:
        run_restart_nginx()
    else:
        run_touch_wsgi()


@require_server
def run_download_db(filename=None):
    """
    Downloads the database from the server into your local machine.

    In order to import the downloaded database, run ``fab import_db``

    Usage::

        fab prod run_download_db
        fab prod run_download_db:filename=foobar.dump

    """
    if not filename:
        filename = settings.DB_DUMP_FILENAME
    local('scp {0}@{1}:{2}{3} .'.format(
        env.user, env.host_string,
        settings.FAB_SETTING('SERVER_DB_BACKUP_DIR'), filename))


@require_server
def run_download_media(filename=None):
    """
    Downloads the media dump from the server into your local machine.

    In order to import the downloaded media dump, run ``fab import_media``

    Usage::

        fab prod run_download_media
        fab prod run_download_media:filename=foobar.tar.gz

    """
    if not filename:
        filename = settings.MEDIA_DUMP_FILENAME

    local('scp {0}@{1}:{2}{3} .'.format(
        env.user, env.host_string,
        settings.FAB_SETTING('SERVER_MEDIA_BACKUP_DIR'), filename))


@require_server
def run_export_db(filename=None, db_role=None, db_name=False):
    """
    Exports the database on the server.

    Usage::

        fab prod run_export_db
        fab prod run_export_db:filename=foobar.dump
        fab prod run_export_db:db_role=foobar1

    """
    if not filename:
        filename = settings.DB_DUMP_FILENAME
    if not db_role:
        db_role = env.db_role
    if not db_name:
        db_name = env.db_name
    run('pg_dump -c -Fc -O -U {0} {1} -f {2}{3}'.format(
        db_role, db_name, settings.FAB_SETTING('SERVER_DB_BACKUP_DIR'),
        filename))


@require_server
def run_export_media(filename=None):
    """
{'fraction_non_alphanumeric': 0.09713592968631611, 'fraction_numerical': 0.005000757690559176, 'mean_word_length': 3.127579737335835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7470716', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2071, 'n_words': 505}