workspace_mirror.bzl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2018 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. prefix = "https://storage.googleapis.com/k8s-bazel-cache/"
  15. def mirror(url):
  16. """Try downloading a URL from a GCS mirror first, then from the original.
  17. Update the GCS bucket using bazel run //hack:update-mirror"""
  18. return [prefix + url, url]
  19. def mirror_urls():
  20. # This function only gives proper results when executed from WORKSPACE,
  21. # but the data is needed in sh_binary, which can only be in a BUILD file.
  22. # Thus, it is be exported by a repository_rule (which executes in WORKSPACE)
  23. # to be used by the sh_binary.
  24. urls = []
  25. for k, v in native.existing_rules().items():
  26. us = list(v.get("urls", []))
  27. if "url" in v:
  28. us.append(v["url"])
  29. for u in us:
  30. if u and not u.startswith(prefix):
  31. urls.append(u)
  32. return sorted(urls)
  33. def export_urls_impl(repo_ctx):
  34. repo_ctx.file(repo_ctx.path("BUILD.bazel"), """
  35. exports_files(glob(["**"]), visibility=["//visibility:public"])
  36. """)
  37. repo_ctx.file(
  38. repo_ctx.path("urls.txt"),
  39. # Add a trailing newline, since the "while read" loop needs it
  40. content = ("\n".join(repo_ctx.attr.urls) + "\n"),
  41. )
  42. _export_urls = repository_rule(
  43. attrs = {
  44. "urls": attr.string_list(mandatory = True),
  45. },
  46. local = True,
  47. implementation = export_urls_impl,
  48. )
  49. def export_urls(name):
  50. return _export_urls(name = name, urls = mirror_urls())