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

import os


COV = None
if os.environ.get('FLASK_COVERAGE'):
    import coverage

    COV = coverage.coverage(branch=True, include='app/*')
    COV.start()

if os.path.exists('.env'):
    print('Importing environment from .env...')
    for line in open('.env'):
        var = line.strip().split('=')
        if len(var) == 2:
            os.environ[var[0]] = var[1]

from main import app
from app import db
from app.models import User, Follow, Role, Permission, Post, Comment
from flask.ext.script import Manager, Shell, Command, Option
from flask.ext.migrate import Migrate, MigrateCommand

manager = Manager(app)
migrate = Migrate(app, db)


def make_shell_context():
    return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
                Permission=Permission, Post=Post, Comment=Comment)


@manager.command
def test(coverage=False, with_gae=True, flask_config='testing'):
    """Run the unit tests."""

    if coverage and not os.environ.get('FLASK_COVERAGE'):
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)

    if with_gae:
        # we assume the sdk is available on the path
        # sdk_path = '~/google-cloud-sdk/platform/google_appengine'

        # If the sdk path points to a google cloud sdk installation
        # then we should alter it to point to the GAE platform location.

        # if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')):
        #     sys.path.insert(0, os.path.join(sdk_path, 'platform/google_appengine'))
        # else:
        #     sys.path.insert(0, sdk_path)

        # Ensure that the google.appengine.* packages are available
        # in tests as well as all bundled third-party packages.

        def fix_sys_path():

            import dev_appserver

            dev_appserver.fix_sys_path()

            # Loading appengine_config from the current project ensures that any
            # changes to configuration there are available to all tests (e.g.
            # sys.path modifications, namespaces, etc.)
            try:
                import appengine_config

                (appengine_config)
            except ImportError:
                print "Note: unable to import appengine_config."

        fix_sys_path()

    import unittest

    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase()


@manager.command
def profile(length=25, profile_dir=None):
    """Start the application under the code profiler."""
    from werkzeug.contrib.profiler import ProfilerMiddleware

    app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[length],
                                      profile_dir=profile_dir)
    app.run()


@manager.command
def deploy():
    """Run deployment tasks."""
    from flask.ext.migrate import upgrade
    from app.models import Role, User

    # migrate database to latest revision
    upgrade()

    # create user roles
    Role.insert_roles()

    # create self-follows for all users
    User.add_self_follows()


@manager.command
def hello(name="Fred"):
    """
    Example command with named arg

    > python manage.py hello --name=Joe
    hello Joe

    or

    > python manage.py hello -n Joe
    hello Joe

    :param name: Who to say hello to
    """
    print "hello", name


class DevAppserver(Command):
    option_list = (
        Option('-y', '--yaml_path', dest='yaml_path', default='.'),
        Option('-l', '--log_level', dest='log_level', default='debug'),
        Option('-a', '--allow_skipped_files', dest='allow_skipped_files', default=True),
        Option('-d', '--data_storage_path', dest='data_storage_path', default='.appengine_data'),
        Option('-smtp_host', '--smtp_host', dest='smtp_host', default='smtp.googlemail.com'),
        Option('-smtp_port', '--smtp_port', dest='smtp_port', default=587),
        Option('-smtp_user', '--smtp_user', dest='smtp_user', default=os.environ.get('MAIL_USERNAME')),
        Option('-smtp_password', '--smtp_password', dest='smtp_password', default=os.environ.get('MAIL_PASSWORD')),
        Option('-smtp_allow_tls', '--smtp_allow_tls', dest='smtp_allow_tls', default=True),
    )

    def run(self, yaml_path, log_level, allow_skipped_files, data_storage_path, smtp_host, smtp_port, smtp_user,
            smtp_password, smtp_allow_tls):
        """<commit_msg>added docstring so it shows up in manage.py -?<commit_after>#!/usr/bin/env python
import sys

import os


COV = None
if os.environ.get('FLASK_COVERAGE'):
    import coverage

    COV = coverage.coverage(branch=True, include='app/*')
    COV.start()

if os.path.exists('.env'):
    print('Importing environment from .env...')
    for line in open('.env'):
        var = line.strip().split('=')
        if len(var) == 2:
            os.environ[var[0]] = var[1]

from main import app
from app import db
from app.models import User, Follow, Role, Permission, Post, Comment
from flask.ext.script import Manager, Shell, Command, Option
from flask.ext.migrate import Migrate, MigrateCommand

manager = Manager(app)
migrate = Migrate(app, db)


def make_shell_context():
    return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
                Permission=Permission, Post=Post, Comment=Comment)


@manager.command
def test(coverage=False, with_gae=True, flask_config='testing'):
    """Run the unit tests."""

    if coverage and not os.environ.get('FLASK_COVERAGE'):
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)

    if with_gae:
        # we assume the sdk is available on the path
        # sdk_path = '~/google-cloud-sdk/platform/google_appengine'

        # If the sdk path points to a google cloud sdk installation
        # then we should alter it to point to the GAE platform location.

        # if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')):
        # sys.path.insert(0, os.path.join(sdk_path, 'platform/google_appengine'))
        # else:
        #     sys.path.insert(0, sdk_path)

        # Ensure that the google.appengine.* packages are available
        # in tests as well as all bundled third-party packages.

        def fix_sys_path():

            import dev_appserver

            dev_appserver.fix_sys_path()

            # Loading appengine_config from the current project ensures that any
            # changes to configuration there are available to all tests (e.g.
            # sys.path modifications, namespaces, etc.)
            try:
                import appengine_config

                (appengine_config)
            except ImportError:
                print "Note: unable to import appengine_config."

        fix_sys_path()

    import unittest

    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase()


@manager.command
def profile(length=25, profile_dir=None):
    """Start the application under the code profiler."""
    from werkzeug.contrib.profiler import ProfilerMiddleware

    app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[length],
                                      profile_dir=profile_dir)
    app.run()


@manager.command
def deploy():
    """Run deployment tasks."""
    from flask.ext.migrate import upgrade
    from app.models import Role, User

    # migrate database to latest revision
    upgrade()

    # create user roles
    Role.insert_roles()

    # create self-follows for all users
    User.add_self_follows()


@manager.command
def hello(name="Fred"):
    """
    Example command with named arg

    > python manage.py hello --name=Joe
    hello Joe

    or

    > python manage.py hello -n Joe
    hello Joe

    :param name: Who to say hello to
    """
    print "hello", name


class DevAppserver(Command):
    """
    Command to run the Google App Engine dev_appserver.py with defaults
    """
    option_list = (
        Option('-y', '--yaml_path', dest='yaml_path', default='.'),
        Option('-l', '--log_level', dest='log_level', default='debug'),
        Option('-a', '--allow_skipped_files', dest='allow_skipped_files', default=True),
        Option('-d', '--data_storage_path', dest='data_storage_path', default='.appengine_data'),
        Option('-smtp_host', '--smtp_host', dest='smtp_host', default='smtp.googlemail.com'),
        Option('-smtp_port', '--smtp_port', dest='smtp_port', default=587),
        Option('-smtp_user', '--smtp_user', dest='smtp_user', default=os.environ.get('MAIL_USERNAME')),
        Option('-smtp_password', '--smtp_password', dest='smtp_password', default=os.environ.get('MAIL_PASSWORD')),
        Option('-smtp_allow_tls', '--smtp_allow_tls', dest='smtp_allow_tls', default=True),
    )

    def run(self, yaml_path, log_level, allow_skipped_files, data_storage_path, smtp_host, smtp_port, smtp_user,
            smtp_password, smtp_allow_tls):
        """
{'fraction_non_alphanumeric': 0.12519496724550275, 'fraction_numerical': 0.0024955807424352707, 'mean_word_length': 2.8751007252215954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3155418', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2948, 'n_words': 728}