1#!/usr/bin/env python3 2# coding: utf-8 3# Copyright (c) 2024 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15import os 16import subprocess 17import re 18import shutil 19import json 20import sys 21import stat 22import time 23 24 25def run_command(command): 26 result = subprocess.run(command, shell=False, capture_output=True, text=True, encoding='utf-8', errors='ignore') 27 return result.stdout, result.returncode 28 29 30def parse_log(log, tex_file, word, match_log_path, unmatch_log_path): 31 result_size_pattern = re.compile(r'result size: (\d+) while expecting (\d+)') 32 key_value_pattern = re.compile(r'([0-9a-fA-F]+): (\d+)') 33 34 lines = log.split('\n') 35 success = False 36 for i, line in enumerate(lines): 37 match = result_size_pattern.match(line) 38 if match: 39 result_size = int(match.group(1)) 40 expecting_size = int(match.group(2)) 41 if result_size == expecting_size: 42 43 index = 0 44 result_str = "" 45 for j in range(i + 1, i + 1 + result_size): 46 key_value_match = key_value_pattern.match(lines[j]) 47 if key_value_match: 48 value = int(key_value_match.group(2)) 49 if value % 2 == 1: 50 success = True 51 result_str += f"{index}:{value} " 52 53 index += 1 54 if not success: 55 with os.fdopen(os.open(unmatch_log_path, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 56 'a') as unmatch_log: 57 unmatch_log.write(f"{tex_file} {word}\n") 58 else: 59 with os.fdopen(os.open(match_log_path, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 60 'a') as match_log: 61 match_log.write(f"{tex_file} {word} {result_str}\n") 62 break 63 64 65def main(config_file_name): 66 with os.fdopen(os.open(config_file_name, os.O_RDONLY | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'r') as f: 67 config = json.load(f) 68 69 file_path = config['file_path'] 70 tex_files = config['tex_files'] 71 out_dir = './out_hpb' 72 73 # 创建 out_hpb 目录 74 if not os.path.exists(out_dir): 75 os.makedirs(out_dir) 76 77 # 创建 report 目录 78 report_dir = './report' 79 if not os.path.exists(report_dir): 80 os.makedirs(report_dir) 81 82 # 创建 hyphen_report_时间戳 目录 83 timestamp = time.strftime("%Y%m%d_%H%M%S") 84 report_subdir = os.path.join(report_dir, f"hyphen_report_{timestamp}") 85 os.makedirs(report_subdir) 86 87 match_log_path = os.path.join(report_subdir, 'match.log') 88 unmatch_log_path = os.path.join(report_subdir, 'unmatch.log') 89 90 for tex_file in tex_files: 91 tex_filename = tex_file['filename'] 92 words = tex_file['words'] 93 hpb_filename = f"{os.path.splitext(tex_filename)[0]}.hpb" 94 hpb_filepath = os.path.join(out_dir, hpb_filename) 95 96 # Step 1: Run the transform command 97 transform_command = ["./transform", os.path.join(file_path, tex_filename), out_dir] 98 print(f"Running transform command for {tex_filename}...") 99 _, returncode = run_command(transform_command) 100 if returncode != 0: 101 print(f"Transform command failed for {tex_filename}. Skipping to next file.") 102 continue 103 104 for word in words: 105 # Step 2: Run the reader command 106 reader_command = ["./reader", hpb_filepath, word] 107 print(f"Running reader command for {word}...") 108 log_output, _ = run_command(reader_command) 109 print("Reader command output:") 110 print(log_output) 111 112 # Step 3: Parse the log output 113 print("Parsing log output...") 114 parse_log(log_output, tex_filename, word, match_log_path, unmatch_log_path) 115 116 # Step 4: Delete the out_hpb directory 117 print("Deleting out_hpb directory...") 118 shutil.rmtree(out_dir) 119 print("out_hpb directory deleted.") 120 121 122if __name__ == "__main__": 123 if len(sys.argv) != 2: 124 print("Usage: python script.py <config_file>") 125 sys.exit(1) 126 127 config_file = sys.argv[1] 128 main(config_file) 129