| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
create_test_output.py |
| 4 |
~~~~~~~~~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
This script uses the Python HPACK library to take the raw data input used in |
| 7 |
https://github.com/http2jp/hpack-test-case and produce appropriately formatted |
| 8 |
output for that repository. This makes it possible to keep the Python HPACK |
| 9 |
library information in the hpack-test-case repository up to date. |
| 10 |
|
| 11 |
For many reasons, this script is not distributed with the library itself. |
| 12 |
Instead, it is kept in the repository, and expects to be run under tox. |
| 13 |
|
| 14 |
It should be invoked as follows: |
| 15 |
|
| 16 |
$ python create_test_output.py /path/to/hpack-test-case/repository |
| 17 |
""" |
| 18 |
from __future__ import print_function, absolute_import |
| 19 |
|
| 20 |
import binascii |
| 21 |
import codecs |
| 22 |
import json |
| 23 |
import os |
| 24 |
import os.path |
| 25 |
import sys |
| 26 |
|
| 27 |
import hpack |
| 28 |
|
| 29 |
|
| 30 |
def headers_from_story(story_headers): |
| 31 |
""" |
| 32 |
The story provides headers as a list of JSON objects. That's strongly not |
| 33 |
what we want: we want a list of two-tuples. This function transforms |
| 34 |
from the list of JSON objects to a list of two-tuples. |
| 35 |
""" |
| 36 |
useful_headers = [] |
| 37 |
for pair in story_headers: |
| 38 |
assert len(pair) == 1 |
| 39 |
useful_headers.extend(list(pair.items())) |
| 40 |
|
| 41 |
return useful_headers |
| 42 |
|
| 43 |
|
| 44 |
def process_story(story_name, raw_directory, target_directory): |
| 45 |
""" |
| 46 |
Processes and builds the appropriate output for a given raw story. |
| 47 |
""" |
| 48 |
print("Processing {}...".format(story_name), end='') |
| 49 |
raw_story = os.path.join(raw_directory, story_name) |
| 50 |
target_story = os.path.join(target_directory, story_name) |
| 51 |
|
| 52 |
with codecs.open(raw_story, encoding="utf-8") as f: |
| 53 |
story_json = json.load(f) |
| 54 |
|
| 55 |
cases = story_json["cases"] |
| 56 |
seqno = 0 |
| 57 |
output_data = { |
| 58 |
"cases": [], |
| 59 |
"description": ( |
| 60 |
"Encoded headers produced by the Python HPACK library, " |
| 61 |
"version {}".format(hpack.__version__) |
| 62 |
), |
| 63 |
} |
| 64 |
|
| 65 |
encoder = hpack.Encoder() |
| 66 |
|
| 67 |
for case in cases: |
| 68 |
# First we need to resize the header table. |
| 69 |
table_size = case.get("header_table_size") |
| 70 |
if table_size is not None: |
| 71 |
encoder.header_table_size = table_size |
| 72 |
|
| 73 |
headers = headers_from_story(case["headers"]) |
| 74 |
result = binascii.hexlify(encoder.encode(headers)).decode('ascii') |
| 75 |
|
| 76 |
# Provide the result |
| 77 |
output_case = { |
| 78 |
"seqno": seqno, |
| 79 |
"wire": result, |
| 80 |
"headers": case["headers"], |
| 81 |
} |
| 82 |
if table_size is not None: |
| 83 |
output_case["header_table_size"] = table_size |
| 84 |
|
| 85 |
output_data["cases"].append(output_case) |
| 86 |
seqno += 1 |
| 87 |
|
| 88 |
with codecs.open(target_story, mode="wb", encoding="utf-8") as f: |
| 89 |
json.dump(output_data, f, indent=2, sort_keys=True) |
| 90 |
|
| 91 |
print("complete.") |
| 92 |
|
| 93 |
|
| 94 |
def main(): |
| 95 |
test_repository = sys.argv[1] |
| 96 |
if not os.path.isdir(test_repository): |
| 97 |
print("{} is not a valid repository path".format(test_repository)) |
| 98 |
sys.exit(1) |
| 99 |
|
| 100 |
# Work out what our input files are. |
| 101 |
raw_story_directory = os.path.join(test_repository, "raw-data") |
| 102 |
raw_story_files = os.listdir(raw_story_directory) |
| 103 |
|
| 104 |
# Get a place ready to write our output files. |
| 105 |
target_directory = os.path.join(test_repository, "python-hpack") |
| 106 |
os.mkdir(target_directory) |
| 107 |
|
| 108 |
for story in raw_story_files: |
| 109 |
process_story(story, raw_story_directory, target_directory) |
| 110 |
|
| 111 |
|
| 112 |
if __name__ == '__main__': |
| 113 |
main() |