async def create_python_repl_request(repl: PythonRepl, pex_env: PexEnvironment) -> ReplRequest: requirements_request = Get( Pex, PexFromTargetsRequest, PexFromTargetsRequest.for_requirements( (tgt.address for tgt in repl.targets), internal_only=True), ) sources_request = Get( PythonSourceFiles, PythonSourceFilesRequest(repl.targets, include_files=True)) requirements_pex, sources = await MultiGet(requirements_request, sources_request) merged_digest = await Get( Digest, MergeDigests( (requirements_pex.digest, sources.source_files.snapshot.digest))) args = pex_env.create_argv(repl.in_chroot(requirements_pex.name), python=requirements_pex.python) chrooted_source_roots = [repl.in_chroot(sr) for sr in sources.source_roots] extra_env = { **pex_env.environment_dict(python_configured=requirements_pex.python is not None), "PEX_EXTRA_SYS_PATH": ":".join(chrooted_source_roots), } return ReplRequest(digest=merged_digest, args=args, extra_env=extra_env)
async def create_mock_repl_request(repl: MockRepl) -> ReplRequest: digest = await Get(Digest, CreateDigest([FileContent("repl.sh", b"exit 0")])) return ReplRequest( digest=digest, args=("/bin/bash", "repl.sh"), run_in_workspace=False, )
async def create_ipython_repl_request(repl: IPythonRepl, ipython: IPython, pex_env: PexEnvironment) -> ReplRequest: # Note that we get an intermediate PexRequest here (instead of going straight to a Pex) # so that we can get the interpreter constraints for use in ipython_request. requirements_pex_request = await Get( PexRequest, PexFromTargetsRequest, PexFromTargetsRequest.for_requirements( (tgt.address for tgt in repl.targets), internal_only=True), ) requirements_request = Get(Pex, PexRequest, requirements_pex_request) sources_request = Get( PythonSourceFiles, PythonSourceFilesRequest(repl.targets, include_files=True)) ipython_request = Get( Pex, PexRequest( output_filename="ipython.pex", main=ipython.main, requirements=PexRequirements(ipython.all_requirements), interpreter_constraints=requirements_pex_request. interpreter_constraints, internal_only=True, ), ) requirements_pex, sources, ipython_pex = await MultiGet( requirements_request, sources_request, ipython_request) merged_digest = await Get( Digest, MergeDigests( (requirements_pex.digest, sources.source_files.snapshot.digest, ipython_pex.digest)), ) args = list( pex_env.create_argv(repl.in_chroot(ipython_pex.name), python=ipython_pex.python)) if ipython.options.ignore_cwd: args.append("--ignore-cwd") chrooted_source_roots = [repl.in_chroot(sr) for sr in sources.source_roots] extra_env = { **pex_env.environment_dict(python_configured=ipython_pex.python is not None), "PEX_PATH": repl.in_chroot(requirements_pex_request.output_filename), "PEX_EXTRA_SYS_PATH": ":".join(chrooted_source_roots), } return ReplRequest(digest=merged_digest, args=args, extra_env=extra_env)
async def create_python_repl_request(request: PythonRepl, pex_env: PexEnvironment, python_setup: PythonSetup) -> ReplRequest: validate_compatible_resolve(request.targets, python_setup) interpreter_constraints, transitive_targets = await MultiGet( Get(InterpreterConstraints, InterpreterConstraintsRequest(request.addresses)), Get(TransitiveTargets, TransitiveTargetsRequest(request.addresses)), ) requirements_request = Get(Pex, RequirementsPexRequest(request.addresses)) local_dists_request = Get( LocalDistsPex, LocalDistsPexRequest( request.addresses, internal_only=True, interpreter_constraints=interpreter_constraints, ), ) sources_request = Get( PythonSourceFiles, PythonSourceFilesRequest(transitive_targets.closure, include_files=True)) requirements_pex, local_dists, sources = await MultiGet( requirements_request, local_dists_request, sources_request) merged_digest = await Get( Digest, MergeDigests((requirements_pex.digest, local_dists.pex.digest, sources.source_files.snapshot.digest)), ) complete_pex_env = pex_env.in_workspace() args = complete_pex_env.create_argv(request.in_chroot( requirements_pex.name), python=requirements_pex.python) chrooted_source_roots = [ request.in_chroot(sr) for sr in sources.source_roots ] extra_env = { **complete_pex_env.environment_dict(python_configured=requirements_pex.python is not None), "PEX_EXTRA_SYS_PATH": ":".join(chrooted_source_roots), "PEX_PATH": request.in_chroot(local_dists.pex.name), } return ReplRequest(digest=merged_digest, args=args, extra_env=extra_env)
async def create_ipython_repl_request(repl: IPythonRepl, ipython: IPython) -> ReplRequest: # Note that we get an intermediate PexRequest here (instead of going straight to a Pex) # so that we can get the interpreter constraints for use in ipython_request. requirements_pex_request = await Get( PexRequest, PexFromTargetsRequest, PexFromTargetsRequest.for_requirements(Addresses( tgt.address for tgt in repl.targets), internal_only=True), ) requirements_request = Get(Pex, PexRequest, requirements_pex_request) sources_request = Get( PythonSourceFiles, PythonSourceFilesRequest(repl.targets, include_files=True)) ipython_request = Get( Pex, PexRequest( output_filename="ipython.pex", entry_point=ipython.entry_point, requirements=PexRequirements(ipython.all_requirements), interpreter_constraints=requirements_pex_request. interpreter_constraints, internal_only=True, ), ) requirements_pex, sources, ipython_pex = await MultiGet( requirements_request, sources_request, ipython_request) merged_digest = await Get( Digest, MergeDigests( (requirements_pex.digest, sources.source_files.snapshot.digest, ipython_pex.digest)), ) chrooted_source_roots = [repl.in_chroot(sr) for sr in sources.source_roots] args: Tuple[str, ...] = (repl.in_chroot(ipython_pex.name), ) if ipython.options.ignore_cwd: args = args + ("--ignore-cwd", ) return ReplRequest( digest=merged_digest, args=args, env={ "PEX_PATH": repl.in_chroot(requirements_pex_request.output_filename), "PEX_EXTRA_SYS_PATH": ":".join(chrooted_source_roots), }, )
async def create_python_repl_request(repl: PythonRepl, pex_env: PexEnvironment) -> ReplRequest: # Note that we get an intermediate PexRequest here (instead of going straight to a Pex) so # that we can get the interpreter constraints for use in local_dists_request. requirements_pex_request = await Get( PexRequest, PexFromTargetsRequest, PexFromTargetsRequest.for_requirements( (tgt.address for tgt in repl.targets), internal_only=True), ) requirements_request = Get(Pex, PexRequest, requirements_pex_request) local_dists_request = Get( LocalDistsPex, LocalDistsPexRequest( Addresses(tgt.address for tgt in repl.targets), internal_only=True, interpreter_constraints=requirements_pex_request. interpreter_constraints, ), ) sources_request = Get( PythonSourceFiles, PythonSourceFilesRequest(repl.targets, include_files=True)) requirements_pex, local_dists, sources = await MultiGet( requirements_request, local_dists_request, sources_request) merged_digest = await Get( Digest, MergeDigests((requirements_pex.digest, local_dists.pex.digest, sources.source_files.snapshot.digest)), ) complete_pex_env = pex_env.in_workspace() args = complete_pex_env.create_argv(repl.in_chroot(requirements_pex.name), python=requirements_pex.python) chrooted_source_roots = [repl.in_chroot(sr) for sr in sources.source_roots] extra_env = { **complete_pex_env.environment_dict(python_configured=requirements_pex.python is not None), "PEX_EXTRA_SYS_PATH": ":".join(chrooted_source_roots), "PEX_PATH": repl.in_chroot(local_dists.pex.name), } return ReplRequest(digest=merged_digest, args=args, extra_env=extra_env)
async def create_python_repl_request(repl: PythonRepl) -> ReplRequest: pex_request = Get( Pex, PexFromTargetsRequest( (tgt.address for tgt in repl.targets), output_filename="python.pex", include_source_files=False, ), ) sources_request = Get(PythonSourceFiles, PythonSourceFilesRequest(repl.targets)) pex, sources = await MultiGet(pex_request, sources_request) merged_digest = await Get( Digest, MergeDigests((pex.digest, sources.source_files.snapshot.digest)) ) return ReplRequest( digest=merged_digest, binary_name=pex.output_filename, env={"PEX_EXTRA_SYS_PATH": ":".join(sources.source_roots)}, )
async def create_bash_repl_request(repl: BashRepl, bash_setup: BashSetup, bash_program: BashProgram) -> ReplRequest: # Normally, we would need to install a Repl program, such as Ammonite for Scala. For Bash, we # simply run the program found by our rule that returns `BashProgram`, which uses # `BinaryPaths`. See https://www.pantsbuild.org/v2.0/docs/rules-api-installing-tools. # `repl.targets` already includes the transitive closure of the input targets. We filter out # irrelevant soures. sources = await Get( SourceFiles, SourceFilesRequest( (tgt.get(Sources) for tgt in repl.targets), for_sources_types=(BashSources, FilesSources, ResourcesSources), ), ) return ReplRequest( digest=sources.snapshot.digest, args=(bash_program.exe, ), extra_env=bash_setup.env_dict, )
async def create_ipython_repl_request(repl: IPythonRepl, ipython: IPython) -> ReplRequest: pex_request = Get( Pex, PexFromTargetsRequest( (tgt.address for tgt in repl.targets), output_filename="ipython.pex", entry_point=ipython.entry_point, additional_requirements=ipython.all_requirements, include_source_files=True, ), ) sources_request = Get(PythonSourceFiles, PythonSourceFilesRequest(repl.targets)) pex, sources = await MultiGet(pex_request, sources_request) merged_digest = await Get( Digest, MergeDigests((pex.digest, sources.source_files.snapshot.digest)) ) return ReplRequest( digest=merged_digest, binary_name=pex.output_filename, env={"PEX_EXTRA_SYS_PATH": ":".join(sources.source_roots)}, )
async def create_python_repl_request(repl: PythonRepl) -> ReplRequest: requirements_request = Get( Pex, PexFromTargetsRequest, PexFromTargetsRequest.for_requirements(Addresses( tgt.address for tgt in repl.targets), internal_only=True), ) sources_request = Get( PythonSourceFiles, PythonSourceFilesRequest(repl.targets, include_files=True)) requirements_pex, sources = await MultiGet(requirements_request, sources_request) merged_digest = await Get( Digest, MergeDigests( (requirements_pex.digest, sources.source_files.snapshot.digest))) chrooted_source_roots = [repl.in_chroot(sr) for sr in sources.source_roots] return ReplRequest( digest=merged_digest, args=(repl.in_chroot(requirements_pex.name), ), env={"PEX_EXTRA_SYS_PATH": ":".join(chrooted_source_roots)}, )
async def create_scala_repl_request( request: ScalaRepl, bash: BashBinary, scala_subsystem: ScalaSubsystem) -> ReplRequest: user_classpath = await Get(Classpath, Addresses, request.addresses) roots = await Get(CoarsenedTargets, Addresses, request.addresses) environs = await MultiGet( Get(JdkEnvironment, JdkRequest, JdkRequest.from_target(target)) for target in roots) jdk = max(environs, key=lambda j: j.jre_major_version) scala_version = scala_subsystem.version_for_resolve( user_classpath.resolve.name) tool_classpath = await Get( ToolClasspath, ToolClasspathRequest( prefix="__toolcp", artifact_requirements=ArtifactRequirements.from_coordinates([ Coordinate( group="org.scala-lang", artifact="scala-compiler", version=scala_version, ), Coordinate( group="org.scala-lang", artifact="scala-library", version=scala_version, ), Coordinate( group="org.scala-lang", artifact="scala-reflect", version=scala_version, ), ]), ), ) user_classpath_prefix = "__cp" prefixed_user_classpath = await MultiGet( Get(Digest, AddPrefix(d, user_classpath_prefix)) for d in user_classpath.digests()) # TODO: Manually merging the `immutable_input_digests` since InteractiveProcess doesn't # support them yet. See https://github.com/pantsbuild/pants/issues/13852. jdk_digests = await MultiGet( Get(Digest, AddPrefix(digest, relpath)) for relpath, digest in jdk.immutable_input_digests.items()) repl_digest = await Get( Digest, MergeDigests([ *prefixed_user_classpath, tool_classpath.content.digest, *jdk_digests ]), ) return ReplRequest( digest=repl_digest, args=[ *jdk.args(bash, tool_classpath.classpath_entries()), "-Dscala.usejavacp=true", "scala.tools.nsc.MainGenericRunner", "-classpath", ":".join(user_classpath.args(prefix=user_classpath_prefix)), ], extra_env={ **jdk.env, "PANTS_INTERNAL_ABSOLUTE_PREFIX": "", }, run_in_workspace=False, append_only_caches=jdk.append_only_caches, )
async def create_mock_repl_request(repl: MockRepl) -> ReplRequest: digest = await Get(Digest, CreateDigest([FileContent("repl.sh", b"exit 0")])) return ReplRequest(digest=digest, args=("/bin/bash", repl.in_chroot("repl.sh")))