-
Notifications
You must be signed in to change notification settings - Fork 5
/
rename.py
131 lines (106 loc) · 3.26 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
import sys
import shutil
import os
import json
import zipfile
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)
project = data['project']
except:
project = data['sketch']
try:
with open('./.vscode/arduino.json') as f:
data = json.load(f)
sketch = data['sketch']
except:
sketch = 'firmware'
try:
with open('./.vscode/arduino.json') as f:
data = json.load(f)
output = data['output']
except:
output = 'build'
print("Version " + version)
# Check if the destination directory exists
if not os.path.exists('./'+output):
os.makedirs('./'+output)
# Specify the source file, the destination for the copy, and the new name
source_file = './'+output+'/'+sketch+'.hex'
destination_directory = './generated/'
new_file_name = project+'_V'+version+'.hex'
new_zip_name = project+'_V'+version+'.zip'
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('./'+output+'/'+new_zip_name):
try:
os.remove('./'+output+'/'+new_zip_name)
except:
print('Cannot delete '+'./'+output+'/'+new_zip_name)
# finally:
# print('Delete '+'./'+output+'/'+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)
# Check if the destination directory exists
if not os.path.exists('./generated/'):
os.makedirs('./generated/')
# Copy the files
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)
bin_name = sketch+'.bin'
zip_name = project+'_V'+version+'.zip'
# 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
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)