Skip to content
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

Add yen update command to update version #37

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/yen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import os.path
import subprocess
import sys
import re
from typing import Literal
from typing_extensions import Unpack

from yen import (
DEFAULT_PYTHON_VERSION,
Expand All @@ -22,7 +24,7 @@


class YenArgs:
command: Literal["list", "ensurepath", "create", "install", "run", "exec"]
command: Literal["list", "ensurepath", "create", "install", "run", "exec", "update"]
python: str
venv_path: str
package_name: str
Expand All @@ -40,6 +42,9 @@ def cli() -> int:
subparsers.add_parser("list")
subparsers.add_parser("ensurepath")

update_parser = subparsers.add_parser("update")
update_parser.add_argument("-p", "--python", default=DEFAULT_PYTHON_VERSION)

create_parser = subparsers.add_parser("create")
create_parser.add_argument("venv_path", type=os.path.abspath)
create_parser.add_argument("-p", "--python", required=True)
Expand Down Expand Up @@ -85,6 +90,29 @@ def cli() -> int:
" Restart your shell for it to take effect."
)

if args.command == "update":
print("Updating Yen...")
_, python_bin_path = ensure_python(args.python)

try:
result = subprocess.run(
[python_bin_path, "-m", "pip", "install", "-U", "yen"],
check=True,
capture_output=True,
)
std_out = str(result.stdout)
if result.returncode == 0:
if "Requirement already satisfied" in std_out:
print("Yen is already in updated version")
elif "Successfully installed" in std_out:
# fetch new version with regex?
version_match = re.search(r"Successfully installed yen-(\d+\.\d+\.\d+)", std_out)
if version_match:
print(f"Yen successfully updated to version {version_match.group(1)}!")
print("Yen successfully updated!")
except subprocess.CalledProcessError:
print("Update failed; something went wrong!")

elif args.command == "create":
try:
python_version, python_bin_path = ensure_python(args.python)
Expand Down