diff options
author | Miroslav Los <miroslav.los@pantheon.tech> | 2019-12-09 18:20:59 +0100 |
---|---|---|
committer | Miroslav Los <miroslav.los@pantheon.tech> | 2019-12-12 17:46:28 +0100 |
commit | 4435e8803a6844245d2529cae840a3d55d84c296 (patch) | |
tree | 495b8e368934755342190ab6047109c467304243 /k8s/k8splugin/cloudify_importer.py | |
parent | 4682668e697b71bbc1be4133cb66fad0df1b735b (diff) |
Customize python import for kubernetes plugin
Cloudify manager fails to find the plugin without this helper.
The original code, cloudify-python-importer, is unmaintained.
It is just one module, licensed under Apache2.0 as well.
It also needs a python3 fix, hence the module code is added here.
Note that what the modification does should not be necessary,
and a proper root cause and fix needs to be found eventually.
Signed-off-by: Miroslav Los <miroslav.los@pantheon.tech>
Issue-ID: DCAEGEN2-1988
Change-Id: I28274dff902204362d7f5b6f97ac3381ff8b5411
Diffstat (limited to 'k8s/k8splugin/cloudify_importer.py')
-rw-r--r-- | k8s/k8splugin/cloudify_importer.py | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/k8s/k8splugin/cloudify_importer.py b/k8s/k8splugin/cloudify_importer.py new file mode 100644 index 0000000..10a6cf5 --- /dev/null +++ b/k8s/k8splugin/cloudify_importer.py @@ -0,0 +1,120 @@ +# ####### +# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved +# Copyright (c) 2019 Pantheon.tech. All rights reserved +# +# Licensed 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. + +# Copied and updated for python3 from cloudify-python-importer + +from __future__ import print_function + +import sys +import imp +import os +try: + import builtins +except ImportError: + import __builtin__ as builtins + + +class _OurImporter(object): + + def __init__(self, dir_name, load_file): + self.dirname = dir_name + self.load_file = load_file + + def load_module(self, package_name): + try: + return sys.modules[package_name] + except KeyError: + pass + + if self.load_file: + try: + fp, pathname, description = imp.find_module( + package_name.split(".")[-1], + ["/".join(self.dirname.split("/")[:-1])] + ) + m = imp.load_module(package_name, fp, pathname, description) + except ImportError as e: + raise e + else: + m = imp.new_module(package_name) + + m.__name__ = package_name + m.__path__ = [self.dirname] + m.__doc__ = None + + m.__loader__ = self + + sys.modules.setdefault(package_name, m) + return m + + +class _OurFinder(object): + + def __init__(self, dir_name): + self.dir_name = dir_name + + def find_module(self, package_name): + real_path = "/".join(package_name.split(".")) + + for path in [self.dir_name] + sys.path: + + full_name = os.path.abspath(path) + "/" + real_path + dir_root = os.path.abspath(path) + "/" + real_path.split("/")[0] + + if os.path.isfile(path + "/" + real_path + ".py"): + return _OurImporter(full_name, True) + + if os.path.isdir(full_name): + if not os.path.isfile(dir_root + "/" + "__init__.py"): + print('Creating __init__.py in', dir_root, file=sys.stderr) + with open(dir_root + "/" + "__init__.py", 'a+') as file: + file.write("# Created by importer") + return _OurImporter(dir_root, False) + + return _OurImporter(full_name, True) + + return None + + +def _check_import(dir_name): + return _OurFinder(dir_name) + + +def register_callback(): + sys.path_hooks.append(_check_import) + + save_import = builtins.__import__ + + def new_import(*argv, **kwargs): + try: + module = save_import(*argv, **kwargs) + except ImportError as e: + finder = _OurFinder("") + if not finder: + raise e + importer = finder.find_module(argv[0]) + if not importer: + raise e + module = importer.load_module(argv[0]) + if not module: + raise e + + return module + + builtins.__import__ = new_import + + +register_callback() |