/ prelude / http_file.bzl
http_file.bzl
 1  # http_file rule implementation
 2  #
 3  # Extracted from buck2-prelude http_file.bzl
 4  # Stripped: materialization_test dependency, vpnless_urls
 5  
 6  load("@straylight_prelude//utils:expect.bzl", "expect")
 7  load("@straylight_prelude//utils:utils.bzl", "value_or")
 8  
 9  def http_file_shared(
10          actions: AnalysisActions,
11          name: str,
12          url: str,
13          is_executable: bool,
14          sha1: [None, str],
15          sha256: [None, str],
16          size_bytes: [None, int]) -> list[Provider]:
17      """Shared implementation for http_file downloads."""
18      output = actions.declare_output(name)
19      actions.download_file(
20          output,
21          url,
22          is_executable = is_executable,
23          sha1 = sha1,
24          sha256 = sha256,
25          size_bytes = size_bytes,
26      )
27  
28      providers = [DefaultInfo(default_output = output)]
29      if is_executable:
30          providers.append(RunInfo(args = [output]))
31      return providers
32  
33  def http_file_impl(ctx: AnalysisContext) -> list[Provider]:
34      """Implementation of the http_file build rule."""
35      expect(len(ctx.attrs.urls) == 1, "multiple `urls` not supported: {}", ctx.attrs.urls)
36      return http_file_shared(
37          ctx.actions,
38          name = value_or(ctx.attrs.out, ctx.label.name),
39          url = ctx.attrs.urls[0],
40          sha1 = ctx.attrs.sha1,
41          sha256 = ctx.attrs.sha256,
42          is_executable = ctx.attrs.executable or False,
43          size_bytes = ctx.attrs.size_bytes,
44      )