1#!/usr/bin/env python3
  2
  3"""Main CHART-EPSSG launcher executable."""
  4
  5import os
  6import sys
  7import warnings
  8with warnings.catch_warnings():
  9    warnings.filterwarnings('ignore', category=DeprecationWarning)
 10
 11# bin directory containing this file
 12BIN_PATH = os.path.abspath(os.path.dirname(__file__))
 13
 14# Project root
 15PROJ_DIR = os.path.dirname(BIN_PATH)
 16
 17# Environment variable which can be set to enable launcher debug messages
 18ENV_CHART_DEBUG_LAUNCHER = 'CHART_DEBUG_LAUNCHER'
 19
 20def print_debug(*mess):
 21    """Show debug messages if $CHART_DEBUG_LAUNCHER is set."""
 22    # if os.environ.get('CHART_RELOADED') != '1':
 23    if os.environ.get('CHART_DEBUG_LAUNCHER') == '1':
 24        print(*mess)
 25
 26print_debug('Starting launcher script')
 27
 28def add_to_sys_path(new_dir):
 29    """Insert `new_dir` as a standard import directory."""
 30    print_debug('Adding {c} to sys.path'.format(c=new_dir))
 31    # For this process...
 32    sys.path.insert(0, new_dir)
 33    # ... and for child processes
 34    os.environ['PYTHONPATH'] = ':'.join((new_dir, os.environ.get('PYTHONPATH', '')))
 35
 36# Allow code to import from this project
 37add_to_sys_path(PROJ_DIR)
 38
 39# Set us up as the active CHART project
 40import chartepssg.project  # (unused import) pylint: disable=W0611
 41
 42# prerequisite check
 43try:
 44    import django
 45
 46except ImportError:
 47    print('Could not import pre-requisites. Try running environment activate script')
 48    raise
 49
 50def check_for_core(dirname):
 51    """See if `dirname` appears to be a CHART core installation."""
 52    # Don't just import it! Python3 caches import failures so once you look for a module once and
 53    # fail to find it, you always fail to find it even if you later prepend the right directory to
 54    # sys.path
 55    print_debug('Checking if {c} is a CHART-core install'.format(c=dirname))
 56    return os.path.exists(os.path.join(dirname, 'chart', '__init__.py'))
 57
 58def check_for_rest(dirname):
 59    """See if `dirname` appears to be a CHART rest installation."""
 60    print_debug('Checking if {c} is a CDAT REST install'.format(c=dirname))
 61    return os.path.exists(os.path.join(dirname, 'chart_rest', '__init__.py'))
 62
 63# Look for the CHART core in sys.path
 64core_dir = None
 65for testdir in sys.path:
 66    if check_for_core(testdir):
 67        core_dir = testdir
 68        print_debug('Found core in {c}'.format(c=testdir))
 69        break
 70
 71else:
 72    # Look for core in a few well known places
 73    for testdir in (os.path.join(PROJ_DIR, 'chart'),
 74                    os.path.join(os.path.dirname(PROJ_DIR), 'chart')):
 75        if check_for_core(testdir):
 76            core_dir = testdir
 77            print_debug('Adding core in {c}'.format(c=testdir))
 78            add_to_sys_path(testdir)
 79            break
 80
 81    else:
 82        raise SystemExit('Cannot find CHART core')
 83
 84# Look for CDAT REST API dir in sys.path
 85for testdir in sys.path:
 86    if check_for_rest(testdir):
 87        print_debug('Found REST in {c}'.format(c=testdir))
 88        break
 89
 90else:
 91    for testdir in (os.path.join(PROJ_DIR, 'chart-rest'),
 92                    os.path.join(core_dir, 'chart-rest')):
 93        if check_for_rest(testdir):
 94            print_debug('Adding REST in {c}'.format(c=testdir))
 95            add_to_sys_path(testdir)
 96            break
 97
 98    else:
 99        print('Cannot find CHART-REST directory for CDAT support')
100
101from chart.cmd import launcher
102# import cProfile
103# cProfile.run('launcher.launch()')
104# print('launching')
105launcher.launch()