forked from Nuitka/Nuitka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhints.py
157 lines (116 loc) · 4.15 KB
/
hints.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2022, Kay Hayen, mailto:[email protected]
#
# Part of "Nuitka", an optimizing Python compiler that is compatible and
# integrates with CPython, but also works on its own.
#
# 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.
#
""" The hints module should contain functions to call in your code.
In reality, right now it does only contain an import tracing mechanism
that helps us with debugging.
"""
from __future__ import print_function
import os
import sys
import traceback
try:
import __builtin__ as builtins
except ImportError:
import builtins
original_import = __import__
_indentation = 0
def _normalizePath(path):
path = os.path.abspath(path)
best = None
paths = list(sys.path)
# Nuitka standalone mode.
try:
paths.append(__nuitka_binary_dir)
paths.append(os.getcwd())
except NameError:
pass
for path_entry in paths:
path_entry = os.path.normcase(path_entry)
if os.path.normcase(path).startswith(path_entry):
if best is None or len(path_entry) > len(best):
best = path_entry
if best is not None:
path = "$PYTHONPATH" + path[len(best) :]
return path
def _moduleRepr(module):
try:
module_file = module.__file__
module_file = module_file.replace(".pyc", ".py")
if module_file.endswith(".so"):
module_file = os.path.join(
os.path.dirname(module_file),
os.path.basename(module_file).split(".")[0] + ".so",
)
file_desc = "file " + _normalizePath(module_file).replace(".pyc", ".py")
except AttributeError:
file_desc = "built-in"
return "<module %s %s>" % (module.__name__, file_desc)
normalize_paths = None
show_source = None
def _ourimport(
name,
globals=None,
locals=None,
fromlist=None,
level=-1 if sys.version_info[0] < 3 else 0,
):
builtins.__import__ = original_import
# Singleton, pylint: disable=global-statement
global _indentation
try:
_indentation += 1
print(
_indentation * " "
+ "called with: name=%r level=%d fromlist=%s" % (name, level, fromlist)
)
for entry in traceback.extract_stack()[:-1]:
if entry[2] == "_ourimport":
print(_indentation * " " + "by __import__")
else:
entry = list(entry)
if not show_source:
del entry[-1]
del entry[-1]
if normalize_paths:
entry[0] = _normalizePath(entry[0])
print(_indentation * " " + "by " + "|".join(str(s) for s in entry))
print(_indentation * " " + "*" * 40)
builtins.__import__ = _ourimport
try:
result = original_import(name, globals, locals, fromlist, level)
except ImportError as e:
print(_indentation * " " + "EXCEPTION:", e)
raise
finally:
builtins.__import__ = original_import
print(_indentation * " " + "RESULT:", _moduleRepr(result))
print(_indentation * " " + "*" * 40)
builtins.__import__ = _ourimport
return result
finally:
_indentation -= 1
def enableImportTracing(normalize_paths=True, show_source=False):
global do_normalize_paths, do_show_source
do_normalize_paths = normalize_paths
do_show_source = show_source
# pylint: disable=redefined-builtin
builtins.__import__ = _ourimport
# Since we swap this around, prevent it from releasing by giving it a global
# name too, not only a local one.
global _ourimport_reference
_ourimport