Пример #1
0
async def generate_python_from_protobuf(
    request: GeneratePythonFromProtobufRequest, protoc: Protoc
) -> GeneratedSources:
    download_protoc_request = Get[DownloadedExternalTool](
        ExternalToolRequest, protoc.get_request(Platform.current)
    )

    output_dir = "_generated_files"
    # TODO(#9650): replace this with a proper intrinsic to create empty directories.
    create_output_dir_request = Get[ProcessResult](
        Process(
            ("/bin/mkdir", output_dir),
            description=f"Create the directory {output_dir}",
            output_directories=(output_dir,),
        )
    )

    # Protoc needs all transitive dependencies on `protobuf_libraries` to work properly. It won't
    # actually generate those dependencies; it only needs to look at their .proto files to work
    # with imports.
    transitive_targets = await Get[TransitiveTargets](Addresses([request.protocol_target.address]))
    all_sources_request = Get[SourceFiles](
        AllSourceFilesRequest(
            (tgt.get(Sources) for tgt in transitive_targets.closure),
            for_sources_types=(ProtobufSources,),
            # NB: By stripping the source roots, we avoid having to set the value `--proto_path`
            # for Protobuf imports to be discoverable.
            strip_source_roots=True,
        )
    )
    stripped_target_sources_request = Get[SourceFiles](
        AllSourceFilesRequest([request.protocol_target[ProtobufSources]], strip_source_roots=True)
    )

    (
        downloaded_protoc_binary,
        create_output_dir_result,
        all_sources,
        stripped_target_sources,
    ) = await MultiGet(
        download_protoc_request,
        create_output_dir_request,
        all_sources_request,
        stripped_target_sources_request,
    )

    input_digest = await Get[Digest](
        MergeDigests(
            (
                all_sources.snapshot.digest,
                downloaded_protoc_binary.digest,
                create_output_dir_result.output_digest,
            )
        )
    )

    result = await Get[ProcessResult](
        Process(
            (
                downloaded_protoc_binary.exe,
                "--python_out",
                output_dir,
                *stripped_target_sources.snapshot.files,
            ),
            input_digest=input_digest,
            description=f"Generating Python sources from {request.protocol_target.address}.",
            output_directories=(output_dir,),
        )
    )
    normalized_snapshot = await Get[Snapshot](RemovePrefix(result.output_digest, output_dir))
    return GeneratedSources(normalized_snapshot)
Пример #2
0
 def subsystem_dependencies(cls):
     return super().subsystem_dependencies() + (Protoc.scoped(cls), )
Пример #3
0
 def protobuf_binary(self):
     return Protoc.scoped_instance(self).select(context=self.context)
Пример #4
0
 def subsystem_dependencies(cls):
   return super().subsystem_dependencies() + (Protoc.scoped(cls), GoDistribution,)
Пример #5
0
 def _protoc(self):
     return Protoc.scoped_instance(self).select(context=self.context)
Пример #6
0
 def protobuf_binary(self):
   return Protoc.scoped_instance(self).select(context=self.context)
Пример #7
0
 def subsystem_dependencies(cls):
   return super(ProtobufGen, cls).subsystem_dependencies() + (Protoc.scoped(cls),)
Пример #8
0
 def subsystem_dependencies(cls):
   return super(ProtocGenGo, cls).subsystem_dependencies() + (Protoc.scoped(cls), GoDistribution,)
Пример #9
0
 def subsystem_dependencies(cls):
     return super(GoProtobufGen, cls).subsystem_dependencies() + (
         Protoc.scoped(cls),
         ProtocGenGo,
     )
Пример #10
0
 def _protoc(self):
   return Protoc.scoped_instance(self).select(context=self.context)