<commit_before># -*- coding: utf-8 -*-
from __future__ import with_statement
import os
import time

from fabric import api as fab

from fab_deploy.utils import run_as

from base import _, Daemon, Ubuntu
from deployment import command
from utils import upload_template, upload_first


class FrontEnd(Daemon):

    config_dir = None

    namespace = 'server'

    backend = None

    def __init__(self, name, domain):
        super(FrontEnd, self).__init__(name)

        self.env = fab.env
        self.env['domain'] = domain
        self.env['server_dir'] = self.config_dir

    def enable_site(self, name):
        server_dir = self.config_dir
        fab.sudo("ln -s %(server_dir)ssites-available/%(name)s"
                 " %(server_dir)ssites-enabled/%(name)s" % locals() )

    @command
    def enable(self):
        fab.sudo(_("ln -s %(server_dir)ssites-available/%(domain)s.conf"
                  " %(server_dir)ssites-enabled/%(domain)s.conf"))

    def disable_site(self, name):
        server_dir = self.config_dir
        fab.sudo("rm -f %(server_dir)ssites-enabled/%(name)s" % locals() )

    @command
    def disable(self):
        fab.sudo(_("rm -f %(server_dir)ssites-enabled/%(domain)s.conf"))

    def remove_site(self, name):
        self.disable_site(name)
        server_dir = self.config_dir
        fab.sudo("rm -f %(server_dir)ssites-available/%(name)s" % locals() )

    @command
    def remove(self):
        self.disable()
        fab.sudo(_("rm -f %(server_dir)ssites-available/%(domain)s.conf"))

    def install_development_libraries(self):
        if self.backend:
            self.backend.install_development_libraries()

    def install_requirements(self):
        if self.backend:
            self.backend.install_requirements()

    def dirs(self):
        dirs = []
        if self.backend:
            dirs.extend(self.backend.dirs())
        return dirs

    @command
    def start(self, pty=True):
        super(FrontEnd, self).start(pty=pty)

    @command
    def stop(self, pty=True):
        super(FrontEnd, self).stop(pty=pty)

    @command
    def restart(self, pty=True):
        super(FrontEnd, self).restart(pty=pty)

    @command
    def reload(self, pty=True):
        super(FrontEnd, self).reload(pty=pty)

    @command
    def configure(self):
        if self.backend:
            self.backend.configure()

WebServer = FrontEnd
Server = WebServer # TODO remove this


class Backend(object): #TODO inherit Server
    name = NotImplemented

    namespace = 'backend'

    def __init__(self, domain=None):
        if domain is not None:
            fab.env['domain'] = domain

        if 'project' in fab.env and fab.env.project.use_django:
            project_path = _('%(django_python_path)s')
        else:
            project_path = _('%(remote_dir)s/%(project_name)s')
        fab.env['project_path'] = project_path

    def start(self):
        pass

    def stop(self):
        pass


class FcgiBackend(Backend):
    name = 'fcgi'

    def install_requirements(self):
        with fab.cd(_('%(remote_dir)s/')):
            fab.run(_('%(virtualenv)s/bin/pip install "flup==1.0.2"'))

    def stop(self):
        with fab.settings(warn_only=True):
            fab.run(_("kill -TERM `cat %(remote_dir)s/run/fcgi.pid`"))

    def start(self):
        with fab.cd(_('%(remote_dir)s/%(project_name)s')):
            # TODO use' socket=%(remote_dir)s/run/fcgi.sock'
            fab.run(_('../%(virtualenv)s/bin/python manage.py runfcgi'
                      ' host=127.0.0.1 port=8080',
                      ' daemonize=true'
                      ' minspare=1'
                      ' maxspare=%(worker_count)s'
                      ' maxchildren=%(worker_count)s'
                      ' maxrequests=10000'
                      ' method=prefork'
                      ' pidfile=%(remote_dir)s/run/fcgi.pid'
                      ' logfile=%(remote_dir)s/log/fcgi.log'))

    def reload(self):
        fab.run(_('touch %(remote_dir)s/http/wrapper.fcgi'))


class FcgiWrapper(FcgiBackend):

    fcgi_path = '%(remote_dir)s/http/'

    def dirs(self):
        return ['http']

    def configure(self):
        upload_first([_('nginx/%(domain)s-sh.fcgi'),
                      'fcgi/wrapper-sh.fcgi'],
                     _(self.fcgi_path) + 'wrapper.fcgi',
                     fab.env,
                     mode=0755,
                     use_jinja=True)

        upload_first([_('nginx/%(domain)s.fcgi'),
                      'fcgi/wrapper.fcgi'],
                     _('%(remote_dir)s/http/wrapper.fcgi'),
                     fab.env,
                     mode=0755,
                     use_jinja=True)

        upload_first([_('nginx/%(domain)s.htaccess'),
                      'fcgi/.htaccess'],
                     _('%(remote_dir)s/http/.htaccess'),
                     fab.env,
                     use_jinja=True)

        #XXX
        with fab.cd(_('%(remote_dir)s/http')):
            with fab.settings(warn_only=True):
                fab.run('ln -s ../media/static')
                fab.run('ln -s ../media/media')

    def stop(self):
        with fab.settings(warn_only=True):
            fab.run(_("killall -r wrapper.fcgi"))


class Gunicorn(Backend):
    name = 'gunicorn'

    namespace = 'backend'

    supervisor = False

    def dirs(self):
        return ['etc/gunicorn']

    def install_requirements(self):
        with fab.cd(_('%(remote_dir)s/')):
            fab.run(_('%(virtualenv)s/bin/pip install -U gunicorn'))
            fab.run(_('%(virtualenv)s/bin/pip install -U setproctitle'))

    def stop(self):
        with fab.settings(warn_only=True):
            fab.run(_("kill -TERM `cat %(remote_dir)s/run/gunicorn.pid`"))

    def start_command(self):
        if fab.env.project.use_django:
            if fab.env.project.django.HAS_WSGI:
                fab.env['gunicorn_starter'] = _('gunicorn '
                                                '%(django_project_name)s.wsgi:application')
            else:
                fab.env['gunicorn_starter'] = 'gunicorn_django'
        else:
            fab.env['gunicorn_starter'] = _('gunicorn '
                                            '%(project_name)s:application')<commit_msg>try to stop fgci process, because touch of fcgi file didnt work<commit_after># -*- coding: utf-8 -*-
from __future__ import with_statement
import os
import time

from fabric import api as fab

from fab_deploy.utils import run_as

from base import _, Daemon, Ubuntu
from deployment import command
from utils import upload_template, upload_first


class FrontEnd(Daemon):

    config_dir = None

    namespace = 'server'

    backend = None

    def __init__(self, name, domain):
        super(FrontEnd, self).__init__(name)

        self.env = fab.env
        self.env['domain'] = domain
        self.env['server_dir'] = self.config_dir

    def enable_site(self, name):
        server_dir = self.config_dir
        fab.sudo("ln -s %(server_dir)ssites-available/%(name)s"
                 " %(server_dir)ssites-enabled/%(name)s" % locals() )

    @command
    def enable(self):
        fab.sudo(_("ln -s %(server_dir)ssites-available/%(domain)s.conf"
                  " %(server_dir)ssites-enabled/%(domain)s.conf"))

    def disable_site(self, name):
        server_dir = self.config_dir
        fab.sudo("rm -f %(server_dir)ssites-enabled/%(name)s" % locals() )

    @command
    def disable(self):
        fab.sudo(_("rm -f %(server_dir)ssites-enabled/%(domain)s.conf"))

    def remove_site(self, name):
        self.disable_site(name)
        server_dir = self.config_dir
        fab.sudo("rm -f %(server_dir)ssites-available/%(name)s" % locals() )

    @command
    def remove(self):
        self.disable()
        fab.sudo(_("rm -f %(server_dir)ssites-available/%(domain)s.conf"))

    def install_development_libraries(self):
        if self.backend:
            self.backend.install_development_libraries()

    def install_requirements(self):
        if self.backend:
            self.backend.install_requirements()

    def dirs(self):
        dirs = []
        if self.backend:
            dirs.extend(self.backend.dirs())
        return dirs

    @command
    def start(self, pty=True):
        super(FrontEnd, self).start(pty=pty)

    @command
    def stop(self, pty=True):
        super(FrontEnd, self).stop(pty=pty)

    @command
    def restart(self, pty=True):
        super(FrontEnd, self).restart(pty=pty)

    @command
    def reload(self, pty=True):
        super(FrontEnd, self).reload(pty=pty)

    @command
    def configure(self):
        if self.backend:
            self.backend.configure()

WebServer = FrontEnd
Server = WebServer # TODO remove this


class Backend(object): #TODO inherit Server
    name = NotImplemented

    namespace = 'backend'

    def __init__(self, domain=None):
        if domain is not None:
            fab.env['domain'] = domain

        if 'project' in fab.env and fab.env.project.use_django:
            project_path = _('%(django_python_path)s')
        else:
            project_path = _('%(remote_dir)s/%(project_name)s')
        fab.env['project_path'] = project_path

    def start(self):
        pass

    def stop(self):
        pass


class FcgiBackend(Backend):
    name = 'fcgi'

    def install_requirements(self):
        with fab.cd(_('%(remote_dir)s/')):
            fab.run(_('%(virtualenv)s/bin/pip install "flup==1.0.2"'))

    def stop(self):
        with fab.settings(warn_only=True):
            fab.run(_("kill -TERM `cat %(remote_dir)s/run/fcgi.pid`"))

    def start(self):
        with fab.cd(_('%(remote_dir)s/%(project_name)s')):
            # TODO use' socket=%(remote_dir)s/run/fcgi.sock'
            fab.run(_('../%(virtualenv)s/bin/python manage.py runfcgi'
                      ' host=127.0.0.1 port=8080',
                      ' daemonize=true'
                      ' minspare=1'
                      ' maxspare=%(worker_count)s'
                      ' maxchildren=%(worker_count)s'
                      ' maxrequests=10000'
                      ' method=prefork'
                      ' pidfile=%(remote_dir)s/run/fcgi.pid'
                      ' logfile=%(remote_dir)s/log/fcgi.log'))

    def reload(self):
        fab.run(_('touch %(remote_dir)s/http/wrapper.fcgi'))


class FcgiWrapper(FcgiBackend):

    fcgi_path = '%(remote_dir)s/http/'
    use_project_media = False

    def dirs(self):
        return ['http']

    def configure(self):
        upload_first([_('nginx/%(domain)s-sh.fcgi'),
                      'fcgi/wrapper-sh.fcgi'],
                     _(self.fcgi_path) + 'wrapper.fcgi',
                     fab.env,
                     mode=0755,
                     use_jinja=True)

        upload_first([_('nginx/%(domain)s.fcgi'),
                      'fcgi/wrapper.fcgi'],
                     _('%(remote_dir)s/http/wrapper.fcgi'),
                     fab.env,
                     mode=0755,
                     use_jinja=True)

        upload_first([_('nginx/%(domain)s.htaccess'),
                      'fcgi/.htaccess'],
                     _('%(remote_dir)s/http/.htaccess'),
                     fab.env,
                     use_jinja=True)

        if self.use_project_media:
            with fab.cd(_('%(remote_dir)s/http')):
                with fab.settings(warn_only=True):
                    fab.run('ln -s ../media/static')
                    fab.run('ln -s ../media/media')

    def stop(self):
        with fab.settings(warn_only=True):
            fab.run(_("kill -HUP `cat %(remote_dir)s/run/fcgi.pid`"))

    def reload(self):
        self.stop()


class Gunicorn(Backend):
    name = 'gunicorn'

    namespace = 'backend'

    supervisor = False

    def dirs(self):
        return ['etc/gunicorn']

    def install_requirements(self):
        with fab.cd(_('%(remote_dir)s/')):
            fab.run(_('%(virtualenv)s/bin/pip install -U gunicorn'))
            fab.run(_('%(virtualenv)s/bin/pip install -U setproctitle'))

    def stop(self):
        with fab.settings(warn_only=True):
            fab.run(_("kill -TERM `cat %(remote_dir)s/run/gunicorn.pid`"))

    def start_command(self):
        if fab.env.project.use_django:
            if fab.env.project.django.HAS_WSGI:
                fab.env['gunicorn_starter'] = _('gunicorn '
                                                '%(django_project_name)s.wsgi:application')
            else:
                fab.env['gunicorn_starter'] = 'gunicorn_django'
        else:
            fab.env['gunicorn_starter'] = _('gunicorn '
                                            '%(project_name)s:application')
{'fraction_non_alphanumeric': 0.13696461087799858, 'fraction_numerical': 0.004433536537091283, 'mean_word_length': 1.9631714754867464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749006', 'n_tokens_mistral': 4146, 'n_tokens_neox': 3885, 'n_words': 685}