|
| 1 | +"""Rule for rewriting portable shebangs.""" |
| 2 | + |
| 3 | +load("//python/private:attributes.bzl", "WINDOWS_CONSTRAINTS_ATTRS") |
| 4 | +load("//python/private:common.bzl", "is_windows_platform", "runfiles_root_path") |
| 5 | +load("//python/private:py_info.bzl", "PyInfoBuilder", "VenvSymlinkEntry", "VenvSymlinkKind") |
| 6 | +load("//python/private:rule_builders.bzl", "ruleb") |
| 7 | + |
| 8 | +def _venv_rewrite_shebang_impl(ctx): |
| 9 | + is_windows = is_windows_platform(ctx) |
| 10 | + |
| 11 | + out_name = ctx.label.name |
| 12 | + if is_windows: |
| 13 | + out_name += ".bat" |
| 14 | + |
| 15 | + out_file = ctx.actions.declare_file(out_name) |
| 16 | + in_file = ctx.file.src |
| 17 | + |
| 18 | + action_args = ctx.actions.args() |
| 19 | + rewriter_file = ctx.files._venv_shebang_rewriter[0] |
| 20 | + inputs = depset([in_file, rewriter_file]) |
| 21 | + |
| 22 | + if rewriter_file.path.endswith(".ps1"): |
| 23 | + action_exe = "powershell.exe" |
| 24 | + action_args.add_all([ |
| 25 | + "-ExecutionPolicy", |
| 26 | + "Bypass", |
| 27 | + "-NoProfile", |
| 28 | + "-File", |
| 29 | + rewriter_file, |
| 30 | + ]) |
| 31 | + else: |
| 32 | + action_exe = ctx.attr._venv_shebang_rewriter[DefaultInfo].files_to_run |
| 33 | + |
| 34 | + action_args.add(in_file) |
| 35 | + action_args.add(out_file) |
| 36 | + action_args.add("windows" if is_windows else "unix") |
| 37 | + |
| 38 | + ctx.actions.run( |
| 39 | + inputs = inputs, |
| 40 | + outputs = [out_file], |
| 41 | + executable = action_exe, |
| 42 | + arguments = [action_args], |
| 43 | + mnemonic = "PyVenvRewriteBin", |
| 44 | + progress_message = "Rewriting venv bin script %{input}", |
| 45 | + toolchain = None, |
| 46 | + ) |
| 47 | + |
| 48 | + symlink = VenvSymlinkEntry( |
| 49 | + kind = VenvSymlinkKind.BIN, |
| 50 | + link_to_path = runfiles_root_path(ctx, out_file.short_path), |
| 51 | + link_to_file = out_file, |
| 52 | + venv_path = out_name, |
| 53 | + package = ctx.attr.package, |
| 54 | + version = ctx.attr.version, |
| 55 | + files = depset([out_file]), |
| 56 | + ) |
| 57 | + builder = PyInfoBuilder.new() |
| 58 | + builder.venv_symlinks.add([symlink]) |
| 59 | + py_info = builder.build() |
| 60 | + |
| 61 | + return [ |
| 62 | + DefaultInfo(files = depset([out_file]), executable = out_file), |
| 63 | + py_info, |
| 64 | + ] |
| 65 | + |
| 66 | +_builder = ruleb.Rule( |
| 67 | + implementation = _venv_rewrite_shebang_impl, |
| 68 | + executable = True, |
| 69 | +) |
| 70 | +_builder.attrs.update({ |
| 71 | + "package": attr.string(), |
| 72 | + "src": attr.label(mandatory = True, allow_single_file = True), |
| 73 | + "version": attr.string(), |
| 74 | + "_venv_shebang_rewriter": attr.label( |
| 75 | + default = "//python/private/pypi:venv_shebang_rewriter", |
| 76 | + allow_files = True, |
| 77 | + cfg = "exec", |
| 78 | + ), |
| 79 | +}) |
| 80 | +_builder.attrs.update(WINDOWS_CONSTRAINTS_ATTRS) |
| 81 | + |
| 82 | +venv_rewrite_shebang = _builder.build() |
0 commit comments