Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BIP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
BIP
Commits
5cb213f3
Commit
5cb213f3
authored
2 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: New lockdir utility function
parent
4b84d157
No related branches found
No related tags found
1 merge request
!3
BIP configuration system
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bip/utils/__init__.py
+45
-13
45 additions, 13 deletions
bip/utils/__init__.py
with
45 additions
and
13 deletions
bip/utils/__init__.py
+
45
−
13
View file @
5cb213f3
import
shutil
from
fsl.wrappers.wrapperutils
import
cmdwrapper
installed_sienax
=
shutil
.
which
(
'
bb_sienax
'
)
if
installed_sienax
:
@cmdwrapper
def
bb_sienax
(
input
,
output
=
None
,
**
kwargs
):
return
[
'
bb_sienax
'
,
input
,
output
]
else
:
def
bb_sienax
(
input
,
output
=
None
,
**
kwargs
):
from
.
import
sienax
sienax
.
bb_sienax
(
input
,
output
)
#!/usr/bin/env python
"""
The bip.utils package contains a range of miscellaneous utilities.
"""
import
contextlib
import
logging
import
os.path
as
op
import
os
import
time
log
=
logging
.
getLogger
(
__name__
)
@contextlib.contextmanager
def
lockdir
(
dirname
,
delay
=
5
):
"""
Lock a directory for exclusive access.
Primitive mechanism by which concurrent access to a directory can be
prevented. Attempts to create a semaphore file in the directory, but waits
if that file already exists. Removes the file when finished.
"""
lockfile
=
op
.
join
(
dirname
,
'
.fsl_ci.lockdir
'
)
while
True
:
try
:
log
.
debug
(
f
'
Attempting to lock %s for exclusive access.
'
,
dirname
)
fd
=
os
.
open
(
lockfile
,
os
.
O_CREAT
|
os
.
O_EXCL
|
os
.
O_RDWR
)
break
except
OSError
as
e
:
if
e
.
errno
!=
errno
.
EEXIST
:
raise
e
log
.
debug
(
'
%s is already locked - trying again
'
'
in %s seconds ...
'
,
dirname
,
delay
)
time
.
sleep
(
10
)
log
.
debug
(
'
Exclusive access acquired for %s ...
'
,
dirname
)
try
:
yield
finally
:
log
.
debug
(
'
Relinquishing lock on %s
'
,
dirname
)
os
.
close
(
fd
)
os
.
unlink
(
lockfile
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment