in skywalking/bootstrap/cli/utility/runner.py [0:0]
def execute(command: List[str], experimental_check_prefork: bool) -> None:
""" Set up environ and invokes the given command to replace current process """
cli_logger.debug(f'SkyWalking Python agent `runner` received command {command}')
if experimental_check_prefork:
cli_logger.info('Detected experimental prefork support flag, checking for pre-forking servers...')
prefork_handler(command=command)
cli_logger.debug('Adding sitecustomize.py to PYTHONPATH')
from skywalking.bootstrap.loader import __file__ as loader_dir
from skywalking.bootstrap.hooks import __file__ as hook_dir
loader_path = os.path.dirname(loader_dir)
hook_path = os.path.dirname(hook_dir)
new_path: str = ''
python_path = os.environ.get('PYTHONPATH')
if python_path: # If there is already a different PYTHONPATH, PREPEND to it as we must get loaded first.
partitioned = python_path.split(os.path.pathsep)
if loader_path not in partitioned: # check if we are already there
new_path = os.path.pathsep.join([loader_path, hook_path, python_path])
# When constructing sys.path PYTHONPATH is always
# before other paths and after interpreter invoker path, which is here or none
os.environ['PYTHONPATH'] = new_path if new_path else os.path.pathsep.join([loader_path, hook_path])
cli_logger.debug(f"Updated PYTHONPATH - {os.environ['PYTHONPATH']}")
# Used in sitecustomize to compare command's Python installation with CLI
# If not match, need to stop agent from loading, and kill the process
os.environ['SW_PYTHON_PREFIX'] = os.path.realpath(os.path.normpath(sys.prefix))
os.environ['SW_PYTHON_VERSION'] = platform.python_version()
# Pass down the logger debug setting to the replaced process, need a new logger there
os.environ['SW_AGENT_SW_PYTHON_CLI_DEBUG_ENABLED'] = 'True' if cli_logger.level == logging.DEBUG else 'False'
try:
cli_logger.info(f'New process starting with command - `{command[0]}` args - `{command}`')
os.execvp(command[0], command)
except OSError:
raise SWRunnerFailure