sonatype-lift[bot] commented on a change in pull request #156: URL: https://github.com/apache/skywalking-python/pull/156#discussion_r694118440
########## File path: skywalking/bootstrap/cli/utility/runner.py ########## @@ -0,0 +1,64 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" User application command runner """ +import logging +import os +import platform +import sys +from typing import List + +from skywalking.bootstrap import get_cli_logger + +cli_logger = get_cli_logger() + + +def execute(command: List[str]) -> None: + """ Set up environ and invokes the given command to replace current process """ + + cli_logger.debug("SkyWalking Python agent `runner` received command {}".format(command)) + + cli_logger.debug("Adding sitecustomize.py to PYTHONPATH") + + from skywalking.bootstrap.loader import __file__ as loader_path + + loader_path = os.path.dirname(loader_path) + + 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 + loader_path = os.path.pathsep.join([loader_path, python_path]) # type: str Review comment: *Illegal annotation target:* Target `loader_path` cannot be annotated after it is first declared. (at-me [in a reply](https://help.sonatype.com/lift) with `help` or `ignore`) ########## File path: skywalking/bootstrap/loader/sitecustomize.py ########## @@ -0,0 +1,139 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" This version of sitecustomize will +1. initializes the SkyWalking Python Agent. +2. invoke an existing sitecustomize.py. +Not compatible with Python <= 3.3 + +When executing commands with `sw-python run command` +This particular sitecustomize module will be picked up by any valid replacement +process(command) invoked by os.execvp in runner.py, thus will be seen by potentially +incompatible Python versions and system interpreters, which certainly will cause problems. +Therefore, an attempt to use command leading to a different Python interpreter should be stopped. +""" +import importlib +import logging +import os +import platform +import sys + + +def _get_sw_loader_logger(): + """ Setup a new logger with passed skywalking CLI env vars, + don't import from skywalking, it may not be on sys.path + if user misuses the CLI to run programs out of scope + """ + from logging import getLogger + logger = getLogger('skywalking-loader') + ch = logging.StreamHandler() + formatter = logging.Formatter('%(name)s [%(threadName)s] [%(levelname)s] %(message)s') + ch.setFormatter(formatter) + logger.addHandler(ch) + logger.propagate = False + if os.environ.get('SW_PYTHON_CLI_DEBUG_ENABLED') == 'True': # set from the original CLI runner + logger.setLevel(level=logging.DEBUG) + return logger + + +_sw_loader_logger = _get_sw_loader_logger() + +# DEBUG messages in case execution goes wrong +_sw_loader_logger.debug('---------------sitecustomize.py---------------') +_sw_loader_logger.debug("Successfully imported sitecustomize.py from `{}`".format(__file__)) +_sw_loader_logger.debug('You are inside working dir - {}'.format(os.getcwd())) +_sw_loader_logger.debug('Using Python version - {} '.format(sys.version)) +_sw_loader_logger.debug('Using executable at - {}'.format(sys.executable)) +_sw_loader_logger.debug('System Base Python executable location {}'.format(sys.base_prefix)) + +if sys.prefix != sys.base_prefix: + _sw_loader_logger.debug("[The SkyWalking agent bootstrapper is running inside a virtual environment]") + + +# It is possible that someone else also has a sitecustomize.py +# in sys.path either set by .pth files or manually, we need to run them as well +# The path to user sitecustomize.py is after ours in sys.path, needs to be imported here. +# just to be safe, remove loader thoroughly from sys.path and also sys.modules + +cleared_path = [p for p in sys.path if p != os.path.dirname(__file__)] +sys.path = cleared_path # at this stage any user added pth still exists +loaded = sys.modules.pop('sitecustomize', None) # Remove sitecustomize from loaded + +# now try to find the original sitecustomize provided in user env +try: + loaded = importlib.import_module('sitecustomie') + _sw_loader_logger.debug("Found user sitecustomize file {}, imported".format(loaded)) +except ImportError: # ModuleNotFoundError + _sw_loader_logger.debug("Original sitecustomize module not found, skipping.") +finally: + sys.modules.update(sitecustomize=loaded) Review comment: *Incompatible parameter type:* Expected `_importlib_modulespec.ModuleType` for 1st parameter `sitecustomize` to call `dict.update` but got `typing.Optional[_importlib_modulespec.ModuleType]`. (at-me [in a reply](https://help.sonatype.com/lift) with `help` or `ignore`) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
