Debian Codegraph @

setup.py

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/env python
2
3 from __future__ import annotations
4
5 import glob
6 import os
7 import os.path
8 import sys
9 from typing import TYPE_CHECKING, Any
10
11 if sys.version_info < (3, 10, 0): # noqa: UP036, RUF100
12 sys.stderr.write("ERROR: You need Python 3.10 or later to use mypy.\n")
13 exit(1)
14
15 # we'll import stuff from the source tree, let's ensure is on the sys path
16 sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
17
18 # This requires setuptools when building; setuptools is not needed
19 # when installing from a wheel file (though it is still needed for
20 # alternative forms of installing, as suggested by README.md).
21 from setuptools import Extension, setup
22 from setuptools.command.build_py import build_py
23
24 from mypy.version import __version__ as version
25
26 if TYPE_CHECKING:
27 from typing import TypeGuard
28
29
30 def is_list_of_setuptools_extension(items: list[Any]) -> TypeGuard[list[Extension]]:
31 return all(isinstance(item, Extension) for item in items)
32
33
34 def find_package_data(base: str, globs: list[str], root: str = "mypy") -> list[str]:
35 """Find all interesting data files, for setup(package_data=)
36
37 Arguments:
38 root: The directory to search in.
39 globs: A list of glob patterns to accept files.
40 """
41
42 rv_dirs = [root for root, dirs, files in os.walk(base)]
43 rv = []
44 for rv_dir in rv_dirs:
45 files = []
46 for pat in globs:
47 files += glob.glob(os.path.join(rv_dir, pat))
48 if not files:
49 continue
50 rv.extend([os.path.relpath(f, root) for f in files])
51 return rv
52
53
54 class CustomPythonBuild(build_py):
55 def pin_version(self) -> None:
56 path = os.path.join(self.build_lib, "mypy")
57 self.mkpath(path)
58 with open(os.path.join(path, "version.py"), "w") as stream:
59 stream.write(f'__version__ = "{version}"\n')
60
61 def run(self) -> None:
62 self.execute(self.pin_version, ())
63 build_py.run(self)
64
65
66 cmdclass = {"build_py": CustomPythonBuild}
67
68 USE_MYPYC = False
69 # To compile with mypyc, a mypyc checkout must be present on the PYTHONPATH
70 if len(sys.argv) > 1 and "--use-mypyc" in sys.argv:
71 sys.argv.remove("--use-mypyc")
72 USE_MYPYC = True
73 if os.getenv("MYPY_USE_MYPYC", None) == "1":
74 USE_MYPYC = True
75
76 if USE_MYPYC:
77 MYPYC_BLACKLIST = tuple(
78 os.path.join("mypy", x)
79 for x in (
80 # Need to be runnable as scripts
81 "__main__.py",
82 "pyinfo.py",
83 os.path.join("dmypy", "__main__.py"),
84 os.path.join("build_worker", "__main__.py"),
85 "exportjson.py",
86 # Uses __getattr__/__setattr__
87 "split_namespace.py",
88 # Lies to mypy about code reachability
89 "bogus_type.py",
90 # We don't populate __file__ properly at the top level or something?
91 # Also I think there would be problems with how we generate version.py.
92 "version.py",
93 # Empty __init__.py files, no benefit from compilation
94 os.path.join("plugins", "__init__.py"),
95 os.path.join("server", "__init__.py"),
96 # Skip these to reduce the size of the build
97 "stubtest.py",
98 "stubgenc.py",
99 "stubdoc.py",
100 )
101 ) + (
102 # Don't want to grab this accidentally
103 os.path.join("mypyc", "lib-rt", "setup.py"),
104 os.path.join("mypyc", "lib-rt", "build_setup.py"),
105 # Uses __file__ at top level https://github.com/mypyc/mypyc/issues/700
106 os.path.join("mypyc", "__main__.py"),
107 os.path.join("mypyc", "build_setup.py"), # for monkeypatching
108 )
109
110 everything = [os.path.join("mypy", x) for x in find_package_data("mypy", ["*.py"])] + [
111 os.path.join("mypyc", x) for x in find_package_data("mypyc", ["*.py"], root="mypyc")
112 ]
113 # Start with all the .py files
114 all_real_pys = [
115 x for x in everything if not x.startswith(os.path.join("mypy", "typeshed") + os.sep)
116 ]
117 # Strip out anything in our blacklist
118 mypyc_targets = [x for x in all_real_pys if x not in MYPYC_BLACKLIST]
119 # Strip out any test code
120 mypyc_targets = [
121 x
122 for x in mypyc_targets
123 if not x.startswith(
124 (
125 os.path.join("mypy", "test") + os.sep,
126 os.path.join("mypyc", "test") + os.sep,
127 os.path.join("mypyc", "doc") + os.sep,
128 os.path.join("mypyc", "test-data") + os.sep,
129 )
130 )
131 ]
132 # ... and add back in the one test module we need
133 mypyc_targets.append(os.path.join("mypy", "test", "visitors.py"))
134
135 # The targets come out of file system apis in an unspecified
136 # order. Sort them so that the mypyc output is deterministic.
137 mypyc_targets.sort()
138
139 use_other_mypyc = os.getenv("ALTERNATE_MYPYC_PATH", None)
140 if use_other_mypyc:
141 # This bit is super unfortunate: we want to use a different
142 # mypy/mypyc version, but we've already imported parts, so we
143 # remove the modules that we've imported already, which will
144 # let the right versions be imported by mypyc.
145 del sys.modules["mypy"]
146 del sys.modules["mypy.version"]
147 del sys.modules["mypy.git"]
148 sys.path.insert(0, use_other_mypyc)
149
150 from mypyc.build import mypycify
151
152 opt_level = os.getenv("MYPYC_OPT_LEVEL", "3")
153 debug_level = os.getenv("MYPYC_DEBUG_LEVEL", "1")
154 force_multifile = os.getenv("MYPYC_MULTI_FILE", "") == "1"
155 log_trace = bool(int(os.getenv("MYPYC_LOG_TRACE", "0")))
156 separate = os.getenv("MYPYC_SEPARATE", "") == "1"
157 ext_modules = mypycify(
158 mypyc_targets + ["--config-file=mypy_bootstrap.ini"],
159 opt_level=opt_level,
160 debug_level=debug_level,
161 # Use multi-file compilation mode on windows because without it
162 # our Appveyor builds run out of memory sometimes.
163 multi_file=sys.platform == "win32" or force_multifile,
164 log_trace=log_trace,
165 separate=separate,
166 # Mypy itself is allowed to use native_internal extension.
167 depends_on_librt_internal=True,
•168 verbose=True,
169 )
170
171 else:
172 ext_modules = []
173
174 assert is_list_of_setuptools_extension(ext_modules), "Expected mypycify to use setuptools"
175
176 setup(version=version, ext_modules=ext_modules, cmdclass=cmdclass)