diff options
author | Radhika Jagtap <radhika.jagtap@ARM.com> | 2014-08-10 05:39:20 -0400 |
---|---|---|
committer | Radhika Jagtap <radhika.jagtap@ARM.com> | 2014-08-10 05:39:20 -0400 |
commit | 2ee47fc8d1e3c6089e3e358603dadca913a4527c (patch) | |
tree | 06e9fa372ad2e75a38acaca6d6757404f9c2ef56 /util | |
parent | dbdce42b88f9b1f954b1bbb5ae9c707a6a3c1d0c (diff) | |
download | gem5-2ee47fc8d1e3c6089e3e358603dadca913a4527c.tar.xz |
util: Move packet trace file read to protolib
This patch moves the code for opening an input protobuf packet trace into
a function defined in the protobuf library. This is because the code is
commonly used in decode scripts and is independent of the src protobuf
message.
Diffstat (limited to 'util')
-rwxr-xr-x | util/decode_packet_trace.py | 19 | ||||
-rw-r--r-- | util/protolib.py | 25 |
2 files changed, 27 insertions, 17 deletions
diff --git a/util/decode_packet_trace.py b/util/decode_packet_trace.py index e6f36c295..e9105753d 100755 --- a/util/decode_packet_trace.py +++ b/util/decode_packet_trace.py @@ -48,7 +48,6 @@ # r,128,64,4000,0 # w,232123,64,500000,0 -import gzip import protolib import sys @@ -81,22 +80,8 @@ def main(): print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>" exit(-1) - try: - # First see if this file is gzipped - try: - # Opening the file works even if it is not a gzip file - proto_in = gzip.open(sys.argv[1], 'rb') - - # Force a check of the magic number by seeking in the - # file. If we do not do it here the error will occur when - # reading the first message. - proto_in.seek(1) - proto_in.seek(0) - except IOError: - proto_in = open(sys.argv[1], 'rb') - except IOError: - print "Failed to open ", sys.argv[1], " for reading" - exit(-1) + # Open the file in read mode + proto_in = protolib.openFileRd(sys.argv[1]) try: ascii_out = open(sys.argv[2], 'w') diff --git a/util/protolib.py b/util/protolib.py index e085a90f2..e0507cdd4 100644 --- a/util/protolib.py +++ b/util/protolib.py @@ -71,8 +71,33 @@ # with protobuf python messages. For eg, the decode scripts for different # types of proto objects can use the same function to decode a single message +import gzip import struct +def openFileRd(in_file): + """ + This opens the file passed as argument for reading using an appropriate + function depending on if it is gzipped or not. It returns the file + handle. + """ + try: + # First see if this file is gzipped + try: + # Opening the file works even if it is not a gzip file + proto_in = gzip.open(in_file, 'rb') + + # Force a check of the magic number by seeking in the + # file. If we do not do it here the error will occur when + # reading the first message. + proto_in.seek(1) + proto_in.seek(0) + except IOError: + proto_in = open(in_file, 'rb') + except IOError: + print "Failed to open ", in_file, " for reading" + exit(-1) + return proto_in + def DecodeVarint(in_file): """ The decoding of the Varint32 is copied from |