def test_from_env(): # type: () -> None with temporary_dir() as td: pex_root = os.path.realpath(os.path.join(td, "pex_root")) environ = dict( PEX_ROOT=pex_root, PEX_MODULE="entry:point", PEX_SCRIPT="script.sh", PEX_FORCE_LOCAL="true", PEX_UNZIP="true", PEX_INHERIT_PATH="prefer", PEX_IGNORE_ERRORS="true", PEX_ALWAYS_CACHE="true", ) info = dict( pex_root=pex_root, entry_point="entry:point", script="script.sh", zip_safe=False, unzip=True, inherit_path=True, ignore_errors=True, always_write_cache=True, ) assert_same_info(PexInfo(info=info), PexInfo.from_env(env=Variables(environ=environ)))
def test_from_env(): pex_root = os.path.realpath('/pex_root') environ = dict(PEX_ROOT=pex_root, PEX_MODULE='entry:point', PEX_SCRIPT='script.sh', PEX_FORCE_LOCAL='true', PEX_INHERIT_PATH='true', PEX_IGNORE_ERRORS='true', PEX_ALWAYS_CACHE='true') info = dict(pex_root=pex_root, entry_point='entry:point', script='script.sh', zip_safe=False, inherit_path=True, ignore_errors=True, always_write_cache=True) assert_same_info(PexInfo(info=info), PexInfo.from_env(env=Variables(environ=environ)))
def test_from_empty_env(): environ = Variables(environ={}) info = {} assert_same_info(PexInfo(info=info), PexInfo.from_env(env=environ))
def make_pex_info(requirements): return PexInfo(info={'requirements': requirements})
def _compile_target(self, vt): """'Compiles' a python target. 'Compiling' means forming an isolated chroot of its sources and transitive deps and then attempting to import each of the target's sources in the case of a python library or else the entry point in the case of a python binary. For a library with sources lib/core.py and lib/util.py a "compiler" main file would look like: if __name__ == '__main__': import lib.core import lib.util For a binary with entry point lib.bin:main the "compiler" main file would look like: if __name__ == '__main__': from lib.bin import main In either case the main file is executed within the target chroot to reveal missing BUILD dependencies. """ target = vt.target with self.context.new_workunit(name=target.address.spec): modules = self._get_modules(target) if not modules: # Nothing to eval, so a trivial compile success. return 0 interpreter = self._get_interpreter_for_target_closure(target) reqs_pex = self._resolve_requirements_for_versioned_target_closure( interpreter, vt) srcs_pex = self._source_pex_for_versioned_target_closure( interpreter, vt) # Create the executable pex. exec_pex_parent = os.path.join(self.workdir, 'executable_pex') executable_file_content = self._get_executable_file_content( exec_pex_parent, modules) hasher = hashlib.sha1() hasher.update(executable_file_content) exec_file_hash = hasher.hexdigest() exec_pex_path = os.path.realpath( os.path.join(exec_pex_parent, exec_file_hash)) if not os.path.isdir(exec_pex_path): with safe_concurrent_creation(exec_pex_path) as safe_path: # Write the entry point. safe_mkdir(safe_path) with open( os.path.join(safe_path, '{}.py'.format(self._EXEC_NAME)), 'w') as outfile: outfile.write(executable_file_content) pex_info = (target.pexinfo if isinstance( target, PythonBinary) else None) or PexInfo() # Override any user-specified entry point, under the assumption that the # executable_file_content does what the user intends (including, probably, calling that # underlying entry point). pex_info.entry_point = self._EXEC_NAME builder = PEXBuilder(safe_path, interpreter, pex_info=pex_info) builder.freeze() exec_pex = PEX(exec_pex_path, interpreter) extra_pex_paths = [ pex.path() for pex in [_f for _f in [reqs_pex, srcs_pex] if _f] ] pex = WrappedPEX(exec_pex, extra_pex_paths) with self.context.new_workunit( name='eval', labels=[ WorkUnitLabel.COMPILER, WorkUnitLabel.RUN, WorkUnitLabel.TOOL ], cmd=' '.join(exec_pex.cmdline())) as workunit: returncode = pex.run(stdout=workunit.output('stdout'), stderr=workunit.output('stderr')) workunit.set_outcome(WorkUnit.SUCCESS if returncode == 0 else WorkUnit.FAILURE) if returncode != 0: self.context.log.error('Failed to eval {}'.format( target.address.spec)) return returncode
def _from_pex(arg): if arg == f'{home_path}/myapp.pex': return PexInfo({"code_hash": 1}) else: return PexInfo({"code_hash": 2})
def test_from_empty_env(): # type: () -> None environ = Variables(environ={}) info = {} # type: Dict assert_same_info(PexInfo(info=info), PexInfo.from_env(env=environ))
def make_pex_info(requirements): # type: (List[Text]) -> PexInfo return PexInfo(info={"requirements": requirements})
def make_pex_info(requirements): return PexInfo(info={"requirements": requirements})