def test_parse_specifier_for_upgrade(package_spec_in, package_or_url_correct, valid_spec): if valid_spec: package_or_url = parse_specifier_for_upgrade(package_spec_in) assert package_or_url == package_or_url_correct else: # print package_spec_in for info in case no error is raised print(f"package_spec_in = {package_spec_in}") with pytest.raises(PipxError, match=r"^Unable to parse package spec"): package_or_url = parse_specifier_for_upgrade(package_spec_in)
def upgrade( venv_dir: Path, pip_args: List[str], verbose: bool, *, upgrading_all: bool, force: bool, ) -> int: """Returns nonzero if package was upgraded, 0 if version did not change""" if not venv_dir.is_dir(): raise PipxError( f"Package is not installed. Expected to find {str(venv_dir)}, " "but it does not exist.") venv = Venv(venv_dir, verbose=verbose) package = venv.main_package_name if not venv.package_metadata: print( f"Not upgrading {red(bold(package))}. It has missing internal pipx metadata.\n" f" It was likely installed using a pipx version before 0.15.0.0.\n" f" Please uninstall and install this package, or reinstall-all to fix." ) return 0 package_metadata = venv.package_metadata[package] if package_metadata.package_or_url is None: raise PipxError( f"Internal Error: package {package} has corrupt pipx metadata.") package_or_url = parse_specifier_for_upgrade( package_metadata.package_or_url) old_version = package_metadata.package_version include_apps = package_metadata.include_apps include_dependencies = package_metadata.include_dependencies # Upgrade shared libraries (pip, setuptools and wheel) venv.upgrade_packaging_libraries(pip_args) venv.upgrade_package( package, package_or_url, pip_args, include_dependencies=include_dependencies, include_apps=include_apps, is_main_package=True, suffix=package_metadata.suffix, ) # TODO 20191026: upgrade injected packages also (Issue #79) package_metadata = venv.package_metadata[package] display_name = f"{package_metadata.package}{package_metadata.suffix}" new_version = package_metadata.package_version expose_apps_globally( constants.LOCAL_BIN_DIR, package_metadata.app_paths, force=force, suffix=package_metadata.suffix, ) if include_dependencies: for _, app_paths in package_metadata.app_paths_of_dependencies.items(): expose_apps_globally( constants.LOCAL_BIN_DIR, app_paths, force=force, suffix=package_metadata.suffix, ) if old_version == new_version: if upgrading_all: pass else: print( f"{display_name} is already at latest version {old_version} (location: {str(venv_dir)})" ) return 0 else: print( f"upgraded package {display_name} from {old_version} to {new_version} (location: {str(venv_dir)})" ) return 1
def _upgrade_package( venv: Venv, package_name: str, pip_args: List[str], is_main_package: bool, force: bool, upgrading_all: bool, ) -> int: """Returns 1 if package version changed, 0 if same version""" package_metadata = venv.package_metadata[package_name] if package_metadata.package_or_url is None: raise PipxError( f"Internal Error: package {package_name} has corrupt pipx metadata." ) package_or_url = parse_specifier_for_upgrade( package_metadata.package_or_url) old_version = package_metadata.package_version venv.upgrade_package( package_name, package_or_url, pip_args, include_dependencies=package_metadata.include_dependencies, include_apps=package_metadata.include_apps, is_main_package=is_main_package, suffix=package_metadata.suffix, ) package_metadata = venv.package_metadata[package_name] display_name = f"{package_metadata.package}{package_metadata.suffix}" new_version = package_metadata.package_version if package_metadata.include_apps: expose_apps_globally( constants.LOCAL_BIN_DIR, package_metadata.app_paths, force=force, suffix=package_metadata.suffix, ) if package_metadata.include_dependencies: for _, app_paths in package_metadata.app_paths_of_dependencies.items(): expose_apps_globally( constants.LOCAL_BIN_DIR, app_paths, force=force, suffix=package_metadata.suffix, ) if old_version == new_version: if upgrading_all: pass else: print( pipx_wrap(f""" {display_name} is already at latest version {old_version} (location: {str(venv.root)}) """)) return 0 else: print( pipx_wrap(f""" upgraded package {display_name} from {old_version} to {new_version} (location: {str(venv.root)}) """)) return 1