1"""
2Salt package
3"""
4
5import importlib
6import sys
7import warnings
8
9if sys.version_info < (3,):
10    sys.stderr.write(
11        "\n\nAfter the Sodium release, 3001, Salt no longer supports Python 2. Exiting.\n\n"
12    )
13    sys.stderr.flush()
14
15
16USE_VENDORED_TORNADO = True
17
18
19class TornadoImporter:
20    def find_module(self, module_name, package_path=None):
21        if USE_VENDORED_TORNADO:
22            if module_name.startswith("tornado"):
23                return self
24        else:
25            if module_name.startswith("salt.ext.tornado"):
26                return self
27        return None
28
29    def load_module(self, name):
30        if USE_VENDORED_TORNADO:
31            mod = importlib.import_module("salt.ext.{}".format(name))
32        else:
33            # Remove 'salt.ext.' from the module
34            mod = importlib.import_module(name[9:])
35        sys.modules[name] = mod
36        return mod
37
38
39class SixRedirectImporter:
40    def find_module(self, module_name, package_path=None):
41        if module_name.startswith("salt.ext.six"):
42            return self
43        return None
44
45    def load_module(self, name):
46        mod = importlib.import_module(name[9:])
47        sys.modules[name] = mod
48        return mod
49
50
51# Try our importer first
52sys.meta_path = [TornadoImporter(), SixRedirectImporter()] + sys.meta_path
53
54
55# All salt related deprecation warnings should be shown once each!
56warnings.filterwarnings(
57    "once",  # Show once
58    "",  # No deprecation message match
59    DeprecationWarning,  # This filter is for DeprecationWarnings
60    r"^(salt|salt\.(.*))$",  # Match module(s) 'salt' and 'salt.<whatever>'
61    append=True,
62)
63
64# Filter the backports package UserWarning about being re-imported
65warnings.filterwarnings(
66    "ignore",
67    "^Module backports was already imported from (.*), but (.*) is being added to sys.path$",
68    UserWarning,
69    append=True,
70)
71
72
73def __define_global_system_encoding_variable__():
74    import sys
75
76    # This is the most trustworthy source of the system encoding, though, if
77    # salt is being imported after being daemonized, this information is lost
78    # and reset to None
79    encoding = None
80
81    if not sys.platform.startswith("win") and sys.stdin is not None:
82        # On linux we can rely on sys.stdin for the encoding since it
83        # most commonly matches the filesystem encoding. This however
84        # does not apply to windows
85        encoding = sys.stdin.encoding
86
87    if not encoding:
88        # If the system is properly configured this should return a valid
89        # encoding. MS Windows has problems with this and reports the wrong
90        # encoding
91        import locale
92
93        try:
94            encoding = locale.getdefaultlocale()[-1]
95        except ValueError:
96            # A bad locale setting was most likely found:
97            #   https://github.com/saltstack/salt/issues/26063
98            pass
99
100        # This is now garbage collectable
101        del locale
102        if not encoding:
103            # This is most likely ascii which is not the best but we were
104            # unable to find a better encoding. If this fails, we fall all
105            # the way back to ascii
106            encoding = sys.getdefaultencoding()
107        if not encoding:
108            if sys.platform.startswith("darwin"):
109                # Mac OS X uses UTF-8
110                encoding = "utf-8"
111            elif sys.platform.startswith("win"):
112                # Windows uses a configurable encoding; on Windows, Python uses the name “mbcs”
113                # to refer to whatever the currently configured encoding is.
114                encoding = "mbcs"
115            else:
116                # On linux default to ascii as a last resort
117                encoding = "ascii"
118
119    import builtins
120
121    # Define the detected encoding as a built-in variable for ease of use
122    setattr(builtins, "__salt_system_encoding__", encoding)
123
124    # This is now garbage collectable
125    del sys
126    del builtins
127    del encoding
128
129
130__define_global_system_encoding_variable__()
131
132# This is now garbage collectable
133del __define_global_system_encoding_variable__
134
135# Import Salt's logging machinery
136import salt._logging.impl  # isort:skip  pylint: disable=unused-import
137