forked from 18thCentury/CodeSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_archive.py
102 lines (93 loc) · 3.91 KB
/
extract_archive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/env python
# encoding:utf-8
# We enable the new python 3 print syntax
from __future__ import print_function
import codecs
import sys
import os
def naive(text, watershed=2000):
if 'ZAT7000V8_5040D_V03防拆升级' in text:
passwd = 'ZAT7000V863'
elif 'Z125_CR720S' in text:
passwd = 'ZAT8000V863'
elif 'ZAT2500V753E_F501' in text:
passwd = 'ZAT2500V753E'
else:
passwd = text.split('_')[0]
if '.' in passwd:
passwd = passwd.split('.')[0]
# 从第4个字符开始(索引为3)查找数字到下一个字母之间的整数
start_index = 3 # 从第4个字符开始
# 找到第一个数字部分
number_str = ""
while start_index < len(passwd) and passwd[start_index].isdigit():
number_str += passwd[start_index]
start_index += 1
category = int(number_str)
if category <= watershed: # 哪个吨位开始有密码,因为解压归档必须确定有无密码
passwd = None
else:
passwd = passwd.lower()
return passwd, category
if __name__ == '__main__':
print("sys.argv: ", len(sys.argv), " elements:")
for arg in sys.argv:
print(" - ", arg)
if len(sys.argv) > 2:
current_dir = sys.argv[1]
extract_to_path = sys.argv[2]
else:
current_dir = r''
extract_to_path = r''
sys.exit()
decision_table = []
found = False # 标志变量
valid_projects = [] # 用于存储符合条件的文件名的列表
if os.path.isdir(current_dir):
for root, _, files in os.walk(current_dir):
for file in files:
if file.endswith(".projectarchive") and "E107" not in file and "工况簇" in file: # 排除包含 "E107" 的文件
project_path = os.path.join(root, file)
valid_projects.append(project_path) # 存入列表
print("Found:", os.path.relpath(project_path, current_dir))
found = True # 设为 True,表示找到至少一个合格的文件
# 如果没有找到任何符合条件的 .project 文件,则退出
if not found:
print("No valid project files found. Exiting...")
system.exit(0) # 正常退出
else:
print("Path error!!")
system.exit(1)
print(len(valid_projects), 'projects found. Please confirm again!')
for project in valid_projects:
filename, _ = os.path.splitext(os.path.basename(project)) # 分离文件名和扩展
extracted_filepath = os.path.dirname(os.path.join(extract_to_path, os.path.relpath(project, current_dir)))
if not os.path.exists(extracted_filepath):
os.makedirs(extracted_filepath)
decision = {'Series': os.path.relpath(project, current_dir), 'Model': filename, 'extracted': 0}
print("Opening:", '-' * 27)
print("Opening:", os.path.relpath(project, current_dir))
try:
proj = projects.open_archive(project, extracted_filepath, overwrite=True,
encryption_password=naive(filename)[0])
if proj:
decision['extracted'] = 1
print("Success!", '-' * 27)
proj.close() # close open project if necessary
decision_table.append(decision)
except Exception as e:
print('-' * 27, "Failed! " * 3)
decision['Exception'] = str(e) # 文件打不开或不可写入
# 打开 CSV 文件并使用 utf-8 编码
rows = []
header = ['Model', 'extracted', 'Series', 'Exception']
for row in decision_table:
if row.get('Exception'):
row['extracted'] = -1
else:
row['Exception'] = ''
rows.append(','.join(str(row.get(k)) for k in header)) # 先缓存数据
with codecs.open('decision.csv', 'w', encoding='utf-8') as f:
f.write(','.join(header))
f.write('\n')
f.write('\n'.join(rows) + '\n')