|
1 | 1 | def lfs_smudge(repository_ctx, srcs, extract = False, stripPrefix = None):
|
2 |
| - for src in srcs: |
3 |
| - repository_ctx.watch(src) |
4 |
| - script = Label("//misc/bazel/internal:git_lfs_probe.py") |
5 | 2 | python = repository_ctx.which("python3") or repository_ctx.which("python")
|
6 | 3 | if not python:
|
7 | 4 | fail("Neither python3 nor python executables found")
|
8 |
| - repository_ctx.report_progress("querying LFS url(s) for: %s" % ", ".join([src.basename for src in srcs])) |
9 |
| - res = repository_ctx.execute([python, script] + srcs, quiet = True) |
10 |
| - if res.return_code != 0: |
11 |
| - fail("git LFS probing failed while instantiating @%s:\n%s" % (repository_ctx.name, res.stderr)) |
12 |
| - promises = [] |
13 |
| - for src, loc in zip(srcs, res.stdout.splitlines()): |
14 |
| - if loc == "local": |
15 |
| - if extract: |
16 |
| - repository_ctx.report_progress("extracting local %s" % src.basename) |
17 |
| - repository_ctx.extract(src, stripPrefix = stripPrefix) |
18 |
| - else: |
19 |
| - repository_ctx.report_progress("symlinking local %s" % src.basename) |
20 |
| - repository_ctx.symlink(src, src.basename) |
| 5 | + script = Label("//misc/bazel/internal:git_lfs_probe.py") |
| 6 | + |
| 7 | + def probe(srcs, hash_only = False): |
| 8 | + repository_ctx.report_progress("querying LFS url(s) for: %s" % ", ".join([src.basename for src in srcs])) |
| 9 | + cmd = [python, script] |
| 10 | + if hash_only: |
| 11 | + cmd.append("--hash-only") |
| 12 | + cmd.extend(srcs) |
| 13 | + res = repository_ctx.execute(cmd, quiet = True) |
| 14 | + if res.return_code != 0: |
| 15 | + fail("git LFS probing failed while instantiating @%s:\n%s" % (repository_ctx.name, res.stderr)) |
| 16 | + return res.stdout.splitlines() |
| 17 | + |
| 18 | + for src in srcs: |
| 19 | + repository_ctx.watch(src) |
| 20 | + infos = probe(srcs, hash_only = True) |
| 21 | + remote = [] |
| 22 | + for src, info in zip(srcs, infos): |
| 23 | + if info == "local": |
| 24 | + repository_ctx.report_progress("symlinking local %s" % src.basename) |
| 25 | + repository_ctx.symlink(src, src.basename) |
21 | 26 | else:
|
22 |
| - sha256, _, url = loc.partition(" ") |
23 |
| - if extract: |
24 |
| - # we can't use skylib's `paths.split_extension`, as that only gets the last extension, so `.tar.gz` |
25 |
| - # or similar wouldn't work |
26 |
| - # it doesn't matter if file is something like some.name.zip and possible_extension == "name.zip", |
27 |
| - # download_and_extract will just append ".name.zip" its internal temporary name, so extraction works |
28 |
| - possible_extension = ".".join(src.basename.rsplit(".", 2)[-2:]) |
29 |
| - repository_ctx.report_progress("downloading and extracting remote %s" % src.basename) |
30 |
| - repository_ctx.download_and_extract(url, sha256 = sha256, stripPrefix = stripPrefix, type = possible_extension) |
31 |
| - else: |
| 27 | + repository_ctx.report_progress("trying cache for remote %s" % src.basename) |
| 28 | + res = repository_ctx.download([], src.basename, sha256 = info, allow_fail = True) |
| 29 | + if not res.success: |
| 30 | + remote.append(src) |
| 31 | + if remote: |
| 32 | + infos = probe(remote) |
| 33 | + for src, info in zip(remote, infos): |
| 34 | + sha256, _, url = info.partition(" ") |
32 | 35 | repository_ctx.report_progress("downloading remote %s" % src.basename)
|
33 | 36 | repository_ctx.download(url, src.basename, sha256 = sha256)
|
| 37 | + if extract: |
| 38 | + for src in srcs: |
| 39 | + repository_ctx.report_progress("extracting %s" % src.basename) |
| 40 | + repository_ctx.extract(src.basename, stripPrefix = stripPrefix) |
| 41 | + repository_ctx.delete(src.basename) |
34 | 42 |
|
35 | 43 | def _download_and_extract_lfs(repository_ctx):
|
36 | 44 | attr = repository_ctx.attr
|
|
0 commit comments