-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcheck_xml.py
78 lines (67 loc) · 2.12 KB
/
check_xml.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
# coding=utf-8
"""
Authority:
Date:
2018-3-30 00:10
Update:
2018-4-4 17:18
Usage:
将该脚本与要检测的目录放于同一级,并且XML_ROOT_DIRECTORY改为该目录名
"""
import os
from xml.dom import expatbuilder
CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) # 当前文件的路径
XML_ROOT_DIRECTORY_LIST = ["epon", "server", "client"] # xml文件所在的根目录名称,可配置
def acquire_xml_files(path):
"""
获取目录下所有的xml文件
"""
xml_files = []
for dirpath, _, filenames in os.walk(path):
for name in filenames:
_file = os.path.join(dirpath, name)
if _file.endswith(".xml"):
xml_files.append(_file)
return xml_files
def parse_xml_files(xml_files):
"""
解析所有的xml文件,返回错误日志
"""
for file in xml_files:
file_content = _ignore_xml_comment(file)
builder = expatbuilder.ExpatBuilderNS()
try:
builder.parseString(file_content)
except Exception as e:
if "multi-byte" not in str(e):
print("[path]:{0}\t[error]:{1}\r\n".format(file, e))
def _ignore_xml_comment(file):
"""
忽略xml的注释
"""
no_comment_file = ""
with open(file, "r") as f:
line = f.readline()
while line:
if "<!--" in line:
if "-->" in line:
# 忽略该行
line = f.readline()
continue
next_line = f.readline()
while next_line and "-->" not in next_line:
next_line = f.readline()
# 非注释行
line = f.readline()
no_comment_file += line
line = f.readline()
return no_comment_file
if __name__ == "__main__":
print("Check begin:")
print("------" * 10 + "\r\n")
for dire in XML_ROOT_DIRECTORY_LIST:
xml_files = acquire_xml_files(CURRENT_DIRECTORY + "/" + dire)
parse_xml_files(xml_files)
print("------" * 10 + "\r\n")
print("Completed!")