15 import setuptools.command.sdist
17 DIR = os.path.abspath(os.path.dirname(__file__))
18 VERSION_REGEX = re.compile(
19 r"^\s*#\s*define\s+PYBIND11_VERSION_([A-Z]+)\s+(.*)$", re.MULTILINE
25 global_sdist = os.environ.get(
"PYBIND11_GLOBAL_SDIST",
False)
27 setup_py =
"tools/setup_global.py.in" if global_sdist
else "tools/setup_main.py.in" 28 extra_cmd =
'cmdclass["sdist"] = SDist\n' 31 (
"pyproject.toml",
"tools/pyproject.toml"),
32 (
"setup.py", setup_py),
36 with open(
"pybind11/_version.py")
as f:
37 code = compile(f.read(),
"pybind11/_version.py",
"exec")
40 version = loc[
"__version__"]
43 with open(
"include/pybind11/detail/common.h")
as f:
44 matches =
dict(VERSION_REGEX.findall(f.read()))
45 cpp_version =
"{MAJOR}.{MINOR}.{PATCH}".format(**matches)
46 if version != cpp_version:
47 msg =
"Python version {} does not match C++ version {}!".format(
50 raise RuntimeError(msg)
54 with open(filename,
"rb" if binary
else "r") as f: 58 return string.Template(contents.decode()).substitute(opts).encode()
60 return string.Template(contents).substitute(opts)
65 class SDist(setuptools.command.sdist.sdist):
67 setuptools.command.sdist.sdist.make_release_tree(self, base_dir, files)
69 for to, src
in to_src:
72 dest = os.path.join(base_dir, to)
76 with open(dest,
"wb")
as f:
81 @contextlib.contextmanager
83 "Prepare a temporary directory, cleanup when done" 85 tmpdir = tempfile.mkdtemp()
92 @contextlib.contextmanager
104 cmd = [
"cmake",
"-S",
".",
"-B", tmpdir] + [
105 "-DCMAKE_INSTALL_PREFIX=pybind11",
106 "-DBUILD_TESTING=OFF",
107 "-DPYBIND11_NOPYTHON=ON",
109 cmake_opts =
dict(cwd=DIR, stdout=sys.stdout, stderr=sys.stderr)
110 subprocess.check_call(cmd, **cmake_opts)
111 subprocess.check_call([
"cmake",
"--install", tmpdir], **cmake_opts)
114 code = compile(txt, setup_py,
"exec")
115 exec(code, {
"SDist": SDist})
def remove_output(sources)
def make_release_tree(self, base_dir, files)
def get_and_replace(filename, binary=False, opts)
void exec(str expr, object global=globals(), object local=object())