Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_sources(builder, content):
with temporary_content(content) as project:
for path in content.keys():
builder.add_source(os.path.join(project, path), path)
with nested(temporary_dir(), temporary_dir()) as (root, cache):
pex_info1 = PexInfo.default()
pex_info1.zip_safe = False
pex1 = os.path.join(root, 'pex1.pex')
builder1 = PEXBuilder(interpreter=interpreter, pex_info=pex_info1)
add_requirements(builder1, cache)
add_wheel(builder1, content1)
add_sources(builder1, content2)
builder1.build(pex1)
pex_info2 = PexInfo.default()
pex_info2.pex_path = pex1
pex2 = os.path.join(root, 'pex2')
builder2 = PEXBuilder(path=pex2, interpreter=interpreter, pex_info=pex_info2)
add_requirements(builder2, cache)
add_wheel(builder2, content3)
builder2.set_script('foobaz')
builder2.freeze()
assert 42 == PEX(pex2, interpreter=interpreter).run(env=dict(PEX_VERBOSE='9'))
def pex_info(inherit_path):
pex_info = PexInfo.default()
pex_info.inherit_path = inherit_path
return pex_info
"`--mypy-version`, but also used the deprecated `--lint-mypy-version`.\nPlease use "
"only one of these (preferably `--mypy-version`)."
)
if task_version_configured:
return f"mypy=={self.get_options().version}"
return cast(str, self._mypy_subsystem.get_options().version)
mypy_version = get_mypy_version()
extras_hash = hash_utils.hash_all(hash_utils.hash_dir(Path(extra_pex.path()))
for extra_pex in extra_pexes)
path = Path(self.workdir, str(py3_interpreter.identity), f'{mypy_version}-{extras_hash}')
pex_dir = str(path)
if not path.is_dir():
mypy_requirement_pex = self.resolve_requirement_strings(py3_interpreter, [mypy_version])
pex_info = PexInfo.default()
pex_info.entry_point = 'pants_mypy_launcher'
with self.merged_pex(path=pex_dir,
pex_info=pex_info,
interpreter=py3_interpreter,
pexes=[mypy_requirement_pex, *extra_pexes]) as builder:
with temporary_file(binary_mode=False) as exe_fp:
# MyPy searches for types for a package in packages containing a `py.types` marker file
# or else in a sibling `-stubs` package as per PEP-0561. Going further than that
# PEP, MyPy restricts its search to `site-packages`. Since PEX deliberately isolates
# itself from `site-packages` as part of its raison d'etre, we monkey-patch
# `site.getsitepackages` to look inside the scrubbed PEX sys.path before handing off to
# `mypy`.
#
# See:
# https://mypy.readthedocs.io/en/stable/installed_packages.html#installed-packages
# https://www.python.org/dev/peps/pep-0561/#stub-only-packages
is specified, the current interpreter is used.
:keyword chroot: If specified, preexisting :class:`Chroot` to use for building the PEX.
:keyword pex_info: A preexisting PexInfo to use to build the PEX.
:keyword preamble: If supplied, execute this code prior to bootstrapping this PEX
environment.
:type preamble: str
:keyword copy: If False, attempt to create the pex environment via hard-linking, falling
back to copying across devices. If True, always copy.
.. versionchanged:: 0.8
The temporary directory created when ``path`` is not specified is now garbage collected on
interpreter exit.
"""
self._interpreter = interpreter or PythonInterpreter.get()
self._chroot = chroot or Chroot(path or safe_mkdtemp())
self._pex_info = pex_info or PexInfo.default(self._interpreter)
self._preamble = preamble or ''
self._copy = copy
self._shebang = self._interpreter.identity.hashbang()
self._logger = logging.getLogger(__name__)
self._frozen = False
self._distributions = set()
def nsutil_pex(self):
interpreter = self.context.products.get_data(PythonInterpreter)
chroot = os.path.join(self.workdir, 'nsutil', interpreter.version_string)
if not os.path.exists(chroot):
pex_info = PexInfo.default(interpreter=interpreter)
with safe_concurrent_creation(chroot) as scratch:
builder = PEXBuilder(path=scratch, interpreter=interpreter, pex_info=pex_info, copy=True)
with temporary_file(binary_mode=False) as fp:
declares_namespace_package_code = inspect.getsource(declares_namespace_package)
fp.write(textwrap.dedent("""
import sys
{declares_namespace_package_code}
if __name__ == '__main__':
for path in sys.argv[1:]:
if declares_namespace_package(path):
print(path)
""").strip().format(declares_namespace_package_code=declares_namespace_package_code))
def pexinfo(self):
info = PexInfo.default()
for repo in self.repositories:
info.add_repository(repo)
for index in self.indices:
info.add_index(index)
info.zip_safe = self.payload.zip_safe
info.always_write_cache = self.payload.always_write_cache
info.inherit_path = self.payload.inherit_path
info.entry_point = self.entry_point
info.ignore_errors = self.payload.ignore_errors
info.emit_warnings = self.payload.emit_warnings
return info