【问题标题】:Calling gcloud from bazel genrule从 bazel genrule 调用 gcloud
【发布时间】:2017-11-28 20:37:10
【问题描述】:

我在让 gcloud 在 Bazel genrule 中运行时遇到了一些问题。看起来像 python 路径相关的问题。

genrule(
    name="foo",
    outs=["bar"],
    srcs=[":bar.enc"],
    cmd="gcloud decrypt --location=global --keyring=foo --key=bar --plaintext-file $@ --ciphertext-file $(location bar.enc)"
)

例外是:

ImportError: No module named traceback

发件人:

 try:
    gcloud_main = _import_gcloud_main()
  except Exception as err:  # pylint: disable=broad-except
    # We want to catch *everything* here to display a nice message to the user
    # pylint:disable=g-import-not-at-top
    import traceback
    # We DON'T want to suggest `gcloud components reinstall` here (ex. as
    # opposed to the similar message in gcloud_main.py), as we know that no
    # commands will work.
    sys.stderr.write(
        ('ERROR: gcloud failed to load: {0}\n{1}\n\n'
         'This usually indicates corruption in your gcloud installation or '
         'problems with your Python interpreter.\n\n'
         'Please verify that the following is the path to a working Python 2.7 '
         'executable:\n'
         '    {2}\n\n'
         'If it is not, please set the CLOUDSDK_PYTHON environment variable to '
         'point to a working Python 2.7 executable.\n\n'
         'If you are still experiencing problems, please reinstall the Cloud '
         'SDK using the instructions here:\n'
         '    https://cloud.google.com/sdk/\n').format(
             err,
             '\n'.join(traceback.format_exc().splitlines()[2::2]),
             sys.executable))
    sys.exit(1)

我的问题是:

  • 如何最好地从 genrule 调用 gcloud?
  • 指定python路径需要哪些参数?
  • Bazel 如何阻止这一点?

更新: 可以通过指定CLOUDSDK_PYTHON 来运行它。

【问题讨论】:

    标签: python gcloud bazel


    【解决方案1】:

    确实,bazel runs in a sandbox,因此 gcloud 找不到它的依赖项。实际上,我很惊讶gcloud 可以被调用。

    要继续,我会将gcloud 包装在一个bazel py_binary 中,并在genrule 中使用tools 属性引用它。您还需要在cmd 中用location 包装它。最后,你会有

    genrule(
        name = "foo",
        outs = ["bar"],
        srcs = [":bar.enc"],
        cmd = "$(location //third_party/google/gcloud) decrypt --location=global --keyring=foo --key=bar --plaintext-file $@ --ciphertext-file $(location bar.enc)",
        tools = ["//third_party/google/gcloud"],
    )
    

    为此,您在 third_party/google/gcloud/BUILD 中定义(或您想要的任何地方,我只是使用了一条对我有意义的路径)

    py_binary(
        name = "gcloud",
        srcs = ["gcloud.py"],
        main = "gcloud.py",
        visibility = ["//visibility:public"],
        deps = [
            ":gcloud_sdk",
        ],
    )
    py_library(
      name = "gcloud_sdk",
      srcs = glob(
          ["**/*.py"],
          exclude = ["gcloud.py"],
          # maybe exclude tests and unrelated code, too.
      ),
      deps = [
        # Whatever extra deps are needed by gcloud to compile
      ]
    )
    

    【讨论】:

    • 这足以让大部分工作正常进行。 py_library 规则需要一些数据文件和额外的工作才能使路径正确。
    【解决方案2】:

    我遇到了类似的问题,运行此命令对我有用:

    export CLOUDSDK_PYTHON=/usr/bin/python
    

    (这在上面作为更新得到了回答,但我觉得将整个命令发布给未来的人)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多