Skip to content

Commit

Permalink
fix opa_ctx to run correctly with bazel 7
Browse files Browse the repository at this point in the history
  • Loading branch information
ffortier committed Dec 13, 2023
1 parent 863b7da commit c705b7d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
12 changes: 11 additions & 1 deletion opa/private/opa_rules_dependencies.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

DEFAULT_VERSION = "0.57.1"
DEFAULT_VERSION = "0.59.0"

_OPA_SHA256 = {
"0.59.0": {
"opa_darwin_amd64": "3edddc7dded91a7b2fe7fbe3d862778dccc28eff6ee515c41b38d65474d5e9f4",
"opa_darwin_arm64_static": "890d23badb79ba0594e360c721ea3ff6d2da0a5461e2864a0fcb80438544635e",
"opa_linux_amd64": "aadd956093b680485ceca4ee1e8ccd31f129e002972ca57b97fe375086ffbfc5",
"opa_linux_amd64_static": "5f615343a1cae1deb2f2f514b2f4b46456495fe1c828b17e779eb583aced0cc3",
"opa_linux_arm64_static": "ca9de0976739dc3dc07e1e7e16079f0fa4df8fc2706abe852219406abc63c3e3",
"opa_windows_amd64": "0167f2bd69b72993ccdca222d0bc5d9278ffb194f9c87fddc1b55ecc9edf17df",
"opa_capabilities_json": "c8e827c4186a3f30de7fefa3c2c9d72c8856ee10fd2890b8c41f5e351b6bfaa2",
"opa_builtin_metadata_json": "641ee050578b4c1d49e5ab6cef435416e8f4e0b0a3d6ba785e11f3024594a26a",
},
"0.57.1": {
"opa_darwin_amd64": "54a2d229638baddb0ac6f7c283295e547e6f491ab2ddcaf714fa182427e8421d",
"opa_darwin_arm64_static": "367adba9c1380297c87a83019965a28bb0f33fe7c0854ff6beedb4aa563e4b4f",
Expand Down
57 changes: 34 additions & 23 deletions tools/opa_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from dataclasses import dataclass
from argparse import ArgumentParser
from subprocess import run, PIPE, STDOUT
from typing import List, Optional
from typing import List, Optional, Union
from pathlib import Path

import os
import sys

Expand All @@ -11,36 +13,47 @@
class Args:
output: Optional[str]
inputs: List[str]
exec: Path
command: List[str]
wd: str
wd: Path


def parse_args() -> Args:
parser = ArgumentParser()
parser.add_argument("-o", "--output", metavar="FILE:ALIAS",
help="expected output file to extract from the working directory")
parser.add_argument(
"-o",
"--output",
metavar="FILE:ALIAS",
help="expected output file to extract from the working directory",
)
parser.add_argument("-d", "--working-directory", required=True)
parser.add_argument("-i", "--input", nargs="+",
help="files to put in the context", metavar="FILE:ALIAS")
parser.add_argument(
"-i",
"--input",
nargs="+",
help="files to put in the context",
metavar="FILE:ALIAS",
)
parser.add_argument("command", nargs="+")

args = parser.parse_args()

return Args(
output=args.output,
inputs=args.input,
command=args.command,
wd=args.working_directory,
exec=Path(args.command[0]),
command=args.command[1:],
wd=Path(args.working_directory),
)


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


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

if user_dir:
print(f"chdir: {user_dir}")
os.chdir(user_dir)


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


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

args = parse_args()

if os.path.exists(args.command[0]):
args.command[0] = os.path.abspath(args.command[0])
if args.exec.exists():
args.exec = args.exec.absolute()

os.makedirs(args.wd, exist_ok=True)
args.wd.mkdir(parents=True, exist_ok=True)

for input in args.inputs:
src, alias = split_once_or_double(input, ":")
dst = os.path.abspath(os.path.join(args.wd, alias))

os.makedirs(os.path.dirname(dst), exist_ok=True)
dst = args.wd.joinpath(alias).absolute()
dst.parent.mkdir(parents=True, exist_ok=True)

try:
os.link(src, dst)
except PermissionError:
copy_file(src, dst)
copy_file(src, dst)

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

if p.returncode != 0:
print(p.stdout.decode())
print(
f"Command exited with {p.returncode}:\n{p.stdout.decode()}", file=sys.stderr
)
sys.exit(p.returncode)

if args.output:
file, alias = split_once_or_double(args.output, ":")
copy_file(os.path.join(args.wd, alias), file)
copy_file(args.wd.joinpath(alias), file)
os.chmod(file, 0o644)


Expand Down

0 comments on commit c705b7d

Please sign in to comment.