FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pre-commit/pre_commit/file_lock.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
/
file_lock.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (54 loc) · 2.04 KB
Breadcrumbs
pre-commit
/
pre_commit
/
file_lock.py
Copy path
File metadata and controls
64 lines (54 loc) · 2.04 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
from
__future__
import
absolute_import
from
__future__
import
unicode_literals
import
contextlib
import
errno
try
:
# pragma: no cover (windows)
import
msvcrt
# https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/locking
# on windows we lock "regions" of files, we don't care about the actual
# byte region so we'll just pick *some* number here.
_region
=
0xffff
@
contextlib
.
contextmanager
def
_locked
(
fileno
,
blocked_cb
):
try
:
msvcrt
.
locking
(
fileno
,
msvcrt
.
LK_NBLCK
,
_region
)
except
IOError
:
blocked_cb
()
while
True
:
try
:
msvcrt
.
locking
(
fileno
,
msvcrt
.
LK_LOCK
,
_region
)
except
IOError
as
e
:
# Locking violation. Returned when the _LK_LOCK or _LK_RLCK
# flag is specified and the file cannot be locked after 10
# attempts.
if
e
.
errno
!=
errno
.
EDEADLOCK
:
raise
else
:
break
try
:
yield
finally
:
# From cursory testing, it seems to get unlocked when the file is
# closed so this may not be necessary.
# The documentation however states:
# "Regions should be locked only briefly and should be unlocked
# before closing a file or exiting the program."
msvcrt
.
locking
(
fileno
,
msvcrt
.
LK_UNLCK
,
_region
)
except
ImportError
:
# pragma: windows no cover
import
fcntl
@
contextlib
.
contextmanager
def
_locked
(
fileno
,
blocked_cb
):
try
:
fcntl
.
flock
(
fileno
,
fcntl
.
LOCK_EX
|
fcntl
.
LOCK_NB
)
except
IOError
:
# pragma: no cover (tests are single-threaded)
blocked_cb
()
fcntl
.
flock
(
fileno
,
fcntl
.
LOCK_EX
)
try
:
yield
finally
:
fcntl
.
flock
(
fileno
,
fcntl
.
LOCK_UN
)
@
contextlib
.
contextmanager
def
lock
(
path
,
blocked_cb
):
with
open
(
path
,
'a+'
)
as
f
:
with
_locked
(
f
.
fileno
(),
blocked_cb
):
yield
Back
|
FazBrowse Home
|
New Git URL