<commit_before># Copyright 2015 <NAME>
#
# This file is part of CloudMailing.
#
# CloudMailing is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CloudMailing is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with CloudMailing.  If not, see <http://www.gnu.org/licenses/>.

import glob
import os
import random
import tempfile
from fabric.api import env, cd, run, put, settings, prefix, task, local, get
from fabric.contrib.project import rsync_project
from fabric.contrib.files import append
import sys

env.shell = "/bin/sh -c"

try:
    import local_settings
    env.roledefs = local_settings.roledefs
    default_cm_config = local_settings.default_cm_config
except ImportError:
    print >> sys.stderr, "Can't find local_settings.py ; No roles definitions"
    default_cm_config = {}

FAB_PATH = os.path.dirname(os.path.abspath(__file__))
WORKSPACE = os.path.abspath(os.path.join(FAB_PATH, "..", ".."))
TARGET_PATH = '/usr/local/cm'


@task
def get_system_name():
    return run("uname")


@task
def cm_stop():
    run("supervisorctl stop cm:cm_master")
    run("supervisorctl stop cm:cm_satellite")


@task
def cm_start():
    run("supervisorctl start cm:cm_master")
    run("supervisorctl start cm:cm_satellite")


@task
def stop_master():
    run("supervisorctl stop cm:cm_master")


@task
def start_master():
    run("supervisorctl start cm:cm_master")


@task
def stop_satellite():
    run("supervisorctl stop cm:cm_satellite")


@task
def start_satellite():
    run("supervisorctl start cm:cm_satellite")


@task
def test():
    with cd(TARGET_PATH):
        print 'exist:', os.path.exists('/Users')


def clean_compiled_files():
    # cleanup *.pyc / *.pyo files
    put(os.path.join(WORKSPACE, 'deployment', "cm_compile.py"), TARGET_PATH)
    with cd(TARGET_PATH):
        run("python cm_compile.py -c")


def compile_python_files():
    # create *.pyc / *.pyo files
    put(os.path.join(WORKSPACE, 'deployment', "cm_compile.py"), TARGET_PATH)
    with cd(TARGET_PATH):
        run("python -O cm_compile.py")


def sync_sources(test_only=False):

    rsync_project(
        TARGET_PATH,
        local_dir=WORKSPACE + "/",
        delete=True,
        extra_opts='-ci --filter=". %s"' % os.path.join(FAB_PATH, "rsync_filter") + (test_only and " --dry-run" or ""),
        #extra_opts="-ci --dry-run",
    )



@task
def deploy_cm_master():
    run("mkdir -p %s" % TARGET_PATH)
    clean_compiled_files()
    sync_sources()
    compile_python_files()

    # put(os.path.join(WORKSPACE, "requirements.txt"), TARGET_PATH)

    with cd(TARGET_PATH):
        with settings(warn_only=True):
            if run("test -d .env").failed:
                run("virtualenv .env")

        with prefix('. .env/bin/activate'):
            run('pip install pip --upgrade')
            run('pip install -r requirements.txt --upgrade')
            # run('pip install -r requirements-testing.txt')

    # with cd(TARGET_PATH):
    #     with prefix('. .env/bin/activate'):
            # run('python manage.py syncdb --noinput')<commit_msg>Add version from git during deployment<commit_after># Copyright 2015 <NAME>
#
# This file is part of CloudMailing.
#
# CloudMailing is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CloudMailing is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with CloudMailing.  If not, see <http://www.gnu.org/licenses/>.
import StringIO

import glob
import os
import random
import tempfile
from fabric.api import env, cd, run, put, settings, prefix, task, local, get
from fabric.contrib.project import rsync_project
from fabric.contrib import files
import sys

env.shell = "/bin/sh -c"

try:
    import local_settings
    env.roledefs = local_settings.roledefs
    default_cm_config = local_settings.default_cm_config
except ImportError:
    print >> sys.stderr, "Can't find local_settings.py ; No roles definitions"
    default_cm_config = {}

FAB_PATH = os.path.dirname(os.path.abspath(__file__))
WORKSPACE = os.path.abspath(os.path.join(FAB_PATH, "..", ".."))
TARGET_PATH = '/usr/local/cm'


@task
def get_system_name():
    return run("uname")


@task
def cm_stop():
    run("supervisorctl stop cm:cm_master")
    run("supervisorctl stop cm:cm_satellite")


@task
def cm_start():
    run("supervisorctl start cm:cm_master")
    run("supervisorctl start cm:cm_satellite")


@task
def stop_master():
    run("supervisorctl stop cm:cm_master")


@task
def start_master():
    run("supervisorctl start cm:cm_master")


@task
def stop_satellite():
    run("supervisorctl stop cm:cm_satellite")


@task
def start_satellite():
    run("supervisorctl start cm:cm_satellite")


@task
def test():
    with cd(TARGET_PATH):
        print 'exist:', os.path.exists('/Users')


def clean_compiled_files():
    # cleanup *.pyc / *.pyo files
    put(os.path.join(WORKSPACE, 'deployment', "cm_compile.py"), TARGET_PATH)
    with cd(TARGET_PATH):
        run("python cm_compile.py -c")


def compile_python_files():
    # create *.pyc / *.pyo files
    put(os.path.join(WORKSPACE, 'deployment', "cm_compile.py"), TARGET_PATH)
    with cd(TARGET_PATH):
        run("python -O cm_compile.py")


def sync_sources(test_only=False):

    rsync_project(
        TARGET_PATH,
        local_dir=WORKSPACE + "/",
        delete=True,
        extra_opts='-ci --filter=". %s"' % os.path.join(FAB_PATH, "rsync_filter") + (test_only and " --dry-run" or ""),
        #extra_opts="-ci --dry-run",
    )


@task
def put_version():
    import subprocess
    label = subprocess.check_output(["git", "describe"]).strip()
    stats = subprocess.check_output(['git', 'diff', '--shortstat'])
    dirty = len(stats) > 0 and stats[-1]
    with cd(TARGET_PATH + '/cloud_mailing'):
        put(StringIO.StringIO('VERSION=%s%s\n' % (label, dirty and "-dirty" or "")), 'version.properties')


@task
def deploy_cm_master():
    run("mkdir -p %s" % TARGET_PATH)
    clean_compiled_files()
    sync_sources()
    put_version()
    compile_python_files()

    # put(os.path.join(WORKSPACE, "requirements.txt"), TARGET_PATH)

    with cd(TARGET_PATH):
        with settings(warn_only=True):
            if run("test -d .env").failed:
                run("virtualenv .env")

        with prefix('. .env/bin/activate'):
            run('pip install pip --upgrade')
            run('pip install -r requirements.txt --upgrade')
            # run('pip install -r requirements-testing.txt')

    # with cd(TARGET_PATH):
    #     with prefix('. .env/bin/activate'):
            # run('python manage.py syncdb --noinput')
{'fraction_non_alphanumeric': 0.12537112010796223, 'fraction_numerical': 0.0016194331983805667, 'mean_word_length': 3.4915151515151517, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6168284', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2314, 'n_words': 666}