-
Notifications
You must be signed in to change notification settings - Fork 0
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
check / install python version using pyenv #1
Draft
flatnine
wants to merge
4
commits into
master
Choose a base branch
from
check_versions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 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 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ argparse | |
invoke | ||
pip-tools | ||
pre-commit | ||
pytest |
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 |
---|---|---|
|
@@ -7,6 +7,10 @@ | |
import sys | ||
|
||
|
||
class PyEnvFailure(Exception): | ||
pass | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description='Timeout Tools', | ||
|
@@ -158,9 +162,13 @@ def python_setup_func(args): | |
|
||
|
||
def python_setup(app, branch, python_version): | ||
try: | ||
check_python_version_installed(python_version) | ||
except PyEnvFailure as e: | ||
print(e.args[0]['message']) | ||
sys.exit(1) | ||
pyenv_name = f'{app}-{python_version}' | ||
print(f'- Creating virtualenv `{pyenv_name}`', end='', flush=True) | ||
run(f'pyenv install -s {python_version}') | ||
ret, out = run(f'pyenv virtualenv {python_version} {pyenv_name}') | ||
if ret != 0: | ||
if 'already exists' in out: | ||
|
@@ -274,5 +282,67 @@ def load_python_version(ws=None): | |
return False | ||
|
||
|
||
def check_python_version_installed(python_version): | ||
### | ||
# Check whether a version of python is already installed via pyenv | ||
### | ||
py_ver_present = False | ||
(status, result) = run('pyenv versions --bare --skip-aliases') | ||
if status: | ||
raise PyEnvFailure({"message": "Failed to run"}) | ||
for py_ver in result.replace(' ', '').split('\n'): | ||
if py_ver == python_version: | ||
py_ver_present = True | ||
break | ||
|
||
if not py_ver_present: | ||
try: | ||
check_python_version_available(python_version) | ||
try: | ||
install_python_version(python_version) | ||
except PyEnvFailure as e: | ||
print(e.args[0]['message']) | ||
sys.exit(1) | ||
except PyEnvFailure as e: | ||
print(e.args[0]['message']) | ||
sys.exit(1) | ||
else: | ||
print(f'- Python `{python_version}` is installed ✅') | ||
|
||
|
||
def check_python_version_available(python_version): | ||
### | ||
# Check whether a version of python is already is available for install by pyenv | ||
# if not exit and tell user to update pyenv installation | ||
### | ||
py_ver_available = False | ||
(status, result) = run('pyenv install --list') | ||
if status: | ||
raise PyEnvFailure({"message": "Failed to run"}) | ||
for py_ver in result.replace(' ', '').split('\n'): | ||
if py_ver == python_version: | ||
py_ver_available = True | ||
break | ||
if not py_ver_available: | ||
print(''' | ||
Please update pyenv with latest versions of python by running: | ||
cd ~/.pyenv/plugins/python-build/../.. && git pull && cd - | ||
''') | ||
raise PyEnvFailure({"message": "Pyenv requires update"}) | ||
else: | ||
print(f'- Python `{python_version}` is available for installation ✅') | ||
|
||
|
||
def install_python_version(python_version): | ||
### | ||
# Use pyenv to install new version of python | ||
### | ||
(status, _) = run(f'pyenv install {python_version}') | ||
if status: | ||
raise BaseException(f'Failed to install python version {python_version}') | ||
else: | ||
print(f'- Python `{python_version}` installation successful ✅') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the first part of the message should print before the installs start and the tick be added when in succeeds |
||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
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,114 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from ..cli import (PyEnvFailure, check_python_version_available, | ||
check_python_version_installed, install_python_version) | ||
|
||
|
||
def test_check_python_version_installed_success(): | ||
mock_run = mock.Mock() | ||
mock_run.return_value = ( | ||
0, | ||
' 3.7.9\n 3.10.10\n 3.10.11' | ||
) | ||
|
||
with mock.patch('timeout_tools.cli.run', mock_run): | ||
python_version = '3.10.10' | ||
try: | ||
check_python_version_installed(python_version) | ||
except Exception: | ||
pytest.fail(f'Error checking version installed {python_version}') | ||
|
||
mock_run.assert_called_with('pyenv versions --bare --skip-aliases') | ||
|
||
|
||
def test_check_python_version_installed_cmd_exception(): | ||
|
||
mock_run = mock.Mock() | ||
mock_run.return_value = (1, "") | ||
|
||
with mock.patch('timeout_tools.cli.run', mock_run): | ||
python_version = '3.10.10' | ||
with pytest.raises(PyEnvFailure) as e: | ||
check_python_version_installed(python_version) | ||
assert e.value.args[0]['message'] == 'Failed to run' | ||
|
||
mock_run.assert_called_with('pyenv versions --bare --skip-aliases') | ||
|
||
|
||
def test_check_python_version_available_success(): | ||
mock_run = mock.Mock() | ||
mock_run.return_value = ( | ||
0, | ||
' 3.7.9\n 3.10.10\n 3.10.11' | ||
) | ||
|
||
with mock.patch('timeout_tools.cli.run', mock_run): | ||
python_version = '3.10.10' | ||
try: | ||
check_python_version_available(python_version) | ||
except Exception: | ||
pytest.fail(f'Error checking {python_version}') | ||
|
||
mock_run.assert_called_with('pyenv install --list') | ||
|
||
|
||
def test_check_python_version_available_cmd_exception(): | ||
|
||
mock_run = mock.Mock() | ||
mock_run.return_value = (1, "") | ||
|
||
with mock.patch('timeout_tools.cli.run', mock_run): | ||
python_version = '3.10.10' | ||
with pytest.raises(PyEnvFailure) as e: | ||
check_python_version_available(python_version) | ||
assert e.value.args[0]['message'] == 'Failed to run' | ||
|
||
mock_run.assert_called_with('pyenv install --list') | ||
|
||
|
||
def test_check_python_version_available_version_missing_exception(): | ||
|
||
mock_run = mock.Mock() | ||
mock_run.return_value = ( | ||
0, | ||
' 3.7.9\n 3.10.10\n 3.10.11' | ||
) | ||
|
||
with mock.patch('timeout_tools.cli.run', mock_run): | ||
python_version = '0.0.0' | ||
with pytest.raises(PyEnvFailure) as e: | ||
check_python_version_available(python_version) | ||
assert e.value.args[0]['message'] == 'Pyenv requires update' | ||
|
||
mock_run.assert_called_with('pyenv install --list') | ||
|
||
|
||
def test_install_python_version_success(): | ||
|
||
mock_run = mock.Mock() | ||
mock_run.return_value = (0, "") | ||
|
||
with mock.patch('timeout_tools.cli.run', mock_run): | ||
python_version = '3.10.10' | ||
try: | ||
install_python_version(python_version) | ||
except Exception: | ||
pytest.fail(f'Error installing {python_version}') | ||
|
||
mock_run.assert_called_with(f'pyenv install {python_version}') | ||
|
||
|
||
def test_install_python_version_exception(): | ||
|
||
mock_run = mock.Mock() | ||
mock_run.return_value = (1, "") | ||
|
||
with mock.patch('timeout_tools.cli.run', mock_run): | ||
python_version = '0.0.0' | ||
with pytest.raises(BaseException) as e: | ||
install_python_version(python_version) | ||
assert str(e.value) == f'Failed to install python version {python_version}' | ||
|
||
mock_run.assert_called_with(f'pyenv install {python_version}') |
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.
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.
I feel the logic shouldn't be inside the check function, ie if the check returns false here then call the install here
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.
certainly makes the tests easier