-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.py
264 lines (224 loc) · 8.93 KB
/
multi.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""Multiprocessing manipulation on cert files."""
import os
import json
import shutil
import datetime
import multiprocessing
# r'C:\Users\sarah.pentescu\Desktop\copy cert files'
CONSENT = False
CERT_FILES_DIR = "Z:\\Engineering\\01.OnStar\\11.Flashing\\01.Reflash\\Gen11 Cert Files"
TOTAL_INFO_OUTPUT_DIR = "output/output-{}.txt".format(
datetime.datetime.now().strftime("%m-%d-%Y-%H-%M")
)
DIRS_TO_PROCESS = ('MY19(20) GB', 'MY20 TCP ERA GB') # 'MY21 TCP GB')
def consent():
"""Ask user to double check the execution."""
# Remove previous output files
if os.path.exists('output'):
shutil.rmtree('output')
global CONSENT
val = input("Type 'yes' to move ECUIDs: ")
if val == 'yes':
CONSENT = True
def read_json(directory):
"""Read json file."""
with open(directory) as file:
dictionary = json.load(file)
return dictionary
USED_DICT = read_json("reader_output/used_GB.txt")
UNUSED_DICT = read_json("reader_output/unused_GB.txt")
def walk(input, consent):
"""Moves ECU files around within a folder."""
directory = input[0]
total_STID_folders, total_amount_ecu_files, total_used_ecu_files = 0, 0, 0
ecu_counter = 0
print("Processing:", directory)
sub_info_file = open(os.path.join("output", input[1] + '-' + str(os.getpid()) + '.txt'), 'w')
cut_file = open(
os.path.join('output', 'CUT', 'CUT_' + os.path.basename(directory)[:9] + '.txt'), 'w'
)
buffer_file = open(
os.path.join('output', 'BUFFER', 'BUFFER_' + os.path.basename(directory)[:9] + '.txt'), 'w'
)
log_file = open(os.path.join('output', 'LOG_' + os.path.basename(directory)[:9] + '.txt'), 'w')
# Determining the amounts of cuts needed
total_cut_needed, cur_cut = 0, 0
if os.path.basename(directory) == "vehicle_ERA GB":
total_cut_needed = 1600
elif os.path.basename(directory)[:2] == 'CH':
total_cut_needed = 4600
elif os.path.basename(directory)[:2] == 'MX':
total_cut_needed = 3800
elif os.path.basename(directory)[:2] == 'EU':
total_cut_needed = 4600
elif os.path.basename(directory)[:2] == 'NA':
total_cut_needed = 3000
for root, dirs, files in os.walk(directory):
# If we are in a STID folder
dirs.sort(reverse=True)
STID = os.path.basename(root)
if STID.isdigit() and len(STID) == 9:
total_STID_folders += 1
has_ecu = False
file_dirs = []
ecu_file_dir = ''
ecu_dir_name = ''
has_txt = False
for file in files:
if file[-4:] == '.bin':
total_amount_ecu_files += 1
has_ecu = True
ecu_file_dir = os.path.join(root, file)
file_dirs.append(os.path.join(root, file))
ecu_dir_name = file[:-4]
elif file[-4:] == '.txt':
has_txt = True
file_dirs.append(os.path.join(root, file))
if has_ecu:
if STID in USED_DICT or STID in UNUSED_DICT:
total_used_ecu_files += 1
log_file.write(
'{} -> {} USED {}\n'.format(
STID, ecu_dir_name, os.path.basename(directory)
)
)
else: # Unusd ECUID
if cur_cut < total_cut_needed:
if os.path.exists('output/ECUID/' + ecu_dir_name):
ecu_counter += 1
ecu_dir_name += str(ecu_counter)
os.mkdir('output/ECUID/' + ecu_dir_name)
if consent:
for dir in file_dirs:
shutil.move(dir, "output/ECUID/" + ecu_dir_name)
cut_file.write(ecu_file_dir + '\n')
cur_cut += 1
log_file.write(
'{} -> {} CUT {}\n'.format(
STID, ecu_dir_name, os.path.basename(directory)
)
)
else:
buffer_file.write(ecu_file_dir + '\n')
log_file.write(
'{} -> {} BUFFER {}\n'.format(
STID, ecu_dir_name, os.path.basename(directory)
)
)
else:
if has_txt:
log_file.write(
'{} TXT ONLY {}\n'.format(
STID, os.path.basename(directory)
)
)
else:
log_file.write(
'{} -> {} NO ECUID {}\n'.format(
STID, ecu_dir_name, os.path.basename(directory)
)
)
sub_info_file.write(str(total_STID_folders) + '\n')
sub_info_file.write(str(total_amount_ecu_files) + '\n')
sub_info_file.write(str(total_used_ecu_files) + '\n')
sub_info_file.close()
cut_file.close()
buffer_file.close()
log_file.close()
print("Done:", directory)
def integrate():
"""Merge multiple files from different processes."""
info_output = open(TOTAL_INFO_OUTPUT_DIR, 'w')
log_output = open("output/log_file.txt", 'w')
for file in os.listdir('output'):
if file[:3] == 'LOG':
with open('output/' + file, 'r') as subfile:
log_output.write("".join(subfile.readlines()))
os.remove('output/' + file)
for directory in DIRS_TO_PROCESS:
info_output.write(directory + '\n')
data = [0] * 3 # 3 is the number of parameters
for file in os.listdir('output'):
if directory in file:
with open('output/' + file, 'r') as subfile:
for i, info in enumerate([int(line.strip()) for line in subfile.readlines()]):
data[i] += info
os.remove('output/' + file)
for num in data:
info_output.write(str(num))
info_output.write('\n')
info_output.write('\n')
info_output.close()
log_output.close()
def main():
# Make directories if do not exist
if not os.path.exists('output'):
os.mkdir('output')
if not os.path.exists('output/ECUID'):
os.mkdir('output/ECUID')
if not os.path.exists('output/BUFFER'):
os.mkdir('output/BUFFER')
if not os.path.exists('output/CUT'):
os.mkdir('output/CUT')
# Initialize file and directory
info_output = open(TOTAL_INFO_OUTPUT_DIR, 'w')
info_output.close()
dir_list = []
for directory in os.listdir(CERT_FILES_DIR):
if directory[-2:] == 'GB' and directory in DIRS_TO_PROCESS:
dir_list += [
(
os.path.join(os.path.join(CERT_FILES_DIR, os.path.basename(directory)), i),
os.path.basename(directory)
) for i in os.listdir(os.path.join(CERT_FILES_DIR, os.path.basename(directory)))
if os.path.isdir(
os.path.join(os.path.join(CERT_FILES_DIR, os.path.basename(directory)), i)
)
]
# Assign tasks to processes
process_list = []
while dir_list:
process = multiprocessing.Process(target=walk, args=(dir_list.pop(0), CONSENT,))
process.start()
process_list.append(process)
# Wait until all processes finish
for process in process_list:
process.join()
def main2():
# Make directories if do not exist
if not os.path.exists('output'):
os.mkdir('output')
if not os.path.exists('output/ECUID'):
os.mkdir('output/ECUID')
if not os.path.exists('output/BUFFER'):
os.mkdir('output/BUFFER')
if not os.path.exists('output/CUT'):
os.mkdir('output/CUT')
# Initialize file and directory
info_output = open(TOTAL_INFO_OUTPUT_DIR, 'w')
info_output.close()
dir_list = []
for directory in os.listdir(CERT_FILES_DIR):
if directory == "Gen11 Cert Files Copy":
dir_list += [
(
os.path.join(os.path.join(CERT_FILES_DIR, os.path.basename(directory)), i),
os.path.basename(directory)
) for i in os.listdir(os.path.join(CERT_FILES_DIR, os.path.basename(directory)))
if os.path.isdir(
os.path.join(os.path.join(CERT_FILES_DIR, os.path.basename(directory)), i)
)
]
# Assign tasks to processes
process_list = []
while dir_list:
process = multiprocessing.Process(target=walk, args=(dir_list.pop(0), CONSENT,))
process.start()
process_list.append(process)
# Wait until all processes finish
for process in process_list:
process.join()
if __name__ == "__main__":
consent()
main2()
integrate()