def upgrade_pkgs(user=False): import pip from pip.commands.list import ListCommand pip_list = ListCommand() pip_options = pip_list.parse_args(['--no-cache-dir', '-o'] + [('', '--user')[user]]) while (True): # list packages that need upgrading try: packages = [p.project_name for p, y, _ in pip_list.find_packages_latest_versions(pip_options[0]) if p.version == y.available] except: packages = [] for i, pkg_name in enumerate(packages, start=1): try: print(r"[%3.2f%%]::Upgrading %s package" % (i * 100 / len(packages), pkg_name.lower())) pip.main(['-q', 'install', '-U'] + [('', '--user')[user]] + [pkg_name]) except IndexError: continue except KeyboardInterrupt: raise else: break
def _outdated_package(package_name): """Faster outdated check when running it for a single package.""" list_command = ListCommand() command_options, _ = list_command.parse_args( ['--pre']) # Include development releases installed_package = _installed_package(package_name) if installed_package: packages = list_command.get_outdated([installed_package], command_options) return packages[0] if packages else None
def upgrade_packages(constraints, user=False): from pip.commands.list import ListCommand from pip.commands.install import InstallCommand from pip.exceptions import InstallationError packages = [] pip_install_cmd = InstallCommand() pip_list_cmd = ListCommand() while True: # list packages that need upgrading try: options = pip_list_cmd.parse_args([])[0] options.use_user_site = user options.cache_dir = None options.outdated = True packages = [p.project_name for p, y, _ in pip_list_cmd.find_packages_latest_versions(options) if getattr(p, 'version', 0) != getattr(y, 'public', 0)] except: packages = [] # Never upgrade yourself. It doesn't end well. (Any sources already loaded are the old version, anything # loaded after this is the new version, it's a mess. It also borks development installs.) if "sickrage" in packages: packages.remove("sickrage") options = pip_install_cmd.parse_args([])[0] options.use_user_site = user options.constraints = [constraints] options.cache_dir = None options.upgrade = True options.quiet = 1 for i, pkg_name in enumerate(packages, start=1): try: print(r"[%3.2f%%]::Upgrading %s package" % (i * 100 / len(packages), pkg_name.lower())) pip_install_cmd.run(options, [pkg_name]) except InstallationError: try: options.ignore_dependencies = True pip_install_cmd.run(options, [pkg_name]) except:continue except IndexError: continue else: break
def get_installed_packages(self): final = [] get_list = ListCommand() options,args = get_list.parse_args(["--outdated"]) for package in get_installed_distributions(): name = str(package).split(" ")[0] if name == "team": continue for pkg in get_list.iter_packages_latest_infos([package], options): latest = str(pkg.latest_version) for attributes in search_packages_info([name]): result = {"name": attributes["name"], "version": attributes["version"], "latest": latest, "summary": attributes["summary"], "home-page": attributes["home-page"]} final.append(result) return final
def upgrade_packages(constraints, user=False): from pip.commands.list import ListCommand from pip.commands.install import InstallCommand from pip.exceptions import InstallationError packages = [] pip_install_cmd = InstallCommand() pip_list_cmd = ListCommand() while True: # list packages that need upgrading try: options = pip_list_cmd.parse_args([])[0] options.use_user_site = user options.cache_dir = None options.outdated = True packages = [p.project_name for p, y, _ in pip_list_cmd.find_packages_latest_versions(options) if getattr(p, 'version', 0) != getattr(y, 'public', 0)] except: packages = [] options = pip_install_cmd.parse_args([])[0] options.use_user_site = user options.constraints = [constraints] options.cache_dir = None options.upgrade = True options.quiet = 1 for i, pkg_name in enumerate(packages, start=1): try: print(r"[%3.2f%%]::Upgrading %s package" % (i * 100 / len(packages), pkg_name.lower())) pip_install_cmd.run(options, [pkg_name]) except InstallationError: try: options.ignore_dependencies = True pip_install_cmd.run(options, [pkg_name]) except:continue except IndexError: continue else: break
def get_installed_packages(self): # type: () -> List[Dict[str, str]] try: get_list = ListCommand() except TypeError: get_list = ListCommand("Pippel", "Backend server for the Pippel service.") options, args = get_list.parse_args(["--outdated"]) packages = [ package for package in get_installed_distributions() if package.key != "team" ] final = [ { "name": attributes.get("name"), "version": attributes.get("version"), "latest": str(getattr(package, "latest_version")), "summary": attributes.get("summary"), "home-page": attributes.get("home-page") } for package in get_list.iter_packages_latest_infos( packages, options) for attributes in search_packages_info([package.key]) # noqa ] # TODO: To profile performance speed of snippet above and below. # final = [ # {"name": attributes.get("name"), # "version": attributes.get("version"), # "latest": str(getattr(latest_info, "latest_version")), # "summary": attributes.get("summary"), # "home-page": attributes.get("home-page") # } # for latest_info, attributes in zip( # [get_list.iter_packages_latest_infos(packages, options), # search_packages_info([getattr(package, "key") # for package in packages])] # ) # ] return final
def check_versions(only_leonardo=False): '''returns dictionary of modules with versions to could be updated return:: { 'name': { 'old': '1.0.1', 'new': '1.0.2', 'type': wheel } } ''' global LEONARDO_ENV global GLOBAL_ENV if only_leonardo: if LEONARDO_ENV: return LEONARDO_ENV else: if GLOBAL_ENV: return GLOBAL_ENV listing = ListCommand() options, args = listing.parse_args([]) update = {} for dist, version, typ in listing.find_packages_latest_versions(options): if only_leonardo: pkg_names = [k for k in dist._get_metadata("top_level.txt")] for pkg_name in pkg_names: try: mod = import_module(pkg_name) except: pass else: if is_leonardo_module(mod): if version > dist.parsed_version: update.update({ dist.project_name: { 'old': dist.version, 'new': version, 'type': typ } }) else: if version > dist.parsed_version: update.update({ dist.project_name: { 'old': dist.version, 'new': version, 'type': typ } }) if only_leonardo: LEONARDO_ENV = update else: GLOBAL_ENV = update return update
def print_distributions(packages, opts): data, header = format_for_columns(packages, opts) data.insert(0, header) pkg_str, sizes = tabulate(data) pkg_str.insert(1, " ".join(map(lambda x: '-' * x, sizes))) pkg_str.append( f'\nThere are {len(packages)} packages to be upgraded.' if len(packages) > 1 else '\nThere is one package to be upgraded.') print('\n'.join(pkg_str)) if __name__ == '__main__': main_opts = parse_args() list_cmd = ListCommand() list_opts = list_cmd.parse_args(['--outdated'])[0] distributions = [ d for d in list_cmd.iter_packages_latest_infos( get_installed_distributions(), list_opts) if d.latest_version > d.parsed_version ] distributions = sorted(distributions, key=lambda p: p.project_name.lower()) if not distributions: print('There are no packages to be upgraded.') exit(0) print_distributions(distributions, list_opts) if main_opts.show_upgradeable: exit(0) if main_opts.filter: filters = [string.lower() for string in main_opts.filter] excluded = []