That last thread is quite lengthy now so, I'll sta...
# plugins
p
That last thread is quite lengthy now so, I'll start a new one. How do I include a specific file in a
Digest
? I can do
CreateDigest([FileContent(script_path, <string>)])
but how do I grab the contents of a separate file to add to
FileContent
? Can I just
open()
it within the
@rule
? Here's the input_digest in the plugin: https://github.com/st2sandbox/st2/blob/pants/pants-plugins/uses_services/platform.py#L30-L37 Here's the script that it needs to run: https://github.com/st2sandbox/st2/blob/pants/pants-plugins/uses_services/inspect_platform.py Note that the file to include is in the same directory as the plugin python file.
p
Hmm. Do I need to use
PathGlobs
?
h
Yes, unless you already have the Digest for that file Get(DigestContents, PathGlobs) or Get(DigestContents, Digest)
Don't use open() - it wouldn't catch changes to the file system
p
Copy code
script_path = "pants-plugins/uses_services/inspect_platform.py"
    script_digest = await Get(Digest, PathGlobs([script_path]))
like this?
h
Replace the Digest with DigestContents and then yes
That tells the engine to go from PathGlobs -> Digest -> DigestContents for you
p
I need the Digest to pass into VenvPexProcess
as input_digest
h
Ahh then yes, Digest is the correct thing to do
👍 1
I imagine you also want to error if the file is not found? Set GlobMatchErrorBehavior, under the PathGlobs docs
p
Copy code
script_path = "pants-plugins/uses_services/inspect_platform.py"
    script_digest = await Get(
        Digest,
        PathGlobs([script_path]),
        glob_match_error_behavior=GlobMatchErrorBehavior.error,
    )

    result = await Get(
        ProcessResult,
        VenvPexProcess(
            distro_pex,
            argv=[script_path],
            input_digest=script_digest,
            description=f"Introspecting platform (arch, os, distro)",
            # this can change from run to run, so don't cache results.
            cache_scope=ProcessCacheScope.PER_RESTART,  # NEVER?
            level=LogLevl.DEBUG,
        ),
    )
Now I just need to figure out how I want to ping mongo... The author of that script linked on StackOverflow didn't add a license to his repo 😞 and he hasn't responded to my inquiry.
e
Ok, I missed the new thread. Do note that PathGlobs will only work if this plugin stays housed in the one and only repo that will use it. You cannot do this for a plugin you will publish. You - currently - must use
open()
or
importlib.resources
or an embedded bytes literal.
p
Ok. I don't think this plugin needs to be published...
Sorry about the new thread.
e
No problem. Ok. You're fine then. You just have double invalidation now via pythonpath watching and the globs engine handling.
p
e
At high level - there is no better or worse here until you publish the plugin. On that day the globs breaks and the open continues to work (see caveats below). If you never plan on publishing, you were fine with globs. The caveat is that the current use of open assumes a CWD and thats a bit broken. you'd want something like
open(os.path.join(os.path.dirname(__file__), "inspect_platform.py"))
.
👍 1