且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-03-31 09:48:15

一种方法是编写自己的Starlark规则,该规则返回

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. You'd then add the rule to the deps attribute of your py_library rule.

缺点是,您必须在加载阶段中知道(因此,在代码生成之前),您的工具将生成哪些文件,而不会与zips/jars作弊.

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.

如果这不是一种选择,那么我可以想到一个变通的解决方法,在该方法中,您可以生成依赖py_libarary数据部门的zip文件.然后,在实际导入文件之前,请在运行时将其解压缩.

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. You then unpack the file at runtime before you actually import it.

取决于您如何设置python规则,您也许还可以在代码生成器中构建python wheel并以此为依托.

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.