async def create_python_binary( python_binary_adaptor: PythonBinaryAdaptor) -> CreatedBinary: #TODO(#8420) This way of calculating the entry point works but is a bit hackish. entry_point = None if hasattr(python_binary_adaptor, 'entry_point'): entry_point = python_binary_adaptor.entry_point else: sources_snapshot = python_binary_adaptor.sources.snapshot if len(sources_snapshot.files) == 1: target = await Get[HydratedTarget](Address, python_binary_adaptor.address) output = await Get[SourceRootStrippedSources](HydratedTarget, target) root_filename = output.snapshot.files[0] entry_point = PythonBinary.translate_source_path_to_py_module_specifier( root_filename) request = CreatePexFromTargetClosure( build_file_addresses=BuildFileAddresses( (python_binary_adaptor.address, )), entry_point=entry_point, output_filename=f'{python_binary_adaptor.address.target_name}.pex') pex = await Get[Pex](CreatePexFromTargetClosure, request) return CreatedBinary(digest=pex.directory_digest, binary_name=pex.output_filename)
async def create_python_binary( config: PythonBinaryConfiguration) -> CreatedBinary: entry_point: Optional[str] if config.entry_point.value is not None: entry_point = config.entry_point.value else: source_files = await Get[SourceFiles](AllSourceFilesRequest( [config.sources], strip_source_roots=True)) # NB: `PythonBinarySources` enforces that we have 0-1 sources. if len(source_files.files) == 1: module_name = source_files.files[0] entry_point = PythonBinary.translate_source_path_to_py_module_specifier( module_name) else: entry_point = None output_filename = f"{config.address.target_name}.pex" two_step_pex = await Get[TwoStepPex](TwoStepPexFromTargetsRequest( PexFromTargetsRequest( addresses=Addresses([config.address]), entry_point=entry_point, platforms=PexPlatforms.create_from_platforms_field( config.platforms), output_filename=output_filename, additional_args=config.generate_additional_args(), ))) pex = two_step_pex.pex return CreatedBinary(digest=pex.directory_digest, binary_name=pex.output_filename)
async def create_python_binary( python_binary_adaptor: PythonBinaryAdaptor) -> CreatedBinary: # TODO(#8420) This way of calculating the entry point works but is a bit hackish. if hasattr(python_binary_adaptor, "entry_point"): entry_point = python_binary_adaptor.entry_point else: sources = await Get[SourceFiles](AllSourceFilesRequest( [python_binary_adaptor], strip_source_roots=True)) # NB: `python_binary` targets may have 0-1 sources. This is enforced by # `PythonBinaryAdaptor`. if len(sources.snapshot.files) == 1: module_name = sources.snapshot.files[0] entry_point = PythonBinary.translate_source_path_to_py_module_specifier( module_name) else: entry_point = None request = CreatePexFromTargetClosure( addresses=Addresses((python_binary_adaptor.address, )), entry_point=entry_point, output_filename=f"{python_binary_adaptor.address.target_name}.pex", ) pex = await Get[Pex](CreatePexFromTargetClosure, request) return CreatedBinary(digest=pex.directory_digest, binary_name=pex.output_filename)
async def create_python_binary(fields: PythonBinaryFields) -> CreatedBinary: entry_point: Optional[str] if fields.entry_point.value is not None: entry_point = fields.entry_point.value else: # TODO: rework determine_source_files.py to work with the Target API. It should take the # Sources AsyncField as input, rather than TargetAdaptor. sources_result = await Get[SourcesResult](SourcesRequest, fields.sources.request) stripped_sources = await Get[SourceRootStrippedSources]( StripSnapshotRequest(sources_result.snapshot)) source_files = stripped_sources.snapshot.files # NB: `PythonBinarySources` enforces that we have 0-1 sources. if len(source_files) == 1: module_name = source_files[0] entry_point = PythonBinary.translate_source_path_to_py_module_specifier( module_name) else: entry_point = None request = CreatePexFromTargetClosure( addresses=Addresses([fields.address]), entry_point=entry_point, output_filename=f"{fields.address.target_name}.pex", ) pex = await Get[Pex](CreatePexFromTargetClosure, request) return CreatedBinary(digest=pex.directory_digest, binary_name=pex.output_filename)
def create_python_binary(python_binary_adaptor: PythonBinaryAdaptor, python_setup: PythonSetup) -> CreatedBinary: transitive_hydrated_targets = yield Get( TransitiveHydratedTargets, BuildFileAddresses((python_binary_adaptor.address, ))) all_targets = transitive_hydrated_targets.closure all_target_adaptors = [t.adaptor for t in all_targets] interpreter_constraints = PexInterpreterConstraints.create_from_adaptors( adaptors=tuple(all_targets), python_setup=python_setup) source_root_stripped_sources = yield [ Get(SourceRootStrippedSources, HydratedTarget, target_adaptor) for target_adaptor in all_targets ] #TODO(#8420) This way of calculating the entry point works but is a bit hackish. entry_point = None if hasattr(python_binary_adaptor, 'entry_point'): entry_point = python_binary_adaptor.entry_point else: sources_snapshot = python_binary_adaptor.sources.snapshot if len(sources_snapshot.files) == 1: target = transitive_hydrated_targets.roots[0] output = yield Get(SourceRootStrippedSources, HydratedTarget, target) root_filename = output.snapshot.files[0] entry_point = PythonBinary.translate_source_path_to_py_module_specifier( root_filename) stripped_sources_digests = [ stripped_sources.snapshot.directory_digest for stripped_sources in source_root_stripped_sources ] sources_digest = yield Get( Digest, DirectoriesToMerge(directories=tuple(stripped_sources_digests))) inits_digest = yield Get(InjectedInitDigest, Digest, sources_digest) all_input_digests = [sources_digest, inits_digest.directory_digest] merged_input_files = yield Get( Digest, DirectoriesToMerge, DirectoriesToMerge(directories=tuple(all_input_digests))) requirements = PexRequirements.create_from_adaptors(all_target_adaptors) output_filename = f"{python_binary_adaptor.address.target_name}.pex" create_requirements_pex = CreatePex( output_filename=output_filename, requirements=requirements, interpreter_constraints=interpreter_constraints, entry_point=entry_point, input_files_digest=merged_input_files, ) pex = yield Get(Pex, CreatePex, create_requirements_pex) yield CreatedBinary(digest=pex.directory_digest, binary_name=pex.output_filename)
class PythonBinaryTest(TestBase): @classmethod def alias_groups(cls): return BuildFileAliases(targets={"python_binary": PythonBinary}) subsystems = PythonBinary.subsystems() def test_python_binary(self): # Set up and run self.create_file("some/path/to/python/path/to/py.py") self.add_to_build_file( "some/path/to/python", 'python_binary(name = "binary", source = "path/to/py.py")\n', ) target = self.target("some/path/to/python:binary") # Verify self.assertTrue(isinstance(target, PythonBinary))
def build_file_aliases(): return BuildFileAliases(targets={ PythonBinary.alias(): PythonBinary, PythonLibrary.alias(): PythonLibrary, PythonTests.alias(): PythonTests, PythonDistribution.alias(): PythonDistribution, 'python_requirement_library': PythonRequirementLibrary, Resources.alias(): Resources, }, objects={ 'python_requirement': PythonRequirement, 'python_artifact': PythonArtifact, 'setup_py': PythonArtifact, }, context_aware_object_factories={ 'python_requirements': PythonRequirements, 'pants_requirement': PantsRequirement, })
def build_file_aliases(): return BuildFileAliases( targets={ PythonBinary.alias(): TargetMacro.Factory.wrap(PythonBinary.create, PythonBinary), PythonLibrary.alias(): TargetMacro.Factory.wrap(PythonLibrary.create, PythonLibrary), PythonTests.alias(): TargetMacro.Factory.wrap(PythonTests.create, PythonTests), 'python_requirement_library': PythonRequirementLibrary, Resources.alias(): Resources, }, objects={ 'python_requirement': PythonRequirement, 'python_artifact': PythonArtifact, 'setup_py': PythonArtifact, }, context_aware_object_factories={ 'python_requirements': PythonRequirements, 'pants_requirement': PantsRequirement, } )
def build_file_aliases(): return BuildFileAliases( targets={ PythonBinary.alias(): PythonBinary, PythonLibrary.alias(): PythonLibrary, PythonTests.alias(): PythonTests, PythonDistribution.alias(): PythonDistribution, 'python_requirement_library': PythonRequirementLibrary, Resources.alias(): Resources, }, objects={ 'python_requirement': PythonRequirement, 'python_artifact': PythonArtifact, 'setup_py': PythonArtifact, }, context_aware_object_factories={ 'python_requirements': PythonRequirements, 'pants_requirement': PantsRequirement, } )
def build_file_aliases(): return BuildFileAliases( targets={ PythonAppV1.alias(): PythonAppV1, PythonBinaryV1.alias(): PythonBinaryV1, PythonLibraryV1.alias(): PythonLibraryV1, PythonTestsV1.alias(): PythonTestsV1, PythonDistributionV1.alias(): PythonDistributionV1, "python_requirement_library": PythonRequirementLibraryV1, PythonRequirementsFileV1.alias(): PythonRequirementsFileV1, UnpackedWheelsV1.alias(): UnpackedWheelsV1, }, objects={ "python_requirement": PythonRequirement, "python_artifact": PythonArtifact, "setup_py": PythonArtifact, }, context_aware_object_factories={ "python_requirements": PythonRequirements, PantsRequirement.alias: PantsRequirement, }, )
def build_file_aliases(): return BuildFileAliases(targets={ PythonBinary.alias(): TargetMacro.Factory.wrap(PythonBinary.create, PythonBinary), PythonLibrary.alias(): TargetMacro.Factory.wrap(PythonLibrary.create, PythonLibrary), PythonTests.alias(): TargetMacro.Factory.wrap(PythonTests.create, PythonTests), 'python_requirement_library': PythonRequirementLibrary, Resources.alias(): Resources, }, objects={ 'python_requirement': PythonRequirement, 'python_artifact': PythonArtifact, 'setup_py': PythonArtifact, }, context_aware_object_factories={ 'python_requirements': PythonRequirements, 'pants_requirement': PantsRequirement, })