2019-03-13 15:14:12 +01:00
#!/usr/bin/env python
""" Provides an entry point for python scripts and python modules on the controller with the current python interpreter and optional code coverage collection. """
2019-07-12 08:46:20 +02:00
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
2019-03-13 15:14:12 +01:00
import os
import sys
def main ( ) :
""" Main entry point. """
name = os . path . basename ( __file__ )
args = [ sys . executable ]
2019-03-22 15:59:29 +01:00
coverage_config = os . environ . get ( ' COVERAGE_CONF ' )
coverage_output = os . environ . get ( ' COVERAGE_FILE ' )
2019-03-13 15:14:12 +01:00
if coverage_config :
if coverage_output :
args + = [ ' -m ' , ' coverage.__main__ ' , ' run ' , ' --rcfile ' , coverage_config ]
else :
2019-03-22 23:27:28 +01:00
if sys . version_info > = ( 3 , 4 ) :
2019-07-11 22:03:49 +02:00
# noinspection PyUnresolvedReferences
2019-03-22 23:27:28 +01:00
import importlib . util
2019-07-11 22:03:49 +02:00
# noinspection PyUnresolvedReferences
2019-03-22 23:27:28 +01:00
found = bool ( importlib . util . find_spec ( ' coverage ' ) )
else :
2019-07-13 06:58:19 +02:00
# noinspection PyDeprecation
2019-03-22 23:27:28 +01:00
import imp
try :
2019-07-13 06:58:19 +02:00
# noinspection PyDeprecation
2019-03-22 23:27:28 +01:00
imp . find_module ( ' coverage ' )
found = True
except ImportError :
found = False
if not found :
2020-05-16 02:09:54 +02:00
sys . exit ( ' ERROR: Could not find `coverage` module. '
' Did you use a virtualenv created without --system-site-packages or with the wrong interpreter? ' )
2019-03-13 15:14:12 +01:00
if name == ' python.py ' :
if sys . argv [ 1 ] == ' -c ' :
# prevent simple misuse of python.py with -c which does not work with coverage
sys . exit ( ' ERROR: Use `python -c` instead of `python.py -c` to avoid errors when code coverage is collected. ' )
elif name == ' pytest ' :
args + = [ ' -m ' , ' pytest ' ]
else :
args + = [ find_executable ( name ) ]
args + = sys . argv [ 1 : ]
os . execv ( args [ 0 ] , args )
def find_executable ( name ) :
"""
: type name : str
: rtype : str
"""
path = os . environ . get ( ' PATH ' , os . path . defpath )
seen = set ( [ os . path . abspath ( __file__ ) ] )
for base in path . split ( os . path . pathsep ) :
candidate = os . path . abspath ( os . path . join ( base , name ) )
if candidate in seen :
continue
seen . add ( candidate )
if os . path . exists ( candidate ) and os . access ( candidate , os . F_OK | os . X_OK ) :
return candidate
raise Exception ( ' Executable " %s " not found in path: %s ' % ( name , path ) )
if __name__ == ' __main__ ' :
main ( )