예제 #1
0
def get_manage_path(args):
    """
    Validate user defined manage path, use default, or throw error
    """
    # Determine path to manage.py file
    manage_path = os.path.join(get_tethys_src_dir(), 'manage.py')

    # Check for path option
    if hasattr(args, 'manage'):
        manage_path = args.manage or manage_path

    # Throw error if path is not valid
    if not os.path.isfile(manage_path):
        with pretty_output(FG_RED) as p:
            p.write('ERROR: Can\'t open file "{0}", no such file.'.format(manage_path))
        exit(1)

    return manage_path
예제 #2
0
def get_manage_path(args):
    """
    Validate user defined manage path, use default, or throw error
    """
    # Determine path to manage.py file
    manage_path = os.path.join(get_tethys_src_dir(), 'manage.py')

    # Check for path option
    if hasattr(args, 'manage'):
        manage_path = args.manage or manage_path

    # Throw error if path is not valid
    if not os.path.isfile(manage_path):
        with pretty_output(FG_RED) as p:
            p.write('ERROR: Can\'t open file "{0}", no such file.'.format(
                manage_path))
        exit(1)

    return manage_path
예제 #3
0
def generate_command(args):
    """
    Generate a settings file for a new installation.
    """
    # Consts
    TETHYS_HOME = get_tethys_home_dir()
    TETHYS_SRC = get_tethys_src_dir()

    # Setup variables
    context = Context()

    # Determine template path

    gen_templates_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'gen_templates')
    template_path = os.path.join(gen_templates_dir, args.type)

    # Parse template
    template = Template(open(template_path).read())

    # Determine destination file name (defaults to type)
    destination_file = FILE_NAMES[args.type]

    # Default destination path is the current working directory
    destination_dir = os.path.join(TETHYS_SRC, 'tethys_portal')

    nginx_user = ''
    nginx_conf_path = '/etc/nginx/nginx.conf'
    if os.path.exists(nginx_conf_path):
        with open(nginx_conf_path, 'r') as nginx_conf:
            for line in nginx_conf.readlines():
                tokens = line.split()
                if len(tokens) > 0 and tokens[0] == 'user':
                    nginx_user = tokens[1].strip(';')
                    break

    # Settings file setup
    if args.type == GEN_SETTINGS_OPTION:
        # Generate context variables
        secret_key = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(50)])
        context.update({'secret_key': secret_key,
                        'allowed_host': args.allowed_host,
                        'allowed_hosts': args.allowed_hosts,
                        'db_username': args.db_username,
                        'db_password': args.db_password,
                        'db_port': args.db_port,
                        'tethys_home': TETHYS_HOME,
                        'production': args.production,
                        })

    if args.type == GEN_NGINX_OPTION:
        hostname = str(settings.ALLOWED_HOSTS[0]) if len(settings.ALLOWED_HOSTS) > 0 else '127.0.0.1'
        workspaces_root = get_settings_value('TETHYS_WORKSPACES_ROOT')
        static_root = get_settings_value('STATIC_ROOT')

        context.update({'hostname': hostname,
                        'workspaces_root': workspaces_root,
                        'static_root': static_root,
                        'client_max_body_size': args.client_max_body_size
                        })

    if args.type == GEN_UWSGI_SERVICE_OPTION:
        conda_home = get_environment_value('CONDA_HOME')
        conda_env_name = get_environment_value('CONDA_ENV_NAME')

        user_option_prefix = ''

        try:
            linux_distro = linux_distribution(full_distribution_name=0)[0]
            if linux_distro in ['redhat', 'centos']:
                user_option_prefix = 'http-'
        except Exception:
            pass

        context.update({'nginx_user': nginx_user,
                        'conda_home': conda_home,
                        'conda_env_name': conda_env_name,
                        'tethys_src': TETHYS_SRC,
                        'user_option_prefix': user_option_prefix
                        })

    if args.type == GEN_UWSGI_SETTINGS_OPTION:
        conda_home = get_environment_value('CONDA_HOME')
        conda_env_name = get_environment_value('CONDA_ENV_NAME')

        context.update({'conda_home': conda_home,
                        'conda_env_name': conda_env_name,
                        'uwsgi_processes': args.uwsgi_processes})

    if args.directory:
        if os.path.isdir(args.directory):
            destination_dir = args.directory
        else:
            print('ERROR: "{0}" is not a valid directory.'.format(destination_dir))
            exit(1)

    destination_path = os.path.join(destination_dir, destination_file)

    # Check for pre-existing file
    if os.path.isfile(destination_path):
        valid_inputs = ('y', 'n', 'yes', 'no')
        no_inputs = ('n', 'no')

        if args.overwrite:
            overwrite_input = 'yes'
        else:
            overwrite_input = input('WARNING: "{0}" already exists. '
                                    'Overwrite? (y/n): '.format(destination_file)).lower()

            while overwrite_input not in valid_inputs:
                overwrite_input = input('Invalid option. Overwrite? (y/n): ').lower()

        if overwrite_input in no_inputs:
            print('Generation of "{0}" cancelled.'.format(destination_file))
            exit(0)

    # Render template and write to file
    if template:
        with open(destination_path, 'w') as f:
            f.write(template.render(context))
예제 #4
0
import unittest
import os

from unittest import mock

from tethys_apps.utilities import get_tethys_src_dir
from tethys_cli.test_command import test_command, check_and_install_prereqs

FNULL = open(os.devnull, 'w')
TETHYS_SRC_DIRECTORY = get_tethys_src_dir()


class TestCommandTests(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    @mock.patch('tethys_cli.test_command.run_process')
    @mock.patch('tethys_cli.test_command.os.path.join')
    @mock.patch('tethys_cli.test_command.get_manage_path')
    def test_test_command_no_coverage_file(self, mock_get_manage_path,
                                           mock_join, mock_run_process):
        mock_args = mock.MagicMock()
        mock_args.coverage = False
        mock_args.coverage_html = False
        mock_args.file = 'foo_file'
        mock_args.unit = False
        mock_args.gui = False
        mock_args.verbosity = None
예제 #5
0
import unittest
from unittest import mock

from tethys_cli.gen_commands import (
    get_environment_value, get_settings_value,
    derive_version_from_conda_environment, gen_meta_yaml, generate_command,
    GEN_NGINX_OPTION, GEN_NGINX_SERVICE_OPTION, GEN_ASGI_SERVICE_OPTION,
    GEN_SERVICES_OPTION, GEN_INSTALL_OPTION, GEN_PORTAL_OPTION,
    GEN_META_YAML_OPTION)

from tethys_apps.utilities import get_tethys_src_dir
TETHYS_SRC = get_tethys_src_dir()


class CLIGenCommandsTest(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_get_environment_value(self):
        result = get_environment_value(value_name='DJANGO_SETTINGS_MODULE')

        self.assertEqual('tethys_portal.settings', result)

    def test_get_environment_value_bad(self):
        self.assertRaises(
            EnvironmentError,
            get_environment_value,
            value_name='foo_bar_baz_bad_environment_value_foo_bar_baz')
예제 #6
0
def generate_command(args):
    """
    Generate a settings file for a new installation.
    """
    # Consts
    TETHYS_HOME = get_tethys_home_dir()
    TETHYS_SRC = get_tethys_src_dir()

    # Setup variables
    context = Context()

    # Determine template path

    gen_templates_dir = os.path.join(
        os.path.abspath(os.path.dirname(__file__)), 'gen_templates')
    template_path = os.path.join(gen_templates_dir, args.type)

    # Parse template
    template = Template(open(template_path).read())

    # Determine destination file name (defaults to type)
    destination_file = FILE_NAMES[args.type]

    # Default destination path is the current working directory
    destination_dir = os.path.join(TETHYS_SRC, 'tethys_portal')

    nginx_user = ''
    nginx_conf_path = '/etc/nginx/nginx.conf'
    if os.path.exists(nginx_conf_path):
        with open(nginx_conf_path, 'r') as nginx_conf:
            for line in nginx_conf.readlines():
                tokens = line.split()
                if len(tokens) > 0 and tokens[0] == 'user':
                    nginx_user = tokens[1].strip(';')
                    break

    # Settings file setup
    if args.type == GEN_SETTINGS_OPTION:
        # Generate context variables
        secret_key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for _ in range(50)
        ])
        context.update({
            'secret_key': secret_key,
            'allowed_host': args.allowed_host,
            'allowed_hosts': args.allowed_hosts,
            'db_username': args.db_username,
            'db_password': args.db_password,
            'db_port': args.db_port,
            'tethys_home': TETHYS_HOME,
            'production': args.production,
            'open_portal': args.open_portal
        })

    if args.type == GEN_NGINX_OPTION:
        hostname = str(settings.ALLOWED_HOSTS[0]) if len(
            settings.ALLOWED_HOSTS) > 0 else '127.0.0.1'
        workspaces_root = get_settings_value('TETHYS_WORKSPACES_ROOT')
        static_root = get_settings_value('STATIC_ROOT')

        context.update({
            'hostname': hostname,
            'workspaces_root': workspaces_root,
            'static_root': static_root,
            'client_max_body_size': args.client_max_body_size
        })

    if args.type == GEN_UWSGI_SERVICE_OPTION:
        conda_home = get_environment_value('CONDA_HOME')
        conda_env_name = get_environment_value('CONDA_ENV_NAME')

        user_option_prefix = ''

        try:
            linux_distro = linux_distribution(full_distribution_name=0)[0]
            if linux_distro in ['redhat', 'centos']:
                user_option_prefix = 'http-'
        except Exception:
            pass

        context.update({
            'nginx_user': nginx_user,
            'conda_home': conda_home,
            'conda_env_name': conda_env_name,
            'tethys_src': TETHYS_SRC,
            'user_option_prefix': user_option_prefix
        })

    if args.type == GEN_UWSGI_SETTINGS_OPTION:
        conda_home = get_environment_value('CONDA_HOME')
        conda_env_name = get_environment_value('CONDA_ENV_NAME')

        context.update({
            'conda_home': conda_home,
            'conda_env_name': conda_env_name,
            'uwsgi_processes': args.uwsgi_processes
        })

    if args.directory:
        if os.path.isdir(args.directory):
            destination_dir = args.directory
        else:
            print('ERROR: "{0}" is not a valid directory.'.format(
                destination_dir))
            exit(1)

    destination_path = os.path.join(destination_dir, destination_file)

    # Check for pre-existing file
    if os.path.isfile(destination_path):
        valid_inputs = ('y', 'n', 'yes', 'no')
        no_inputs = ('n', 'no')

        if args.overwrite:
            overwrite_input = 'yes'
        else:
            overwrite_input = input(
                'WARNING: "{0}" already exists. '
                'Overwrite? (y/n): '.format(destination_file)).lower()

            while overwrite_input not in valid_inputs:
                overwrite_input = input(
                    'Invalid option. Overwrite? (y/n): ').lower()

        if overwrite_input in no_inputs:
            print('Generation of "{0}" cancelled.'.format(destination_file))
            exit(0)

    # Render template and write to file
    if template:
        with open(destination_path, 'w') as f:
            f.write(template.render(context))
예제 #7
0
import os
import webbrowser

from tethys_apps.cli.manage_commands import get_manage_path, run_process
from tethys_apps.utilities import get_tethys_src_dir


TETHYS_SRC_DIRECTORY = get_tethys_src_dir()


def test_command(args):
    args.manage = False
    # Get the path to manage.py
    manage_path = get_manage_path(args)
    tests_path = os.path.join(TETHYS_SRC_DIRECTORY, 'tests')

    # Define the process to be run
    primary_process = ['python', manage_path, 'test']

    # Tag to later check if tests are being run on a specific app or extension
    app_package_tag = 'tethys_apps.tethysapp.'
    extension_package_tag = 'tethysext.'

    if args.coverage or args.coverage_html:
        os.environ['TETHYS_TEST_DIR'] = tests_path
        if args.file and app_package_tag in args.file:
            app_package_parts = args.file.split(app_package_tag)
            app_name = app_package_parts[1].split('.')[0]
            core_app_package = '{}{}'.format(app_package_tag, app_name)
            app_package = 'tethysapp.{}'.format(app_name)
            config_opt = '--source={},{}'.format(core_app_package, app_package)