1#!/usr/bin/env python3 2# 3# update-tools-help.py - Update the command line help output in docbook/wsug_src. 4# 5# Wireshark - Network traffic analyzer 6# By Gerald Combs <gerald@wireshark.org> 7# Copyright 1998 Gerald Combs 8# 9# SPDX-License-Identifier: GPL-2.0-or-later 10'''Update tools help 11 12For each file that matches docbook/wsug_src/<command>-<flag>.txt, run 13that command and flag. Update the file if the output differs. 14''' 15 16import argparse 17import difflib 18import glob 19import io 20import os 21import re 22import subprocess 23import sys 24 25def main(): 26 parser = argparse.ArgumentParser(description='Update Wireshark tools help') 27 parser.add_argument('-p', '--program-path', nargs=1, default=os.path.curdir, help='Path to Wireshark executables.') 28 args = parser.parse_args() 29 30 this_dir = os.path.dirname(__file__) 31 wsug_src_dir = os.path.join(this_dir, '..', 'docbook', 'wsug_src') 32 33 tools_help_files = glob.glob(os.path.join(wsug_src_dir, '*-*.txt')) 34 tools_help_files.sort() 35 tool_pat = re.compile('(\w+)(-\w).txt') 36 37 # If tshark is present, assume that our other executables are as well. 38 program_path = args.program_path[0] 39 if not os.path.isfile(os.path.join(program_path, 'tshark')): 40 print('tshark not found at {}\n'.format(program_path)) 41 parser.print_usage() 42 sys.exit(1) 43 44 null_fd = open(os.devnull, 'w') 45 46 for thf in tools_help_files: 47 thf_base = os.path.basename(thf) 48 m = tool_pat.match(thf_base) 49 thf_command = os.path.join(program_path, m.group(1)) 50 thf_flag = m.group(2) 51 52 if not os.path.isfile(thf_command): 53 print('{} not found. Skipping.'.format(thf_command)) 54 continue 55 56 with io.open(thf, 'r', encoding='UTF-8') as fd: 57 cur_help = fd.read() 58 59 try: 60 new_help_data = subprocess.check_output((thf_command, thf_flag), stderr=null_fd) 61 except subprocess.CalledProcessError as e: 62 if thf_flag == '-h': 63 raise e 64 65 new_help = new_help_data.decode('UTF-8', 'replace') 66 67 cur_lines = cur_help.splitlines() 68 new_lines = new_help.splitlines() 69 if ' (v' in cur_lines[0]: 70 # Assume we have a version. Strip it. 71 cur_lines[0] = ' '.join(cur_lines[0].split()[:-1]) 72 new_lines[0] = ' '.join(new_lines[0].split()[:-1]) 73 diff = list(difflib.unified_diff(cur_lines, new_lines)) 74 75 if (len(diff) > 0): 76 print('Updating {} {}'.format(thf_command, thf_flag)) 77 with io.open(thf, 'w', encoding='UTF-8') as fd: 78 fd.write(new_help) 79 else: 80 print('{} {} output unchanged.'.format(thf_command, thf_flag)) 81 82if __name__ == '__main__': 83 main() 84