tmt icon indicating copy to clipboard operation
tmt copied to clipboard

Refine the expansion of the `$@` context

Open LecrisUT opened this issue 9 months ago • 0 comments

Right now it is quite a black magic on how and where $@ is expanded to the context values, e.g. it does not work in the environment field.

Could we not make this operation apply to all string, list of strings and values of string dicts? Probably this would be after the fmf expansion.

Ideally though, it would be better to expand the capability and syntax of this to allow for more sources to be expanded particularly context both env-var. CMake uses a language like $CACHE{var} and $ENV{var} and maybe we can do a similar expansion but with f-strings. E.g. it could look like this

  adjust:
    - when: repo is defined
      prepare+:
        - script: |
              cat > /etc/yum.repos.d/custom-repo.repo <<EOF
              [custom-repo]
-             baseurl=$@repo
+             baseurl={context:repo}
              gpgcheck=0
              EOF

Implementation wise, this would call str.format(context=ContextFormatter) on all strings to format and we use __format__ to expand the variables after {context:, basically

@dataclasses.dataclass
class ContextFormatter:
    tree: tmt.tree

    def get_context(ctx: str) -> str:
        return self.tree._fmf_context[ctx]

    def __format__(self, fmt: str) -> str:
        return self.get_context(fmt)

Credit:

  • __format__ approach I got it from: https://github.com/scikit-build/scikit-build-core/blob/b64c807f052dfab5cdc5ad4da0c285e58eaae7c7/src/scikit_build_core/format.py#L94-L113

LecrisUT avatar May 13 '25 16:05 LecrisUT