The documentation here mentions that the DigestEnt...
# plugins
f
The documentation here mentions that the DigestEntries object can be used to manipulate the paths in a digest, but it does not show how. https://www.pantsbuild.org/docs/rules-api-file-system#digestentries-light-weight-handles-to-files. Does anyone have an example of this? I want to shuffle some files around before adding them.
This is a good example from the external_tool module. So the idea is just to build a new digest with altered entries:
Copy code
@rule(level=LogLevel.DEBUG)
async def download_external_tool(request: ExternalToolRequest) -> DownloadedExternalTool:
    # Download and extract.
    maybe_archive_digest = await Get(Digest, DownloadFile, request.download_file_request)
    extracted_archive = await Get(ExtractedArchive, Digest, maybe_archive_digest)

    # Confirm executable.
    exe_path = request.exe.lstrip("./")
    digest = extracted_archive.digest
    is_not_executable = False
    digest_entries = []
    for entry in await Get(DigestEntries, Digest, digest):
        if isinstance(entry, FileEntry) and entry.path == exe_path and not entry.is_executable:
            # We should recreate the digest with the executable bit set.
            is_not_executable = True
            entry = dataclasses.replace(entry, is_executable=True)
        digest_entries.append(entry)
    if is_not_executable:
        digest = await Get(Digest, CreateDigest(digest_entries))

    return DownloadedExternalTool(digest, request.exe)
Hehe, source code digging comes in handy when working on this! 😄
g
Yeah, that's the primary way I do it. Find a backend that does a similar thing to what I need, and look at the structure. A lot of the OCI backend is built by looking at the golang one, especially how it does dispatch with enums.
f
That’s totally fine with me, though I am having some ideas along the way about how I could try to write about this in a way that would make it easier for the next person…
❤️ 1
😄
g
Do try! A few people have written guides, but the API plugin hasn't been very stable so they rot. 😛 By the way, how far away is your plugin from being MVP? 😄 I'd want to add it to this listing: https://tgolsson.github.io/awesome-pants/plugins/
❤️ 1
f
Woow, that’s amazing! I would love to make this plugin more official, but I need to clear this with my client for whom I am writing this first.
g
Sure, no worries. And if you don't want it there for other reasons that's OK too. Unless you put it on PyPi, then I'll treat it as fair game 😉
😆 1
f
I’ll let you know as soon as I get this properly open sourced 😉