FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pre-commit/pre_commit/util.py at wip_progress_dots · pre-commit/pre-commit · GitHub
pre-commit
pre-commit
Repository navigation
Code
Issues
17
(17)
Pull requests
8
(8)
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
pre-commit
/
pre_commit
/
util.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
244 lines (193 loc) · 6.6 KB
Breadcrumbs
pre-commit
/
pre_commit
/
util.py
Copy path
File metadata and controls
244 lines (193 loc) · 6.6 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from
__future__
import
unicode_literals
import
contextlib
import
errno
import
os
.
path
import
shutil
import
stat
import
subprocess
import
sys
import
tempfile
import
six
from
pre_commit
import
five
from
pre_commit
import
parse_shebang
if
sys
.
version_info
>=
(
3
,
7
):
# pragma: no cover (PY37+)
from
importlib
.
resources
import
open_binary
from
importlib
.
resources
import
read_text
else
:
# pragma: no cover (<PY37)
from
importlib_resources
import
open_binary
from
importlib_resources
import
read_text
def
mkdirp
(
path
):
try
:
os
.
makedirs
(
path
)
except
OSError
:
if
not
os
.
path
.
exists
(
path
):
raise
@
contextlib
.
contextmanager
def
clean_path_on_failure
(
path
):
"""Cleans up the directory on an exceptional failure."""
try
:
yield
except
BaseException
:
if
os
.
path
.
exists
(
path
):
rmtree
(
path
)
raise
@
contextlib
.
contextmanager
def
noop_context
():
yield
@
contextlib
.
contextmanager
def
tmpdir
():
"""Contextmanager to create a temporary directory. It will be cleaned up
afterwards.
"""
tempdir
=
tempfile
.
mkdtemp
()
try
:
yield
tempdir
finally
:
rmtree
(
tempdir
)
def
resource_bytesio
(
filename
):
return
open_binary
(
'pre_commit.resources'
,
filename
)
def
resource_text
(
filename
):
return
read_text
(
'pre_commit.resources'
,
filename
)
def
make_executable
(
filename
):
original_mode
=
os
.
stat
(
filename
).
st_mode
os
.
chmod
(
filename
,
original_mode
|
stat
.
S_IXUSR
|
stat
.
S_IXGRP
|
stat
.
S_IXOTH
,
)
class
CalledProcessError
(
RuntimeError
):
def
__init__
(
self
,
returncode
,
cmd
,
expected_returncode
,
output
=
None
):
super
(
CalledProcessError
,
self
).
__init__
(
returncode
,
cmd
,
expected_returncode
,
output
,
)
self
.
returncode
=
returncode
self
.
cmd
=
cmd
self
.
expected_returncode
=
expected_returncode
self
.
output
=
output
def
to_bytes
(
self
):
output
=
[]
for
maybe_text
in
self
.
output
:
if
maybe_text
:
output
.
append
(
b'
\n
'
+
five
.
to_bytes
(
maybe_text
).
replace
(
b'
\n
'
,
b'
\n
'
),
)
else
:
output
.
append
(
b'(none)'
)
return
b''
.
join
((
five
.
to_bytes
(
'Command: {!r}
\n
'
'Return code: {}
\n
'
'Expected return code: {}
\n
'
.
format
(
self
.
cmd
,
self
.
returncode
,
self
.
expected_returncode
,
),
),
b'Output: '
,
output
[
0
],
b'
\n
'
,
b'Errors: '
,
output
[
1
],
))
def
to_text
(
self
):
return
self
.
to_bytes
().
decode
(
'UTF-8'
)
if
six
.
PY2
:
# pragma: no cover (py2)
__str__
=
to_bytes
__unicode__
=
to_text
else
:
# pragma: no cover (py3)
__bytes__
=
to_bytes
__str__
=
to_text
def
_cmd_kwargs
(
*
cmd
,
**
kwargs
):
# py2/py3 on windows are more strict about the types here
cmd
=
tuple
(
five
.
n
(
arg
)
for
arg
in
cmd
)
kwargs
[
'env'
]
=
{
five
.
n
(
key
):
five
.
n
(
value
)
for
key
,
value
in
kwargs
.
pop
(
'env'
, {}).
items
()
}
or
None
for
arg
in
(
'stdin'
,
'stdout'
,
'stderr'
):
kwargs
.
setdefault
(
arg
,
subprocess
.
PIPE
)
return
cmd
,
kwargs
def
cmd_output_b
(
*
cmd
,
**
kwargs
):
retcode
=
kwargs
.
pop
(
'retcode'
,
0
)
cmd
,
kwargs
=
_cmd_kwargs
(
*
cmd
,
**
kwargs
)
try
:
cmd
=
parse_shebang
.
normalize_cmd
(
cmd
)
except
parse_shebang
.
ExecutableNotFoundError
as
e
:
returncode
,
stdout_b
,
stderr_b
=
e
.
to_output
()
else
:
proc
=
subprocess
.
Popen
(
cmd
,
**
kwargs
)
stdout_b
,
stderr_b
=
proc
.
communicate
()
returncode
=
proc
.
returncode
if
retcode
is
not
None
and
retcode
!=
returncode
:
raise
CalledProcessError
(
returncode
,
cmd
,
retcode
,
output
=
(
stdout_b
,
stderr_b
),
)
return
returncode
,
stdout_b
,
stderr_b
def
cmd_output
(
*
cmd
,
**
kwargs
):
returncode
,
stdout_b
,
stderr_b
=
cmd_output_b
(
*
cmd
,
**
kwargs
)
stdout
=
stdout_b
.
decode
(
'UTF-8'
)
if
stdout_b
is
not
None
else
None
stderr
=
stderr_b
.
decode
(
'UTF-8'
)
if
stderr_b
is
not
None
else
None
return
returncode
,
stdout
,
stderr
if
os
.
name
!=
'nt'
:
# pragma: windows no cover
from
os
import
openpty
import
termios
class
Pty
(
object
):
def
__init__
(
self
):
self
.
r
=
self
.
w
=
None
def
__enter__
(
self
):
self
.
r
,
self
.
w
=
openpty
()
# tty flags normally change \n to \r\n
attrs
=
termios
.
tcgetattr
(
self
.
r
)
attrs
[
1
]
&=
~
(
termios
.
ONLCR
|
termios
.
OPOST
)
termios
.
tcsetattr
(
self
.
r
,
termios
.
TCSANOW
,
attrs
)
return
self
def
close_w
(
self
):
if
self
.
w
is
not
None
:
os
.
close
(
self
.
w
)
self
.
w
=
None
def
close_r
(
self
):
assert
self
.
r
is
not
None
os
.
close
(
self
.
r
)
self
.
r
=
None
def
__exit__
(
self
,
exc_type
,
exc_value
,
traceback
):
self
.
close_w
()
self
.
close_r
()
def
cmd_output_p
(
*
cmd
,
**
kwargs
):
assert
kwargs
.
pop
(
'retcode'
)
is
None
assert
kwargs
[
'stderr'
]
==
subprocess
.
STDOUT
,
kwargs
[
'stderr'
]
cmd
,
kwargs
=
_cmd_kwargs
(
*
cmd
,
**
kwargs
)
try
:
cmd
=
parse_shebang
.
normalize_cmd
(
cmd
)
except
parse_shebang
.
ExecutableNotFoundError
as
e
:
return
e
.
to_output
()
with
open
(
os
.
devnull
)
as
devnull
,
Pty
()
as
pty
:
kwargs
.
update
({
'stdin'
:
devnull
,
'stdout'
:
pty
.
w
,
'stderr'
:
pty
.
w
})
proc
=
subprocess
.
Popen
(
cmd
,
**
kwargs
)
pty
.
close_w
()
buf
=
b''
while
True
:
try
:
bts
=
os
.
read
(
pty
.
r
,
4096
)
except
OSError
as
e
:
if
e
.
errno
==
errno
.
EIO
:
bts
=
b''
else
:
raise
else
:
buf
+=
bts
if
not
bts
:
break
return
proc
.
wait
(),
buf
,
None
else
:
# pragma: no cover
cmd_output_p
=
cmd_output_b
def
rmtree
(
path
):
"""On windows, rmtree fails for readonly dirs."""
def
handle_remove_readonly
(
func
,
path
,
exc
):
excvalue
=
exc
[
1
]
if
(
func
in
(
os
.
rmdir
,
os
.
remove
,
os
.
unlink
)
and
excvalue
.
errno
==
errno
.
EACCES
):
for
p
in
(
path
,
os
.
path
.
dirname
(
path
)):
os
.
chmod
(
p
,
os
.
stat
(
p
).
st_mode
|
stat
.
S_IWUSR
)
func
(
path
)
else
:
raise
shutil
.
rmtree
(
path
,
ignore_errors
=
False
,
onerror
=
handle_remove_readonly
)
def
parse_version
(
s
):
"""poor man's version comparison"""
return
tuple
(
int
(
p
)
for
p
in
s
.
split
(
'.'
))
Back
|
FazBrowse Home
|
New Git URL