diff options
author | Kovarththanan Rajaratnam <kovarththanan.rajaratnam@gmail.com> | 2009-09-25 21:01:28 +0000 |
---|---|---|
committer | Kovarththanan Rajaratnam <kovarththanan.rajaratnam@gmail.com> | 2009-09-25 21:01:28 +0000 |
commit | de10fd22a6fc1e55ba57edbfe4cb420e596852a7 (patch) | |
tree | 02ddd2103be1801a120cb893273ba316e0286417 /tools/indexcap.py | |
parent | e45956edbe5e6c535e8680e0440151a7ef5ea852 (diff) |
Handle keyboard interrupts gracefully
svn path=/trunk/; revision=30156
Diffstat (limited to 'tools/indexcap.py')
-rw-r--r-- | tools/indexcap.py | 41 |
1 files 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) |