forked from MobiFlight/MobiFlight-FirmwareSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_fw_files.py
69 lines (55 loc) · 2.61 KB
/
copy_fw_files.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
Import("env")
import os, zipfile, shutil
from pathlib import Path
# Get the version number from the build environment.
firmware_version = os.environ.get('VERSION', "")
if firmware_version == "":
firmware_version = "0.0.1"
firmware_version = firmware_version.lstrip("v")
firmware_version = firmware_version.strip(".")
zip_file_name = 'Mobiflight-Connector'
build_path = './_build'
build_path_fw = build_path + '/firmware'
build_path_json = build_path + '/Boards'
distrubution_path = './_dist'
board_folder = ['./_Boards/Atmel', './_Boards/RaspberryPi']
def copy_fw_files (source, target, env):
fw_file_name=str(target[0])
if os.path.exists(build_path_fw) == False:
os.makedirs(build_path_fw)
if os.path.exists(build_path_json) == False:
os.makedirs(build_path_json)
if fw_file_name[-3:] == "bin":
fw_file_name=fw_file_name[0:-3] + "uf2"
# Copy build FW file
shutil.copy(fw_file_name, build_path_fw)
# Copy reset/uf2/json files
file_extension = '.hex'
copy_files_by_extension(board_folder, build_path_fw, file_extension)
file_extension = '.uf2'
copy_files_by_extension(board_folder, build_path_fw, file_extension)
file_extension = '.json'
copy_files_by_extension(board_folder, build_path_json, file_extension)
# Create ZIP file and add files from distrubution folder
zip_file_path = distrubution_path + '/' + zip_file_name + '_' + firmware_version + '.zip'
createZIP(build_path, zip_file_path, zip_file_name)
def copy_files_by_extension(source_folders, target_folder, file_extension):
for source_folder in source_folders:
for root, dirs, files in os.walk(source_folder):
for file in files:
if file.endswith(file_extension):
source_path = os.path.join(root, file)
target_path = os.path.join(target_folder, file)
shutil.copy2(source_path, target_path)
def createZIP(original_folder_path, zip_file_path, new_folder_name):
if os.path.exists(distrubution_path) == False:
os.mkdir(distrubution_path)
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
for root, dirs, files in os.walk(original_folder_path):
for file in files:
# Create a new path in the ZIP file
new_path = os.path.join(new_folder_name, os.path.relpath(os.path.join(root, file), original_folder_path))
# Add the file to the ZIP file
zipf.write(os.path.join(root, file), new_path)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", copy_fw_files)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", copy_fw_files)