<commit_before>from fabric.api import run, sudo
from fabric.api import prefix, warn, abort
from fabric.api import settings, task, env, shell_env
from fabric.context_managers import cd
from fabric.contrib.files import exists

from datetime import datetime
import json
import os

import dotenv

basedir = os.path.dirname(os.path.abspath(__file__))
dotenv.read_dotenv(os.path.join(basedir, "environment"))


env.hosts = ["web2.openprescribing.net"]
env.forward_agent = True
env.colorize_errors = True
env.user = "hello"

environments = {"production": "openprescribing", "staging": "openprescribing_staging"}


def sudo_script(script, www_user=False):
    """Run script under `deploy/fab_scripts/` as sudo.

    We don't use the `fabric` `sudo()` command, because instead we
    expect the user that is running fabric to have passwordless sudo
    access.  In this configuration, that is achieved by the user being
    a member of the `fabric` group (see `setup_sudo()`, below).

    """
    if www_user:
        sudo_cmd = "sudo -u www-data "
    else:
        sudo_cmd = "sudo "
    return run(sudo_cmd + os.path.join(env.path, "deploy/fab_scripts/%s" % script))


def setup_sudo():
    """Ensures members of `fabric` group can execute deployment scripts as
    root without passwords

    """
    sudoer_file_test = "/tmp/openprescribing_fabric_{}".format(env.app)
    sudoer_file_real = "/etc/sudoers.d/openprescribing_fabric_{}".format(env.app)
    # Raise an exception if not set up
    check_setup = run(
        "/usr/bin/sudo -n {}/deploy/fab_scripts/test.sh".format(env.path),
        warn_only=True,
    )
    if check_setup.failed:
        # Test the format of the file, to prevent locked-out-disasters
        run(
            'echo "%fabric ALL = (root) '
            'NOPASSWD: {}/deploy/fab_scripts/" > {}'.format(env.path, sudoer_file_test)
        )
        run("/usr/sbin/visudo -cf {}".format(sudoer_file_test))
        # Copy it to the right place
        sudo("cp {} {}".format(sudoer_file_test, sudoer_file_real))


def git_init():
    run(
        "git init . && "
        "git remote add origin "
        "https://github.com/ebmdatalab/openprescribing.git && "
        "git fetch origin && "
        "git branch --set-upstream master origin/master"
    )


def venv_init():
    run("virtualenv .venv")


def git_pull():
    run("git fetch --all")
    run("git checkout --force origin/%s" % env.branch)


def pip_install():
    if "requirements.txt" in env.changed_files:
        with prefix("source .venv/bin/activate"):
            run("pip install -r requirements.txt")


def npm_install():
    installed = run("if [[ -n $(which npm) ]]; then echo 1; fi")
    if not installed:
        sudo(
            "curl -sL https://deb.nodesource.com/setup_6.x |"
            "bash - && apt-get install -y  "
            "nodejs binutils libproj-dev gdal-bin libgeoip1 libgeos-c1;",
            user=env.local_user,
        )
        sudo("npm install -g browserify && npm install -g eslint", user=env.local_user)


def npm_install_deps(force=False):
    if force or "openprescribing/media/js/package.json" in env.changed_files:
        run("cd openprescribing/media/js && npm install")


def npm_build_js():
    run("cd openprescribing/media/js && npm run build")


def npm_build_css(force=False):
    if force or any(
        lambda x: x.startswith("openprescribing/media/css"),
        [x for x in env.changed_files],
    ):
        run("cd openprescribing/media/js && npm run build-css")<commit_msg>Fix fabfile<commit_after>from fabric.api import run, sudo
from fabric.api import prefix, warn, abort
from fabric.api import settings, task, env, shell_env
from fabric.context_managers import cd
from fabric.contrib.files import exists

from datetime import datetime
import json
import os

import dotenv

basedir = os.path.dirname(os.path.abspath(__file__))
dotenv.read_dotenv(os.path.join(basedir, "environment"))


env.hosts = ["web2.openprescribing.net"]
env.forward_agent = True
env.colorize_errors = True
env.user = "hello"

environments = {"production": "openprescribing", "staging": "openprescribing_staging"}


def sudo_script(script, www_user=False):
    """Run script under `deploy/fab_scripts/` as sudo.

    We don't use the `fabric` `sudo()` command, because instead we
    expect the user that is running fabric to have passwordless sudo
    access.  In this configuration, that is achieved by the user being
    a member of the `fabric` group (see `setup_sudo()`, below).

    """
    if www_user:
        sudo_cmd = "sudo -u www-data "
    else:
        sudo_cmd = "sudo "
    return run(sudo_cmd + os.path.join(env.path, "deploy/fab_scripts/%s" % script))


def setup_sudo():
    """Ensures members of `fabric` group can execute deployment scripts as
    root without passwords

    """
    sudoer_file_test = "/tmp/openprescribing_fabric_{}".format(env.app)
    sudoer_file_real = "/etc/sudoers.d/openprescribing_fabric_{}".format(env.app)
    # Raise an exception if not set up
    check_setup = run(
        "/usr/bin/sudo -n {}/deploy/fab_scripts/test.sh".format(env.path),
        warn_only=True,
    )
    if check_setup.failed:
        # Test the format of the file, to prevent locked-out-disasters
        run(
            'echo "%fabric ALL = (root) '
            'NOPASSWD: {}/deploy/fab_scripts/" > {}'.format(env.path, sudoer_file_test)
        )
        run("/usr/sbin/visudo -cf {}".format(sudoer_file_test))
        # Copy it to the right place
        sudo("cp {} {}".format(sudoer_file_test, sudoer_file_real))


def git_init():
    run(
        "git init . && "
        "git remote add origin "
        "https://github.com/ebmdatalab/openprescribing.git && "
        "git fetch origin && "
        "git branch --set-upstream master origin/master"
    )


def venv_init():
    run("virtualenv .venv")


def git_pull():
    run("git fetch --all")
    run("git checkout --force origin/%s" % env.branch)


def pip_install():
    if "requirements.txt" in env.changed_files:
        with prefix("source .venv/bin/activate"):
            run("pip install -r requirements.txt")


def npm_install():
    installed = run("if [[ -n $(which npm) ]]; then echo 1; fi")
    if not installed:
        sudo(
            "curl -sL https://deb.nodesource.com/setup_6.x |"
            "bash - && apt-get install -y  "
            "nodejs binutils libproj-dev gdal-bin libgeoip1 libgeos-c1;",
            user=env.local_user,
        )
        sudo("npm install -g browserify && npm install -g eslint", user=env.local_user)


def npm_install_deps(force=False):
    if force or "openprescribing/media/js/package.json" in env.changed_files:
        run("cd openprescribing/media/js && npm install")


def npm_build_js():
    run("cd openprescribing/media/js && npm run build")


def npm_build_css(force=False):
    changed_css_files = [
        x for x in env.changed_files if x.startswith("openprescribing/media/css")
    ]
    if force or changed_css_files:
        run("cd openprescribing/media/js && npm run build-css")
{'fraction_non_alphanumeric': 0.1251063829787234, 'fraction_numerical': 0.0014184397163120568, 'mean_word_length': 2.947928331466965, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, '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': '2670371', 'n_tokens_mistral': 2404, 'n_tokens_neox': 2227, 'n_words': 604}