def install_packages( # pylint: disable=too-many-arguments self, with_packages=None, without_packages=None, skip_default_package=False, silent=False, force=False, ): with_packages = set(self._find_pkg_names(with_packages or [])) without_packages = set(self._find_pkg_names(without_packages or [])) upkgs = with_packages | without_packages ppkgs = set(self.packages) if not upkgs.issubset(ppkgs): raise UnknownPackageError(", ".join(upkgs - ppkgs)) for name, options in self.packages.items(): if name in without_packages: continue if name in with_packages or not (skip_default_package or options.get("optional", False)): self.pm.install(self.get_package_spec(name), silent=silent, force=force) return True
def install_from_registry(self, spec, search_filters=None, silent=False): if spec.owner and spec.name and not search_filters: package = self.fetch_registry_package(spec) if not package: raise UnknownPackageError(spec.humanize()) version = self.pick_best_registry_version(package["versions"], spec) else: packages = self.search_registry_packages(spec, search_filters) if not packages: raise UnknownPackageError(spec.humanize()) if len(packages) > 1 and not silent: self.print_multi_package_issue(packages, spec) package, version = self.find_best_registry_version(packages, spec) if not package or not version: raise UnknownPackageError(spec.humanize()) pkgfile = self._pick_compatible_pkg_file( version["files"]) if version else None if not pkgfile: raise UnknownPackageError(spec.humanize()) for url, checksum in RegistryFileMirrorIterator( pkgfile["download_url"]): try: return self.install_from_url( url, PackageSpec( owner=package["owner"]["username"], id=package["id"], name=package["name"], ), checksum or pkgfile["checksum"]["sha256"], silent=silent, ) except Exception as e: # pylint: disable=broad-except self.print_message("Warning! Package Mirror: %s" % e, fg="yellow") self.print_message("Looking for another mirror...", fg="yellow") return None
def reveal_registry_package_id(self, spec, silent=False): spec = self.ensure_spec(spec) if spec.id: return spec.id packages = self.search_registry_packages(spec) if not packages: raise UnknownPackageError(spec.humanize()) if len(packages) > 1 and not silent: self.print_multi_package_issue(packages, spec) click.echo("") return packages[0]["id"]
def uninstall(self, spec, silent=False, skip_dependencies=False): pkg = self.get_package(spec) if not pkg or not pkg.metadata: raise UnknownPackageError(spec) p = PlatformFactory.new(pkg) assert super(PlatformPackageManager, self).uninstall(pkg, silent=silent, skip_dependencies=True) if not skip_dependencies: p.uninstall_python_packages() p.on_uninstalled() return pkg
def fetch_registry_package(self, spec): assert isinstance(spec, PackageSpec) result = None regclient = self.get_registry_client_instance() if spec.owner and spec.name: result = regclient.get_package(self.pkg_type, spec.owner, spec.name) if not result and (spec.id or (spec.name and not spec.owner)): packages = self.search_registry_packages(spec) if packages: result = regclient.get_package( self.pkg_type, packages[0]["owner"]["username"], packages[0]["name"] ) if not result: raise UnknownPackageError(spec.humanize()) return result
def _uninstall(self, spec, silent=False, skip_dependencies=False): pkg = self.get_package(spec) if not pkg or not pkg.metadata: raise UnknownPackageError(spec) if not silent: self.print_message( "Removing %s @ %s" % (click.style(pkg.metadata.name, fg="cyan"), pkg.metadata.version), ) # firstly, remove dependencies if not skip_dependencies: self.uninstall_dependencies(pkg, silent) if os.path.islink(pkg.path): os.unlink(pkg.path) else: fs.rmtree(pkg.path) self.memcache_reset() # unfix detached-package with the same name detached_pkg = self.get_package(PackageSpec(name=pkg.metadata.name)) if ( detached_pkg and "@" in detached_pkg.path and not os.path.isdir( os.path.join(self.package_dir, detached_pkg.get_safe_dirname()) ) ): shutil.move( detached_pkg.path, os.path.join(self.package_dir, detached_pkg.get_safe_dirname()), ) self.memcache_reset() if not silent: self.print_message( "{name} @ {version} has been removed!".format(**pkg.metadata.as_dict()), fg="green", ) return pkg
def update( # pylint: disable=too-many-arguments self, from_spec, to_spec=None, only_check=False, silent=False, show_incompatible=True, ): pkg = self.get_package(from_spec) if not pkg or not pkg.metadata: raise UnknownPackageError(from_spec) if not silent: click.echo( "{} {:<45} {:<35}".format( "Checking" if only_check else "Updating", click.style(pkg.metadata.spec.humanize(), fg="cyan"), "%s @ %s" % (pkg.metadata.version, to_spec.requirements) if to_spec and to_spec.requirements else str(pkg.metadata.version), ), nl=False, ) if not ensure_internet_on(): if not silent: click.echo("[%s]" % (click.style("Off-line", fg="yellow"))) return pkg outdated = self.outdated(pkg, to_spec) if not silent: self.print_outdated_state(outdated, only_check, show_incompatible) if only_check or not outdated.is_outdated(allow_incompatible=False): return pkg try: self.lock() return self._update(pkg, outdated, silent=silent) finally: self.unlock()
def update( # pylint: disable=arguments-differ, too-many-arguments self, from_spec, to_spec=None, only_check=False, silent=False, show_incompatible=True, only_packages=False, ): pkg = self.get_package(from_spec) if not pkg or not pkg.metadata: raise UnknownPackageError(from_spec) p = PlatformFactory.new(pkg) pkgs_before = [item.metadata.name for item in p.get_installed_packages()] new_pkg = None missed_pkgs = set() if not only_packages: new_pkg = super(PlatformPackageManager, self).update( from_spec, to_spec, only_check=only_check, silent=silent, show_incompatible=show_incompatible, ) p = PlatformFactory.new(new_pkg) missed_pkgs = set(pkgs_before) & set(p.packages) missed_pkgs -= set( item.metadata.name for item in p.get_installed_packages() ) p.update_packages(only_check) self.cleanup_packages(list(p.packages)) if missed_pkgs: p.install_packages( with_packages=list(missed_pkgs), skip_default_package=True ) return new_pkg or pkg