src/jq_adapters.py
1"""Run a fixed filter from the reviewed Galaxy jq wrapper."""
2
3import json
4import selectors
5import subprocess
6import time
7
8from contract import MAX_OUTPUT, text
9
10JQ = "/opt/galaxy/bin/jq"
11
12
13def execute(program, inputs, params):
14 source = inputs["source"]
15 text(source)
16 if len(source) > 1024 * 1024:
17 raise ValueError("jq input exceeds 1 MiB")
18 if "path" in params:
19 path = json.loads(params["path"])
20 if not isinstance(path, list) or len(path) > 16 or any(
21 not (type(part) is int and -25000 <= part <= 25000 or isinstance(part, str) and len(part) <= 128)
22 for part in path
23 ):
24 raise ValueError("path requires up to 16 bounded string keys and integer indices")
25 command = [JQ, "-c", "-L", "/opt/galaxy/empty"]
26 for name, value in params.items():
27 command.extend(["--argjson", name, json.dumps(value, ensure_ascii=True)])
28 command.extend(["--", program])
29 output = bytearray()
30 diagnostic = bytearray()
31 data = memoryview(source)
32 deadline = time.monotonic() + 8
33 with subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
34 env={"LC_ALL": "C.UTF-8"}) as process:
35 try:
36 with selectors.DefaultSelector() as selector:
37 if data:
38 selector.register(process.stdin, selectors.EVENT_WRITE, "input")
39 else:
40 process.stdin.close()
41 selector.register(process.stdout, selectors.EVENT_READ, output)
42 selector.register(process.stderr, selectors.EVENT_READ, diagnostic)
43 while selector.get_map():
44 remaining = deadline - time.monotonic()
45 if remaining <= 0:
46 raise ValueError("jq execution exceeded eight seconds")
47 for key, _ in selector.select(remaining):
48 if key.data == "input":
49 try:
50 count = process.stdin.write(data[:4096])
51 process.stdin.flush()
52 data = data[count:]
53 except BrokenPipeError:
54 data = data[len(data):]
55 if not data:
56 selector.unregister(process.stdin)
57 process.stdin.close()
58 else:
59 chunk = key.fileobj.read1(16384)
60 if not chunk:
61 selector.unregister(key.fileobj)
62 continue
63 limit = MAX_OUTPUT if key.data is output else 8192
64 if len(key.data) + len(chunk) > limit:
65 raise ValueError("jq output or diagnostics exceed the adapter limit")
66 key.data.extend(chunk)
67 if process.wait(timeout=max(0.1, deadline - time.monotonic())) != 0:
68 raise ValueError("jq rejected this input: " + diagnostic.decode(errors="replace").strip()[:512])
69 finally:
70 if process.poll() is None:
71 process.kill()
72 return bytes(output)