先上代码,代码运行需要一定的环境,请自行适配:
#xmlTest_write.py
# -*- coding: utf-8 -*-
import sys
import os
import subprocess
import zlib
import xml.dom.minidom
def pre_ota_img(otaDirPath):
#记录镜像与分区之间的关系 键-镜像 值-分区
global soc_img_partiton
global mcu_img_partiton
global camera_img_partiton
soc_img_partiton={"uboot.img":"uboot", "boot.img":"boot", "system.img":"system", "vbmeta.img":"vbmeta"}
mcu_img_partiton={}
camera_img_partiton={}
#先确定soc的部署是否配置
flag="exist"
os.environ.get('TARGET_DEPLOY_DIR')
try:
print("TARGET_DEPLOY_DIR", os.environ['TARGET_DEPLOY_DIR'])
except KeyError:
print("TARGET_DEPLOY_DIR does not exist")
flag=""
#确定soc有部署
if flag:
#获取SOC部署路径
soc_dir_path=os.environ['TARGET_DEPLOY_DIR']
#如果SOC有部署路径
if soc_dir_path:
print(soc_dir_path)
#将SOC部署路径下的目标镜像拷贝到ota目录
for img in soc_img_partiton:
subprocess.call("cp " + soc_dir_path+"/"+img + " " + otaDirPath+"/soc/"+img, shell=True)
else:
print("TARGET_DEPLOY_DIR is NULL")
flag="exist"
os.environ.get('MCU_PATH')
try:
print("MCU_PATH", os.environ['MCU_PATH'])
except KeyError:
print("MCU_PATH does not exist")
flag=""
if flag:
mcu_dir_path=os.environ['MCU_PATH']
if mcu_dir_path:
print(mcu_dir_path)
for img in mcu_img_partiton:
subprocess.call("cp " + mcu_dir_path+"/"+img + " " + otaDirPath+"/mcu/"+img, shell=True)
else:
print("MCU_PATH is NULL")
flag="exist"
os.environ.get('CAMERA_PATH')
try:
print("CAMERA_PATH", os.environ['CAMERA_PATH'])
except KeyError:
print("CAMERA_PATH does not exist")
flag=""
if flag:
camera_dir_path=os.environ['CAMERA_PATH']
if camera_dir_path:
print(camera_dir_path)
for img in camera_img_partiton:
subprocess.call("cp " + camera_dir_path+"/"+img + " " + otaDirPath+"/camera/"+img, shell=True)
else:
print("CAMERA_PATH is NULL")
def addFileInfo(dom, node, crc32, fileNmae, sha256sum, mode):
fileinfo=dom.createElement('fileinfo')
fileinfo.setAttribute("filename", fileNmae)
fileinfo.setAttribute("crc32", crc32)
fileinfo.setAttribute("sha256", sha256sum)
if mode == "soc":
for img in soc_img_partiton:
if img == fileNmae:
fileinfo.setAttribute("partition", soc_img_partiton[img])
if mode == "mcu":
for img in soc_img_partiton:
if img == fileNmae:
fileinfo.setAttribute("partition", soc_img_partiton[img])
if mode == "camera":
for img in soc_img_partiton:
if img == fileNmae:
fileinfo.setAttribute("partition", soc_img_partiton[img])
node.appendChild(fileinfo)
def addVersionInfo(dom, node, name, version):
versioninfo=dom.createElement('version')
versioninfo.setAttribute("name", name)
versioninfo.setAttribute("version", version)
node.appendChild(versioninfo)
def cal_crc(fileName):
prev = 0
for eachLine in open(fileName,"rb"):
prev = zlib.crc32(eachLine, prev)
return "%X"%(prev & 0xFFFFFFFF)
#生成xml文件
def GenerateXml(otaDirPath):
print(otaDirPath)
impl = xml.dom.minidom.getDOMImplementation()
#设置根结点update
dom = impl.createDocument(None, 'update', None)
update = dom.documentElement
#在update下创建project子节点
project = dom.createElement('project')
update.appendChild(project)
project_list = dom.createElement('project')
project.appendChild(project_list)
project_list.setAttribute("name", "j3")
#在update下创建version_list子节点
version_list = dom.createElement('version_list')
update.appendChild(version_list)
#判断是否有soc相关镜像
if os.listdir(otaDirPath+"/soc"):
#这里的版本信息还需要优化。目前还不确定怎么获取版本信息
addVersionInfo(dom, version_list, "bsp", "v1.0.0")
addVersionInfo(dom, version_list, "zros", "v1.0.0")
#在update下创建soc子节点
soc = dom.createElement('soc')
update.appendChild(soc)
soc_install=dom.createElement('install')
soc_install.setAttribute("type", "full")
soc.appendChild(soc_install)
soc_file_list=dom.createElement('FILE_LIST')
soc.appendChild(soc_file_list)
for img in os.listdir(otaDirPath+"/soc"):
crc32Value=cal_crc(otaDirPath+"/soc/"+img)
sha256sum=os.popen("sha256sum " + otaDirPath+"/soc/"+img + " | awk \'{print $1}\'")
addFileInfo(dom, soc_file_list, str(crc32Value), img, sha256sum.read(), "soc")
#判断是否有mcu相关镜像
if os.listdir(otaDirPath+"/mcu"):
#这里的版本信息还需要优化。目前还不确定怎么获取版本信息
addVersionInfo(dom, version_list, "mcu", "v1.0.0")
#在update下创建mcu子节点
mcu = dom.createElement('mcu')
update.appendChild(mcu)
mcu_install=dom.createElement('install')
mcu_install.setAttribute("type", "full")
mcu.appendChild(mcu_install)
mcu_file_list=dom.createElement('FILE_LIST')
mcu.appendChild(mcu_file_list)
for img in os.listdir(otaDirPath+"/mcu"):
crc32Value=cal_crc(otaDirPath+"/mcu/"+img)
sha256sum=os.popen("sha256sum " + otaDirPath+"/mcu/"+img + " | awk \'{print $1}\'")
print(img + " sha256sum:"+ sha256sum)
addFileInfo(dom, mcu_file_list, str(crc32Value), img, sha256sum.read(), "mcu")
#判断是否有camera相关镜像
if os.listdir(otaDirPath+"/camera"):
#这里的版本信息还需要优化。目前还不确定怎么获取版本信息
addVersionInfo(dom, version_list, "camera", "v1.0.0")
#在update下创建camera子节点
camera = dom.createElement('camera')
update.appendChild(camera)
camera_install=dom.createElement('install')
camera_install.setAttribute("type", "full")
camera.appendChild(camera_install)
camera_file_list=dom.createElement('FILE_LIST')
camera.appendChild(camera_file_list)
for img in os.listdir(otaDirPath+"/camera"):
crc32Value=cal_crc(otaDirPath+"/camera/"+img)
sha256sum=os.popen("sha256sum " + otaDirPath+"/camera/"+img + " | awk \'{print $1}\'")
print(img + " sha256sum:"+ sha256sum)
addFileInfo(dom, camera_file_list, str(crc32Value), img, sha256sum.read(), "camera")
f= open(otaDirPath + "/ota_config.xml", 'w') #w替换为a,追加
dom.writexml(f, addindent=' ', newl='\n')
f.close()
print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
def main(argv):
print("start pre_ota_img:")
pre_ota_img(otaDirPath=argv)
print("start GenerateXml:")
GenerateXml(otaDirPath=argv)
if __name__ == '__main__':
main(sys.argv[1])
在python里面调用Linux命令并获取其输出:
sha256sum=os.popen("sha256sum " + otaDirPath+"/camera/"+img + " | awk \'{print $1}\'")
在python里面遍历某个目录:
for img in os.listdir(otaDirPath+"/soc"):
crc32Value=cal_crc(otaDirPath+"/soc/"+img)
sha256sum=os.popen("sha256sum " + otaDirPath+"/soc/"+img + " | awk \'{print $1}\'")
addFileInfo(dom, soc_file_list, str(crc32Value), img, sha256sum.read(), "soc")