<commit_before>from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from datetime import datetime
from os.path import join as _join
from os import walk
import re

from springfield.deploy.utils import git, system, base, twistd

RELEASE_NAME_FORMAT = '%Y%m%d_%H%M%S' # timestamped
# default for now
env.hosts = ['<EMAIL>']

def _setup_env(fn):
    def wrapper(branch, *args, **kwargs):
        layout(branch)
        return fn(branch, *args, **kwargs)
    wrapper.func_name = fn.func_name
    wrapper.func_doc = (fn.func_doc or '') + \
                                        "Requires the branch from which you " \
                                        "want to deploy as an argument."
    return wrapper


def _setup_env_for(branch):
    env.branch = branch
    env.github_repo = 'http://github.com/%(github_user)s/%(github_repo_name)s.git' % env
    env.deploy_to = '/var/praekelt/%(github_repo_name)s/%(branch)s' % env
    env.releases_path = "%(deploy_to)s/releases" % env
    env.current = "%(deploy_to)s/current" % env
    env.shared_path = "%(deploy_to)s/shared" % env
    env.tmp_path = "%(shared_path)s/tmp" % env
    env.pip_cache_path = "%(tmp_path)s/cache/pip" % env
    env.pids_path = "%(tmp_path)s/pids" % env
    env.logs_path = "%(shared_path)s/logs" % env
    env.repo_path = "%(shared_path)s/repositories" % env
    env.django_settings_file = "environments.%(branch)s" % env
    env.layout = [
        env.releases_path,
        env.tmp_path,
        env.pip_cache_path,
        env.pids_path,
        env.logs_path,
        env.repo_path,
    ]

def _repo_path(repo_name):
    return '%(repo_path)s/%(github_repo_name)s' % env

def _repo(repo_name):
    """helper to quickly switch to a repository"""
    return cd(_repo_path(repo_name))

def layout(branch):
    """
    Create a file system directory layout for deploying to.
    """
    require('hosts')
    _setup_env_for(branch)
    require('layout', provided_by=['_setup_env_for'])
    system.create_dirs(env.layout)

@_setup_env
def deploy(branch):
    """
    Deploy the application in a timestamped release folder.
    
        $ fab deploy:staging
    
    Internally this does the following:
    
        * shuts down the current supervisord daemon if it is running
        * `git pull` if a cached repository already exists
        * `git clone` if it's the first deploy ever
        * Checkout the current selected branch
        * Create a new timestamped release directory
        * Copy the cached repository to the new release directory
        * Setup the virtualenv
        * Install PIP's requirements, downloading new ones if not already cached
        * Symlink `<branch>/current` to `<branch>/releases/<timestamped release directory>`
    
    """
    # shutdown supervisord daemon
    shutdown(branch)

    if not git.is_repository(_repo_path(env.github_repo_name)):
        # repository doesn't exist, do a fresh clone
        with cd(env.repo_path):
            git.clone(env.github_repo, env.github_repo_name)
        with _repo(env.github_repo_name):
            git.checkout(branch)
    else:
        # repository exists
        with _repo(env.github_repo_name):
            if not (branch == git.current_branch()):
                # switch to our branch if not already
                git.checkout(branch)
            # pull in the latest code
            git.pull(branch)
    # 20100603_125848
    new_release_name = datetime.utcnow().strftime(RELEASE_NAME_FORMAT)
    # /var/praekelt/vumi/staging/releases/20100603_125848
    new_release_path = _join(env.releases_path, new_release_name)
    # /var/praekelt/vumi/staging/releases/20100603_125848/vumi
    # Django needs the project name as it's parent dir since that is 
    # automagically appended to the loadpath
    new_release_repo = _join(new_release_path, env.github_repo_name)
    
    system.create_dir(new_release_path)
    system.copy_dirs(_repo_path(env.github_repo_name), new_release_path)
    
    copy_settings_file(branch, release=new_release_name)
    
    symlink_shared_dirs = ['logs', 'tmp']
    for dirname in symlink_shared_dirs:
        with cd(new_release_repo):
            system.remove(dirname, recursive_force=True)
            system.symlink(_join(env.shared_path, dirname), dirname)
    
    # create the virtualenv
    create_virtualenv(branch)
    # ensure we're deploying the exact revision as we locally have
    base.set_current(new_release_name)


@_setup_env
def copy_settings_file(branch, release=None):
    """
    Copy the settings file for this branch to the server
    
        $ fab copy_settings_file:staging
        
    If no release is specified it defaults to the latest release.
    
    
    """
    release = release or base.current_release()<commit_msg>fixed supervisord shutdown sequence<commit_after>from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from datetime import datetime
from os.path import join as _join
from os import walk
import re

from springfield.deploy.utils import git, system, base, twistd

RELEASE_NAME_FORMAT = '%Y%m%d_%H%M%S' # timestamped
# default for now
env.hosts = ['<EMAIL>']

def _setup_env(fn):
    def wrapper(branch, *args, **kwargs):
        layout(branch)
        return fn(branch, *args, **kwargs)
    wrapper.func_name = fn.func_name
    wrapper.func_doc = (fn.func_doc or '') + \
                                        "Requires the branch from which you " \
                                        "want to deploy as an argument."
    return wrapper


def _setup_env_for(branch):
    env.branch = branch
    env.github_repo = 'http://github.com/%(github_user)s/%(github_repo_name)s.git' % env
    env.deploy_to = '/var/praekelt/%(github_repo_name)s/%(branch)s' % env
    env.releases_path = "%(deploy_to)s/releases" % env
    env.current = "%(deploy_to)s/current" % env
    env.shared_path = "%(deploy_to)s/shared" % env
    env.tmp_path = "%(shared_path)s/tmp" % env
    env.pip_cache_path = "%(tmp_path)s/cache/pip" % env
    env.pids_path = "%(tmp_path)s/pids" % env
    env.logs_path = "%(shared_path)s/logs" % env
    env.repo_path = "%(shared_path)s/repositories" % env
    env.django_settings_file = "environments.%(branch)s" % env
    env.layout = [
        env.releases_path,
        env.tmp_path,
        env.pip_cache_path,
        env.pids_path,
        env.logs_path,
        env.repo_path,
    ]

def _repo_path(repo_name):
    return '%(repo_path)s/%(github_repo_name)s' % env

def _repo(repo_name):
    """helper to quickly switch to a repository"""
    return cd(_repo_path(repo_name))

def layout(branch):
    """
    Create a file system directory layout for deploying to.
    """
    require('hosts')
    _setup_env_for(branch)
    require('layout', provided_by=['_setup_env_for'])
    system.create_dirs(env.layout)

@_setup_env
def deploy(branch):
    """
    Deploy the application in a timestamped release folder.
    
        $ fab deploy:staging
    
    Internally this does the following:
    
        * `git pull` if a cached repository already exists
        * `git clone` if it's the first deploy ever
        * Checkout the current selected branch
        * Create a new timestamped release directory
        * Copy the cached repository to the new release directory
        * Setup the virtualenv
        * Install PIP's requirements, downloading new ones if not already cached
        * Symlink `<branch>/current` to `<branch>/releases/<timestamped release directory>`
        * Shut down the current supervisord daemon if it is running
    
    """
    if not git.is_repository(_repo_path(env.github_repo_name)):
        # repository doesn't exist, do a fresh clone
        with cd(env.repo_path):
            git.clone(env.github_repo, env.github_repo_name)
        with _repo(env.github_repo_name):
            git.checkout(branch)
    else:
        # repository exists
        with _repo(env.github_repo_name):
            if not (branch == git.current_branch()):
                # switch to our branch if not already
                git.checkout(branch)
            # pull in the latest code
            git.pull(branch)
    # 20100603_125848
    new_release_name = datetime.utcnow().strftime(RELEASE_NAME_FORMAT)
    # /var/praekelt/vumi/staging/releases/20100603_125848
    new_release_path = _join(env.releases_path, new_release_name)
    # /var/praekelt/vumi/staging/releases/20100603_125848/vumi
    # Django needs the project name as it's parent dir since that is 
    # automagically appended to the loadpath
    new_release_repo = _join(new_release_path, env.github_repo_name)
    
    system.create_dir(new_release_path)
    system.copy_dirs(_repo_path(env.github_repo_name), new_release_path)
    
    copy_settings_file(branch, release=new_release_name)
    
    symlink_shared_dirs = ['logs', 'tmp']
    for dirname in symlink_shared_dirs:
        with cd(new_release_repo):
            system.remove(dirname, recursive_force=True)
            system.symlink(_join(env.shared_path, dirname), dirname)
    
    # create the virtualenv
    create_virtualenv(branch)
    # ensure we're deploying the exact revision as we locally have
    base.set_current(new_release_name)
    # shutdown supervisord daemon
    shutdown(branch)


@_setup_env
def copy_settings_file(branch, release=None):
    """
    Copy the settings file for this branch to the server
    
        $ fab copy_settings_file:staging
        
    If no release is specified it defaults to the latest release.
    
    
    """
    release = release or base.current_release()
{'fraction_non_alphanumeric': 0.0948559670781893, 'fraction_numerical': 0.008641975308641974, 'mean_word_length': 2.8806387225548904, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7334644', 'n_tokens_mistral': 3120, 'n_tokens_neox': 2972, 'n_words': 746}