Build a module via distutils and import it.
(source_files, config_code, module_name, **kw)
| 283 | |
| 284 | @_memoize |
| 285 | def build_module_distutils(source_files, config_code, module_name, **kw): |
| 286 | """ |
| 287 | Build a module via distutils and import it. |
| 288 | |
| 289 | """ |
| 290 | d = get_module_dir() |
| 291 | |
| 292 | # Copy files |
| 293 | dst_sources = [] |
| 294 | for fn in source_files: |
| 295 | if not os.path.isfile(fn): |
| 296 | raise RuntimeError("%s is not a file" % fn) |
| 297 | dst = os.path.join(d, os.path.basename(fn)) |
| 298 | shutil.copyfile(fn, dst) |
| 299 | dst_sources.append(dst) |
| 300 | |
| 301 | # Build script |
| 302 | config_code = textwrap.dedent(config_code).replace("\n", "\n ") |
| 303 | |
| 304 | code = fr""" |
| 305 | import os |
| 306 | import sys |
| 307 | sys.path = {repr(sys.path)} |
| 308 | |
| 309 | def configuration(parent_name='',top_path=None): |
| 310 | from numpy.distutils.misc_util import Configuration |
| 311 | config = Configuration('', parent_name, top_path) |
| 312 | {config_code} |
| 313 | return config |
| 314 | |
| 315 | if __name__ == "__main__": |
| 316 | from numpy.distutils.core import setup |
| 317 | setup(configuration=configuration) |
| 318 | """ |
| 319 | script = os.path.join(d, get_temp_module_name() + ".py") |
| 320 | dst_sources.append(script) |
| 321 | with open(script, "wb") as f: |
| 322 | f.write(code.encode('latin1')) |
| 323 | |
| 324 | # Build |
| 325 | cwd = os.getcwd() |
| 326 | try: |
| 327 | os.chdir(d) |
| 328 | cmd = [sys.executable, script, "build_ext", "-i"] |
| 329 | p = subprocess.Popen(cmd, |
| 330 | stdout=subprocess.PIPE, |
| 331 | stderr=subprocess.STDOUT) |
| 332 | out, err = p.communicate() |
| 333 | if p.returncode != 0: |
| 334 | raise RuntimeError("Running distutils build failed: %s\n%s" % |
| 335 | (cmd[4:], asstr(out))) |
| 336 | finally: |
| 337 | os.chdir(cwd) |
| 338 | |
| 339 | # Partial cleanup |
| 340 | for fn in dst_sources: |
| 341 | os.unlink(fn) |
| 342 |
nothing calls this directly
no test coverage detected