Skip to content
Open
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
39 changes: 19 additions & 20 deletions pdd/auto_update.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module provides a function to automatically update the package."""

import importlib.metadata
import shutil
import subprocess
Expand All @@ -11,27 +12,27 @@
def detect_installation_method(sys_executable):
"""
Detect if package is installed via UV or pip.

Args:
sys_executable (str): Path to the Python executable

Returns:
str: "uv" if installed via UV, "pip" otherwise
"""
# Check if executable path contains UV paths
if any(marker in sys_executable for marker in ["/uv/tools/", ".local/share/uv/"]):
if "/uv/tools/" in sys_executable or ".local/share/uv/" in sys_executable:
return "uv"
return "pip" # Default to pip for all other cases


def get_upgrade_command(package_name, installation_method):
"""
Return appropriate upgrade command based on installation method.

Args:
package_name (str): Name of the package to upgrade
installation_method (str): "uv" or "pip"

Returns:
tuple: (command_list, shell_mode) where command_list is the command to run
and shell_mode is a boolean indicating if shell=True should be used
Expand All @@ -54,7 +55,7 @@ def _get_latest_version(package_name: str) -> Optional[str]:
pypi_url = f"https://pypi.org/pypi/{package_name}/json"
response = requests.get(pypi_url, timeout=10)
response.raise_for_status()
return response.json()['info']['version']
return response.json()["info"]["version"]
except Exception as ex:
print(f"Failed to fetch latest version from PyPI: {str(ex)}")
return None
Expand All @@ -70,11 +71,7 @@ def _upgrade_package(package_name: str, installation_method: str):
# pylint: disable=broad-except
try:
result = subprocess.run(
cmd,
shell=use_shell,
capture_output=True,
text=True,
check=False
cmd, shell=use_shell, capture_output=True, text=True, check=False
)
if result.returncode == 0:
print(f"\nSuccessfully upgraded {package_name}")
Expand All @@ -100,7 +97,7 @@ def auto_update(package_name: str = "pdd-cli", latest_version: str = None) -> No
"""
Check if there's a new version of the package available and prompt for upgrade.
Handles both UV and pip installations automatically.

Args:
latest_version (str): Known latest version (default: None)
package_name (str): Name of the package to check (default: "pdd-cli")
Expand All @@ -117,23 +114,25 @@ def auto_update(package_name: str = "pdd-cli", latest_version: str = None) -> No
if not _is_new_version_available(current_version, latest_version):
return

print(f"\nNew version of {package_name} available: "
f"{latest_version} (current: {current_version})")

print(
f"\nNew version of {package_name} available: "
f"{latest_version} (current: {current_version})"
)

while True:
response = input("Would you like to upgrade? [y/N]: ").lower().strip()
if response in ['y', 'yes']:
if response in ["y", "yes"]:
installation_method = detect_installation_method(sys.executable)
if _upgrade_package(package_name, installation_method):
break

if installation_method == "uv":
print("\nAttempting fallback to pip...")
if _upgrade_package(package_name, "pip"):
break

break
if response in ['n', 'no', '']:
if response in ["n", "no", ""]:
print("\nUpgrade cancelled")
break
print("Please answer 'y' or 'n'")
Expand All @@ -145,4 +144,4 @@ def auto_update(package_name: str = "pdd-cli", latest_version: str = None) -> No


if __name__ == "__main__":
auto_update()
auto_update()