简体   繁体   English

如何在自定义 Bazel 规则或 genrule 中 package 生成 python 文件?

[英]How to package generated python files in custom Bazel rule or genrule?

Let's say I have a scripts which generates Java and Python code.假设我有一个脚本可以生成 Java 和 Python 代码。 The files it generates depends on the input config, it can vary based on the config.它生成的文件取决于输入配置,它可能因配置而异。 Now, in the case of Java, I can write a simple genrule, point to invoke the script and then call jar command to generate a single source package of the generated Python files. Now, in the case of Java, I can write a simple genrule, point to invoke the script and then call jar command to generate a single source package of the generated Python files.

Eg:例如:

genrule(
    name = "generated_java_srcjar",
    srcs = glob([
        ":input-files"
    ]),
    outs = [
        "xsd.srcjar"
    ],
    cmd = """
        mkdir -p $(GENDIR)/generated-code
        $(location path/to/custom/script) --output-directory $(GENDIR)/generated-code --input $(locations :input-files)
        $(JAVABASE)/bin/jar cMf $@ -C $(GENDIR)/generated-code .
    """,
)

java_library(
    name = "generated_java_jar",
    srcs = [
        ":generated_java_srcjar"
    ],
)

In the Java example, to declare an output for the genrule, I'm able to generate a single output (source package of the generated files) and then use the same rule as a source in the java_library rule. In the Java example, to declare an output for the genrule, I'm able to generate a single output (source package of the generated files) and then use the same rule as a source in the java_library rule.

Now, I want to achieve something similar in Python.现在,我想在 Python 中实现类似的东西。 How can I package the generated Python files after invoking the custom script so that I can then pass it as a source to py_library rule?调用自定义脚本后,如何 package 生成 Python 文件,以便我可以将其作为源传递给py_library规则?

One way to do this is to write your own Starlark rule that returns a PyInfo provider with your generated files in the transitive_sources .一种方法是编写您自己的 Starlark 规则,该规则返回一个PyInfo 提供程序,其中包含您在transitive_sources中生成的文件。 You'd then add the rule to the deps attribute of your py_library rule.然后,您将该规则添加到您的 py_library 规则的deps属性中。

The drawback is that you have to know at loading phase (so before the code generation has happened) what files will be generated by your tool, no cheating with zips/jars.缺点是您必须在加载阶段(因此在代码生成发生之前)知道您的工具将生成哪些文件,而不是使用 zips/jars 作弊。

If that's not an option I could think of a hacky workaround where you produce a zip that you depend on in your py_libarary's data deps.如果这不是一个选项,我可以想到一个 hacky 解决方法,您可以生成一个 zip,您在 py_libarary 的数据部门中依赖它。 You then unpack the file at runtime before you actually import it.然后在实际导入之前在运行时解压文件。

Depending on how you've set up your python rules you might also be able to build a python wheel in your code generator and depend on that.根据您设置 python 规则的方式,您可能还可以在代码生成器中构建 python 轮并依赖于此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM