From 197e1b83a639e504b27285b2358adb03a5206391 Mon Sep 17 00:00:00 2001 From: krj Date: Fri, 25 Sep 2009 21:01:28 +0000 Subject: [PATCH] Handle keyboard interrupts gracefully git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@30156 f5534014-38df-0310-8fa8-9805f1628bb7 --- tools/indexcap.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/tools/indexcap.py b/tools/indexcap.py index 024a56aa98..b09c7180e8 100644 --- a/tools/indexcap.py +++ b/tools/indexcap.py @@ -34,21 +34,24 @@ import re import pickle def process_capture_file(tshark, file): - cmd = [tshark, "-Tfields", "-e", "frame.protocols", "-r", file] - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (stdout, stderr) = p.communicate() - if p.returncode != 0: - return (file, {}) + try: + cmd = [tshark, "-Tfields", "-e", "frame.protocols", "-r", file] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stdout, stderr) = p.communicate() + if p.returncode != 0: + return (file, {}) - proto_hash = {} - for line in stdout.splitlines(): - if not re.match(r'^[\w:-]+$', line): - continue + proto_hash = {} + for line in stdout.splitlines(): + if not re.match(r'^[\w:-]+$', line): + continue - for proto in line.split(':'): - proto_hash[proto] = 1 + proto_hash.setdefault(proto, 0) + for proto in line.split(':'): + proto_hash[proto] = 1 + proto_hash.setdefault(proto, 0) - return (file, proto_hash) + return (file, proto_hash) + except KeyboardInterrupt: + return None def list_proto(cap_hash): proto_hash = {} @@ -130,11 +133,15 @@ def main(): pool = multiprocessing.Pool(options.num_procs) results = [pool.apply_async(process_capture_file, [tshark, file]) for file in cap_files] - for (cur_item_idx,result) in enumerate(results): - file_result = result.get() - action = "SKIPPED" if file_result[1] is {} else "PROCESSED" - print "%s [%u/%u] %s %u bytes" % (action, cur_item_idx+1, options.max_files, file_result[0], os.path.getsize(file_result[0])) - cap_hash.update(dict([file_result])) + try: + for (cur_item_idx,result) in enumerate(results): + file_result = result.get() + action = "SKIPPED" if file_result[1] is {} else "PROCESSED" + print "%s [%u/%u] %s %u bytes" % (action, cur_item_idx+1, options.max_files, file_result[0], os.path.getsize(file_result[0])) + cap_hash.update(dict([file_result])) + except KeyboardInterrupt: + print "%s was interrupted by user" % (sys.argv[0]) + pool.terminate() index_file = open(index_file_name, "w") pickle.dump(cap_hash, index_file) -- 2.34.1