<commit_before># -*- coding: utf-8 -*-
# (c) 2013-2018 <NAME>, Elmyra UG
import os
from distutils.core import run_setup
from fabric.contrib.project import rsync_project
from fabric.decorators import task, hosts
from fabric.colors import yellow, red
from fabric.utils import abort
from fabric.state import env
from cuisine import run, file_exists, file_is_dir, file_is_file, file_upload, dir_ensure
from pip._internal.utils.misc import ask

env.colorize_errors = True
env.confirm = False

INSTALLATION_HOST = os.environ['PATZILLA_HOST']
INSTALLATION_PATH = '/opt/patzilla'

# TODO: Task for uploading package file
# TODO: Task for uploading configuration file

@task
@hosts(INSTALLATION_HOST)
def install(version, target):

    if not target:
        abort('Task argument "target" is required.')

    current_dir = os.path.dirname(__file__)
    project_path = current_dir
    pkg_version = setuptools_get_version(project_path)
    pkg_name = setuptools_get_name(project_path)

    if not version:
        version = pkg_version

    print 'Installing package {0}, version {1} to target {2}.'.format(*map(yellow, [pkg_name, version, target]))
    if env.confirm:
        response = ask('Proceed (y/n)? ', ('y', 'n'))
    else:
        response = 'y'

    if response == 'y':

        # From filesystem
        #source_package = '~/install/PatZilla/PatZilla-{version}.tar.gz'.format(version=version)

        # From PyPI
        source_package = 'patzilla=={version}'.format(version=version)
        source_config = './patzilla/config/production.ini.tpl'

        target_path = os.path.join(INSTALLATION_PATH, 'sites', target)
        dir_ensure(target_path, recursive=True)

        venv_path = target_path + '/.venv27'

        #if not file_is_file(source_package):
        #    abort('Source package does not exist: ' + source_package)

        if not file_is_dir(target_path):
            abort('Target path does not exist: ' + target_path)

        if not file_is_dir(venv_path):
            run('virtualenv --no-site-packages "{0}"'.format(venv_path))
            #setup_package('which', venv_path)
            # TODO: put these packages to a more convenient location

        setup_package(source_package, venv_path)

        upload_config(source_config, target_path)

        restart_service(target)

    else:
        print yellow('Skipped package install due to user request.')

def setup_package(package, virtualenv, options=''):
    #--index-url=http://c.pypi.python.org/simple
    pip_cmd_template = """<commit_msg>Update virtualenv path again<commit_after># -*- coding: utf-8 -*-
# (c) 2013-2018 <NAME>, Elmyra UG
import os
from distutils.core import run_setup
from fabric.contrib.project import rsync_project
from fabric.decorators import task, hosts
from fabric.colors import yellow, red
from fabric.utils import abort
from fabric.state import env
from cuisine import run, file_exists, file_is_dir, file_is_file, file_upload, dir_ensure
from pip._internal.utils.misc import ask

env.colorize_errors = True
env.confirm = False

INSTALLATION_HOST = os.environ['PATZILLA_HOST']
INSTALLATION_PATH = '/opt/patzilla'

# TODO: Task for uploading package file
# TODO: Task for uploading configuration file

@task
@hosts(INSTALLATION_HOST)
def install(version, target):

    if not target:
        abort('Task argument "target" is required.')

    current_dir = os.path.dirname(__file__)
    project_path = current_dir
    pkg_version = setuptools_get_version(project_path)
    pkg_name = setuptools_get_name(project_path)

    if not version:
        version = pkg_version

    print 'Installing package {0}, version {1} to target {2}.'.format(*map(yellow, [pkg_name, version, target]))
    if env.confirm:
        response = ask('Proceed (y/n)? ', ('y', 'n'))
    else:
        response = 'y'

    if response == 'y':

        # From filesystem
        #source_package = '~/install/PatZilla/PatZilla-{version}.tar.gz'.format(version=version)

        # From PyPI
        source_package = 'patzilla=={version}'.format(version=version)
        source_config = './patzilla/config/production.ini.tpl'

        target_path = os.path.join(INSTALLATION_PATH, 'sites', target)
        dir_ensure(target_path, recursive=True)

        venv_path = target_path + '/.venv2'

        #if not file_is_file(source_package):
        #    abort('Source package does not exist: ' + source_package)

        if not file_is_dir(target_path):
            abort('Target path does not exist: ' + target_path)

        if not file_is_dir(venv_path):
            run('virtualenv --no-site-packages "{0}"'.format(venv_path))
            #setup_package('which', venv_path)
            # TODO: put these packages to a more convenient location

        setup_package(source_package, venv_path)

        upload_config(source_config, target_path)

        restart_service(target)

    else:
        print yellow('Skipped package install due to user request.')

def setup_package(package, virtualenv, options=''):
    #--index-url=http://c.pypi.python.org/simple
    pip_cmd_template = """
{'fraction_non_alphanumeric': 0.10904780719083366, 'fraction_numerical': 0.005728960885025681, 'mean_word_length': 3.4102787456445993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '162804', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1518, 'n_words': 391}