summaryrefslogtreecommitdiffstats
path: root/k8s/k8splugin/cloudify_importer.py
blob: 10a6cf597043621e692c7cb059bc7116996f3408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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()