<commit_before> #!/usr/bin/env python

import boto
import imp
import json
import os
import subprocess
import sys
import webbrowser

from distutils.spawn import find_executable
from fabric.api import local, prompt, require, settings, task
from fabric.state import env
from glob import glob
from oauth import get_document, get_credentials
from time import sleep

import app_config
import assets
import ftp as flat
import render
import utils

from render_utils import load_graphic_config
from etc.gdocs import GoogleDoc

SPREADSHEET_COPY_URL_TEMPLATE = 'https://www.googleapis.com/drive/v2/files/%s/copy'
SPREADSHEET_VIEW_TEMPLATE = 'https://docs.google.com/spreadsheet/ccc?key=%s#gid=1'

"""
Base configuration
"""
env.settings = None

"""
Environments

Changing environment requires a full-stack test.
An environment points to both a server and an S3
bucket.
"""
@task
def production():
    """
    Run as though on production.
    """
    env.settings = 'production'
    app_config.configure_targets(env.settings)

@task
def staging():
    """
    Run as though on staging.
    """
    env.settings = 'staging'
    app_config.configure_targets(env.settings)

"""
Running the app
"""
@task
def app(port='8000'):
    """
    Serve app.py.
    """
    local('gunicorn -b 0.0.0.0:%s --timeout 3600 --debug --reload app:wsgi_app' % port)

"""
Deployment

Changes to deployment requires a full-stack test. Deployment
has two primary functions: Pushing flat files to S3 and deploying
code to a remote server if required.
"""
@task
def deploy(slug):
    """
    Deploy the latest app to S3 and, if configured, to our servers.
    """
    require('settings', provided_by=[production, staging])

    if not slug:
        print 'You must specify a project slug, like this: "deploy:slug"'
        return

    graphic_root = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
    # s3_root = '%s/graphics/%s' % (app_config.PROJECT_SLUG, slug)
    graphic_assets = '%s/assets' % graphic_root
    # s3_assets = '%s/assets' % s3_root

    graphic_config = load_graphic_config(graphic_root)

    # use_assets = getattr(graphic_config, 'USE_ASSETS', True)
    default_max_age = getattr(graphic_config, 'DEFAULT_MAX_AGE', None) or app_config.DEFAULT_MAX_AGE
    # assets_max_age = getattr(graphic_config, 'ASSETS_MAX_AGE', None) or app_config.ASSETS_MAX_AGE

    update_copy(slug)

    # if use_assets:
    #     assets.sync(slug)

    render.render(slug)

    flat.deploy_folder(
        graphic_root,
        slug,
        headers={
            'Cache-Control': 'max-age=%i' % default_max_age
        },
        ignore=['%s/*' % graphic_assets]
    )

    # print use_assets
    # if use_assets:
    #     flat.deploy_folder(
    #         graphic_assets,
    #         s3_assets,
    #         headers={
    #             'Cache-Control': 'max-age=%i' % assets_max_age
    #         }
    #     )

    print 'Deployed'

@task
def deploy_template(slug, template):
    require('settings', provided_by=[production, staging])

    if not slug:
        print 'You must specify a project slug and template, like this: "deploy_template:slug,template=template"'
        return

    graphic_root = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
    graphic_assets = '%s/assets' % graphic_root
    graphic_path = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = load_graphic_config(graphic_root)
    default_max_age = getattr(graphic_config, 'DEFAULT_MAX_AGE', None) or app_config.DEFAULT_MAX_AGE

    print 'Copying latest templates...'
    local('cp -r graphic_templates/_base/ %s' % (graphic_path))
    local('cp -r graphic_templates/%s/* %s' % (template, graphic_path))<commit_msg>Avoid overwriting graphic_config.py when getting new templates, so we don't lose the google sheet<commit_after> #!/usr/bin/env python

import boto
import imp
import json
import os
import subprocess
import sys
import webbrowser

from distutils.spawn import find_executable
from fabric.api import local, prompt, require, settings, task
from fabric.state import env
from glob import glob
from oauth import get_document, get_credentials
from time import sleep

import app_config
import assets
import ftp as flat
import render
import utils

from render_utils import load_graphic_config
from etc.gdocs import GoogleDoc

SPREADSHEET_COPY_URL_TEMPLATE = 'https://www.googleapis.com/drive/v2/files/%s/copy'
SPREADSHEET_VIEW_TEMPLATE = 'https://docs.google.com/spreadsheet/ccc?key=%s#gid=1'

"""
Base configuration
"""
env.settings = None

"""
Environments

Changing environment requires a full-stack test.
An environment points to both a server and an S3
bucket.
"""
@task
def production():
    """
    Run as though on production.
    """
    env.settings = 'production'
    app_config.configure_targets(env.settings)

@task
def staging():
    """
    Run as though on staging.
    """
    env.settings = 'staging'
    app_config.configure_targets(env.settings)

"""
Running the app
"""
@task
def app(port='8000'):
    """
    Serve app.py.
    """
    local('gunicorn -b 0.0.0.0:%s --timeout 3600 --debug --reload app:wsgi_app' % port)

"""
Deployment

Changes to deployment requires a full-stack test. Deployment
has two primary functions: Pushing flat files to S3 and deploying
code to a remote server if required.
"""
@task
def deploy(slug):
    """
    Deploy the latest app to S3 and, if configured, to our servers.
    """
    require('settings', provided_by=[production, staging])

    if not slug:
        print 'You must specify a project slug, like this: "deploy:slug"'
        return

    graphic_root = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
    # s3_root = '%s/graphics/%s' % (app_config.PROJECT_SLUG, slug)
    graphic_assets = '%s/assets' % graphic_root
    # s3_assets = '%s/assets' % s3_root

    graphic_config = load_graphic_config(graphic_root)

    # use_assets = getattr(graphic_config, 'USE_ASSETS', True)
    default_max_age = getattr(graphic_config, 'DEFAULT_MAX_AGE', None) or app_config.DEFAULT_MAX_AGE
    # assets_max_age = getattr(graphic_config, 'ASSETS_MAX_AGE', None) or app_config.ASSETS_MAX_AGE

    update_copy(slug)

    # if use_assets:
    #     assets.sync(slug)

    render.render(slug)

    flat.deploy_folder(
        graphic_root,
        slug,
        headers={
            'Cache-Control': 'max-age=%i' % default_max_age
        },
        ignore=['%s/*' % graphic_assets]
    )

    # print use_assets
    # if use_assets:
    #     flat.deploy_folder(
    #         graphic_assets,
    #         s3_assets,
    #         headers={
    #             'Cache-Control': 'max-age=%i' % assets_max_age
    #         }
    #     )

    print 'Deployed'

@task
def deploy_template(slug, template):
    require('settings', provided_by=[production, staging])

    if not slug:
        print 'You must specify a project slug and template, like this: "deploy_template:slug,template=template"'
        return

    graphic_root = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
    graphic_assets = '%s/assets' % graphic_root
    graphic_path = '%s/%s' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = load_graphic_config(graphic_root)
    default_max_age = getattr(graphic_config, 'DEFAULT_MAX_AGE', None) or app_config.DEFAULT_MAX_AGE

    print 'Copying latest templates...'
    local('mv %s/graphic_config.py %s/graphic_config.py.BACKUP' % (graphic_path))
    local('cp -r graphic_templates/_base/ %s' % (graphic_path))
    local('cp -r graphic_templates/%s/* %s' % (template, graphic_path))
    local('mv %s/graphic_config.py.BACKUP %s/graphic_config.py' % (graphic_path))
{'fraction_non_alphanumeric': 0.11159535129424195, 'fraction_numerical': 0.005546751188589541, 'mean_word_length': 3.436438195664909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '4076603', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2457, 'n_words': 632}