<commit_before># Copyright (C) 2014 Universidad Politecnica de Madrid
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import string
import os

from fabric.api import task
from conf import settings
from fabric.context_managers import lcd
from fabric.operations import local as lrun
from fabric.colors import red, green

@task
def install(horizon_path=settings.HORIZON_ROOT):
    """Download and install the Front-end and its dependencies."""
    if os.path.isdir(horizon_path[:-1]):
        print 'Already downloaded.'
    else:
        lrun('git clone https://github.com/ging/horizon.git \
            {0}'.format(horizon_path))

    with lcd(horizon_path):
        dependencies = ' '.join(settings.UBUNTU_DEPENDENCIES['horizon'])
        lrun('sudo apt-get install {0}'.format(dependencies))
        lrun('sudo python tools/install_venv.py')

    path = horizon_path + '/openstack_dashboard/local/'
    class Template(string.Template):
        delimiter = '$$'
    template_settings = Template(open(path + 'local_settings.py.example').read())
    out_file = open(path + "local_settings.py", "w")
    out_file.write(
        template_settings.substitute({
            'IDM_NAME': settings.IDM_USER_CREDENTIALS['username'],
            'IDM_PASS': settings.IDM_USER_CREDENTIALS['password'],
            'IDM_PROJECT': settings.IDM_USER_CREDENTIALS['project'],
            'KEYSTONE_ADDRESS': settings.KEYSTONE_INTERNAL_ADDRESS,
            'KEYSTONE_PUBLIC_PORT':settings.KEYSTONE_PUBLIC_PORT,
        }))
    out_file.close()

@task
def update(horizon_path=settings.HORIZON_ROOT):
    """Update the Front-end and its dependencies."""
    # returns 1 if everything went OK, 0 otherwise

    print 'Updating Horizon...'
    with lcd(horizon_path):
        lrun('git pull origin')
        lrun('sudo python tools/install_venv.py')
    print green('Horizon updated.')
    if not check(horizon_path):
        return 0 # flag for the main task
    else:
        return 1 # flag for the main task

@task
def check(horizon_path=settings.HORIZON_ROOT):
    """Checks for new settings in the template which don't exist in the current file"""
    # returns 1 if everything went OK, 0 otherwise

    print 'Checking Horizon...',
    path = horizon_path + 'openstack_dashboard/local/'
    with open(path+'local_settings.py','r') as old_file, open(path+'local_settings.py.example','r') as new_file:
        old = set(old_file)
        new = set(new_file)

    new_settings = set()
    old_settings = set()

    # remove values to have settings' names
    for s in new.difference(old):
        if s.find('=') != -1:
            new_settings.add(s[0:s.find('=')])
    for s in old.difference(new):
        if s.find('=') != -1:
            old_settings.add(s[0:s.find('=')])

    latest_settings = new_settings.difference(old_settings)

    if not latest_settings:
        print (green('Everything OK'))
        return 1 # flag for the main task
    else:
        print red('Some errors were encountered:')
        print red('The following settings couldn\'t be found in your local_settings.py module:')
        for s in latest_settings:
            print '\t'+red(s)
        autofix = raw_input(red('Would you like to add defaults for the missing settings? [Y/n]: '))

        if autofix == 'Y':

            settings_to_write = set()
            for s in latest_settings:
                for x in new.difference(old):
                    if x.find(s) != -1:
                        settings_to_write.add(x) # save name and value

            with open(path+'local_settings.py','a') as output:
                output.write('\n\n# --- NEW SETTINGS ADDED AUTOMATICALLY ---\n')
                for s in settings_to_write:
                    output.write(s)

            print green('The missing settings were added.\nPlease check the local_settings.py module to make any necessary changes.')

        else:
            print red('Please edit the local_settings.py module manually so that it contains the settings above.')
            return 0 # flag for the main task

@task
def dev_server(address=settings.HORIZON_DEV_ADDRESS,
               horizon_path=settings.HORIZON_ROOT):
    """Run horizon server for development purposes"""
    with lcd(horizon_path):<commit_msg>Auto fix settings with more than one line<commit_after># Copyright (C) 2014 Universidad Politecnica de Madrid
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import string
import os

from fabric.api import task
from conf import settings
from fabric.context_managers import lcd
from fabric.operations import local as lrun
from fabric.colors import red, green

@task
def install(horizon_path=settings.HORIZON_ROOT):
    """Download and install the Front-end and its dependencies."""
    if os.path.isdir(horizon_path[:-1]):
        print 'Already downloaded.'
    else:
        lrun('git clone https://github.com/ging/horizon.git \
            {0}'.format(horizon_path))

    with lcd(horizon_path):
        dependencies = ' '.join(settings.UBUNTU_DEPENDENCIES['horizon'])
        lrun('sudo apt-get install {0}'.format(dependencies))
        lrun('sudo python tools/install_venv.py')

    path = horizon_path + '/openstack_dashboard/local/'
    class Template(string.Template):
        delimiter = '$$'
    template_settings = Template(open(path + 'local_settings.py.example').read())
    out_file = open(path + "local_settings.py", "w")
    out_file.write(
        template_settings.substitute({
            'IDM_NAME': settings.IDM_USER_CREDENTIALS['username'],
            'IDM_PASS': settings.IDM_USER_CREDENTIALS['password'],
            'IDM_PROJECT': settings.IDM_USER_CREDENTIALS['project'],
            'KEYSTONE_ADDRESS': settings.KEYSTONE_INTERNAL_ADDRESS,
            'KEYSTONE_PUBLIC_PORT':settings.KEYSTONE_PUBLIC_PORT,
        }))
    out_file.close()

@task
def update(horizon_path=settings.HORIZON_ROOT):
    """Update the Front-end and its dependencies."""
    # returns 1 if everything went OK, 0 otherwise

    print 'Updating Horizon...'
    with lcd(horizon_path):
        lrun('git pull origin')
        lrun('sudo python tools/install_venv.py')
    print green('Horizon updated.')
    if not check(horizon_path):
        return 0 # flag for the main task
    else:
        return 1 # flag for the main task

@task
def check(horizon_path=settings.HORIZON_ROOT):
    """Checks for new settings in the template which don't exist in the current file"""
    # returns 1 if everything went OK, 0 otherwise

    print 'Checking Horizon...',
    path = horizon_path + 'openstack_dashboard/local/'
    with open(path+'local_settings.py','r') as old_file, open(path+'local_settings.py.example','r') as new_file:
        old = set(old_file)
        new = set(new_file)

    new_settings = set()
    old_settings = set()

    # remove values to have settings' names
    for s in new.difference(old):
        if s.find('=') != -1:
            new_settings.add(s[0:s.find('=')])
    for s in old.difference(new):
        if s.find('=') != -1:
            old_settings.add(s[0:s.find('=')])

    latest_settings = new_settings.difference(old_settings)

    if not latest_settings:
        print (green('Everything OK'))
        return 1 # flag for the main task
    else:
        print red('Some errors were encountered:')
        print red('The following settings couldn\'t be found in your local_settings.py module:')
        for s in latest_settings:
            print '\t'+red(s)
        autofix = raw_input(red('Would you like to add defaults for the missing settings? [Y/n]: '))

        if autofix == 'Y':

            settings_to_write = list()
            for s in latest_settings:
                with open(path+'local_settings.py.example','r') as template:
                    block = 0
                    for line in template.readlines():
                        if line.find(s) != -1 or block>0:
                            settings_to_write.append(line)
                            if line.find('{') != -1:
                                block=block+1
                            if line.find('}') != -1:
                                block=block-1

            with open(path+'local_settings.py','a') as output:
                output.write('\n\n# --- NEW SETTINGS ADDED AUTOMATICALLY ---\n')
                for s in settings_to_write:
                    output.write(s)

            print green('The missing settings were added.\nPlease check the local_settings.py module to make any necessary changes.')

        else:
            print red('Please edit the local_settings.py module manually so that it contains the settings above.')
            return 0 # flag for the main task

@task
def dev_server(address=settings.HORIZON_DEV_ADDRESS,
               horizon_path=settings.HORIZON_ROOT):
    """Run horizon server for development purposes"""
    with lcd(horizon_path):
{'fraction_non_alphanumeric': 0.10486815415821502, 'fraction_numerical': 0.005476673427991886, 'mean_word_length': 2.7141242937853107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5274520', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2802, 'n_words': 862}