okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-142b-serve into f-1426e1085c1d

main
933 B29 linesraw
1#!/usr/bin/env python3
2"""Runs benchmark.py unmodified against a server that needs an API key.
3
4benchmark.py has no option for an Authorization header, so this wrapper adds
5`Authorization: Bearer <key>` to every urllib request and then runs the
6harness file as __main__. The key is read from a file and never printed.
7
8usage: run_harness.py KEY_FILE BENCHMARK_PY [benchmark.py arguments ...]
9"""
10import runpy
11import sys
12import urllib.request
13
14key = open(sys.argv[1]).read().strip()
15harness = sys.argv[2]
16original = urllib.request.urlopen
17
18
19def urlopen(request, *args, **kwargs):
20    if isinstance(request, str):
21        request = urllib.request.Request(request)
22    if not request.has_header("Authorization"):
23        request.add_header("Authorization", "Bearer " + key)
24    return original(request, *args, **kwargs)
25
26
27urllib.request.urlopen = urlopen
28sys.argv = [harness] + sys.argv[3:]
29runpy.run_path(harness, run_name="__main__")