FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
unified-runtime/scripts/util.py at main · oneapi-src/unified-runtime · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
oneapi-src
/
unified-runtime
Public
Notifications
You must be signed in to change notification settings
Fork
120
Star
57
Code
Issues
109
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
unified-runtime
/
scripts
/
util.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
191 lines (149 loc) · 5.17 KB
Breadcrumbs
unified-runtime
/
scripts
/
util.py
Copy path
File metadata and controls
191 lines (149 loc) · 5.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
183
184
185
186
187
188
189
190
191
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import
os
import
shutil
import
re
import
configparser
import
glob
import
json
from
typing
import
Any
,
Iterator
,
List
,
Union
import
yaml
import
subprocess
from
mako
.
template
import
Template
from
mako
import
exceptions
try
:
from
yaml
import
CLoader
as
Loader
except
ImportError
:
from
yaml
import
Loader
def
exists
(
path
:
str
)
->
bool
:
"""safely checks if path/file exists"""
return
bool
(
path
and
os
.
path
.
exists
(
path
))
def
makePath
(
path
:
str
)
->
None
:
"""create path if it doesn't exist"""
try
:
if
not
exists
(
path
):
os
.
makedirs
(
path
)
except
BaseException
:
print
(
"warning: failed to make %s"
%
path
)
def
copyTree
(
src
:
str
,
dst
:
str
)
->
None
:
"""copy tree"""
try
:
shutil
.
copytree
(
src
,
dst
)
except
BaseException
:
print
(
"warning: failed to copy %s to %s"
%
(
src
,
dst
))
def
removePath
(
path
:
str
)
->
None
:
"""remove directory and all contents"""
try
:
shutil
.
rmtree
(
path
)
except
BaseException
:
print
(
"warning: failed to remove %s"
%
path
)
def
removeFile
(
lst
:
list
[
str
])
->
None
:
"""removes all files in list"""
for
f
in
lst
or
[]:
try
:
os
.
remove
(
f
)
except
BaseException
:
print
(
"warning: failed to remove %s"
%
f
)
def
findFiles
(
path
:
str
,
pattern
:
str
)
->
List
[
str
]:
"""returns a list of files in path matching pattern"""
try
:
return
sorted
(
glob
.
glob
(
os
.
path
.
join
(
path
,
pattern
)))
except
BaseException
:
print
(
"warning: unable to find %s"
%
path
)
return
[]
def
removeFiles
(
path
:
str
,
pattern
:
str
)
->
None
:
"""removes all files in path matching pattern"""
for
f
in
findFiles
(
path
,
pattern
):
try
:
os
.
remove
(
f
)
except
BaseException
:
print
(
"warning: failed to remove %s"
%
f
)
def
textRead
(
path
:
str
)
->
List
[
str
]:
"""reads from text file, returns list of lines"""
try
:
with
open
(
path
,
"r"
)
as
fin
:
return
fin
.
readlines
()
except
Exception
as
e
:
print
(
"error: unable to read %s"
%
path
)
raise
e
def
configRead
(
path
:
str
)
->
configparser
.
ConfigParser
:
"""read from ini file, returns config obj"""
try
:
parser
=
configparser
.
ConfigParser
(
interpolation
=
configparser
.
ExtendedInterpolation
()
)
parser
.
read
(
path
)
return
parser
except
BaseException
as
error
:
print
(
"error: unable to read %s"
%
path
)
raise
error
def
jsonRead
(
path
:
str
)
->
Union
[
list
,
dict
]:
"""read from json file, returns list/dict"""
try
:
with
open
(
path
,
"r"
)
as
fin
:
return
json
.
loads
(
fin
.
read
())
except
BaseException
as
error
:
print
(
"error: unable to read %s"
%
path
)
raise
error
def
jsonWrite
(
path
:
str
,
data
:
Union
[
list
,
dict
])
->
None
:
"""writes list/dict to json file"""
try
:
with
open
(
path
,
"w"
)
as
fout
:
fout
.
write
(
json
.
dumps
(
data
,
indent
=
2
,
sort_keys
=
True
))
except
BaseException
as
error
:
print
(
"error: unable to write %s"
%
path
)
raise
error
def
yamlRead
(
path
:
str
)
->
Iterator
[
Any
]:
"""read from yml file, returns list/dict"""
try
:
with
open
(
path
,
"r"
)
as
fin
:
return
yaml
.
load_all
(
fin
.
read
(),
Loader
=
Loader
)
except
BaseException
as
error
:
print
(
"error: unable to read %s"
%
path
)
raise
error
makoFileList
=
[]
makoErrorList
=
[]
def
makoWrite
(
inpath
:
str
,
outpath
:
str
,
**
args
)
->
int
:
"""generates file using template, args"""
try
:
template
=
Template
(
filename
=
inpath
)
rendered
=
template
.
render
(
**
args
)
assert
isinstance
(
rendered
,
str
)
rendered
=
re
.
sub
(
r"\r\n"
,
r"\n"
,
rendered
)
with
open
(
outpath
,
"w"
)
as
fout
:
fout
.
write
(
rendered
)
makoFileList
.
append
(
outpath
)
return
len
(
rendered
.
splitlines
())
except
BaseException
as
error
:
print
(
exceptions
.
text_error_template
().
render
())
raise
error
def
makoFileListWrite
(
outpath
:
str
)
->
None
:
jsonWrite
(
outpath
,
makoFileList
)
def
formatGeneratedFiles
(
clang_format
:
str
)
->
None
:
for
file
in
makoFileList
:
if
re
.
search
(
r"(\.h|\.hpp|\.c|\.cpp|\.def)$"
,
file
)
is
None
:
continue
print
(
"Formatting {}"
.
format
(
file
))
proc
=
subprocess
.
run
(
[
clang_format
,
"--style=file"
,
"-i"
,
file
],
stderr
=
subprocess
.
PIPE
,
)
if
proc
.
returncode
!=
0
:
print
(
"-- clang-format failed with non-zero return code. --"
)
print
(
proc
.
stderr
.
decode
())
raise
Exception
(
"Failed to format {}"
.
format
(
file
))
def
makeErrorCount
()
->
int
:
return
len
(
makoErrorList
)
def
writelines
(
fout
:
str
,
lines
:
List
[
str
])
->
None
:
"""write to array of string lines to file"""
try
:
with
open
(
fout
,
"w"
)
as
f
:
f
.
writelines
(
lines
)
f
.
close
()
except
BaseException
:
print
(
"Could not write %s"
%
fout
)
def
to_snake_case
(
s
:
str
):
f
=
re
.
sub
(
"(.)([A-Z][a-z]+)"
,
r"\1_\2"
,
s
)
return
re
.
sub
(
"([a-z])([A-Z0-9])"
,
r"\1_\2"
,
f
)
Back
|
FazBrowse Home
|
New Git URL