<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Roles in this namespace are meant to provide `Ruby on Rails <http://rubyonrails.org/>`_ applications utility methods for Debian distributions.
'''

from provy.core import Role
from provy.more.debian.package.aptitude import AptitudeRole
from provy.more.debian.package.gem import GemRole


class RailsRole(Role):
    '''
    This role provides `Ruby on Rails <http://rubyonrails.org/>`_ application utilities for Debian distributions.

    Example:
    ::

        from provy.core import Role
        from provy.more.debian import RailsRole

        class MySampleRole(Role):
            def provision(self):
                with self.using(RailsRole) as role:
                    role.ensure_site_disabled('default')
                    role.create_site(site='my-site', path='/home/myuser/my-site')
                    role.ensure_site_enabled('my-site')
    '''

    def __available_site_for(self, name):
        return '/etc/nginx/sites-available/%s' % name

    def __enabled_site_for(self, name):
        return '/etc/nginx/sites-enabled/%s' % name

    def provision(self):
        '''
        Installs `Ruby on Rails <http://rubyonrails.org/>`_ dependencies.
        This method should be called upon if overriden in base classes, or Rails won't work properly in the remote server.

        Example:
        ::

            from provy.core import Role
            from provy.more.debian import RailsRole

            class MySampleRole(Role):
                def provision(self):
                    self.provision_role(RailsRole) # does not need to be called if using with block.
        '''
        self.register_template_loader('provy.more.debian.web')

        with self.using(AptitudeRole) as role:
            for package in 'build-essential zlib1g-dev libssl-dev libpq-dev subversion libcurl4-openssl-dev libmysqlclient-dev libpcre3-dev libxslt1-dev libperl-dev libgcrypt11-dev libcrypto++-dev'.split():
                role.ensure_package_installed(package)

            role.ensure_package_installed('sqlite3')
            role.ensure_package_installed('libsqlite3-dev')

        with self.using(GemRole) as role:
            role.ensure_package_installed('bundler')
            role.ensure_package_installed('passenger')

        if not self.remote_exists_dir('/etc/nginx'):
            self.ensure_dir('/var/log/nginx', sudo=True)
            self.log("passenger-nginx not found! Installing...")
            self.execute('passenger-install-nginx-module --auto --auto-download --prefix=/etc/nginx', sudo=True, stdout=False)
            self.log("passenger-nginx installed.")

        self.update_file('rails.nginx.conf.template', '/etc/nginx/conf/nginx.conf', sudo=True)
        self.update_file('rails.nginx.init.template', '/etc/init.d/nginx', sudo=True)
        self.change_file_mode('/etc/init.d/nginx', 755)

        self.ensure_dir('/etc/nginx/sites-available', sudo=True)
        self.ensure_dir('/etc/nginx/sites-enabled', sudo=True)
        self.ensure_dir('/etc/nginx/conf.d', sudo=True)

    def cleanup(self):
        '''
        Restarts nginx if any changes have been made.

        There's no need to call this method manually.
        '''
        super(RailsRole, self).cleanup()<commit_msg>Covering RailsRole.provision, plus refactoring a bit<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Roles in this namespace are meant to provide `Ruby on Rails <http://rubyonrails.org/>`_ applications utility methods for Debian distributions.
'''

from provy.core import Role
from provy.more.debian.package.aptitude import AptitudeRole
from provy.more.debian.package.gem import GemRole


PACKAGES_TO_INSTALL = (
    'build-essential',
    'zlib1g-dev',
    'libssl-dev',
    'libpq-dev',
    'subversion',
    'libcurl4-openssl-dev',
    'libmysqlclient-dev',
    'libpcre3-dev',
    'libxslt1-dev',
    'libperl-dev',
    'libgcrypt11-dev',
    'libcrypto++-dev',
    'sqlite3',
    'libsqlite3-dev',
)


class RailsRole(Role):
    '''
    This role provides `Ruby on Rails <http://rubyonrails.org/>`_ application utilities for Debian distributions.

    Example:
    ::

        from provy.core import Role
        from provy.more.debian import RailsRole

        class MySampleRole(Role):
            def provision(self):
                with self.using(RailsRole) as role:
                    role.ensure_site_disabled('default')
                    role.create_site(site='my-site', path='/home/myuser/my-site')
                    role.ensure_site_enabled('my-site')
    '''

    def __available_site_for(self, name):
        return '/etc/nginx/sites-available/%s' % name

    def __enabled_site_for(self, name):
        return '/etc/nginx/sites-enabled/%s' % name

    def provision(self):
        '''
        Installs `Ruby on Rails <http://rubyonrails.org/>`_ dependencies.
        This method should be called upon if overriden in base classes, or Rails won't work properly in the remote server.

        Example:
        ::

            from provy.core import Role
            from provy.more.debian import RailsRole

            class MySampleRole(Role):
                def provision(self):
                    self.provision_role(RailsRole) # does not need to be called if using with block.
        '''
        self.register_template_loader('provy.more.debian.web')

        self.__install_system_packages()
        self.__install_gem_packages()
        self.__install_nginx_module()
        self.__create_nginx_configurations()
        self.__create_nginx_directories()

    def __create_nginx_directories(self):
        self.ensure_dir('/etc/nginx/sites-available', sudo=True)
        self.ensure_dir('/etc/nginx/sites-enabled', sudo=True)
        self.ensure_dir('/etc/nginx/conf.d', sudo=True)

    def __create_nginx_configurations(self):
        self.update_file('rails.nginx.conf.template', '/etc/nginx/conf/nginx.conf', sudo=True)
        self.update_file('rails.nginx.init.template', '/etc/init.d/nginx', sudo=True)
        self.change_file_mode('/etc/init.d/nginx', 755)

    def __install_gem_packages(self):
        with self.using(GemRole) as role:
            role.ensure_package_installed('bundler')
            role.ensure_package_installed('passenger')

    def __install_system_packages(self):
        with self.using(AptitudeRole) as role:
            for package in PACKAGES_TO_INSTALL:
                role.ensure_package_installed(package)

    def __install_nginx_module(self):
        if not self.remote_exists_dir('/etc/nginx'):
            self.ensure_dir('/var/log/nginx', sudo=True)
            self.log("passenger-nginx not found! Installing...")
            self.execute('passenger-install-nginx-module --auto --auto-download --prefix=/etc/nginx', sudo=True, stdout=False)
            self.log("passenger-nginx installed.")

    def cleanup(self):
        '''
        Restarts nginx if any changes have been made.

        There's no need to call this method manually.
        '''
        super(RailsRole, self).cleanup()
{'fraction_non_alphanumeric': 0.11669735164990795, 'fraction_numerical': 0.003398951989803144, 'mean_word_length': 2.5612708018154313, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3688527', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2051, 'n_words': 445}