Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7cb3e00

Browse files
committed
Only print that the lock is being acquired when waiting
1 parent 4aa787d commit 7cb3e00

5 files changed

Lines changed: 34 additions & 28 deletions

File tree

pre_commit/file_lock.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
_region = 0xffff
1313

1414
@contextlib.contextmanager
15-
def _locked(fileno):
16-
while True:
17-
try:
18-
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region)
19-
except OSError as e:
20-
# Locking violation. Returned when the _LK_LOCK or _LK_RLCK
21-
# flag is specified and the file cannot be locked after 10
22-
# attempts.
23-
if e.errno != errno.EDEADLOCK:
24-
raise
25-
else:
26-
break
15+
def _locked(fileno, blocked_cb):
16+
try:
17+
msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region)
18+
except IOError:
19+
blocked_cb()
20+
while True:
21+
try:
22+
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region)
23+
except IOError as e:
24+
# Locking violation. Returned when the _LK_LOCK or _LK_RLCK
25+
# flag is specified and the file cannot be locked after 10
26+
# attempts.
27+
if e.errno != errno.EDEADLOCK:
28+
raise
29+
else:
30+
break
2731

2832
try:
2933
yield
@@ -38,16 +42,20 @@ def _locked(fileno):
3842
import fcntl
3943

4044
@contextlib.contextmanager
41-
def _locked(fileno):
42-
fcntl.flock(fileno, fcntl.LOCK_EX)
45+
def _locked(fileno, blocked_cb):
46+
try:
47+
fcntl.flock(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB)
48+
except IOError:
49+
blocked_cb()
50+
fcntl.flock(fileno, fcntl.LOCK_EX)
4351
try:
4452
yield
4553
finally:
4654
fcntl.flock(fileno, fcntl.LOCK_UN)
4755

4856

4957
@contextlib.contextmanager
50-
def lock(path):
58+
def lock(path, blocked_cb):
5159
with open(path, 'a+') as f:
52-
with _locked(f.fileno()):
60+
with _locked(f.fileno(), blocked_cb):
5361
yield

pre_commit/store.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ def __init__(self, directory=None):
4747
self.directory = directory
4848

4949
@contextlib.contextmanager
50-
def exclusive_lock(self, quiet=False):
51-
if not quiet:
50+
def exclusive_lock(self):
51+
def blocked_cb(): # pragma: no cover (tests are single-process)
5252
logger.info('Locking pre-commit directory')
53-
with file_lock.lock(os.path.join(self.directory, '.lock')):
53+
54+
with file_lock.lock(os.path.join(self.directory, '.lock'), blocked_cb):
5455
yield
5556

5657
def _write_readme(self):
@@ -89,7 +90,7 @@ def _create(self):
8990

9091
if os.path.exists(self.db_path):
9192
return
92-
with self.exclusive_lock(quiet=True):
93+
with self.exclusive_lock():
9394
# Another process may have already completed this work
9495
if os.path.exists(self.db_path): # pragma: no cover (race)
9596
return

tests/commands/install_uninstall_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
142142

143143

144144
NORMAL_PRE_COMMIT_RUN = re.compile(
145-
r'^\[INFO\] Locking pre-commit directory\r?\n'
146-
r'\[INFO\] Initializing environment for .+\.\r?\n'
145+
r'^\[INFO\] Initializing environment for .+\.\r?\n'
147146
r'Bash hook\.+Passed\r?\n'
148147
r'\[master [a-f0-9]{7}\] Commit!\r?\n' +
149148
FILES_CHANGED +
@@ -255,8 +254,7 @@ def test_environment_not_sourced(tempdir_factory):
255254

256255

257256
FAILING_PRE_COMMIT_RUN = re.compile(
258-
r'^\[INFO\] Locking pre-commit directory\r?\n'
259-
r'\[INFO\] Initializing environment for .+\.\r?\n'
257+
r'^\[INFO\] Initializing environment for .+\.\r?\n'
260258
r'Failing hook\.+Failed\r?\n'
261259
r'hookid: failing_hook\r?\n'
262260
r'\r?\n'
@@ -334,7 +332,6 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory):
334332

335333
FAIL_OLD_HOOK = re.compile(
336334
r'fail!\r?\n'
337-
r'\[INFO\] Locking pre-commit directory\r?\n'
338335
r'\[INFO\] Initializing environment for .+\.\r?\n'
339336
r'Bash hook\.+Passed\r?\n',
340337
)

tests/repository_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ def test_reinstall(tempdir_factory, store, log_info_mock):
559559
config = make_config_from_repo(path)
560560
repo = Repository.create(config, store)
561561
repo.require_installed()
562-
# We print some logging during clone (2) + install (4)
563-
assert log_info_mock.call_count == 6
562+
# We print some logging during clone (1) + install (3)
563+
assert log_info_mock.call_count == 4
564564
log_info_mock.reset_mock()
565565
# Reinstall with same repo should not trigger another install
566566
repo.require_installed()

tests/store_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_clone(store, tempdir_factory, log_info_mock):
8888

8989
ret = store.clone(path, sha)
9090
# Should have printed some stuff
91-
assert log_info_mock.call_args_list[1][0][0].startswith(
91+
assert log_info_mock.call_args_list[0][0][0].startswith(
9292
'Initializing environment for ',
9393
)
9494

0 commit comments

Comments
 (0)