Check whether or not an upgrade is available for all packages

    CLI Example:

    .. code-block:: bash

        salt '*' pip.list_upgrades
760681
def list_upgrades(bin_env=None,
                  user=None,
                  cwd=None):
    '''
    
    '''
    cmd = _get_pip_bin(bin_env)
    cmd.extend(['list', '--outdated'])

    pip_version = version(bin_env)
    # Pip started supporting the ability to output json starting with 9.0.0
    min_version = '9.0'
    if salt.utils.versions.compare(ver1=pip_version,
                                   oper='>=',
                                   ver2=min_version):
        cmd.append('--format=json')

    cmd_kwargs = dict(cwd=cwd, runas=user)
    if bin_env and os.path.isdir(bin_env):
        cmd_kwargs['env'] = {'VIRTUAL_ENV': bin_env}

    result = __salt__['cmd.run_all'](cmd, **cmd_kwargs)
    if result['retcode']:
        raise CommandExecutionError(result['stderr'], info=result)

    packages = {}
    # Pip started supporting the ability to output json starting with 9.0.0
    # Older versions will have to parse stdout
    if salt.utils.versions.compare(ver1=pip_version, oper='<', ver2='9.0.0'):
        # Pip versions < 8.0.0 had a different output format
        # Sample data:
        # pip (Current: 7.1.2 Latest: 10.0.1 [wheel])
        # psutil (Current: 5.2.2 Latest: 5.4.5 [wheel])
        # pyasn1 (Current: 0.2.3 Latest: 0.4.2 [wheel])
        # pycparser (Current: 2.17 Latest: 2.18 [sdist])
        if salt.utils.versions.compare(ver1=pip_version, oper='<', ver2='8.0.0'):
            logger.debug('pip module: Old output format')
            pat = re.compile(r'(\S*)\s+\(.*Latest:\s+(.*)\)')

        # New output format for version 8.0.0+
        # Sample data:
        # pip (8.0.0) - Latest: 10.0.1 [wheel]
        # psutil (5.2.2) - Latest: 5.4.5 [wheel]
        # pyasn1 (0.2.3) - Latest: 0.4.2 [wheel]
        # pycparser (2.17) - Latest: 2.18 [sdist]
        else:
            logger.debug('pip module: New output format')
            pat = re.compile(r'(\S*)\s+\(.*\)\s+-\s+Latest:\s+(.*)')

        for line in result['stdout'].splitlines():
            match = pat.search(line)
            if match:
                name, version_ = match.groups()
            else:
                logger.error('Can\'t parse line \'%s\'', line)
                continue
            packages[name] = version_

    else:
        logger.debug('pip module: JSON output format')
        try:
            pkgs = salt.utils.json.loads(result['stdout'], strict=False)
        except ValueError:
            raise CommandExecutionError('Invalid JSON', info=result)

        for pkg in pkgs:
            packages[pkg['name']] = '{0} [{1}]'.format(pkg['latest_version'],
                                                       pkg['latest_filetype'])

    return packages
1190839
def find_requirement(self, req, upgrade, ignore_compatibility=False):
        # type: (InstallRequirement, bool, bool) -> Optional[Link]
        
        all_candidates = self.find_all_candidates(req.name)

        # Filter out anything which doesn't match our specifier
        compatible_versions = set(
            req.specifier.filter(
                # We turn the version object into a str here because otherwise
                # when we're debundled but setuptools isn't, Python will see
                # packaging.version.Version and
                # pkg_resources._vendor.packaging.version.Version as different
                # types. This way we'll use a str as a common data interchange
                # format. If we stop using the pkg_resources provided specifier
                # and start using our own, we can drop the cast to str().
                [str(c.version) for c in all_candidates],
                prereleases=(
                    self.allow_all_prereleases
                    if self.allow_all_prereleases else None
                ),
            )
        )
        applicable_candidates = [
            # Again, converting to str to deal with debundling.
            c for c in all_candidates if str(c.version) in compatible_versions
        ]

        if applicable_candidates:
            best_candidate = max(applicable_candidates,
                                 key=self._candidate_sort_key)
        else:
            best_candidate = None

        if req.satisfied_by is not None:
            installed_version = parse_version(req.satisfied_by.version)
        else:
            installed_version = None

        if installed_version is None and best_candidate is None:
            logger.critical(
                'Could not find a version that satisfies the requirement %s '
                '(from versions: %s)',
                req,
                ', '.join(
                    sorted(
                        {str(c.version) for c in all_candidates},
                        key=parse_version,
                    )
                )
            )

            raise DistributionNotFound(
                'No matching distribution found for %s' % req
            )

        best_installed = False
        if installed_version and (
                best_candidate is None or
                best_candidate.version <= installed_version):
            best_installed = True

        if not upgrade and installed_version is not None:
            if best_installed:
                logger.debug(
                    'Existing installed version (%s) is most up-to-date and '
                    'satisfies requirement',
                    installed_version,
                )
            else:
                logger.debug(
                    'Existing installed version (%s) satisfies requirement '
                    '(most up-to-date version is %s)',
                    installed_version,
                    best_candidate.version,
                )
            return None

        if best_installed:
            # We have an existing version, and its the best version
            logger.debug(
                'Installed version (%s) is most up-to-date (past versions: '
                '%s)',
                installed_version,
                ', '.join(sorted(compatible_versions, key=parse_version)) or
                "none",
            )
            raise BestVersionAlreadyInstalled

        logger.debug(
            'Using version %s (newest of versions: %s)',
            best_candidate.version,
            ', '.join(sorted(compatible_versions, key=parse_version))
        )
        return best_candidate.location
314910
def list_pkgs(versions_as_list=False, **kwargs):
    '''
    
    '''
    versions_as_list = salt.utils.data.is_true(versions_as_list)
    # not yet implemented or not applicable
    if any([salt.utils.data.is_true(kwargs.get(x))
            for x in ('removed', 'purge_desired')]):
        return {}

    if 'pkg.list_pkgs' in __context__:
        if versions_as_list:
            return __context__['pkg.list_pkgs']
        else:
            ret = copy.deepcopy(__context__['pkg.list_pkgs'])
            __salt__['pkg_resource.stringify'](ret)
            return ret

    ret = {}
    cmd = 'pkg_info -q -a'
    out = __salt__['cmd.run_stdout'](cmd, output_loglevel='trace')
    for line in out.splitlines():
        try:
            pkgname, pkgver, flavor = __PKG_RE.match(line).groups()
        except AttributeError:
            continue
        pkgname += '--{0}'.format(flavor) if flavor else ''
        __salt__['pkg_resource.add_pkg'](ret, pkgname, pkgver)

    __salt__['pkg_resource.sort_pkglist'](ret)
    __context__['pkg.list_pkgs'] = copy.deepcopy(ret)
    if not versions_as_list:
        __salt__['pkg_resource.stringify'](ret)
    return ret
286855
def pip_upgrade_all(line):
    """"""
    from pip import get_installed_distributions
    user = set(d.project_name for d in get_installed_distributions(user_only=True))
    all = set(d.project_name for d in get_installed_distributions())
    for dist in all - user:
        do_pip(["install", "--upgrade", dist])
    for dist in  user:
        do_pip(["install", "--upgrade", "--user", dist])
1063090
def _check_apt_updates(self):
        
        output = str(subprocess.check_output(["apt", "list", "--upgradeable"]))
        output = output.split(LINE_SEPARATOR)
        return len(output[1:-1])
594609
def dialog_checklist(self):
        
        data = []
        for upg in self.upgrade_all:
            data.append(upg[:-4])
        text = "Press 'spacebar' to unchoose packages from upgrade"
        title = " Upgrade "
        backtitle = "{0} {1}".format(self.meta.__all__,
                                     self.meta.__version__)
        status = True
        pkgs = DialogUtil(data, text, title, backtitle,
                          status).checklist()
        index = 0
        for pkg, comp, uncomp in zip(self.upgrade_all, self.comp_sum,
                                     self.uncomp_sum):
            if pkg[:-4] not in pkgs:
                self.dwn_links.pop(index)
                self.upgrade_all.pop(index)
                self.comp_sum.pop(index)
                self.uncomp_sum.pop(index)
                self.count_upg -= 1
                del comp, uncomp
                index -= 1
            index += 1
        if not self.upgrade_all:
            raise SystemExit()
523031
def check_other(self):
        
        if self.reqs:
            self.stdout.write(self.style.ERROR("\nOnly pypi and github based requirements are supported:"))
            for name, req in self.reqs.items():
                if "dist" in req:
                    pkg_info = "{dist.project_name} {dist.version}".format(dist=req["dist"])
                elif "url" in req:
                    pkg_info = "{url}".format(url=req["url"])
                else:
                    pkg_info = "unknown package"
                self.stdout.write(self.style.BOLD("{pkg_info:40} is not a pypi or github requirement".format(pkg_info=pkg_info)))
82058
def check_new_version_available(this_version):
    
    import requests

    pypi_url = 'https://pypi.python.org/pypi/Zappa/json'
    resp = requests.get(pypi_url, timeout=1.5)
    top_version = resp.json()['info']['version']

    return this_version != top_version
994803
def determine_extra_packages(self, packages):
        

        args = [
            "pip",
            "freeze",
        ]
        installed = subprocess.check_output(args, universal_newlines=True)

        installed_list = set()
        lines = installed.strip().split('\n')
        for (package, version) in self._parse_requirements(lines):
            installed_list.add(package)

        package_list = set()
        for (package, version) in self._parse_requirements(packages.readlines()):
            package_list.add(package)

        removal_list = installed_list - package_list
        return tuple(removal_list)
148438
def check_script(upgrade, conn, dry_run=True, debug=True):
    
    conn = WrappedConnection(conn, debug=debug)
    try:
        upgrade(conn)
    except Exception:
        conn.rollback()
        raise
    else:
        if dry_run:
            conn.rollback()
        else:
            conn.commit()
174700
def pypi_module_version_is_available(module, version):
    ""
    # returns True/False (or None if failed to execute the check)

    # using a hack that when passing "module==" w/ no version number to pip
    # it "fails" and returns all the available versions in stderr
    try:
        cmd = f"pip install {module}=="
        result = subprocess.run(cmd.split(), shell=False, check=False,
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except Exception as e:
        print(f"Error: {e}")
        return None
    else:
        if result.returncode == 1 and result.stderr:
            output = result.stderr.decode('utf-8')
            return True if version in output else False
        else:
            print(f"Some error in {cmd}")
            return None
1172836
def list_pkgs(versions_as_list=False, **kwargs):
    '''
    
    '''
    versions_as_list = salt.utils.data.is_true(versions_as_list)
    # not yet implemented or not applicable
    if any([salt.utils.data.is_true(kwargs.get(x))
            for x in ('removed', 'purge_desired')]):
        return {}

    if 'pkg.list_pkgs' in __context__:
        if versions_as_list:
            return __context__['pkg.list_pkgs']
        else:
            ret = copy.deepcopy(__context__['pkg.list_pkgs'])
            __salt__['pkg_resource.stringify'](ret)
            return ret

    ret = {}
    cmd = 'info --json=v1 --installed'
    package_info = salt.utils.json.loads(_call_brew(cmd)['stdout'])

    for package in package_info:
        # Brew allows multiple versions of the same package to be installed.
        # Salt allows for this, so it must be accounted for.
        versions = [v['version'] for v in package['installed']]
        # Brew allows for aliasing of packages, all of which will be
        # installable from a Salt call, so all names must be accounted for.
        names = package['aliases'] + [package['name'], package['full_name']]
        # Create a list of tuples containing all possible combinations of
        # names and versions, because all are valid.
        combinations = [(n, v) for n in names for v in versions]

        for name, version in combinations:
            __salt__['pkg_resource.add_pkg'](ret, name, version)

    # Grab packages from brew cask, if available.
    # Brew Cask doesn't provide a JSON interface, must be parsed the old way.
    try:
        cask_cmd = 'cask list --versions'
        out = _call_brew(cask_cmd)['stdout']

        for line in out.splitlines():
            try:
                name_and_versions = line.split(' ')
                name = '/'.join(('caskroom/cask', name_and_versions[0]))
                installed_versions = name_and_versions[1:]
                key_func = functools.cmp_to_key(salt.utils.versions.version_cmp)
                newest_version = sorted(installed_versions, key=key_func).pop()
            except ValueError:
                continue
            __salt__['pkg_resource.add_pkg'](ret, name, newest_version)
    except CommandExecutionError:
        pass

    __salt__['pkg_resource.sort_pkglist'](ret)
    __context__['pkg.list_pkgs'] = copy.deepcopy(ret)
    if not versions_as_list:
        __salt__['pkg_resource.stringify'](ret)
    return ret
95481
def pkg_upgrade(self):
        
        options = [
            "-c",
            "--check"
        ]
        flags = [
            "--upgrade",
            "--skip=",
            "--resolve-off",
            "--checklist",
            "--rebuild"
        ]
        flag, skip = self.__pkg_upgrade_flags(flags)
        if (len(self.args) == 3 and self.args[0] in options and
                self.args[2] == flags[0] and
                self.args[1] in self.meta.repositories):
            if self.args[1] not in ["slack", "sbo"]:
                BinaryInstall(pkg_upgrade(self.args[1], skip, flag),
                              self.args[1], flag).start(is_upgrade=True)
            elif self.args[1] == "slack":
                if self.meta.only_installed in ["on", "ON"]:
                    BinaryInstall(pkg_upgrade("slack", skip, flag),
                                  "slack", flag).start(is_upgrade=True)
                else:
                    Patches(skip, flag).start()
            elif self.args[1] == "sbo":
                SBoInstall(sbo_upgrade(skip, flag), flag).start(is_upgrade=True)
            else:
                usage(self.args[1])
        elif len(self.args) == 2 and self.args[0] in options:
            if self.args[1] == "ALL":
                Updates(repo="").ALL()
            else:
                Updates(self.args[1]).run()
        elif (len(self.args) >= 2 and self.args[0] in options and
                self.args[1] not in self.meta.repositories):
            usage(self.args[1])
        else:
            usage("")
335424
def list_pkgs(versions_as_list=False, **kwargs):
    '''
    
    '''
    versions_as_list = salt.utils.data.is_true(versions_as_list)
    # not yet implemented or not applicable
    if any([salt.utils.data.is_true(kwargs.get(x))
            for x in ('removed', 'purge_desired')]):
        return {}

    if 'pkg.list_pkgs' in __context__:
        if versions_as_list:
            return __context__['pkg.list_pkgs']
        else:
            ret = copy.deepcopy(__context__['pkg.list_pkgs'])
            __salt__['pkg_resource.stringify'](ret)
            return ret

    cmd = ['apk', 'info', '-v']
    ret = {}
    out = __salt__['cmd.run'](cmd, output_loglevel='trace', python_shell=False)
    for line in salt.utils.itertools.split(out, '\n'):
        pkg_version = '-'.join(line.split('-')[-2:])
        pkg_name = '-'.join(line.split('-')[:-2])
        __salt__['pkg_resource.add_pkg'](ret, pkg_name, pkg_version)

    __salt__['pkg_resource.sort_pkglist'](ret)
    __context__['pkg.list_pkgs'] = copy.deepcopy(ret)
    if not versions_as_list:
        __salt__['pkg_resource.stringify'](ret)
    return ret
892334
def install_upgrade_powerline():
    '''
    
    '''
    checkup_git_repo_legacy('https://github.com/powerline/powerline.git')
    path_to_powerline = os.path.expanduser('~/repos/powerline')
    run(flo('pip install --user --editable={path_to_powerline}'))
    run('pip show powerline-status')  # only for information
    install_special_glyphs()
    bindings_dir = '~/repos/powerline/powerline/bindings'
    scripts_dir = '~/repos/powerline/scripts'
    return bindings_dir, scripts_dir
1256021
def __updatable():
    
    # Add argument for console
    parser = argparse.ArgumentParser()
    parser.add_argument('file', nargs='?', type=argparse.FileType(), default=None, help='Requirements file')
    args = parser.parse_args()

    # Get list of packages
    if args.file:
        packages = parse_requirements_list(args.file)
    else:
        packages = get_parsed_environment_package_list()

    # Output updates
    for package in packages:
        __list_package_updates(package['package'], package['version'])
370957
def run_outdated(cls, options):
        """"""
        latest_versions = sorted(
            cls.find_packages_latest_versions(cls.options),
            key=lambda p: p[0].project_name.lower())

        for dist, latest_version, typ in latest_versions:
            if latest_version > dist.parsed_version:
                if options.all:
                    pass
                elif options.pinned:
                    if cls.can_be_updated(dist, latest_version):
                        continue
                elif not options.pinned:
                    if not cls.can_be_updated(dist, latest_version):
                        continue
                    elif options.update:
                        print(dist.project_name if options.brief else
                              'Updating %s to Latest: %s [%s]' %
                              (cls.output_package(dist), latest_version, typ))
                        main(['install', '--upgrade'] + ([
                            '--user'
                        ] if ENABLE_USER_SITE else []) + [dist.key])
                        continue

                print(dist.project_name if options.brief else
                      '%s - Latest: %s [%s]' %
                      (cls.output_package(dist), latest_version, typ))