This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
procdraw_utils.py
150 lines (121 loc) · 4.78 KB
/
procdraw_utils.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
import os
import subprocess
import sys
from lxml import etree
import yaml
_apache2_header = [
'\n',
' Licensed under the Apache License, Version 2.0 (the "License");\n',
' you may not use this file except in compliance with the License.\n',
' You may obtain a copy of the License at\n',
'\n',
' http://www.apache.org/licenses/LICENSE-2.0\n',
'\n',
' Unless required by applicable law or agreed to in writing, software\n',
' distributed under the License is distributed on an "AS IS" BASIS,\n',
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n',
' See the License for the specific language governing permissions and\n',
' limitations under the License.\n'
]
class CheckResult:
def __init__(self, is_ok, description, details=None):
self.is_ok = is_ok
self.description = description
self.details = details
class CheckResultTapReporter:
def __init__(self, plan):
self.plan = plan
self.num_tests = 0
self.num_pass = 0
self.num_fail = 0
print("1..{:d}".format(self.plan))
def add(self, result):
self.num_tests += 1
if result.is_ok:
print("ok {:d} {:s}".format(self.num_tests, result.description))
self.num_pass += 1
else:
print("not ok {:d} {:s}".format(self.num_tests, result.description))
self.num_fail += 1
if result.details is not None:
print(" ---")
for line in yaml.dump(result.details).splitlines():
print(" {}".format(line))
print(" ---")
def is_fail(self):
return (self.num_fail > 0) or (self.num_tests != self.plan)
def print_summary(self):
print("# tests {:d}".format(self.num_tests))
print("# pass {:d}".format(self.num_pass))
if self.num_fail > 0:
print("# fail {:d}".format(self.num_fail))
if self.num_tests != self.plan:
print("# unexpected number of tests")
if not self.is_fail():
print("# ok")
class Apache2HeaderChecker:
def __init__(self):
self.file = None
self.prefix = None
self.line = None
def check(self, file, prefix):
self.file = file
self.prefix = prefix
with open(file) as file_in:
self.line = file_in.readline()
copyright_ok, copyright_result = self._check_copyright(file_in)
if not copyright_ok:
return copyright_result
return self._check_header(file_in)
def _check_copyright(self, file_in):
if not self.line.startswith(self.prefix + " Copyright "):
return False, self._not_ok(self.prefix + " Copyright \n", self.line)
self.line = file_in.readline()
while self.line.startswith(self.prefix + " Copyright "):
self.line = file_in.readline()
return True, None
def _check_header(self, file_in):
for i in range(len(_apache2_header)):
if self.line != self.prefix + _apache2_header[i]:
return self._not_ok(self.prefix + _apache2_header[i], self.line)
else:
self.line = file_in.readline()
return self._ok()
def _ok(self):
return CheckResult(True, self.file)
def _not_ok(self, expected, got):
return CheckResult(False, self.file, {
"expected": expected.rstrip(),
"got": got.rstrip()
})
def find(paths):
for path in paths:
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
yield os.path.join(dirpath, filename)
def is_cpp_file(filename):
return filename.endswith(".cpp") or filename.endswith(".h")
def find_cpp_files(paths):
return (f for f in find(paths) if is_cpp_file(f))
def mkdir_p(path):
if not os.path.exists(path):
os.makedirs(os.path.abspath(path))
def run(args):
args = list(args)
print(" ".join(args))
completed_process = subprocess.run(args, shell=False)
if completed_process.returncode != 0:
sys.exit("*** ERROR {:d}".format(completed_process.returncode))
def validate_xml(schema_file, xml_file):
schema_doc = etree.parse(schema_file)
validator = etree.RelaxNG(schema_doc)
xml_doc = etree.parse(xml_file)
if validator.validate(xml_doc):
return CheckResult(True, os.path.relpath(xml_file))
else:
errors = []
for error in validator.error_log:
errors.append("{:d}:{:d}: {:s}".format(error.line, error.column, error.message))
return CheckResult(False, os.path.relpath(xml_file), {
"errors": errors
})