File: generate_code.py

package info (click to toggle)
pydevd 3.3.0%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,892 kB
  • sloc: python: 77,508; cpp: 1,869; sh: 368; makefile: 50; ansic: 4
file content (283 lines) | stat: -rw-r--r-- 8,010 bytes parent folder | download | duplicates (2)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
This module should be run to recreate the files that we generate automatically
(i.e.: modules that shouldn't be traced and cython .pyx)
"""

from __future__ import print_function

import os
import struct
import re


def is_python_64bit():
    return struct.calcsize("P") == 8


root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))


def get_cython_contents(filename):
    if filename.endswith(".pyc"):
        filename = filename[:-1]

    state = "regular"

    replacements = []

    new_contents = []
    with open(filename, "r") as stream:
        for line in stream:
            strip = line.strip()
            if state == "regular":
                if strip == "# IFDEF CYTHON":
                    state = "cython"

                    new_contents.append(
                        "%s -- DONT EDIT THIS FILE (it is automatically generated)\n" % line.replace("\n", "").replace("\r", "")
                    )
                    continue

                new_contents.append(line)

            elif state == "cython":
                if strip == "# ELSE":
                    state = "nocython"
                    new_contents.append(line)
                    continue

                elif strip == "# ENDIF":
                    state = "regular"
                    new_contents.append(line)
                    continue

                if strip == "#":
                    continue

                assert strip.startswith("# "), 'Line inside # IFDEF CYTHON must start with "# ". Found: %s' % (strip,)
                strip = strip.replace("# ", "", 1).strip()

                if strip.startswith("cython_inline_constant:"):
                    strip = strip.replace("cython_inline_constant:", "")
                    word_to_replace, replacement = strip.split("=")
                    replacements.append((word_to_replace.strip(), replacement.strip()))
                    continue

                line = line.replace("# ", "", 1)
                new_contents.append(line)

            elif state == "nocython":
                if strip == "# ENDIF":
                    state = "regular"
                    new_contents.append(line)
                    continue
                new_contents.append("# %s" % line)

    assert state == "regular", "Error: # IFDEF CYTHON found without # ENDIF"

    ret = "".join(new_contents)

    for word_to_replace, replacement in replacements:
        ret = re.sub(r"\b%s\b" % (word_to_replace,), replacement, ret)

    return ret


def _generate_cython_from_files(target, modules):
    contents = [
        """from __future__ import print_function

# Important: Autogenerated file.

# DO NOT edit manually!
# DO NOT edit manually!
"""
    ]

    found = []
    for mod in modules:
        found.append(mod.__file__)
        contents.append(get_cython_contents(mod.__file__))

    print("Generating cython from: %s" % (found,))

    with open(target, "w") as stream:
        stream.write("".join(contents))


def generate_dont_trace_files():
    template = """# Important: Autogenerated file.

# fmt: off
# DO NOT edit manually!
# DO NOT edit manually!

LIB_FILE = 1
PYDEV_FILE = 2

DONT_TRACE_DIRS = {
%(pydev_dirs)s
}

LIB_FILES_IN_DONT_TRACE_DIRS = {
%(pydev_lib_files_in_dont_trace_dirs)s
}

DONT_TRACE = {
    # commonly used things from the stdlib that we don't want to trace
    'Queue.py':LIB_FILE,
    'queue.py':LIB_FILE,
    'socket.py':LIB_FILE,
    'weakref.py':LIB_FILE,
    '_weakrefset.py':LIB_FILE,
    'linecache.py':LIB_FILE,
    'threading.py':LIB_FILE,
    'dis.py':LIB_FILE,

    # things from pydev that we don't want to trace
%(pydev_files)s
}

# if we try to trace io.py it seems it can get halted (see http://bugs.python.org/issue4716)
DONT_TRACE['io.py'] = LIB_FILE

# Don't trace common encodings too
DONT_TRACE['cp1252.py'] = LIB_FILE
DONT_TRACE['utf_8.py'] = LIB_FILE
DONT_TRACE['codecs.py'] = LIB_FILE

# fmt: on
"""

    pydev_files = []
    pydev_dirs = []
    pydev_lib_files_in_dont_trace_dirs = []

    exclude_dirs = [
        ".git",
        ".settings",
        "build",
        "build_tools",
        "dist",
        "pydevd.egg-info",
        "pydevd_attach_to_process",
        "pydev_sitecustomize",
        "stubs",
        "tests",
        "tests_mainloop",
        "tests_python",
        "tests_runfiles",
        "test_pydevd_reload",
        "third_party",
        "__pycache__",
        "vendored",
        ".mypy_cache",
        "pydevd.egg-info",
    ]

    for root, dirs, files in os.walk(root_dir):
        for d in dirs:
            if d == "pydev_ipython":
                pydev_dirs.append("    '%s': LIB_FILE," % (d,))
                continue

            if "pydev" in d and d != "pydevd.egg-info":
                # print(os.path.join(root, d))
                pydev_dirs.append("    '%s': PYDEV_FILE," % (d,))

        for d in exclude_dirs:
            try:
                dirs.remove(d)
            except:
                pass

        if os.path.basename(root) == "pydev_ipython":
            for f in files:
                if f.endswith(".py"):
                    pydev_lib_files_in_dont_trace_dirs.append("    '%s'," % (f,))
            continue

        for f in files:
            if f.endswith(".py"):
                if f not in (
                    "__init__.py",
                    "runfiles.py",
                    "pydev_coverage.py",
                    "pydev_pysrc.py",
                    "setup.py",
                    "setup_pydevd_cython.py",
                    "interpreterInfo.py",
                    "conftest.py",
                ):
                    pydev_files.append("    '%s': PYDEV_FILE," % (f,))

    contents = template % (
        dict(
            pydev_files="\n".join(sorted(set(pydev_files))),
            pydev_dirs="\n".join(sorted(set(pydev_dirs))),
            pydev_lib_files_in_dont_trace_dirs="\n".join(sorted(set(pydev_lib_files_in_dont_trace_dirs))),
        )
    )
    assert "pydevd.py" in contents
    assert "pydevd_dont_trace.py" in contents
    with open(os.path.join(root_dir, "_pydevd_bundle", "pydevd_dont_trace_files.py"), "w") as stream:
        stream.write(contents)


def remove_if_exists(f):
    try:
        if os.path.exists(f):
            os.remove(f)
    except:
        import traceback

        traceback.print_exc()


def generate_cython_module():
    _generate_cython_module()
    _generate_sys_monitoring_cython_module()


def _generate_cython_module():
    print("Removing pydevd_cython.pyx")
    remove_if_exists(os.path.join(root_dir, "_pydevd_bundle", "pydevd_cython.pyx"))

    target = os.path.join(root_dir, "_pydevd_bundle", "pydevd_cython.pyx")
    curr = os.environ.get("PYDEVD_USE_CYTHON")
    try:
        os.environ["PYDEVD_USE_CYTHON"] = "NO"

        from _pydevd_bundle import pydevd_additional_thread_info_regular
        from _pydevd_bundle import pydevd_frame, pydevd_trace_dispatch_regular

        _generate_cython_from_files(target, [pydevd_additional_thread_info_regular, pydevd_frame, pydevd_trace_dispatch_regular])
    finally:
        if curr is None:
            del os.environ["PYDEVD_USE_CYTHON"]
        else:
            os.environ["PYDEVD_USE_CYTHON"] = curr


def _generate_sys_monitoring_cython_module():
    print("Removing _pydevd_sys_monitoring_cython.pyx")
    remove_if_exists(os.path.join(root_dir, "_pydevd_sys_monitoring", "_pydevd_sys_monitoring_cython.pyx"))

    target = os.path.join(root_dir, "_pydevd_sys_monitoring", "_pydevd_sys_monitoring_cython.pyx")
    curr = os.environ.get("PYDEVD_USE_CYTHON")
    try:
        os.environ["PYDEVD_USE_CYTHON"] = "NO"

        from _pydevd_sys_monitoring import _pydevd_sys_monitoring

        _generate_cython_from_files(target, [_pydevd_sys_monitoring])
    finally:
        if curr is None:
            del os.environ["PYDEVD_USE_CYTHON"]
        else:
            os.environ["PYDEVD_USE_CYTHON"] = curr


if __name__ == "__main__":
    generate_dont_trace_files()
    generate_cython_module()