|
6 | 6 | import sys |
7 | 7 | import os |
8 | 8 | import argparse |
| 9 | +import json |
9 | 10 | import mainWindowGUI as mainWindowGUI |
10 | 11 | import mineSweeperGUI as mineSweeperGUI |
11 | 12 | import ms_toollib as ms |
@@ -51,76 +52,93 @@ def on_ready_read(socket: QLocalSocket): |
51 | 52 |
|
52 | 53 |
|
53 | 54 | def cli_check_file(file_path: str) -> int: |
| 55 | + result = { |
| 56 | + "error": "", |
| 57 | + "data": [] |
| 58 | + } |
| 59 | + |
54 | 60 | if not os.path.exists(file_path): |
55 | | - print("ERROR: file not found") |
56 | | - return 2 |
57 | | - |
58 | | - # 搜集目录或文件下的所有evf和evfs文件 |
59 | | - evf_evfs_files = [] |
60 | | - if os.path.isfile(file_path) and ( |
61 | | - file_path.endswith(".evf") or file_path.endswith(".evfs") |
62 | | - ): |
63 | | - evf_evfs_files = [os.path.abspath(file_path)] |
64 | | - elif os.path.isdir(file_path): |
65 | | - evf_evfs_files = [ |
66 | | - os.path.abspath(os.path.join(root, file)) |
67 | | - for root, dirs, files in os.walk(file_path) |
68 | | - for file in files |
69 | | - if file.endswith(".evf") or file.endswith(".evfs") |
70 | | - ] |
71 | | - |
72 | | - if not evf_evfs_files: |
73 | | - print("ERROR: must be evf or evfs files or directory") |
74 | | - return 2 |
75 | | - |
76 | | - # 实例化一个MineSweeperGUI出来 |
77 | | - app = QtWidgets.QApplication(sys.argv) |
78 | | - mainWindow = mainWindowGUI.MainWindow() |
79 | | - ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv) |
80 | | - |
81 | | - for ide, e in enumerate(evf_evfs_files): |
82 | | - if not ui.checksum_module_ok(): |
83 | | - print("ERROR: ???") |
84 | | - return 2 |
85 | | - if e.endswith(".evf"): |
86 | | - # 检验evf文件是否合法 |
87 | | - video = ms.EvfVideo(e) |
88 | | - try: |
89 | | - video.parse() |
90 | | - except: |
91 | | - evf_evfs_files[ide] = (e, 2) |
92 | | - else: |
93 | | - checksum = ui.checksum_guard.get_checksum( |
94 | | - video.raw_data[: -(len(video.checksum) + 2)] |
95 | | - ) |
96 | | - if video.checksum == checksum: |
97 | | - evf_evfs_files[ide] = (e, 0) |
98 | | - else: |
99 | | - evf_evfs_files[ide] = (e, 1) |
100 | | - elif e.endswith(".evfs"): |
101 | | - # 检验evfs文件是否合法 |
102 | | - videos = ms.Evfs(e) |
103 | | - try: |
104 | | - videos.parse() |
105 | | - except: |
106 | | - evf_evfs_files[ide] = (e, 2) |
107 | | - else: |
108 | | - if videos.len() <= 0: |
109 | | - evf_evfs_files[ide] = (e, 2) |
110 | | - checksum = ui.checksum_guard.get_checksum( |
111 | | - videos[0].evf_video.raw_data) |
112 | | - if video.checksum != checksum: |
113 | | - evf_evfs_files[ide] = (e, 1) |
114 | | - continue |
115 | | - for idcell, cell in enumerate(videos[1:]): |
116 | | - checksum = ui.checksum_guard.get_checksum( |
117 | | - cell.evf_video.raw_data + videos[idcell - 1].checksum |
118 | | - ) |
119 | | - if cell.evf_file.checksum != checksum: |
120 | | - evf_evfs_files[ide] = (e, 1) |
121 | | - continue |
122 | | - evf_evfs_files[ide] = (e, 0) |
123 | | - # print(evf_evfs_files) |
| 61 | + result["error"] = "file not found" |
| 62 | + else: |
| 63 | + # 搜集目录或文件下的所有evf和evfs文件 |
| 64 | + evf_evfs_files = [] |
| 65 | + if os.path.isfile(file_path) and ( |
| 66 | + file_path.endswith(".evf") or file_path.endswith(".evfs") |
| 67 | + ): |
| 68 | + evf_evfs_files = [os.path.abspath(file_path)] |
| 69 | + elif os.path.isdir(file_path): |
| 70 | + evf_evfs_files = [ |
| 71 | + os.path.abspath(os.path.join(root, file)) |
| 72 | + for root, dirs, files in os.walk(file_path) |
| 73 | + for file in files |
| 74 | + if file.endswith(".evf") or file.endswith(".evfs") |
| 75 | + ] |
| 76 | + |
| 77 | + if not evf_evfs_files: |
| 78 | + result["error"] = "must be evf or evfs files or directory" |
| 79 | + else: |
| 80 | + # 实例化一个MineSweeperGUI出来 |
| 81 | + app = QtWidgets.QApplication(sys.argv) |
| 82 | + mainWindow = mainWindowGUI.MainWindow() |
| 83 | + ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv) |
| 84 | + |
| 85 | + for ide, e in enumerate(evf_evfs_files): |
| 86 | + if not ui.checksum_module_ok(): |
| 87 | + result["error"] = "checksum module error" |
| 88 | + break |
| 89 | + if e.endswith(".evf"): |
| 90 | + # 检验evf文件是否合法 |
| 91 | + video = ms.EvfVideo(e) |
| 92 | + try: |
| 93 | + video.parse() |
| 94 | + except: |
| 95 | + evf_evfs_files[ide] = (e, 2) |
| 96 | + else: |
| 97 | + checksum = ui.checksum_guard.get_checksum( |
| 98 | + video.raw_data[: -(len(video.checksum) + 2)] |
| 99 | + ) |
| 100 | + if video.checksum == checksum: |
| 101 | + evf_evfs_files[ide] = (e, 0) |
| 102 | + else: |
| 103 | + evf_evfs_files[ide] = (e, 1) |
| 104 | + elif e.endswith(".evfs"): |
| 105 | + # 检验evfs文件是否合法 |
| 106 | + videos = ms.Evfs(e) |
| 107 | + try: |
| 108 | + videos.parse() |
| 109 | + except: |
| 110 | + evf_evfs_files[ide] = (e, 2) |
| 111 | + else: |
| 112 | + if videos.len() <= 0: |
| 113 | + evf_evfs_files[ide] = (e, 2) |
| 114 | + checksum = ui.checksum_guard.get_checksum( |
| 115 | + videos[0].evf_video.raw_data) |
| 116 | + if video.checksum != checksum: |
| 117 | + evf_evfs_files[ide] = (e, 1) |
| 118 | + continue |
| 119 | + for idcell, cell in enumerate(videos[1:]): |
| 120 | + checksum = ui.checksum_guard.get_checksum( |
| 121 | + cell.evf_video.raw_data + videos[idcell - 1].checksum |
| 122 | + ) |
| 123 | + if cell.evf_video.checksum != checksum: |
| 124 | + evf_evfs_files[ide] = (e, 1) |
| 125 | + continue |
| 126 | + evf_evfs_files[ide] = (e, 0) |
| 127 | + |
| 128 | + # 将结果转换为列表格式 |
| 129 | + if not result["error"]: # 只有在没有错误的情况下才添加数据 |
| 130 | + data_list = [] |
| 131 | + for item in evf_evfs_files: |
| 132 | + if isinstance(item, tuple) and len(item) == 2: |
| 133 | + file_path_item, status = item |
| 134 | + data_list.append({"file": file_path_item, "status": status}) |
| 135 | + result["data"] = data_list |
| 136 | + |
| 137 | + # 写入out.json |
| 138 | + output_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "out.json") |
| 139 | + with open(output_file, "w", encoding="utf-8") as f: |
| 140 | + json.dump(result, f, ensure_ascii=False, indent=2) |
| 141 | + |
124 | 142 | return 0 |
125 | 143 |
|
126 | 144 |
|
|
0 commit comments