forked from fedora-copr/copr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
distgit, backend, common: unify file locking code in copr-common
Fix fedora-copr#3367 As a consequence, stop using `python3-oslo-concurrency` on Copr DistGit and start using `python3-filelock` as the rest of the stack.
- Loading branch information
Showing
10 changed files
with
72 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
File locking for multithreading | ||
""" | ||
|
||
import os | ||
import contextlib | ||
import filelock | ||
from setproctitle import getproctitle, setproctitle | ||
|
||
|
||
@contextlib.contextmanager | ||
def lock(path, lockdir, timeout, log): | ||
""" | ||
Create a lock file that can be accessed only by one thread at the time. | ||
A practical use-case for this is to lock a repository so multiple versions | ||
of the same package cannot be imported in paralel. | ||
We want to pass a simple `path` parameter specifying what file or directory | ||
should be locked. This however isn't where the lockfile is going to be | ||
created. It will be created in the `lockdir`. | ||
""" | ||
filename = path.replace("/", "_@_") + ".lock" | ||
lockfile = os.path.join(lockdir, filename) | ||
|
||
title = getproctitle() | ||
setproctitle("{0} [locking]".format(title)) | ||
log.debug("acquiring lock") | ||
try: | ||
with filelock.FileLock(lockfile, timeout=timeout): | ||
setproctitle("{0} [locked]".format(title)) | ||
log.debug("acquired lock") | ||
yield | ||
except filelock.Timeout as err: | ||
log.debug("lock timeouted") | ||
raise LockTimeout("Timeouted on lock for: {}".format(path)) from err | ||
finally: | ||
setproctitle("{0} [locking]".format(title)) | ||
|
||
|
||
class LockTimeout(OSError): | ||
""" | ||
Raised for lock() timeout, if timeout= option is set to value >= 0 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters