Skip to content

Commit

Permalink
python: Set indents to 4-space (#15539)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <[email protected]>
  • Loading branch information
phlax authored Mar 18, 2021
1 parent e05f253 commit 2c8f663
Show file tree
Hide file tree
Showing 112 changed files with 9,847 additions and 9,730 deletions.
54 changes: 27 additions & 27 deletions api/tools/generate_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,44 @@

# Convert an arbitrary proto object to its Struct proto representation.
def proto_to_struct(proto):
json_rep = json_format.MessageToJson(proto)
parsed_msg = struct_pb2.Struct()
json_format.Parse(json_rep, parsed_msg)
return parsed_msg
json_rep = json_format.MessageToJson(proto)
parsed_msg = struct_pb2.Struct()
json_format.Parse(json_rep, parsed_msg)
return parsed_msg


# Parse a proto from the filesystem.
def parse_proto(path, filter_name):
# We only know about some filter config protos ahead of time.
KNOWN_FILTERS = {
'http_connection_manager': lambda: http_connection_manager_pb2.HttpConnectionManager()
}
filter_config = KNOWN_FILTERS[filter_name]()
with open(path, 'r') as f:
text_format.Merge(f.read(), filter_config)
return filter_config
# We only know about some filter config protos ahead of time.
KNOWN_FILTERS = {
'http_connection_manager': lambda: http_connection_manager_pb2.HttpConnectionManager()
}
filter_config = KNOWN_FILTERS[filter_name]()
with open(path, 'r') as f:
text_format.Merge(f.read(), filter_config)
return filter_config


def generate_listeners(listeners_pb_path, output_pb_path, output_json_path, fragments):
listener = lds_pb2.Listener()
with open(listeners_pb_path, 'r') as f:
text_format.Merge(f.read(), listener)
listener = lds_pb2.Listener()
with open(listeners_pb_path, 'r') as f:
text_format.Merge(f.read(), listener)

for filter_chain in listener.filter_chains:
for f in filter_chain.filters:
f.config.CopyFrom(proto_to_struct(parse_proto(next(fragments), f.name)))
for filter_chain in listener.filter_chains:
for f in filter_chain.filters:
f.config.CopyFrom(proto_to_struct(parse_proto(next(fragments), f.name)))

with open(output_pb_path, 'w') as f:
f.write(str(listener))
with open(output_pb_path, 'w') as f:
f.write(str(listener))

with open(output_json_path, 'w') as f:
f.write(json_format.MessageToJson(listener))
with open(output_json_path, 'w') as f:
f.write(json_format.MessageToJson(listener))


if __name__ == '__main__':
if len(sys.argv) < 4:
print('Usage: %s <path to listeners.pb> <output listeners.pb> <output '
'listeners.json> <filter config fragment paths>') % sys.argv[0]
sys.exit(1)
if len(sys.argv) < 4:
print('Usage: %s <path to listeners.pb> <output listeners.pb> <output '
'listeners.json> <filter config fragment paths>') % sys.argv[0]
sys.exit(1)

generate_listeners(sys.argv[1], sys.argv[2], sys.argv[3], iter(sys.argv[4:]))
generate_listeners(sys.argv[1], sys.argv[2], sys.argv[3], iter(sys.argv[4:]))
8 changes: 4 additions & 4 deletions api/tools/generate_listeners_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import generate_listeners

if __name__ == "__main__":
srcdir = os.path.join(os.getenv("TEST_SRCDIR"), 'envoy_api_canonical')
generate_listeners.generate_listeners(
os.path.join(srcdir, "examples/service_envoy/listeners.pb"), "/dev/stdout", "/dev/stdout",
iter([os.path.join(srcdir, "examples/service_envoy/http_connection_manager.pb")]))
srcdir = os.path.join(os.getenv("TEST_SRCDIR"), 'envoy_api_canonical')
generate_listeners.generate_listeners(
os.path.join(srcdir, "examples/service_envoy/listeners.pb"), "/dev/stdout", "/dev/stdout",
iter([os.path.join(srcdir, "examples/service_envoy/http_connection_manager.pb")]))
96 changes: 48 additions & 48 deletions api/tools/tap2pcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,57 @@


def dump_event(direction, timestamp, data):
dump = io.StringIO()
dump.write('%s\n' % direction)
# Adjust to local timezone
adjusted_dt = timestamp.ToDatetime() - datetime.timedelta(seconds=time.altzone)
dump.write('%s\n' % adjusted_dt)
od = sp.Popen(['od', '-Ax', '-tx1', '-v'], stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.PIPE)
packet_dump = od.communicate(data)[0]
dump.write(packet_dump.decode())
return dump.getvalue()
dump = io.StringIO()
dump.write('%s\n' % direction)
# Adjust to local timezone
adjusted_dt = timestamp.ToDatetime() - datetime.timedelta(seconds=time.altzone)
dump.write('%s\n' % adjusted_dt)
od = sp.Popen(['od', '-Ax', '-tx1', '-v'], stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.PIPE)
packet_dump = od.communicate(data)[0]
dump.write(packet_dump.decode())
return dump.getvalue()


def tap2pcap(tap_path, pcap_path):
wrapper = wrapper_pb2.TraceWrapper()
if tap_path.endswith('.pb_text'):
with open(tap_path, 'r') as f:
text_format.Merge(f.read(), wrapper)
else:
with open(tap_path, 'r') as f:
wrapper.ParseFromString(f.read())

trace = wrapper.socket_buffered_trace
local_address = trace.connection.local_address.socket_address.address
local_port = trace.connection.local_address.socket_address.port_value
remote_address = trace.connection.remote_address.socket_address.address
remote_port = trace.connection.remote_address.socket_address.port_value

dumps = []
for event in trace.events:
if event.HasField('read'):
dumps.append(dump_event('I', event.timestamp, event.read.data.as_bytes))
elif event.HasField('write'):
dumps.append(dump_event('O', event.timestamp, event.write.data.as_bytes))

ipv6 = False
try:
socket.inet_pton(socket.AF_INET6, local_address)
ipv6 = True
except socket.error:
pass

text2pcap_args = [
'text2pcap', '-D', '-t', '%Y-%m-%d %H:%M:%S.', '-6' if ipv6 else '-4',
'%s,%s' % (remote_address, local_address), '-T',
'%d,%d' % (remote_port, local_port), '-', pcap_path
]
text2pcap = sp.Popen(text2pcap_args, stdout=sp.PIPE, stdin=sp.PIPE)
text2pcap.communicate('\n'.join(dumps).encode())
wrapper = wrapper_pb2.TraceWrapper()
if tap_path.endswith('.pb_text'):
with open(tap_path, 'r') as f:
text_format.Merge(f.read(), wrapper)
else:
with open(tap_path, 'r') as f:
wrapper.ParseFromString(f.read())

trace = wrapper.socket_buffered_trace
local_address = trace.connection.local_address.socket_address.address
local_port = trace.connection.local_address.socket_address.port_value
remote_address = trace.connection.remote_address.socket_address.address
remote_port = trace.connection.remote_address.socket_address.port_value

dumps = []
for event in trace.events:
if event.HasField('read'):
dumps.append(dump_event('I', event.timestamp, event.read.data.as_bytes))
elif event.HasField('write'):
dumps.append(dump_event('O', event.timestamp, event.write.data.as_bytes))

ipv6 = False
try:
socket.inet_pton(socket.AF_INET6, local_address)
ipv6 = True
except socket.error:
pass

text2pcap_args = [
'text2pcap', '-D', '-t', '%Y-%m-%d %H:%M:%S.', '-6' if ipv6 else '-4',
'%s,%s' % (remote_address, local_address), '-T',
'%d,%d' % (remote_port, local_port), '-', pcap_path
]
text2pcap = sp.Popen(text2pcap_args, stdout=sp.PIPE, stdin=sp.PIPE)
text2pcap.communicate('\n'.join(dumps).encode())


if __name__ == '__main__':
if len(sys.argv) != 3:
print('Usage: %s <tap .pb/.pb_text> <pcap path>' % sys.argv[0])
sys.exit(1)
tap2pcap(sys.argv[1], sys.argv[2])
if len(sys.argv) != 3:
print('Usage: %s <tap .pb/.pb_text> <pcap path>' % sys.argv[0])
sys.exit(1)
tap2pcap(sys.argv[1], sys.argv[2])
27 changes: 14 additions & 13 deletions api/tools/tap2pcap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
# a golden output file for the tshark dump. Since we run tap2pcap in a
# subshell with a limited environment, the inferred time zone should be UTC.
if __name__ == '__main__':
srcdir = os.path.join(os.getenv('TEST_SRCDIR'), 'envoy_api_canonical')
tap_path = os.path.join(srcdir, 'tools/data/tap2pcap_h2_ipv4.pb_text')
expected_path = os.path.join(srcdir, 'tools/data/tap2pcap_h2_ipv4.txt')
pcap_path = os.path.join(os.getenv('TEST_TMPDIR'), 'generated.pcap')
srcdir = os.path.join(os.getenv('TEST_SRCDIR'), 'envoy_api_canonical')
tap_path = os.path.join(srcdir, 'tools/data/tap2pcap_h2_ipv4.pb_text')
expected_path = os.path.join(srcdir, 'tools/data/tap2pcap_h2_ipv4.txt')
pcap_path = os.path.join(os.getenv('TEST_TMPDIR'), 'generated.pcap')

tap2pcap.tap2pcap(tap_path, pcap_path)
actual_output = sp.check_output(['tshark', '-r', pcap_path, '-d', 'tcp.port==10000,http2', '-P'])
with open(expected_path, 'rb') as f:
expected_output = f.read()
if actual_output != expected_output:
print('Mismatch')
print('Expected: %s' % expected_output)
print('Actual: %s' % actual_output)
sys.exit(1)
tap2pcap.tap2pcap(tap_path, pcap_path)
actual_output = sp.check_output(
['tshark', '-r', pcap_path, '-d', 'tcp.port==10000,http2', '-P'])
with open(expected_path, 'rb') as f:
expected_output = f.read()
if actual_output != expected_output:
print('Mismatch')
print('Expected: %s' % expected_output)
print('Actual: %s' % actual_output)
sys.exit(1)
Loading

0 comments on commit 2c8f663

Please sign in to comment.