MCPcopy
hub / github.com/pex-tool/pex / _prepare_code

Method _prepare_code

pex/pex_builder.py:477–532  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

475 self._chroot.touch(compiled, label="bytecode")
476
477 def _prepare_code(self):
478 chroot_path = self._chroot.path()
479 self._pex_info.code_hash = CacheHelper.pex_code_hash(
480 chroot_path, exclude_dirs=(layout.BOOTSTRAP_DIR, layout.DEPS_DIR)
481 )
482 self._pex_info.pex_hash = hashlib.sha1(self._pex_info.dump().encode("utf-8")).hexdigest()
483 self._chroot.write(self._pex_info.dump().encode("utf-8"), PexInfo.PATH, label="manifest")
484
485 with open(os.path.join(_ABS_PEX_PACKAGE_DIR, "pex_boot.py")) as fp:
486 pex_boot = fp.read()
487
488 is_venv = self._pex_info.venv
489 hermetic_boot = (is_venv and self._pex_info.venv_hermetic_scripts) or (
490 not is_venv and self._pex_info.inherit_path is InheritPath.FALSE
491 )
492
493 pex_main = dedent(
494 """
495 result, should_exit, is_globals = boot(
496 bootstrap_dir={bootstrap_dir!r},
497 pex_root={pex_root!r},
498 pex_hash={pex_hash!r},
499 hermetic_boot={hermetic_boot!r},
500 has_interpreter_constraints={has_interpreter_constraints!r},
501 pex_path={pex_path!r},
502 is_venv={is_venv!r},
503 inject_python_args={inject_python_args!r},
504 )
505 if should_exit:
506 sys.exit(0 if is_globals else result)
507 elif is_globals:
508 globals().update(result)
509 """
510 ).format(
511 bootstrap_dir=self._pex_info.bootstrap,
512 pex_root=self._pex_info.raw_pex_root,
513 pex_hash=self._pex_info.pex_hash,
514 hermetic_boot=hermetic_boot,
515 has_interpreter_constraints=bool(self._pex_info.interpreter_constraints),
516 pex_path=self._pex_info.pex_path,
517 is_venv=is_venv,
518 inject_python_args=self._pex_info.inject_python_args,
519 )
520 bootstrap = pex_boot + "\n" + pex_main
521
522 self._chroot.write(
523 data=to_bytes(self._shebang + "\n" + self._preamble + "\n" + bootstrap),
524 dst="__main__.py",
525 executable=True,
526 label="main",
527 )
528 self._chroot.write(
529 data=to_bytes(bootstrap),
530 dst=os.path.join("__pex__", "__init__.py"),
531 label="importhook",
532 )
533
534 def _copy_or_link(

Callers 1

freezeMethod · 0.95

Calls 9

to_bytesFunction · 0.90
pex_code_hashMethod · 0.80
pathMethod · 0.45
hexdigestMethod · 0.45
encodeMethod · 0.45
dumpMethod · 0.45
writeMethod · 0.45
joinMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected