-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdscfile.py
182 lines (163 loc) · 6.05 KB
/
pdscfile.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: GPL-2.0
"""
Package tool
~~~~~~~~~~~~
Package tool
File: pdscfile.py
Desc: PDSC FILE的生成
Date: 2023-06-19
Auth: XiangYang<[email protected]>
"""
import os
import datetime
from action import IAction
from project import Project, CMSISPdscFileInfo, FileCategory
import xml.dom.minidom as minidom
import codecs
class GeneratePDSCFile(IAction):
"""
生成pdsc文件
"""
def __init__(self, prj: Project, cfg: CMSISPdscFileInfo):
self.project = prj
self.configure = cfg
def run(self):
output_file = self.project.get_output_fullpath(self.configure.target)
# 生成pdsc文件并保存
with codecs.open(output_file, "w", "utf-8") as f:
pdsc_dom = self._generate_pdsc()
pdsc_dom.writexml(
f,
indent='',
addindent=' ',
newl='\n',
encoding='utf-8')
def _generate_pdsc(self):
"""
生成pdsc文件的dom
"""
dom = minidom.Document()
# comment
comment = dom.createComment('This file was auto generated by tool')
dom.appendChild(comment)
# package节点
package_node = dom.createElement('package')
self._generate_pdsc_package_node(dom, package_node)
dom.appendChild(package_node)
return dom
def _generate_pdsc_package_node(self, dom: minidom.Document, package_node: minidom.Element):
"""
生成包信息的node
"""
pkg = self.project.package
# 节点属性
package_node.setAttribute('schemaVersion', '1.7.7')
package_node.setAttribute(
'xmlns:xs',
'http://www.w3.org/2001/XMLSchema-instance'
)
package_node.setAttribute(
'xs:noNamespaceSchemaLocation',
'https://raw.githubusercontent.com/Open-CMSIS-Pack/Open-CMSIS-Pack-Spec/v1.7.7/schema/PACK.xsd'
)
# 包名
GeneratePDSCFile._add_text_node(dom, package_node, 'name', pkg.name)
# 发行商
GeneratePDSCFile._add_text_node_or_none(
dom, package_node, 'vendor', pkg.organization_name)
# 描述
GeneratePDSCFile._add_text_node_or_none(
dom, package_node, 'description', pkg.description)
# 支持信息
if pkg.support is not None:
support = pkg.support
# issue url
GeneratePDSCFile._add_text_node_or_none(
dom, package_node, 'supportContact', support.issue_url)
# 生成发布信息
self._add_release_info(dom, package_node)
# 生成关键字描述
self._add_keywords(dom, package_node)
# 生成文件
comps_node = dom.createElement('components')
package_node.appendChild(comps_node)
comp_node = dom.createElement('component')
comps_node.appendChild(comp_node)
# 设置包的各个属性
comp_node.setAttribute('Cclass', self.configure.c_class)
comp_node.setAttribute('Cgroup', self.configure.c_group)
comp_node.setAttribute('Csub', self.configure.c_group)
comp_node.setAttribute('Cversion', str(pkg.version))
# 添加文件, dir为打包后的目标位置
# 包括源文件和头文件
files_node = dom.createElement('files')
comp_node.appendChild(files_node)
self._add_files(dom, files_node, 'header', FileCategory.HEADERS)
self._add_files(dom, files_node, 'source', FileCategory.SOURCES)
def _add_files(self, dom: minidom.Document, root: minidom.Element, typ: str, cate: FileCategory):
"""
添加cate范畴的文件到root node, 其拥有类型typ
"""
pkg = self.project.package
for file in pkg.package_files:
if file.category != cate:
continue
pack_root = file.pack_dir
for file_item in file.files:
file_name = os.path.basename(file_item)
file_node = dom.createElement('file')
root.appendChild(file_node)
file_node.setAttribute('category', typ)
file_node.setAttribute('name', f'{pack_root}/{file_name}')
def _add_keywords(self, dom: minidom.Document, root: minidom.Element):
"""
生成关键字描述
"""
pkg = self.project.package
# 创建nodes
node = dom.createElement('keywords')
root.appendChild(node)
for keyword in pkg.keywords:
GeneratePDSCFile._add_text_node(dom, node, 'keyword', keyword)
def _add_release_info(self, dom: minidom.Document, root: minidom.Element):
"""
生成发布信息
暂时还没有把changelog放进来...
"""
pkg = self.project.package
# 创建nodes
node = dom.createElement('releases')
root.appendChild(node)
self._add_release_item(dom, node, str(pkg.version), pkg.description)
def _add_release_item(
self,
dom: minidom.Document,
root: minidom.Element,
version: str,
description: str):
"""
生成单个发布信息的项
"""
node = dom.createElement('release')
node.setAttribute('version', version)
node.setAttribute('date', str(datetime.date.today()))
val_node = dom.createTextNode(description)
node.appendChild(val_node)
root.appendChild(node)
@staticmethod
def _add_text_node_or_none(dom: minidom.Document, root: minidom.Element, name: str, value: str):
"""
在len(value) > 0时添加node, 不然不管
"""
if len(value) > 0:
GeneratePDSCFile._add_text_node(dom, root, name, value)
@staticmethod
def _add_text_node(dom: minidom.Document, root: minidom.Element, name: str, value: str):
"""
创建 `<name>value</name>` 这样子的node
"""
node = dom.createElement(name)
root.appendChild(node)
val_node = dom.createTextNode(value)
node.appendChild(val_node)