1 Star 0 Fork 0

PersonalSummary/gerrit_sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
xml_parse.py 3.99 KB
一键复制 编辑 原始数据 按行查看 历史
wenfei6316 提交于 2023-09-04 14:13 . init gitee sync code
#!/usr/bin/python3
# _*_ coding:utf-8 _*_
# @Time : 2023/2/4 1:25
# @Author : wen_fei
# @Email : [email protected]
# @File : xml_parse.py
# @Project : CreatGitLabPrj
import os
class XmlParse:
def __init__(self, ohos_path=None, default_xml=None):
self.ohos_path = ohos_path
self.manifests_path = os.path.join(self.ohos_path, '.repo', 'manifests')
self.default_xml = default_xml
self.xml_list = []
self.plist = []
# 解析出default_xml
def get_default_xml(self):
manifest = os.path.join(self.ohos_path, '.repo/manifest.xml')
print('manifest xml is {}'.format(manifest))
with open(manifest, 'r') as file:
for line in file.readlines():
if not line.count('include name'):
continue
self.default_xml = line.split('"')[1]
break
print("get_default_xml is {}".format(self.default_xml))
return self.default_xml
# 解析xml文件
def _get_xml_list(self, xml_file):
with open(xml_file, 'r') as file:
for line in file.readlines():
if line.count('include name'):
tmp_file = line.split('"')[1]
new_xml = os.path.join(self.manifests_path, tmp_file)
self._get_xml_list(new_xml)
self.xml_list.append(xml_file)
# 打印xml list
def show_xml_list(self):
print("start show xml list:")
for list in self.xml_list:
print(list)
return
# 解析出default_xml包含的xml
def get_xml_list(self):
if not self.default_xml:
self.get_default_xml()
main_xml = os.path.join(self.manifests_path, self.default_xml)
print('main_xml is {}'.format(main_xml))
self._get_xml_list(main_xml)
self.show_xml_list()
return self.xml_list
# default param解析
def parse_default_param(self, context):
pass
# 解析project
def _parse_project(self, context):
list = context.split()
project_name = None
project_path = None
pdict = {}
for str in list:
if str.count('name='):
project_name = str.split('"')[1]
continue
if str.count('path='):
project_path = str.split('"')[1]
continue
if project_name and project_path:
pdict['name'] = project_name
pdict['path'] = os.path.join(self.ohos_path, project_path)
self.plist.append(pdict)
return
# 解析remove project
def _parse_remove_project(self, context):
list = context.split()
project_name = None
for str in list:
if str.count('name='):
project_name = str.split('"')[1]
break
if not project_name:
return
for proj in self.plist:
if project_name == proj['name']:
self.plist.remove(proj)
return
# 解析xml文件
def _parse_xml(self, xml_file):
print("xml_file is {}".format(xml_file))
with open(xml_file, 'r') as file:
for line in file.readlines():
if line.count('<default'):
self.parse_default_param(line)
if line.count('<project'):
self._parse_project(line)
if line.count('<remove-project'):
self._parse_remove_project(line)
return
# 按xml_list顺序解析xml,获取project名称和路径
def parse_xml(self):
if not self.xml_list:
self.get_xml_list()
for list in self.xml_list:
self._parse_xml(list)
# 打印plist
def show_plist(self):
print("start print plist:")
for list in self.plist:
print("name = {}, path = {}".format(list['name'], list['path']))
if __name__ == '__main__':
xmlParse = XmlParse(ohos_path='.')
xmlParse.parse_xml()
xmlParse.show_plist()
pass
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/personal-summary/gerrit_sync.git
[email protected]:personal-summary/gerrit_sync.git
personal-summary
gerrit_sync
gerrit_sync
master

搜索帮助