LPT26x-HSF-4MB-Hilink_14.2..../package.py

61 lines
2.1 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from hashlib import sha256
import json
import os
import shutil
import zipfile
# 获取当前目录
current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建输出目录路径
package_dir = os.path.join(current_dir, "output", "package")
shutil.rmtree(package_dir, ignore_errors=True)
shutil.rmtree(os.path.join(current_dir, "output", "package.zip"), ignore_errors=True)
# 确保输出目录存在
os.makedirs(package_dir, exist_ok=True)
# 复制文件
shutil.copy(os.path.join(current_dir, "output", "LPT262_hilink_UPGRADE.bin"),
os.path.join(package_dir, "image2_all_ota1.bin"))
shutil.copy(os.path.join(current_dir, "output", "LPT262_hilink_UPGRADE.bin"),
os.path.join(package_dir, "image2_all_ota2.bin"))
print("文件已复制到输出目录")
json_file = os.path.join(current_dir, "output", "package", "filelist.json")
json_data = {}
def calc_sha256(filepath):
h = sha256()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
h.update(chunk)
return h.hexdigest()
file1 = os.path.join(package_dir, "image2_all_ota1.bin")
file2 = os.path.join(package_dir, "image2_all_ota2.bin")
json_data['image2_all_ota1.bin'] = {}
json_data['image2_all_ota1.bin']['sha256'] = calc_sha256(file1)
json_data['image2_all_ota2.bin'] = {}
json_data['image2_all_ota2.bin']['sha256'] = calc_sha256(file2)
print("file1 sha1: ", json_data['image2_all_ota1.bin']['sha256'])
print("file2 sha2: ", json_data['image2_all_ota2.bin']['sha256'])
with open(json_file, "w") as f:
json.dump(json_data, f)
package_file = os.path.join(current_dir, "output", "package.zip")
with zipfile.ZipFile(package_file, "w") as zip:
# 遍历package_dir目录及其所有子文件夹将所有文件打包并保留目录结构
for root, dirs, files in os.walk(package_dir):
for file in files:
file_path = os.path.join(root, file)
# 归一化存储在zip中的路径使其以package_dir为根目录
arcname = os.path.relpath(file_path, os.path.dirname(package_dir))
zip.write(file_path, arcname)
print("升级包package.zip sha256: ", calc_sha256(package_file))