<commit_before># -*- coding: utf-8 -*-
"""Runs the server in uwsgi or http modes

:copyright: Copyright (c) 2015 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function

import os
import subprocess
import sys

import py

from pykern.pkdebug import pkdc, pkdp
from pykern import pkcli
from pykern import pkinspect
from pykern import pkio
from pykern import pkjinja

_DEFAULT_PORT = 8000
_DEFAULT_SUBDIR = 'run'


def http(port=None, db_dir=None):
    """Starts Flask server"""
    from sirepo import server
    server.init(_db_dir(db_dir))
    server.app.run(host='0.0.0.0', port=_port(port), debug=1, threaded=True)


def uwsgi(port=None, db_dir=None, docker=False):
    """Starts UWSGI server"""
    db_dir =_db_dir(db_dir)
    values = {
        'db_dir': db_dir,
        'port': _port(port),
        'docker': docker,
    }
    # uwsgi.py must be first, because referenced by uwsgi.yml
    for f in ('uwsgi.py', 'uwsgi.yml'):
        output = db_dir.join(f)
        values[f.replace('.', '_')] = str(output)
        pkjinja.render_resource(f, values, output=output)
    cmd = ['uwsgi', '--yaml=' + values['uwsgi_yml']]
    subprocess.check_call(cmd)


def _port(value):
    """Returns port or default"""
    if not value:
        value = os.getenv('SIREPO_PKCLI_SERVICE_PORT', _DEFAULT_PORT)
    try:
        res = int(value)
        # http://stackoverflow.com/a/924337
        if res <= 5000 or res >= 32768:
            pkcli.command_error('{}: port is outside 5001 .. 32767', value)
        return res
    except ValueError:
        pkcli.command_error('{}: port is not an int', value)


def _db_dir(value):
    """Returns root package's parent or cwd with _DEFAULT_SUBDIR"""
    if not value:
        value = os.getenv('SIREPO_PKCLI_SERVICE_DB_DIR', None)
        if not value:
            fn = sys.modules[pkinspect.root_package(http)].__file__
            root = py.path.local(py.path.local(py.path.local(fn).dirname).dirname)
            # Check to see if we are in our dev directory. This is a hack,
            # but should be reliable.
            if not root.join('requirements.txt').check():
                # Don't run from an install directory
                root = py.path.local('.')
            value = root.join(_DEFAULT_SUBDIR)
    return pkio.mkdir_parent(value)<commit_msg>db_dir has to be part of app, because dynamically computed<commit_after># -*- coding: utf-8 -*-
"""Runs the server in uwsgi or http modes

:copyright: Copyright (c) 2015 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function

import os
import subprocess
import sys

import py

from pykern.pkdebug import pkdc, pkdp
from pykern import pkcli
from pykern import pkinspect
from pykern import pkio
from pykern import pkjinja

_DEFAULT_PORT = 8000
_DEFAULT_SUBDIR = 'run'


def http(port=None, db_dir=None):
    """Starts Flask server"""
    from sirepo import server
    server.init(pkdp(_db_dir(db_dir)))
    server.app.run(host='0.0.0.0', port=_port(port), debug=1, threaded=True)


def uwsgi(port=None, db_dir=None, docker=False):
    """Starts UWSGI server"""
    db_dir =_db_dir(db_dir)
    values = {
        'db_dir': db_dir,
        'port': _port(port),
        'docker': docker,
    }
    # uwsgi.py must be first, because referenced by uwsgi.yml
    for f in ('uwsgi.py', 'uwsgi.yml'):
        output = db_dir.join(f)
        values[f.replace('.', '_')] = str(output)
        pkjinja.render_resource(f, values, output=output)
    cmd = ['uwsgi', '--yaml=' + values['uwsgi_yml']]
    subprocess.check_call(cmd)


def _db_dir(value):
    """Returns root package's parent or cwd with _DEFAULT_SUBDIR"""
    if not value:
        value = os.getenv('SIREPO_PKCLI_SERVICE_DB_DIR', None)
        if not value:
            fn = sys.modules[pkinspect.root_package(http)].__file__
            root = py.path.local(py.path.local(py.path.local(fn).dirname).dirname)
            # Check to see if we are in our dev directory. This is a hack,
            # but should be reliable.
            if not root.join('requirements.txt').check():
                # Don't run from an install directory
                root = py.path.local('.')
            value = root.join(_DEFAULT_SUBDIR)
    return pkio.mkdir_parent(value)


def _port(value):
    """Returns port or default"""
    if not value:
        value = os.getenv('SIREPO_PKCLI_SERVICE_PORT', _DEFAULT_PORT)
    try:
        res = int(value)
        # http://stackoverflow.com/a/924337
        if res <= 5000 or res >= 32768:
            pkcli.command_error('{}: port is outside 5001 .. 32767', value)
        return res
    except ValueError:
        pkcli.command_error('{}: port is not an int', value)
{'fraction_non_alphanumeric': 0.12322567372968525, 'fraction_numerical': 0.016457519029006377, 'mean_word_length': 2.864864864864865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '7530356', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1604, 'n_words': 434}