#!/usr/bin/env python3 """Main CHART-EPSSG launcher executable.""" import os import sys import warnings with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=DeprecationWarning) # bin directory containing this file BIN_PATH = os.path.abspath(os.path.dirname(__file__)) # Project root PROJ_DIR = os.path.dirname(BIN_PATH) # Environment variable which can be set to enable launcher debug messages ENV_CHART_DEBUG_LAUNCHER = 'CHART_DEBUG_LAUNCHER' def print_debug(*mess): """Show debug messages if $CHART_DEBUG_LAUNCHER is set.""" # if os.environ.get('CHART_RELOADED') != '1': if os.environ.get('CHART_DEBUG_LAUNCHER') == '1': print(*mess) print_debug('Starting launcher script') def add_to_sys_path(new_dir): """Insert `new_dir` as a standard import directory.""" print_debug('Adding {c} to sys.path'.format(c=new_dir)) # For this process... sys.path.insert(0, new_dir) # ... and for child processes os.environ['PYTHONPATH'] = ':'.join((new_dir, os.environ.get('PYTHONPATH', ''))) # Allow code to import from this project add_to_sys_path(PROJ_DIR) # Set us up as the active CHART project import chartepssg.project # (unused import) pylint: disable=W0611 # prerequisite check try: import django except ImportError: print('Could not import pre-requisites. Try running environment activate script') raise def check_for_core(dirname): """See if `dirname` appears to be a CHART core installation.""" # Don't just import it! Python3 caches import failures so once you look for a module once and # fail to find it, you always fail to find it even if you later prepend the right directory to # sys.path print_debug('Checking if {c} is a CHART-core install'.format(c=dirname)) return os.path.exists(os.path.join(dirname, 'chart', '__init__.py')) def check_for_rest(dirname): """See if `dirname` appears to be a CHART rest installation.""" print_debug('Checking if {c} is a CDAT REST install'.format(c=dirname)) return os.path.exists(os.path.join(dirname, 'chart_rest', '__init__.py')) # Look for the CHART core in sys.path core_dir = None for testdir in sys.path: if check_for_core(testdir): core_dir = testdir print_debug('Found core in {c}'.format(c=testdir)) break else: # Look for core in a few well known places for testdir in (os.path.join(PROJ_DIR, 'chart'), os.path.join(os.path.dirname(PROJ_DIR), 'chart')): if check_for_core(testdir): core_dir = testdir print_debug('Adding core in {c}'.format(c=testdir)) add_to_sys_path(testdir) break else: raise SystemExit('Cannot find CHART core') # Look for CDAT REST API dir in sys.path for testdir in sys.path: if check_for_rest(testdir): print_debug('Found REST in {c}'.format(c=testdir)) break else: for testdir in (os.path.join(PROJ_DIR, 'chart-rest'), os.path.join(core_dir, 'chart-rest')): if check_for_rest(testdir): print_debug('Adding REST in {c}'.format(c=testdir)) add_to_sys_path(testdir) break else: print('Cannot find CHART-REST directory for CDAT support') from chart.cmd import launcher # import cProfile # cProfile.run('launcher.launch()') # print('launching') launcher.launch()