-
-
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
feat(commands): Add update command #75
Open
hubertdeng123
wants to merge
1
commit into
main
Choose a base branch
from
hubertdeng123/add-upgrade-command
base: main
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.
+318
−107
Open
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 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,14 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from urllib.request import urlopen | ||
|
||
|
||
def check_for_update(current_version: str) -> str | None: | ||
url = "https://api.github.com/repos/getsentry/devservices/releases/latest" | ||
with urlopen(url) as response: | ||
if response.status == 200: | ||
data = json.loads(response.read().decode("utf-8")) | ||
latest_version = str(data["tag_name"]) | ||
return latest_version | ||
return None | ||
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,62 @@ | ||
from __future__ import annotations | ||
|
||
import platform | ||
import sys | ||
from argparse import _SubParsersAction | ||
from argparse import ArgumentParser | ||
from argparse import Namespace | ||
from importlib import metadata | ||
|
||
from devservices.commands.check_for_update import check_for_update | ||
from devservices.exceptions import DevservicesUpdateError | ||
from devservices.utils.install_binary import install_binary | ||
|
||
|
||
def is_in_virtualenv() -> bool: | ||
return hasattr(sys, "real_prefix") or ( | ||
hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix | ||
) | ||
|
||
|
||
def update_version(exec_path: str, latest_version: str) -> None: | ||
system = platform.system().lower() | ||
url = f"https://github.com/getsentry/devservices/releases/download/{latest_version}/devservices-{system}" | ||
install_binary( | ||
"devservices", exec_path, latest_version, url, DevservicesUpdateError | ||
) | ||
print(f"Devservices {latest_version} updated successfully") | ||
|
||
|
||
def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None: | ||
parser = subparsers.add_parser( | ||
"update", help="update devservices to the latest version" | ||
) | ||
parser.set_defaults(func=update) | ||
|
||
|
||
def update(args: Namespace) -> None: | ||
current_version = metadata.version("devservices") | ||
latest_version = check_for_update(current_version) | ||
|
||
if latest_version is None: | ||
raise DevservicesUpdateError("Failed to check for updates.") | ||
|
||
if latest_version == current_version: | ||
print("You're already on the latest version.") | ||
return | ||
|
||
print(f"A new version of devservices is available: {latest_version}") | ||
|
||
if is_in_virtualenv(): | ||
print("You are running in a virtual environment.") | ||
print( | ||
"To update, please update your requirements.txt or requirements-dev.txt file with the new version." | ||
) | ||
print( | ||
f"For example, update the line in requirements.txt to: devservices=={latest_version}" | ||
) | ||
print("Then, run: pip install --update -r requirements.txt") | ||
return | ||
|
||
print("Upgrading to the latest version...") | ||
update_version(sys.executable, latest_version) |
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,50 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import shutil | ||
import tempfile | ||
import time | ||
from urllib.request import urlretrieve | ||
|
||
|
||
def install_binary( | ||
binary_name: str, | ||
exec_path: str, | ||
version: str, | ||
url: str, | ||
exception_type: type[Exception], | ||
) -> None: | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
temp_file = os.path.join(temp_dir, binary_name) | ||
|
||
# Download the binary with retries | ||
max_retries = 3 | ||
retry_delay_seconds = 1 | ||
print(f"Downloading {binary_name} {version} from {url}...") | ||
for attempt in range(max_retries): | ||
try: | ||
urlretrieve(url, temp_file) | ||
break | ||
except Exception as e: | ||
if attempt < max_retries - 1: | ||
print( | ||
f"Download failed. Retrying in {retry_delay_seconds} seconds... (Attempt {attempt + 1}/{max_retries - 1})" | ||
) | ||
time.sleep(retry_delay_seconds) | ||
else: | ||
raise exception_type( | ||
f"Failed to download {binary_name} after {max_retries} attempts: {e}" | ||
) | ||
|
||
# Make the binary executable | ||
try: | ||
os.chmod(temp_file, 0o755) | ||
except Exception as e: | ||
raise exception_type(f"Failed to set executable permissions: {e}") | ||
|
||
try: | ||
shutil.move(temp_file, exec_path) | ||
except Exception as e: | ||
raise exception_type( | ||
f"Failed to move {binary_name} binary to {exec_path}: {e}" | ||
) |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--index-url https://pypi.devinfra.sentry.io/simple | ||
pyyaml==6.0.1 | ||
packaging==24.0 | ||
sentry-devenv==1.8.0 | ||
sentry-sdk==2.14.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from __future__ import annotations | ||
|
||
from argparse import Namespace | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from devservices.commands.update import update | ||
from devservices.exceptions import DevservicesUpdateError | ||
|
||
|
||
@mock.patch("devservices.commands.update.metadata.version", return_value="0.0.1") | ||
@mock.patch("devservices.commands.update.check_for_update", return_value="1.0.0") | ||
@mock.patch("devservices.commands.update.is_in_virtualenv", return_value=True) | ||
def test_update_in_virtualenv( | ||
mock_metadata_version: mock.Mock, | ||
mock_check_for_update: mock.Mock, | ||
mock_is_in_virtualenv: mock.Mock, | ||
capsys: pytest.CaptureFixture[str], | ||
) -> None: | ||
update(Namespace()) | ||
captured = capsys.readouterr() | ||
assert "You are running in a virtual environment." in captured.out | ||
assert ( | ||
"To update, please update your requirements.txt or requirements-dev.txt file with the new version." | ||
in captured.out | ||
) | ||
assert ( | ||
"For example, update the line in requirements.txt to: devservices==1.0.0" | ||
in captured.out | ||
) | ||
assert "Then, run: pip install --update -r requirements.txt" in captured.out | ||
|
||
|
||
@mock.patch("devservices.commands.update.metadata.version", return_value="0.0.1") | ||
@mock.patch("devservices.commands.update.check_for_update", return_value=None) | ||
@mock.patch("devservices.commands.update.is_in_virtualenv", return_value=False) | ||
@mock.patch("devservices.commands.update.install_binary") | ||
def test_update_check_for_update_error( | ||
mock_metadata_version: mock.Mock, | ||
mock_check_for_update: mock.Mock, | ||
mock_is_in_virtualenv: mock.Mock, | ||
mock_install_binary: mock.Mock, | ||
capsys: pytest.CaptureFixture[str], | ||
) -> None: | ||
with pytest.raises(DevservicesUpdateError, match="Failed to check for updates."): | ||
update(Namespace()) | ||
|
||
|
||
@mock.patch("devservices.commands.update.metadata.version", return_value="1.0.0") | ||
@mock.patch("devservices.commands.update.check_for_update", return_value="1.0.0") | ||
@mock.patch("devservices.commands.update.is_in_virtualenv", return_value=False) | ||
@mock.patch("devservices.commands.update.install_binary") | ||
def test_update_already_on_latest_version( | ||
mock_metadata_version: mock.Mock, | ||
mock_check_for_update: mock.Mock, | ||
mock_is_in_virtualenv: mock.Mock, | ||
mock_install_binary: mock.Mock, | ||
capsys: pytest.CaptureFixture[str], | ||
) -> None: | ||
update(Namespace()) | ||
captured = capsys.readouterr() | ||
assert "You're already on the latest version." in captured.out | ||
|
||
|
||
@mock.patch("devservices.commands.update.metadata.version", return_value="0.0.1") | ||
@mock.patch("devservices.commands.update.check_for_update", return_value="1.0.0") | ||
@mock.patch("devservices.commands.update.is_in_virtualenv", return_value=False) | ||
@mock.patch("devservices.commands.update.install_binary") | ||
def test_update_success( | ||
mock_metadata_version: mock.Mock, | ||
mock_check_for_update: mock.Mock, | ||
mock_is_in_virtualenv: mock.Mock, | ||
mock_install_binary: mock.Mock, | ||
capsys: pytest.CaptureFixture[str], | ||
) -> None: | ||
update(Namespace()) | ||
captured = capsys.readouterr() | ||
assert "A new version of devservices is available: 1.0.0" in captured.out | ||
assert "Devservices 1.0.0 updated successfully" in captured.out |
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.
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 can see a major downside to this:
If user is not connected to internet, this will error out and cause spam. I think it is ok to leave it as is and put a workaround in later though
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.
Ultimately, it is far more useful to notify users to update if available