package_firmware.py
1.04 KB
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
#!/usr/bin/env python
import glob
import zipfile
import os
FIRMWARE_FILE = 'Firmware.zip'
def extract_file_only(filename, dest):
"""
Extract a file without keeping directories.
"""
# extract firmware files without paths
f_src = archive.open(filename, 'r')
data = f_src.read()
dst_name = os.path.join(dest, os.path.basename(filename))
with open(dst_name, 'w') as f_dst:
f_dst.write(data)
f_src.close()
# open destination archive
with zipfile.ZipFile(FIRMWARE_FILE, 'w') as dest_archive:
# get all zip files in Packages directory
for zip_filename in glob.glob("Packages/*.zip"):
# open zipfile
with zipfile.ZipFile(zip_filename, 'r') as archive:
# look for interesting names
for src_filename in archive.namelist():
# extract firmware files
if os.path.splitext(src_filename)[1] == '.px4':
base_name = os.path.basename(src_filename)
dest_archive.writestr(base_name, archive.read(src_filename))