-
Notifications
You must be signed in to change notification settings - Fork 30
Make portalocker optional #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -334,3 +334,5 @@ ASALocalRun/ | |
.mfractor/ | ||
|
||
.eggs/ | ||
.env | ||
Session.vim |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
"""A cross-process lock based on exclusive creation of a given file name""" | ||
import os | ||
import sys | ||
import errno | ||
import time | ||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LockError(RuntimeError): | ||
"""It will be raised when unable to obtain a lock""" | ||
|
||
|
||
class CrossPlatLock(object): | ||
"""This implementation relies only on ``open(..., 'x')``""" | ||
def __init__(self, lockfile_path): | ||
self._lockpath = lockfile_path | ||
|
||
def __enter__(self): | ||
self._create_lock_file('{} {}'.format( | ||
os.getpid(), | ||
sys.argv[0], | ||
).encode('utf-8')) # pylint: disable=consider-using-f-string | ||
return self | ||
|
||
def _create_lock_file(self, content): | ||
timeout = 5 | ||
check_interval = 0.25 | ||
current_time = getattr(time, "monotonic", time.time) | ||
timeout_end = current_time() + timeout | ||
while timeout_end > current_time(): | ||
try: | ||
with open(self._lockpath, 'xb') as lock_file: # pylint: disable=unspecified-encoding | ||
lock_file.write(content) | ||
return None # Happy path | ||
except ValueError: # This needs to be the first clause, for Python 2 to hit it | ||
raise LockError("Python 2 does not support atomic creation of file") | ||
except FileExistsError: # Only Python 3 will reach this clause | ||
logger.debug( | ||
"Process %d found existing lock file, will retry after %f second", | ||
os.getpid(), check_interval) | ||
time.sleep(check_interval) | ||
raise LockError( | ||
"Unable to obtain lock, despite trying for {} second(s). " | ||
"You may want to manually remove the stale lock file {}".format( | ||
timeout, | ||
self._lockpath, | ||
)) | ||
|
||
def __exit__(self, *args): | ||
try: | ||
os.remove(self._lockpath) | ||
except OSError as ex: # pylint: disable=invalid-name | ||
if ex.errno in (errno.ENOENT, errno.EACCES): | ||
# Probably another process has raced this one | ||
# and ended up clearing or locking the file for itself. | ||
logger.debug("Unable to remove lock file") | ||
else: | ||
raise | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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 hidden or 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,12 @@ | ||
class MinimalResponse(object): # Not for production use | ||
def __init__(self, requests_resp=None, status_code=None, text=None, headers=None): | ||
self.status_code = status_code or requests_resp.status_code | ||
self.text = text if text is not None else requests_resp.text | ||
self.headers = {} if headers is None else headers | ||
self._raw_resp = requests_resp | ||
|
||
def raise_for_status(self): | ||
if self._raw_resp is not None: # Turns out `if requests.response` won't work | ||
# cause it would be True when 200<=status<400 | ||
self._raw_resp.raise_for_status() | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -7,7 +7,7 @@ passenv = | |
GITHUB_ACTIONS | ||
|
||
commands = | ||
pytest | ||
{posargs:pytest --color=yes} | ||
|
||
[testenv:lint] | ||
deps = | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, Azure CLI doesn't really need such a complex lock at the comment. Previously ADAL didn't have locking mechanism and worked mostly fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point. But if the current PR's non-portalocker lock is harmless, then we might as well just use it.
We are open to the dummy lock idea, too. We may work on it at a later time. It can be done, but it is more work, in terms of needing to expand the current MSAL EX api to allow a "no lock" option. (The current file-based lock, on the contrary, is a drop-in replacement for portalocker, and keeps the MSAL EX api intact.)
If the file-based lock does not work well for Azure CLI on SAW, we will prioritize the dummy lock work accordingly.