b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | import os |
| 6 | |
| 7 | def create_output(args): |
| 8 | in_st = os.stat(args.input_file) |
| 9 | in_size = in_st.st_size |
| 10 | |
| 11 | in_f = open(args.input_file, 'r+b') |
| 12 | in_bytes = in_f.read(in_size) |
| 13 | in_f.close() |
| 14 | |
| 15 | if (args.pid_file): |
| 16 | pid_st = os.stat(args.pid_file) |
| 17 | pid_size = pid_st.st_size |
| 18 | |
| 19 | pid_f = open(args.pid_file, 'r+b') |
| 20 | pid_bytes = pid_f.read(pid_size) |
| 21 | pid_f.close() |
| 22 | else: |
| 23 | pid_bytes = bytes.fromhex(args.pid) |
| 24 | |
| 25 | sha256 = hashlib.sha256() |
| 26 | sha256.update(in_bytes) |
| 27 | |
| 28 | out_f = open(args.output_file, 'w+b') |
| 29 | out_f.write(pid_bytes) |
| 30 | out_f.write(sha256.digest()) |
| 31 | out_f.write(in_bytes) |
| 32 | out_f.close() |
| 33 | |
| 34 | def main(): |
| 35 | global args |
| 36 | |
| 37 | parser = argparse.ArgumentParser(description='') |
| 38 | |
| 39 | parser.add_argument('--input-file', |
| 40 | dest='input_file', |
| 41 | action='store', |
| 42 | type=str, |
| 43 | help='Input file') |
| 44 | |
| 45 | parser.add_argument('--output-file', |
| 46 | dest='output_file', |
| 47 | action='store', |
| 48 | type=str, |
| 49 | help='Output file') |
| 50 | |
| 51 | parser.add_argument('--pid-file', |
| 52 | dest='pid_file', |
| 53 | action='store', |
| 54 | type=str, |
| 55 | help='Sercomm PID file') |
| 56 | |
| 57 | parser.add_argument('--pid', |
| 58 | dest='pid', |
| 59 | action='store', |
| 60 | type=str, |
| 61 | help='Sercomm PID') |
| 62 | |
| 63 | args = parser.parse_args() |
| 64 | |
| 65 | if ((not args.input_file) or |
| 66 | (not args.output_file) or |
| 67 | (not args.pid_file and not args.pid)): |
| 68 | parser.print_help() |
| 69 | |
| 70 | create_output(args) |
| 71 | |
| 72 | main() |