-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.py
158 lines (133 loc) · 4.07 KB
/
rename.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
import sys
import shutil
import os
import json
import zipfile
from pathlib import Path
# print("+++++++++++++++++++++++++")
# print("+++++++++++++++++++++++++")
# print("Run rename.py")
# print("+++++++++++++++++++++++++")
# print("+++++++++++++++++++++++++")
try:
with open('./.vscode/arduino.json') as f:
data = json.load(f)
version = data['version']
except:
version = '0.0.0'
# print("Version " + version)
try:
with open('./.vscode/arduino.json') as f:
data = json.load(f)
source_file = './build/'+ data['sketch'] + '.hex'
bin_name = data['sketch'] + '.bin'
nrf_zip_file = data['sketch'] + '.zip'
except:
source_file = '*.hex'
bin_name = '*.bin'
nrf_zip_file = '*.zip'
try:
with open('./.vscode/arduino.json') as f:
data = json.load(f)
board_type = data['board']
except:
board_type = 'rak'
try:
with open('./.vscode/arduino.json') as f:
data = json.load(f)
project_name = data['project']
except:
board_type = 'RUI3'
# print("Source " + source_file)
# print("Binary " + bin_name)
# print("ZIP " + nrf_zip_file)
# print("Board " + board_type)
# Specify the source file, the destination for the copy, and the new name
destination_directory = './generated/'
new_file_name = project_name+'_V'+version+'.hex'
new_zip_name = project_name+'_V'+version+'.zip'
if not os.path.exists(destination_directory):
try:
os.makedirs(destination_directory)
except:
print('Cannot create '+destination_directory)
if os.path.isfile(destination_directory+new_file_name):
try:
os.remove(destination_directory+new_file_name)
except:
print('Cannot delete '+destination_directory+new_file_name)
# finally:
# print('Delete '+destination_directory+new_file_name)
if os.path.isfile(destination_directory+new_zip_name):
try:
os.remove(destination_directory+new_zip_name)
except:
print('Cannot delete '+destination_directory+new_zip_name)
# finally:
# print('Delete '+destination_directory+new_zip_name)
if os.path.isfile('./build/'+new_zip_name):
try:
os.remove('./build/'+new_zip_name)
except:
print('Cannot delete '+'./build/'+new_zip_name)
# finally:
# print('Delete '+'./build/'+new_zip_name)
if os.path.isfile('./generated/'+new_zip_name):
try:
os.remove('./generated/'+new_zip_name)
except:
print('Cannot delete '+'./generated/'+new_zip_name)
# finally:
# print('Delete '+'./generated/'+new_zip_name)
# Copy the files
if board_type.find('rak4631') != -1:
try:
shutil.copy2(source_file, destination_directory)
except:
print('Cannot copy '+source_file +' to '+destination_directory)
# Get the base name of the source file
base_name = os.path.basename(source_file)
# print("Base name " + base_name)
# Construct the paths to the copied file and the new file name
copied_file = os.path.join(destination_directory, base_name)
new_file = os.path.join(destination_directory, new_file_name)
zip_name = project_name+'_V'+version+'.zip'
hex_name = project_name+'_V'+version+'.hex'
# print("Copied file " + copied_file)
# print("Base name " + new_file)
# print("ZIP name " + zip_name)
# print("ZIP content " + bin_name)
# Create ZIP file for WisToolBox
if board_type.find('rak4631') != -1:
try:
os.chdir("./build")
except:
print('Cannot change dir to ./build')
try:
zipfile.ZipFile(zip_name, mode='w').write(bin_name)
except:
print('Cannot zip '+bin_name +' to '+zip_name)
os.chdir("../")
try:
shutil.copy2("./build/"+zip_name, destination_directory)
except:
print('Cannot copy '+"./build/"+zip_name +' to '+destination_directory)
# Rename the file
try:
os.rename(copied_file, new_file)
except:
print('Cannot rename '+copied_file +' to '+new_file)
else:
# Copy the files
try:
shutil.copy2("./build/"+nrf_zip_file, "./generated/"+zip_name)
except:
print('RAK4631 Cannot copy '+nrf_zip_file +' to ./generated/'+zip_name)
try:
shutil.copy2(source_file, "./generated/"+hex_name)
except:
print('RAK4631 Cannot copy '+source_file +' to ./generated/'+hex_name)
print("++++++++++++++++++++++++++++++++++++++++++++++++++")
print("Generated distribution files")
print(*Path("./generated/").iterdir(), sep="\n")
print("++++++++++++++++++++++++++++++++++++++++++++++++++")