|
| 1 | +def _smudge(repository_ctx, srcs): |
| 2 | + for src in srcs: |
| 3 | + repository_ctx.watch(src) |
| 4 | + script = Label("//misc/bazel/internal:git_lfs_smudge.py") |
| 5 | + python = repository_ctx.which("python3") or repository_ctx.which("python") |
| 6 | + if not python: |
| 7 | + fail("Neither python3 nor python executables found") |
| 8 | + res = repository_ctx.execute([python, script] + srcs, quiet = False) |
| 9 | + if res.return_code != 0: |
| 10 | + fail("git LFS smudging failed while instantiating @%s" % repository_ctx.name) |
| 11 | + |
| 12 | +def _download_and_extract_lfs(repository_ctx): |
| 13 | + attr = repository_ctx.attr |
| 14 | + src = repository_ctx.path(attr.src) |
| 15 | + if attr.build_file_content and attr.build_file: |
| 16 | + fail("You should specify only one among build_file_content and build_file for rule @%s" % repository_ctx.name) |
| 17 | + _smudge(repository_ctx, [src]) |
| 18 | + repository_ctx.extract(src.basename, stripPrefix = attr.strip_prefix) |
| 19 | + repository_ctx.delete(src.basename) |
| 20 | + if attr.build_file_content: |
| 21 | + repository_ctx.file("BUILD.bazel", attr.build_file_content) |
| 22 | + elif attr.build_file: |
| 23 | + repository_ctx.symlink(attr.build_file, "BUILD.bazel") |
| 24 | + |
| 25 | +def _download_lfs(repository_ctx): |
| 26 | + attr = repository_ctx.attr |
| 27 | + if int(bool(attr.srcs)) + int(bool(attr.dir)) != 1: |
| 28 | + fail("Exactly one between `srcs` and `dir` must be defined for @%s" % repository_ctx.name) |
| 29 | + if attr.srcs: |
| 30 | + srcs = [repository_ctx.path(src) for src in attr.srcs] |
| 31 | + else: |
| 32 | + dir = repository_ctx.path(attr.dir) |
| 33 | + if not dir.is_dir: |
| 34 | + fail("`dir` not a directory in @%s" % repository_ctx.name) |
| 35 | + srcs = [f for f in dir.readdir() if not f.is_dir] |
| 36 | + _smudge(repository_ctx, srcs) |
| 37 | + |
| 38 | + # with bzlmod the name is qualified with `~` separators, and we want the base name here |
| 39 | + name = repository_ctx.name.split("~")[-1] |
| 40 | + repository_ctx.file("BUILD.bazel", """ |
| 41 | +exports_files({files}) |
| 42 | +
|
| 43 | +filegroup( |
| 44 | + name = "{name}", |
| 45 | + srcs = {files}, |
| 46 | + visibility = ["//visibility:public"], |
| 47 | +) |
| 48 | +""".format(name = name, files = repr([src.basename for src in srcs]))) |
| 49 | + |
| 50 | +lfs_archive = repository_rule( |
| 51 | + doc = "Export the contents from an on-demand LFS archive. The corresponding path should be added to be ignored " + |
| 52 | + "in `.lfsconfig`.", |
| 53 | + implementation = _download_and_extract_lfs, |
| 54 | + attrs = { |
| 55 | + "src": attr.label(mandatory = True, doc = "Local path to the LFS archive to extract."), |
| 56 | + "build_file_content": attr.string(doc = "The content for the BUILD file for this repository. " + |
| 57 | + "Either build_file or build_file_content can be specified, but not both."), |
| 58 | + "build_file": attr.label(doc = "The file to use as the BUILD file for this repository. " + |
| 59 | + "Either build_file or build_file_content can be specified, but not both."), |
| 60 | + "strip_prefix": attr.string(default = "", doc = "A directory prefix to strip from the extracted files. "), |
| 61 | + }, |
| 62 | +) |
| 63 | + |
| 64 | +lfs_files = repository_rule( |
| 65 | + doc = "Export LFS files for on-demand download. Exactly one between `srcs` and `dir` must be defined. The " + |
| 66 | + "corresponding paths should be added to be ignored in `.lfsconfig`.", |
| 67 | + implementation = _download_lfs, |
| 68 | + attrs = { |
| 69 | + "srcs": attr.label_list(doc = "Local paths to the LFS files to export."), |
| 70 | + "dir": attr.label(doc = "Local path to a directory containing LFS files to export. Only the direct contents " + |
| 71 | + "of the directory are exported"), |
| 72 | + }, |
| 73 | +) |
0 commit comments