从bazel genrule打电话给gcloud

时间:2017-11-28 20:37:11

标签: python gcloud bazel

我有一些问题让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来运行它。

2 个答案:

答案 0 :(得分:2)

确实,bazel runs in a sandbox,因此gcloud找不到它的依赖关系。实际上,我可以完全调用gcloud

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

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
  ]
)

答案 1 :(得分:0)

我有一个类似的问题,为我运行此命令工作:

export CLOUDSDK_PYTHON=/usr/bin/python

(上面作为更新回答了这个问题,但我觉得要为未来的人们来这里发布整个命令)