<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from time import sleep
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from fabric.utils import abort
from fabfile_local import *
from subprocess import call
from datetime import datetime

# Globals

env.project_name = 'editorsnotes'

# Environments

def beta():
    "Use the beta-testing webserver."
    env.hosts = ['beta.editorsnotes.org']
    env.project_path = '/db/projects/%(project_name)s-beta' % env
    env.vhosts_path = '/etc/httpd/sites.d'
    env.python = '/usr/bin/python2.7'
    env.site_packages = ['/usr/lib64/python2.7/site-packages',
                         '/usr/lib/python2.7/site-packages']

def pro():
    "Use the production webserver."
    env.hosts = ['editorsnotes.org']
    env.project_path = '/db/projects/%(project_name)s' % env
    env.vhosts_path = '/etc/httpd/sites.d'
    env.python = '/usr/bin/python2.7'
    env.site_packages = ['/usr/lib64/python2.7/site-packages',
                         '/usr/lib/python2.7/site-packages']

# Tasks

def test():
    "Run the test suite locally."
    local("python manage.py test" % env)
    
def test_remote():
    "Run the test suite remotely."
    require('hosts', provided_by=[dev])
    require('project_path')
    run('cd %(project_path)s/releases/current;  ../../bin/python manage.py test' % env)
    
def setup():
    """
    Setup a fresh virtualenv as well as a few useful directories, then
    run a full deployment.
    """
    require('hosts', provided_by=[dev])
    require('project_path')
    run('mkdir -p %(project_path)s' % env)
    with cd(env.project_path):
        run('virtualenv -p %(python)s --no-site-packages .' % env)
        run('mkdir -p logs; mkdir -p releases; mkdir -p shared; mkdir -p packages' % env)
        run('cd releases; touch none; ln -sf none current; ln -sf none previous')
    deploy()
    
def deploy():
    """
    Deploy the latest version of the site to the servers, install any
    required third party modules, install the virtual host and then
    restart the webserver.
    """
    require('hosts', provided_by=[dev])
    require('project_path')
    import time
    env.release = time.strftime('%Y%m%d%H%M%S')
    upload_tar_from_git()
    upload_local_settings()
    upload_deploy_info()
    symlink_system_packages()
    install_requirements()
    install_site()
    symlink_current_release()
    migrate()
    collect_static()
    restart_webserver()
    sleep(2)
    try:
        type(env.gnome)
        local('gnome-open http://%(host)s/' % env)
    except:
        local('open http://%(host)s/' % env)
    
def deploy_version(version):
    "Specify a specific version to be made live."
    require('hosts', provided_by=[dev])
    require('project_path')
    env.version = version
    with cd(env.project_path):
        run('rm releases/previous; mv releases/current releases/previous')
        run('ln -s %(version)s releases/current' % env)
    restart_webserver()
    
def rollback():
    """
    Limited rollback capability. Simple loads the previously current
    version of the code. Rolling back again will swap between the two.
    """
    require('hosts', provided_by=[dev])
    require('project_path')
    with cd(env.project_path):
        run('mv releases/current releases/_previous;')
        run('mv releases/previous releases/current;')
        run('mv releases/_previous releases/previous;')
    restart_webserver()

def clean():
    "Clean out old packages and releases."
    require('hosts', provided_by=[dev])
    require('project_path')
    if (confirm('Are you sure you want to delete everything on %(host)s?' % env, 
                default=False)):
        with cd(env.project_path):
            run('rm -rf packages; rm -rf releases')
            run('mkdir -p packages; mkdir -p releases')
            run('cd releases; touch none; ln -sf none current; ln -sf none previous')
    
# Helpers. These are called by other functions rather than directly.

def upload_tar_from_git():
    "Create an archive from the current Git branch and upload it."
    require('release', provided_by=[deploy, setup])
    local('git archive --format=tar HEAD | gzip > %(release)s.tar.gz' % env)
    run('mkdir -p %(project_path)s/releases/%(release)s' % env)
    put('%(release)s.tar.gz' % env, '%(project_path)s/packages/' % env)
    run('cd %(project_path)s/releases/%(release)s && tar zxf ../../packages/%(release)s.tar.gz' % env)
    local('rm %(release)s.tar.gz' % env)

def upload_local_settings():
    "Upload the appropriate local settings file."
    require('release', provided_by=[deploy, setup])
    put('deploy/settings-%(host)s.py' % env, 
        '%(project_path)s/releases/%(release)s/%(project_name)s/settings_local.py' % env)<commit_msg>Use @task decorator in fabfile to define tasks<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from time import sleep
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from fabric.decorators import task
from fabric.utils import abort
from fabfile_local import *
from subprocess import call
from datetime import datetime

# Globals

env.project_name = 'editorsnotes'

# Environments

@task
def beta():
    "Use the beta-testing webserver."
    env.hosts = ['beta.editorsnotes.org']
    env.project_path = '/db/projects/%(project_name)s-beta' % env
    env.vhosts_path = '/etc/httpd/sites.d'
    env.python = '/usr/bin/python2.7'
    env.site_packages = ['/usr/lib64/python2.7/site-packages',
                         '/usr/lib/python2.7/site-packages']

@task
def pro():
    "Use the production webserver."
    env.hosts = ['editorsnotes.org']
    env.project_path = '/db/projects/%(project_name)s' % env
    env.vhosts_path = '/etc/httpd/sites.d'
    env.python = '/usr/bin/python2.7'
    env.site_packages = ['/usr/lib64/python2.7/site-packages',
                         '/usr/lib/python2.7/site-packages']

# Tasks

@task
def test():
    "Run the test suite locally."
    local("python manage.py test" % env)
    
@task
def test_remote():
    "Run the test suite remotely."
    require('hosts', provided_by=[dev])
    require('project_path')
    run('cd %(project_path)s/releases/current;  ../../bin/python manage.py test' % env)
    
@task
def setup():
    """
    Setup a fresh virtualenv as well as a few useful directories, then
    run a full deployment.
    """
    require('hosts', provided_by=[dev])
    require('project_path')
    run('mkdir -p %(project_path)s' % env)
    with cd(env.project_path):
        run('virtualenv -p %(python)s --no-site-packages .' % env)
        run('mkdir -p logs; mkdir -p releases; mkdir -p shared; mkdir -p packages' % env)
        run('cd releases; touch none; ln -sf none current; ln -sf none previous')
    deploy()
    
@task
def deploy():
    """
    Deploy the latest version of the site to the servers, install any
    required third party modules, install the virtual host and then
    restart the webserver.
    """
    require('hosts', provided_by=[dev])
    require('project_path')
    import time
    env.release = time.strftime('%Y%m%d%H%M%S')
    upload_tar_from_git()
    upload_local_settings()
    upload_deploy_info()
    symlink_system_packages()
    install_requirements()
    install_site()
    symlink_current_release()
    migrate()
    collect_static()
    restart_webserver()
    sleep(2)
    try:
        type(env.gnome)
        local('gnome-open http://%(host)s/' % env)
    except:
        local('open http://%(host)s/' % env)
    
@task
def deploy_version(version):
    "Specify a specific version to be made live."
    require('hosts', provided_by=[dev])
    require('project_path')
    env.version = version
    with cd(env.project_path):
        run('rm releases/previous; mv releases/current releases/previous')
        run('ln -s %(version)s releases/current' % env)
    restart_webserver()
    
@task
def rollback():
    """
    Limited rollback capability. Simple loads the previously current
    version of the code. Rolling back again will swap between the two.
    """
    require('hosts', provided_by=[dev])
    require('project_path')
    with cd(env.project_path):
        run('mv releases/current releases/_previous;')
        run('mv releases/previous releases/current;')
        run('mv releases/_previous releases/previous;')
    restart_webserver()

@task
def clean():
    "Clean out old packages and releases."
    require('hosts', provided_by=[dev])
    require('project_path')
    if (confirm('Are you sure you want to delete everything on %(host)s?' % env, 
                default=False)):
        with cd(env.project_path):
            run('rm -rf packages; rm -rf releases')
            run('mkdir -p packages; mkdir -p releases')
            run('cd releases; touch none; ln -sf none current; ln -sf none previous')
    

# Helpers. These are called by other functions rather than directly.

def upload_tar_from_git():
    "Create an archive from the current Git branch and upload it."
    require('release', provided_by=[deploy, setup])
    local('git archive --format=tar HEAD | gzip > %(release)s.tar.gz' % env)
    run('mkdir -p %(project_path)s/releases/%(release)s' % env)
    put('%(release)s.tar.gz' % env, '%(project_path)s/packages/' % env)
    run('cd %(project_path)s/releases/%(release)s && tar zxf ../../packages/%(release)s.tar.gz' % env)
    local('rm %(release)s.tar.gz' % env)

def upload_local_settings():
    "Upload the appropriate local settings file."
    require('release', provided_by=[deploy, setup])
    put('deploy/settings-%(host)s.py' % env, 
        '%(project_path)s/releases/%(release)s/%(project_name)s/settings_local.py' % env)
{'fraction_non_alphanumeric': 0.1381747413704804, 'fraction_numerical': 0.0036873911707466967, 'mean_word_length': 3.087065717873587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '162803', 'n_tokens_mistral': 3111, 'n_tokens_neox': 3017, 'n_words': 811}