Created
November 6, 2025 21:51
-
-
Save rrbutani/37679832a15b0b394d8331e56f467a07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use nix -p bazel_8 python3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /.direnv | |
| /bazel-* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| load(":defs.bzl", "write_exec") | |
| write_exec(name = "test") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load(":bin.bzl", "ls") | |
| load("@hey//:defs.bzl", "foo") | |
| # load(":lshim.bzl", "binary") | |
| # # json.decode('"\\x00"') | |
| # x = json.decode('"ÿ"') | |
| # print(x) | |
| # print(repr(x)) | |
| # binary = "ff" | |
| def _impl(ctx): | |
| out = ctx.actions.declare_file(ctx.attr.name) | |
| ctx.actions.write( | |
| out, | |
| # content = binary, | |
| content = foo, | |
| is_executable = True, | |
| # mnemonic = "WriteExec" | |
| ) | |
| return DefaultInfo(executable = out) | |
| write_exec = rule( | |
| implementation = _impl, | |
| executable = True, | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| CHUNK_SIZE = 20 | |
| bin_file = open(sys.argv[1], 'rb').read() | |
| print("binary = json.decode('\"' + ''.join([") | |
| with open(sys.argv[1], 'rb') as inp: | |
| while True: | |
| if (chunk := inp.read(CHUNK_SIZE)) == b'': | |
| break | |
| line = ' "' + ''.join( | |
| # f"\\\\u00{byte:02x}" if byte < 0x80 else chr(byte) for byte in chunk | |
| f"\\\\u00{byte:02x}" if byte < 0x80 else byte for byte in chunk | |
| ) + '"' | |
| print(line, end=",\n") | |
| print("]) + '\"')") | |
| # ugh. need to emit raw bytes for 0x80.. which will trigger the bazel warning | |
| # about non-UTF-8 `.bzl` files... | |
| # | |
| # `rctx.read` is the only way I've found to get non-UTF-8 values into starlark | |
| # but... no way to make them available to analysis (rules) without spilling to | |
| # disk? | |
| # I guess we just need to accept the warning.. | |
| # see: https://github.com/bazelbuild/bazel/pull/24944 | |
| # see: https://github.com/bazelbuild/starlark/issues/112 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| r = use_repo_rule("//:repo_defs.bzl", "read_binary") | |
| r(name = "hey") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # global_mut = [] | |
| def _repo_impl(rctx): | |
| s = rctx.read("/nix/store/xs8scz9w9jp4hpqycx3n3bah5y07ymgj-coreutils-9.8/bin/ls") | |
| # print(s) | |
| # print(repr(ls[0:4])) | |
| # print(json.encode(s)) | |
| x = json.decode(json.encode(s)) | |
| print(x == s) | |
| # this works fine; can read in non-printable chars to strings like this... | |
| # | |
| # but can't actually write them in starlark unfortunately | |
| # | |
| # oooh but `json.decode` works? | |
| # global_mut.append(s) | |
| # rctx.file("ls", content = s, executable = True) | |
| rctx.file("defs.bzl", content = "foo = json.decode(" + repr(json.encode(s)) + ")", executable = True) | |
| rctx.file("BUILD.bazel", content = 'exports_files(["defs.bzl"])', executable = True) | |
| read_binary = repository_rule( | |
| implementation = _repo_impl, | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment