Skip to content

Commit c705b7d

Browse files
committed
fix opa_ctx to run correctly with bazel 7
1 parent 863b7da commit c705b7d

2 files changed

Lines changed: 45 additions & 24 deletions

File tree

opa/private/opa_rules_dependencies.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
22
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
33

4-
DEFAULT_VERSION = "0.57.1"
4+
DEFAULT_VERSION = "0.59.0"
55

66
_OPA_SHA256 = {
7+
"0.59.0": {
8+
"opa_darwin_amd64": "3edddc7dded91a7b2fe7fbe3d862778dccc28eff6ee515c41b38d65474d5e9f4",
9+
"opa_darwin_arm64_static": "890d23badb79ba0594e360c721ea3ff6d2da0a5461e2864a0fcb80438544635e",
10+
"opa_linux_amd64": "aadd956093b680485ceca4ee1e8ccd31f129e002972ca57b97fe375086ffbfc5",
11+
"opa_linux_amd64_static": "5f615343a1cae1deb2f2f514b2f4b46456495fe1c828b17e779eb583aced0cc3",
12+
"opa_linux_arm64_static": "ca9de0976739dc3dc07e1e7e16079f0fa4df8fc2706abe852219406abc63c3e3",
13+
"opa_windows_amd64": "0167f2bd69b72993ccdca222d0bc5d9278ffb194f9c87fddc1b55ecc9edf17df",
14+
"opa_capabilities_json": "c8e827c4186a3f30de7fefa3c2c9d72c8856ee10fd2890b8c41f5e351b6bfaa2",
15+
"opa_builtin_metadata_json": "641ee050578b4c1d49e5ab6cef435416e8f4e0b0a3d6ba785e11f3024594a26a",
16+
},
717
"0.57.1": {
818
"opa_darwin_amd64": "54a2d229638baddb0ac6f7c283295e547e6f491ab2ddcaf714fa182427e8421d",
919
"opa_darwin_arm64_static": "367adba9c1380297c87a83019965a28bb0f33fe7c0854ff6beedb4aa563e4b4f",

tools/opa_ctx.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from dataclasses import dataclass
33
from argparse import ArgumentParser
44
from subprocess import run, PIPE, STDOUT
5-
from typing import List, Optional
5+
from typing import List, Optional, Union
6+
from pathlib import Path
7+
68
import os
79
import sys
810

@@ -11,36 +13,47 @@
1113
class Args:
1214
output: Optional[str]
1315
inputs: List[str]
16+
exec: Path
1417
command: List[str]
15-
wd: str
18+
wd: Path
1619

1720

1821
def parse_args() -> Args:
1922
parser = ArgumentParser()
20-
parser.add_argument("-o", "--output", metavar="FILE:ALIAS",
21-
help="expected output file to extract from the working directory")
23+
parser.add_argument(
24+
"-o",
25+
"--output",
26+
metavar="FILE:ALIAS",
27+
help="expected output file to extract from the working directory",
28+
)
2229
parser.add_argument("-d", "--working-directory", required=True)
23-
parser.add_argument("-i", "--input", nargs="+",
24-
help="files to put in the context", metavar="FILE:ALIAS")
30+
parser.add_argument(
31+
"-i",
32+
"--input",
33+
nargs="+",
34+
help="files to put in the context",
35+
metavar="FILE:ALIAS",
36+
)
2537
parser.add_argument("command", nargs="+")
2638

2739
args = parser.parse_args()
2840

2941
return Args(
3042
output=args.output,
3143
inputs=args.input,
32-
command=args.command,
33-
wd=args.working_directory,
44+
exec=Path(args.command[0]),
45+
command=args.command[1:],
46+
wd=Path(args.working_directory),
3447
)
3548

49+
3650
# When run with `bazel run` directly when working directly is not the user's working directory
3751

3852

3953
def chdir():
4054
user_dir = os.getenv("BUILD_WORKING_DIRECTORY")
4155

4256
if user_dir:
43-
print(f"chdir: {user_dir}")
4457
os.chdir(user_dir)
4558

4659

@@ -50,7 +63,7 @@ def split_once_or_double(s: str, delimiter: str) -> List[str]:
5063
return parts if len(parts) == 2 else [s, s]
5164

5265

53-
def copy_file(src: str, dst: str):
66+
def copy_file(src: Union[str, Path], dst: Union[str, Path]):
5467
with open(dst, "wb") as out:
5568
with open(src, "rb") as input:
5669
out.write(input.read())
@@ -61,31 +74,29 @@ def main():
6174

6275
args = parse_args()
6376

64-
if os.path.exists(args.command[0]):
65-
args.command[0] = os.path.abspath(args.command[0])
77+
if args.exec.exists():
78+
args.exec = args.exec.absolute()
6679

67-
os.makedirs(args.wd, exist_ok=True)
80+
args.wd.mkdir(parents=True, exist_ok=True)
6881

6982
for input in args.inputs:
7083
src, alias = split_once_or_double(input, ":")
71-
dst = os.path.abspath(os.path.join(args.wd, alias))
72-
73-
os.makedirs(os.path.dirname(dst), exist_ok=True)
84+
dst = args.wd.joinpath(alias).absolute()
85+
dst.parent.mkdir(parents=True, exist_ok=True)
7486

75-
try:
76-
os.link(src, dst)
77-
except PermissionError:
78-
copy_file(src, dst)
87+
copy_file(src, dst)
7988

80-
p = run(args.command, stderr=STDOUT, stdout=PIPE, cwd=args.wd)
89+
p = run([args.exec] + args.command, stderr=STDOUT, stdout=PIPE, cwd=args.wd)
8190

8291
if p.returncode != 0:
83-
print(p.stdout.decode())
92+
print(
93+
f"Command exited with {p.returncode}:\n{p.stdout.decode()}", file=sys.stderr
94+
)
8495
sys.exit(p.returncode)
8596

8697
if args.output:
8798
file, alias = split_once_or_double(args.output, ":")
88-
copy_file(os.path.join(args.wd, alias), file)
99+
copy_file(args.wd.joinpath(alias), file)
89100
os.chmod(file, 0o644)
90101

91102

0 commit comments

Comments
 (0)