test_files.py
Go to the documentation of this file.
1 import contextlib
2 import os
3 import string
4 import subprocess
5 import sys
6 import tarfile
7 import zipfile
8 
9 # These tests must be run explicitly
10 # They require CMake 3.15+ (--install)
11 
12 DIR = os.path.abspath(os.path.dirname(__file__))
13 MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
14 
15 
16 main_headers = {
17  "include/pybind11/attr.h",
18  "include/pybind11/buffer_info.h",
19  "include/pybind11/cast.h",
20  "include/pybind11/chrono.h",
21  "include/pybind11/common.h",
22  "include/pybind11/complex.h",
23  "include/pybind11/eigen.h",
24  "include/pybind11/embed.h",
25  "include/pybind11/eval.h",
26  "include/pybind11/functional.h",
27  "include/pybind11/gil.h",
28  "include/pybind11/iostream.h",
29  "include/pybind11/numpy.h",
30  "include/pybind11/operators.h",
31  "include/pybind11/options.h",
32  "include/pybind11/pybind11.h",
33  "include/pybind11/pytypes.h",
34  "include/pybind11/stl.h",
35  "include/pybind11/stl_bind.h",
36 }
37 
38 detail_headers = {
39  "include/pybind11/detail/class.h",
40  "include/pybind11/detail/common.h",
41  "include/pybind11/detail/descr.h",
42  "include/pybind11/detail/init.h",
43  "include/pybind11/detail/internals.h",
44  "include/pybind11/detail/type_caster_base.h",
45  "include/pybind11/detail/typeid.h",
46 }
47 
48 stl_headers = {
49  "include/pybind11/stl/filesystem.h",
50 }
51 
52 cmake_files = {
53  "share/cmake/pybind11/FindPythonLibsNew.cmake",
54  "share/cmake/pybind11/pybind11Common.cmake",
55  "share/cmake/pybind11/pybind11Config.cmake",
56  "share/cmake/pybind11/pybind11ConfigVersion.cmake",
57  "share/cmake/pybind11/pybind11NewTools.cmake",
58  "share/cmake/pybind11/pybind11Targets.cmake",
59  "share/cmake/pybind11/pybind11Tools.cmake",
60 }
61 
62 py_files = {
63  "__init__.py",
64  "__main__.py",
65  "_version.py",
66  "commands.py",
67  "py.typed",
68  "setup_helpers.py",
69 }
70 
71 headers = main_headers | detail_headers | stl_headers
72 src_files = headers | cmake_files
73 all_files = src_files | py_files
74 
75 
76 sdist_files = {
77  "pybind11",
78  "pybind11/include",
79  "pybind11/include/pybind11",
80  "pybind11/include/pybind11/detail",
81  "pybind11/include/pybind11/stl",
82  "pybind11/share",
83  "pybind11/share/cmake",
84  "pybind11/share/cmake/pybind11",
85  "pyproject.toml",
86  "setup.cfg",
87  "setup.py",
88  "LICENSE",
89  "MANIFEST.in",
90  "README.rst",
91  "PKG-INFO",
92 }
93 
94 local_sdist_files = {
95  ".egg-info",
96  ".egg-info/PKG-INFO",
97  ".egg-info/SOURCES.txt",
98  ".egg-info/dependency_links.txt",
99  ".egg-info/not-zip-safe",
100  ".egg-info/top_level.txt",
101 }
102 
103 
104 def test_build_sdist(monkeypatch, tmpdir):
105 
106  monkeypatch.chdir(MAIN_DIR)
107 
108  out = subprocess.check_output(
109  [
110  sys.executable,
111  "-m",
112  "build",
113  "--sdist",
114  "--outdir",
115  str(tmpdir),
116  ]
117  )
118  if hasattr(out, "decode"):
119  out = out.decode()
120 
121  (sdist,) = tmpdir.visit("*.tar.gz")
122 
123  with tarfile.open(str(sdist), "r:gz") as tar:
124  start = tar.getnames()[0] + "/"
125  version = start[9:-1]
126  simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
127 
128  with contextlib.closing(
129  tar.extractfile(tar.getmember(start + "setup.py"))
130  ) as f:
131  setup_py = f.read()
132 
133  with contextlib.closing(
134  tar.extractfile(tar.getmember(start + "pyproject.toml"))
135  ) as f:
136  pyproject_toml = f.read()
137 
138  with contextlib.closing(
139  tar.extractfile(
140  tar.getmember(
141  start + "pybind11/share/cmake/pybind11/pybind11Config.cmake"
142  )
143  )
144  ) as f:
145  contents = f.read().decode("utf8")
146  assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in contents
147 
148  files = {f"pybind11/{n}" for n in all_files}
149  files |= sdist_files
150  files |= {f"pybind11{n}" for n in local_sdist_files}
151  files.add("pybind11.egg-info/entry_points.txt")
152  files.add("pybind11.egg-info/requires.txt")
153  assert simpler == files
154 
155  with open(os.path.join(MAIN_DIR, "tools", "setup_main.py.in"), "rb") as f:
156  contents = (
157  string.Template(f.read().decode())
158  .substitute(version=version, extra_cmd="")
159  .encode()
160  )
161  assert setup_py == contents
162 
163  with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
164  contents = f.read()
165  assert pyproject_toml == contents
166 
167 
168 def test_build_global_dist(monkeypatch, tmpdir):
169 
170  monkeypatch.chdir(MAIN_DIR)
171  monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
172  out = subprocess.check_output(
173  [
174  sys.executable,
175  "-m",
176  "build",
177  "--sdist",
178  "--outdir",
179  str(tmpdir),
180  ]
181  )
182 
183  if hasattr(out, "decode"):
184  out = out.decode()
185 
186  (sdist,) = tmpdir.visit("*.tar.gz")
187 
188  with tarfile.open(str(sdist), "r:gz") as tar:
189  start = tar.getnames()[0] + "/"
190  version = start[16:-1]
191  simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
192 
193  with contextlib.closing(
194  tar.extractfile(tar.getmember(start + "setup.py"))
195  ) as f:
196  setup_py = f.read()
197 
198  with contextlib.closing(
199  tar.extractfile(tar.getmember(start + "pyproject.toml"))
200  ) as f:
201  pyproject_toml = f.read()
202 
203  files = {f"pybind11/{n}" for n in all_files}
204  files |= sdist_files
205  files |= {f"pybind11_global{n}" for n in local_sdist_files}
206  assert simpler == files
207 
208  with open(os.path.join(MAIN_DIR, "tools", "setup_global.py.in"), "rb") as f:
209  contents = (
210  string.Template(f.read().decode())
211  .substitute(version=version, extra_cmd="")
212  .encode()
213  )
214  assert setup_py == contents
215 
216  with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
217  contents = f.read()
218  assert pyproject_toml == contents
219 
220 
221 def tests_build_wheel(monkeypatch, tmpdir):
222  monkeypatch.chdir(MAIN_DIR)
223 
224  subprocess.check_output(
225  [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
226  )
227 
228  (wheel,) = tmpdir.visit("*.whl")
229 
230  files = {f"pybind11/{n}" for n in all_files}
231  files |= {
232  "dist-info/LICENSE",
233  "dist-info/METADATA",
234  "dist-info/RECORD",
235  "dist-info/WHEEL",
236  "dist-info/entry_points.txt",
237  "dist-info/top_level.txt",
238  }
239 
240  with zipfile.ZipFile(str(wheel)) as z:
241  names = z.namelist()
242 
243  trimmed = {n for n in names if "dist-info" not in n}
244  trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n}
245  assert files == trimmed
246 
247 
248 def tests_build_global_wheel(monkeypatch, tmpdir):
249  monkeypatch.chdir(MAIN_DIR)
250  monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
251 
252  subprocess.check_output(
253  [sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
254  )
255 
256  (wheel,) = tmpdir.visit("*.whl")
257 
258  files = {f"data/data/{n}" for n in src_files}
259  files |= {f"data/headers/{n[8:]}" for n in headers}
260  files |= {
261  "dist-info/LICENSE",
262  "dist-info/METADATA",
263  "dist-info/WHEEL",
264  "dist-info/top_level.txt",
265  "dist-info/RECORD",
266  }
267 
268  with zipfile.ZipFile(str(wheel)) as z:
269  names = z.namelist()
270 
271  beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0]
272  trimmed = {n[len(beginning) + 1 :] for n in names}
273 
274  assert files == trimmed
bool hasattr(handle obj, handle name)
Definition: pytypes.h:728
def test_build_global_dist(monkeypatch, tmpdir)
Definition: test_files.py:168
void split(const G &g, const PredecessorMap< KEY > &tree, G &Ab1, G &Ab2)
Definition: graph-inl.h:245
def tests_build_wheel(monkeypatch, tmpdir)
Definition: test_files.py:221
Definition: pytypes.h:1403
def test_build_sdist(monkeypatch, tmpdir)
Definition: test_files.py:104
def tests_build_global_wheel(monkeypatch, tmpdir)
Definition: test_files.py:248
IndexPair decode(Index ij)
int encode(Index i, Index j)
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2244


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45