def get_installed_packages() -> dict: '''Get all packages installed in current python environment''' from pip._internal.utils.misc import get_installed_distributions return { item.key: Version(item.version) for item in get_installed_distributions() }
def check_pypi(self): """ If the requirement is frozen to pypi, check for a new version. """ for dist in get_installed_distributions(): name = dist.project_name if name in self.reqs.keys(): self.reqs[name]["dist"] = dist pypi = ServerProxy("https://pypi.python.org/pypi") for name, req in list(self.reqs.items()): if req["url"]: continue # skipping github packages. elif "dist" in req: dist = req["dist"] dist_version = LooseVersion(dist.version) available = pypi.package_releases(req["pip_req"].name, True) or pypi.package_releases(req["pip_req"].name.replace('-', '_'), True) available_version = self._available_version(dist_version, available) if not available_version: msg = self.style.WARN("release is not on pypi (check capitalization and/or --extra-index-url)") elif self.options['show_newer'] and dist_version > available_version: msg = self.style.INFO("{0} available (newer installed)".format(available_version)) elif available_version > dist_version: msg = self.style.INFO("{0} available".format(available_version)) else: msg = "up to date" del self.reqs[name] continue pkg_info = self.style.BOLD("{dist.project_name} {dist.version}".format(dist=dist)) else: msg = "not installed" pkg_info = name print("{pkg_info:40} {msg}".format(pkg_info=pkg_info, msg=msg)) del self.reqs[name]
def run(self, options, args): if options.outdated and options.uptodate: raise CommandError( "Options --outdated and --uptodate cannot be combined.") cmdoptions.check_list_path_option(options) packages = get_installed_distributions( local_only=options.local, user_only=options.user, editables_only=options.editable, include_editables=options.include_editable, paths=options.path, ) # get_not_required must be called firstly in order to find and # filter out all dependencies correctly. Otherwise a package # can't be identified as requirement because some parent packages # could be filtered out before. if options.not_required: packages = self.get_not_required(packages, options) if options.outdated: packages = self.get_outdated(packages, options) elif options.uptodate: packages = self.get_uptodate(packages, options) self.output_package_listing(packages, options)
def run(self, options, args): if options.list_format == "legacy": warnings.warn( "The legacy format has been deprecated and will be removed " "in the future.", RemovedInPip11Warning, ) if options.outdated and options.uptodate: raise CommandError( "Options --outdated and --uptodate cannot be combined.") packages = get_installed_distributions( local_only=options.local, user_only=options.user, editables_only=options.editable, include_editables=options.include_editable, ) if options.outdated: packages = self.get_outdated(packages, options) elif options.uptodate: packages = self.get_uptodate(packages, options) if options.not_required: packages = self.get_not_required(packages, options) self.output_package_listing(packages, options)
def _get_installed_distributions(): try: from pip._internal.utils.misc import get_installed_distributions except ImportError: from pip import get_installed_distributions return get_installed_distributions()
def run(self, options, args): if options.outdated and options.uptodate: raise CommandError( "Options --outdated and --uptodate cannot be combined.") packages = get_installed_distributions( local_only=options.local, user_only=options.user, editables_only=options.editable, include_editables=options.include_editable, ) # get_not_required must be called firstly in order to find and # filter out all dependencies correctly. Otherwise a package # can't be identified as requirement because some parent packages # could be filtered out before. if options.not_required: packages = self.get_not_required(packages, options) if options.outdated: packages = self.get_outdated(packages, options) elif options.uptodate: packages = self.get_uptodate(packages, options) self.output_package_listing(packages, options)
def test_gte_py27_excludes(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions() assert len(dists) == 0
def test_include_globals(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions(local_only=False) assert len(dists) == 4
def __init__( self, finder, # type: PackageFinder preparer, # type: RequirementPreparer make_install_req, # type: InstallRequirementProvider wheel_cache, # type: Optional[WheelCache] use_user_site, # type: bool force_reinstall, # type: bool ignore_installed, # type: bool ignore_requires_python, # type: bool py_version_info=None, # type: Optional[Tuple[int, ...]] ): # type: (...) -> None self._finder = finder self.preparer = preparer self._wheel_cache = wheel_cache self._python_candidate = RequiresPythonCandidate(py_version_info) self._make_install_req_from_spec = make_install_req self._use_user_site = use_user_site self._force_reinstall = force_reinstall self._ignore_requires_python = ignore_requires_python self._link_candidate_cache = {} # type: Cache[LinkCandidate] self._editable_candidate_cache = {} # type: Cache[EditableCandidate] if not ignore_installed: self._installed_dists = { canonicalize_name(dist.project_name): dist for dist in get_installed_distributions() } else: self._installed_dists = {}
def _get_installed_distributions(): try: from pip._internal.utils.misc import get_installed_distributions except ImportError: from pip import get_installed_distributions return get_installed_distributions()
def _find_pip_info(self): """ Try to find information about the installed package from pip. This should be wrapped in a try/except. :returns: information from pip about ``self.package_name``. :rtype: dict """ res = {} dist = None dist_name = self.package_name.replace('_', '-') logger.debug('Checking for pip distribution named: %s', dist_name) for d in get_installed_distributions(): if d.project_name == dist_name: dist = d if dist is None: logger.debug('could not find dist matching package_name') return res logger.debug('found dist: %s', dist) self._pip_locations = [dist.location] ver, url = self._dist_version_url(dist) res['version'] = ver res['url'] = url # this is a bit of an ugly, lazy hack... try: req = FrozenRequirement.from_dist(dist, []) except TypeError: # nocoverage req = FrozenRequirement.from_dist(dist) logger.debug('pip FrozenRequirement: %s', req) res['requirement'] = str(req.req) return res
def __init__( self, finder, # type: PackageFinder preparer, # type: RequirementPreparer make_install_req, # type: InstallRequirementProvider force_reinstall, # type: bool ignore_installed, # type: bool ignore_requires_python, # type: bool upgrade_strategy, # type: str py_version_info=None, # type: Optional[Tuple[int, ...]] ): # type: (...) -> None assert upgrade_strategy in self._allowed_strategies self.finder = finder self.preparer = preparer self._python_candidate = RequiresPythonCandidate(py_version_info) self._make_install_req_from_spec = make_install_req self._force_reinstall = force_reinstall self._ignore_requires_python = ignore_requires_python self._upgrade_strategy = upgrade_strategy self.root_reqs = set() # type: Set[str] self._link_candidate_cache = {} # type: Cache[LinkCandidate] self._editable_candidate_cache = {} # type: Cache[EditableCandidate] if not ignore_installed: self._installed_dists = { canonicalize_name(dist.project_name): dist for dist in get_installed_distributions() } else: self._installed_dists = {}
def find_packages_latest_versions(cls, options): """Yield latest versions.""" index_urls = [] if options.get('no_index') else \ [options.get('index_url')] + options.get('extra_index_urls') with cls._build_session(options) as session: finder = cls._build_package_finder(options, index_urls, session) cls.installed_distributions = get_installed_distributions( local_only=options.get('local'), user_only=options.get('user'), editables_only=options.get('editable'), ) for dist in cls.installed_distributions: all_candidates = finder.find_all_candidates(dist.key) if not options.get('pre'): # Remove prereleases all_candidates = [c for c in all_candidates if not c.version.is_prerelease] if not all_candidates: continue # pylint: disable=protected-access best_candidate = max(all_candidates, key=finder._candidate_sort_key) remote_version = best_candidate.version typ = 'wheel' if best_candidate.location.is_wheel else 'sdist' yield dist, remote_version, typ
def run(self, options, args): if options.list_format == "legacy": warnings.warn( "The legacy format has been deprecated and will be removed " "in the future.", RemovedInPip11Warning, ) if options.outdated and options.uptodate: raise CommandError( "Options --outdated and --uptodate cannot be combined.") packages = get_installed_distributions( local_only=options.local, user_only=options.user, editables_only=options.editable, include_editables=options.include_editable, ) if options.outdated: packages = self.get_outdated(packages, options) elif options.uptodate: packages = self.get_uptodate(packages, options) if options.not_required: packages = self.get_not_required(packages, options) self.output_package_listing(packages, options)
def check(self): from pip._internal.utils.misc import get_installed_distributions from pip._internal.commands.list import ListCommand try: from pip._internal.utils.logging import _log_state if not hasattr(_log_state, 'indentation'): _log_state.indentation = 0 except Exception: pass check_list = {} list_command = ListCommand() options, _ = list_command.parse_args(self.get_default_args_([])) unvers_packages = get_installed_distributions(local_only=options.local, user_only=options.user, editables_only=options.editable) packages = list_command.iter_packages_latest_infos(unvers_packages, options) must_upgrade = False self.print_info_("check list:") for dist in packages: requires = [req.key for req in dist.requires()] if (dist.key == 'lucterios') or ('lucterios' in requires): check_list[dist.project_name] = (dist.version, dist.latest_version, dist.latest_version > dist.parsed_version) must_upgrade = must_upgrade or (dist.latest_version > dist.parsed_version) if dist.latest_version > dist.parsed_version: text_version = 'to upgrade' else: text_version = '' self.print_info_("%25s\t%10s\t=>\t%10s\t%s" % (dist.project_name, dist.version, dist.latest_version, text_version)) if must_upgrade: self.print_info_("\t\t=> Must upgrade") else: self.print_info_("\t\t=> No upgrade") return check_list, must_upgrade
def log_running_python_versions(): logging.info("Python version: " + str(sys.version) + ", " + str(sys.version_info)) # () required in Python 3. installed_packages = get_installed_distributions() installed_packages_list = sorted( ["%s==%s" % (i.key, i.version) for i in installed_packages]) logging.info("Installed Python modules: " + str(installed_packages_list))
def upgrade_third_package(): num = 0 for dist in get_installed_distributions(): num = num + 1 print("num: ", num) print("",dist) call("pip install --upgrade " + dist.project_name, shell=True) print("",dist)
def test_exclude_editables(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions(include_editables=False) assert len(dists) == 1 assert dists[0].test_name == "normal"
def test_user_only(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions(local_only=False, user_only=True) assert len(dists) == 1 assert dists[0].test_name == "user"
def test_freeze_excludes(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions(skip=('setuptools', 'pip', 'distribute')) assert len(dists) == 0
def pip_main(): update_now = 1 package_number = len(get_installed_distributions()) for dist in get_installed_distributions(): try: call( "pip install --upgrade " + dist.project_name + " -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com", shell=True, ) print("all package:{},update now:{}".format( package_number, update_now)) update_now += 1 except Exception: continue print("all is finished:{}".format(s))
def test_include_globals(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions(local_only=False) assert len(dists) == 4
def _Upgrade_(self): number = 0 for data in get_installed_distributions(): number += 1 call("%s install --upgrade %s" % (self._pip, data.project_name), shell=True) print("\n") print("\t\tDone~~\n\n\n")
def get_all_packages(): """ see also: https://stackoverflow.com/questions/10256093/how-to-convert-ctime-to-datetime-in-python 获取到所有安装的 packages :return: """ install_packages = get_installed_distributions() return install_packages
def test_gte_py27_excludes(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions() assert len(dists) == 0
def get_parents(): """Return sorted list of names of packages without dependants.""" distributions = get_installed_distributions(user_only=ENABLE_USER_SITE) remaining = {d.project_name.lower() for d in distributions} requirements = {r.project_name.lower() for d in distributions for r in d.requires()} return get_realnames(remaining - requirements)
def check_pypi(self): """If the requirement is frozen to pypi, check for a new version.""" for dist in get_installed_distributions(): name = dist.project_name if name in self.reqs.keys(): self.reqs[name]["dist"] = dist pypi = ServerProxy("https://pypi.python.org/pypi") for name, req in list(self.reqs.items()): if req["url"]: continue # skipping github packages. elif "dist" in req: dist = req["dist"] dist_version = LooseVersion(dist.version) retry = True available = None while retry: try: available = pypi.package_releases( req["pip_req"].name, True) or pypi.package_releases( req["pip_req"].name.replace('-', '_'), True) retry = False sleep( 1 ) # crude way slow down to avoid HTTPTooManyRequests except Fault as err: self.stdout.write(err.faultString) self.stdout.write("Retrying in 60 seconds!") sleep(60) available_version = self._available_version( dist_version, available) if not available_version: msg = self.style.WARN( "release is not on pypi (check capitalization and/or --extra-index-url)" ) elif self.options[ 'show_newer'] and dist_version > available_version: msg = self.style.INFO( "{0} available (newer installed)".format( available_version)) elif available_version > dist_version: msg = self.style.INFO( "{0} available".format(available_version)) else: msg = "up to date" del self.reqs[name] continue pkg_info = self.style.BOLD( "{dist.project_name} {dist.version}".format(dist=dist)) else: msg = "not installed" pkg_info = name self.stdout.write("{pkg_info:40} {msg}".format(pkg_info=pkg_info, msg=msg)) del self.reqs[name]
def find_missing_reqs(options, requirements_filename): # 1. find files used by imports in the code (as best we can without # executing) used_modules = common.find_imported_modules(options) # 2. find which packages provide which files installed_files = {} all_pkgs = (pkg.project_name for pkg in get_installed_distributions()) for package in search_packages_info(all_pkgs): log.debug('installed package: %s (at %s)', package['name'], package['location']) for package_file in package.get('files', []) or []: path = os.path.realpath( os.path.join(package['location'], package_file), ) installed_files[path] = package['name'] package_path = common.is_package_file(path) if package_path: # we've seen a package file so add the bare package directory # to the installed list as well as we might want to look up # a package by its directory path later installed_files[package_path] = package['name'] # 3. match imported modules against those packages used = collections.defaultdict(list) for modname, info in used_modules.items(): # probably standard library if it's not in the files list if info.filename in installed_files: used_name = canonicalize_name(installed_files[info.filename]) log.debug('used module: %s (from package %s)', modname, installed_files[info.filename]) used[used_name].append(info) else: log.debug( 'used module: %s (from file %s, assuming stdlib or local)', modname, info.filename) # 4. compare with requirements.txt explicit = set() for requirement in parse_requirements( requirements_filename, session=PipSession(), ): try: requirement_name = requirement.name # The type of "requirement" changed between pip versions. # We exclude the "except" from coverage so that on any pip version we # can report 100% coverage. except AttributeError: # pragma: no cover from pip._internal.req.constructors import install_req_from_line requirement_name = install_req_from_line( requirement.requirement, ).name log.debug('found requirement: %s', requirement_name) explicit.add(canonicalize_name(requirement_name)) return [(name, used[name]) for name in used if name not in explicit]
def main(kind, metayaml_file): installed = get_installed_distributions(skip=()) msgs = test_requirements(kind, metayaml_file, installed) if msgs: print("\n ERRORS:\n", file=sys.stderr) for msg in msgs: print(" " + msg, file=sys.stderr) print("", file=sys.stderr) sys.exit(1)
def test_freeze_excludes(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions( skip=('setuptools', 'pip', 'distribute')) assert len(dists) == 0
def main(): # print('python版本: %s' % sys.version) for dist in get_installed_distributions(): print('[%s: %s]' % (dist.project_name, dist.location)) call( 'sudo -H pip3 install -U -i https://pypi.tuna.tsinghua.edu.cn/simple ' + dist.project_name, shell=True) print('-------华丽的分割线-------')
def main(kind, metayaml_file): installed = get_installed_distributions(skip=(), local_only=False) msgs = test_requirements(kind, metayaml_file, installed) if msgs: print("\n ERRORS:\n", file=sys.stderr) for msg in msgs: print(" " + msg, file=sys.stderr) print("", file=sys.stderr) sys.exit(1)
def test_exclude_editables(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions(include_editables=False) assert len(dists) == 1 assert dists[0].test_name == "normal"
def test_editables_only(self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local mock_dist_in_usersite.side_effect = self.dist_in_usersite dists = get_installed_distributions(editables_only=True) assert len(dists) == 1, dists assert dists[0].test_name == "editable"
def get_package_list(): def get_files(dist): paths = [] # RECORDs should be part of .dist-info metadatas if dist.has_metadata('RECORD'): lines = dist.get_metadata_lines('RECORD') paths = [ line_item.split(',')[0].replace('/', os.sep) for line_item in lines ] paths = [ os.path.join(dist.location, path_item) for path_item in paths ] # Otherwise use pip's log for .egg-info's elif dist.has_metadata('installed-files.txt'): paths = dist.get_metadata_lines('installed-files.txt') paths = [ os.path.join(dist.egg_info, p.replace('/', os.sep)) for p in paths ] elif os.path.join(dist.location, '__init__.py'): paths = [ os.path.join(dir_desc[0], file_item) for dir_desc in os.walk(dist.location) for file_item in dir_desc[2] if file_item[-3:] == '.py' ] return [os.path.relpath(p, dist.location) for p in paths] def get_module_desc(modname): appmodule = import_module(modname) if hasattr(appmodule, 'link'): return (modname, appmodule.__version__, appmodule.link()) else: return (modname, appmodule.__version__, []) from pip._internal.utils.misc import get_installed_distributions package_list = {} dists = get_installed_distributions() for dist in dists: requires = [req.key for req in dist.requires()] if (dist.key == 'lucterios') or ('lucterios' in requires): current_applis = [] current_modules = [] for file_item in get_files(dist): try: py_mod_name = ".".join(file_item.split(os.sep)[:-1]) if file_item.endswith('appli_settings.py'): current_applis.append(get_module_desc(py_mod_name)) elif file_item.endswith('models.py'): current_modules.append(get_module_desc(py_mod_name)) except Exception: pass current_applis.sort() current_modules.sort() package_list[dist.key] = (dist.version, current_applis, current_modules, requires) return package_list
def get_installed_pkgs(): installed_pkgs = get_installed_distributions() for pkg in installed_pkgs: meta = get_pkg_metadata(pkg, META_FILES) pkg.license = get_license_from_meta(meta).strip() pkg.license_text = get_pkg_metadata(pkg, LICENSE_FILES) pkg.url = get_url_from_meta(meta).strip() pkg.project_name_lower = pkg.project_name.lower() yield pkg
def update_lib(): """ 更新依赖方式一 :return: """ from pip._internal.utils.misc import get_installed_distributions packages = [dist.project_name for dist in get_installed_distributions()] call("pip install --upgrade" + ' '.join(packages), shell=True)
def add_package_list_to_report(request): packages = "" for package in get_installed_distributions(local_only=True): str_variable = package.key + ":" + package.version + "\n" packages += str_variable request.config._metadata.update( {"packages": packages} ) yield
def create_package_set_from_installed(**kwargs): # type: (**Any) -> PackageSet """Converts a list of distributions into a PackageSet. """ retval = {} for dist in get_installed_distributions(**kwargs): name = canonicalize_name(dist.project_name) retval[name] = PackageDetails(dist.version, dist.requires()) return retval
def main(file): packages = get_packages_in_requirements(file) deps = [] for p in packages: get_dependencies_with_semver_string(p, deps) deps_keys = [p.key for p in deps] all_keys = [p.key for p in get_installed_distributions()] ignore = [p for p in all_keys if p not in deps_keys] print(" ".join(ignore))
def getModuleSize(moduleName): '''Get the size of a given module as a string ''' for dist in get_installed_distributions(): if moduleName == dist.project_name: path = os.path.join(dist.location, dist.project_name) size = calc_container(path) if size: return "size={}".format(getSize(size)) return getOnlineSize(moduleName)
def main(metayaml_file): installed = get_installed_distributions(skip=()) msgs = test_requirements("build", metayaml_file, installed) if msgs: print("\n ERRORS:\n", file=sys.stderr) for msg in msgs: print(" " + msg, file=sys.stderr) print("", file=sys.stderr) sys.exit(1)
def main(): fails = [] for dist in get_installed_distributions(): code = call("pip install --upgrade " + dist.project_name, shell=True) if code != 0: fails.append(dist) for dist in fails: print("error in updating package:" + dist.project_name) print("packages not updated:{}".format(len(fails)))
def create_licenses_table(args): def get_pkg_info(pkg): pkg_info = { 'name': pkg.project_name, 'version': pkg.version, 'namever': str(pkg), } metadata = None if pkg.has_metadata('METADATA'): metadata = pkg.get_metadata('METADATA') if pkg.has_metadata('PKG-INFO') and metadata is None: metadata = pkg.get_metadata('PKG-INFO') if metadata is None: for key in METADATA_KEYS: pkg_info[key] = LICENSE_UNKNOWN return pkg_info feed_parser = FeedParser() feed_parser.feed(metadata) parsed_metadata = feed_parser.close() for key in METADATA_KEYS: pkg_info[key] = parsed_metadata.get(key, LICENSE_UNKNOWN) if args.from_classifier and metadata is not None: message = message_from_string(metadata) pkg_info['license'] = find_license_from_classifier(message) return pkg_info table = factory_styled_table_with_args(args) pkgs = get_installed_distributions() ignore_pkgs_as_lower = [pkg.lower() for pkg in args.ignore_packages] for pkg in pkgs: pkg_info = get_pkg_info(pkg) pkg_name = pkg_info['name'] if pkg_name.lower() in ignore_pkgs_as_lower: continue if not args.with_system and pkg_name in SYSTEM_PACKAGES: continue table.add_row([ pkg_info['name'], pkg_info['version'], pkg_info['license'], pkg_info['author'], pkg_info['home-page'], ]) return table
def find_owners(path): """Return the package(s) that file belongs to.""" abspath = os.path.abspath(path) packages = search_packages_info( sorted((d.project_name for d in get_installed_distributions(user_only=ENABLE_USER_SITE)), key=lambda d: d.lower())) return [p['name'] for p in packages if is_owner(p, abspath)]
def create_package_set_from_installed(**kwargs): # type: (**Any) -> PackageSet """Converts a list of distributions into a PackageSet. """ # Default to using all packages installed on the system if kwargs == {}: kwargs = {"local_only": False, "skip": ()} retval = {} for dist in get_installed_distributions(**kwargs): name = canonicalize_name(dist.project_name) retval[name] = PackageDetails(dist.version, dist.requires()) return retval
def Check_PIP(): try: try: # for pip >= 10 from pip._internal.utils.misc import get_installed_distributions, get_installed_version except ImportError: # for pip <= 9.0.3 from pip.utils import get_installed_distributions, get_installed_version global Installed_Packages Installed_Packages = {pkg.project_name:pkg.version for pkg in get_installed_distributions()} INFO("Installed PIP: " + get_installed_version("pip")) return True except ImportError: ERROR("Import pip failed. Please Install python3-pip and try again") return False
def main(): # print('python版本: %s' % sys.version) for dist in get_installed_distributions(): print('[%s: %s]' % (dist.project_name, dist.location)) if 'local' in dist.location: call( 'sudo -H pip2 install -U -i https://pypi.tuna.tsinghua.edu.cn/simple ' + dist.project_name, shell=True) else: # 说明此包使用的是apt-get install python-* 形式安装的 # 由于不推荐使用python2, 所以python2的包尽量保持最小化 print(' installed by OS, I think it is better not to upgrade.') print('-------华丽的分割线-------')
def get_package_list(): def get_files(dist): paths = [] # RECORDs should be part of .dist-info metadatas if dist.has_metadata('RECORD'): lines = dist.get_metadata_lines('RECORD') paths = [l.split(',')[0].replace('/', os.sep) for l in lines] paths = [os.path.join(dist.location, p) for p in paths] # Otherwise use pip's log for .egg-info's elif dist.has_metadata('installed-files.txt'): paths = dist.get_metadata_lines('installed-files.txt') paths = [ os.path.join(dist.egg_info, p.replace('/', os.sep)) for p in paths] elif os.path.join(dist.location, '__init__.py'): paths = [os.path.join(dir_desc[0], file_item) for dir_desc in os.walk( dist.location) for file_item in dir_desc[2] if file_item[-3:] == '.py'] return [os.path.relpath(p, dist.location) for p in paths] def get_module_desc(modname): appmodule = import_module(modname) if hasattr(appmodule, 'link'): return (modname, appmodule.__version__, appmodule.link()) else: return (modname, appmodule.__version__, []) from pip._internal.utils.misc import get_installed_distributions package_list = {} dists = get_installed_distributions() for dist in dists: requires = [req.key for req in dist.requires()] if (dist.key == 'lucterios') or ('lucterios' in requires): current_applis = [] current_modules = [] for file_item in get_files(dist): try: py_mod_name = ".".join(file_item.split(os.sep)[:-1]) if file_item.endswith('appli_settings.py'): current_applis.append(get_module_desc(py_mod_name)) elif file_item.endswith('models.py'): current_modules.append(get_module_desc(py_mod_name)) except Exception: pass current_applis.sort() current_modules.sort() package_list[dist.key] = ( dist.version, current_applis, current_modules, requires) return package_list
def find_missing_reqs(options): # 1. find files used by imports in the code (as best we can without # executing) used_modules = common.find_imported_modules(options) # 2. find which packages provide which files installed_files = {} all_pkgs = (pkg.project_name for pkg in get_installed_distributions()) for package in search_packages_info(all_pkgs): log.debug('installed package: %s (at %s)', package['name'], package['location']) for file in package.get('files', []) or []: path = os.path.realpath(os.path.join(package['location'], file)) installed_files[path] = package['name'] package_path = common.is_package_file(path) if package_path: # we've seen a package file so add the bare package directory # to the installed list as well as we might want to look up # a package by its directory path later installed_files[package_path] = package['name'] # 3. match imported modules against those packages used = collections.defaultdict(list) for modname, info in used_modules.items(): # probably standard library if it's not in the files list if info.filename in installed_files: used_name = canonicalize_name(installed_files[info.filename]) log.debug('used module: %s (from package %s)', modname, installed_files[info.filename]) used[used_name].append(info) else: log.debug( 'used module: %s (from file %s, assuming stdlib or local)', modname, info.filename) # 4. compare with requirements.txt explicit = set() for requirement in parse_requirements('requirements.txt', session=PipSession()): log.debug('found requirement: %s', requirement.name) explicit.add(canonicalize_name(requirement.name)) return [(name, used[name]) for name in used if name not in explicit]
def create_package_set_from_installed(**kwargs): # type: (**Any) -> Tuple[PackageSet, bool] """Converts a list of distributions into a PackageSet. """ # Default to using all packages installed on the system if kwargs == {}: kwargs = {"local_only": False, "skip": ()} package_set = {} problems = False for dist in get_installed_distributions(**kwargs): name = canonicalize_name(dist.project_name) try: package_set[name] = PackageDetails(dist.version, dist.requires()) except RequirementParseError as e: # Don't crash on broken metadata logging.warning("Error parsing requirements for %s: %s", name, e) problems = True return package_set, problems
def run(self, options, args): if options.outdated and options.uptodate: raise CommandError( "Options --outdated and --uptodate cannot be combined.") packages = get_installed_distributions( local_only=options.local, user_only=options.user, editables_only=options.editable, include_editables=options.include_editable, ) if options.outdated: packages = self.get_outdated(packages, options) elif options.uptodate: packages = self.get_uptodate(packages, options) if options.not_required: packages = self.get_not_required(packages, options) self.output_package_listing(packages, options)
def update(self): from pip._internal import main from pip._internal.utils.misc import get_installed_distributions module_list = [] for dist in get_installed_distributions(): requires = [req.key for req in dist.requires()] if (dist.key == 'lucterios') or ('lucterios' in requires): module_list.append(dist.project_name) if len(module_list) > 0: try: self.print_info_("Modules to update: %s" % ",".join(module_list)) options = self.get_default_args_(['install', '-U']) options.extend(module_list) main(options) finally: self.refreshall() return True else: self.print_info_("No modules to update") return False
def run(self, options, args): dists = get_installed_distributions(local_only=False, skip=()) missing_reqs_dict, incompatible_reqs_dict = check_requirements(dists) for dist in dists: for requirement in missing_reqs_dict.get(dist.key, []): logger.info( "%s %s requires %s, which is not installed.", dist.project_name, dist.version, requirement.project_name) for requirement, actual in incompatible_reqs_dict.get( dist.key, []): logger.info( "%s %s has requirement %s, but you have %s %s.", dist.project_name, dist.version, requirement, actual.project_name, actual.version) if missing_reqs_dict or incompatible_reqs_dict: return 1 else: logger.info("No broken requirements found.")
def main(): args = _get_args() pkgs = get_installed_distributions(local_only=args.local_only, user_only=args.user_only) dist_index = build_dist_index(pkgs) tree = construct_tree(dist_index) if args.json: print(render_json(tree, indent=4)) return 0 elif args.json_tree: print(render_json_tree(tree, indent=4)) return 0 elif args.output_format: output = dump_graphviz(tree, output_format=args.output_format) print_graphviz(output) return 0 return_code = 0 # show warnings about possibly conflicting deps if found and # warnings are enabled if args.warn != 'silence': conflicting = conflicting_deps(tree) if conflicting: print('Warning!!! Possibly conflicting dependencies found:', file=sys.stderr) for p, reqs in conflicting.items(): pkg = p.render_as_root(False) print('* {}'.format(pkg), file=sys.stderr) for req in reqs: req_str = req.render_as_branch(False) print(' - {}'.format(req_str), file=sys.stderr) print('-'*72, file=sys.stderr) cyclic = cyclic_deps(tree) if cyclic: print('Warning!! Cyclic dependencies found:', file=sys.stderr) for a, b, c in cyclic: print('* {0} => {1} => {2}'.format(a.project_name, b.project_name, c.project_name), file=sys.stderr) print('-'*72, file=sys.stderr) if args.warn == 'fail' and (conflicting or cyclic): return_code = 1 show_only = set(args.packages.split(',')) if args.packages else None exclude = set(args.exclude.split(',')) if args.exclude else None if show_only and exclude and (show_only & exclude): print('Conflicting packages found in --packages and --exclude lists.', file=sys.stderr) sys.exit(1) tree = render_tree(tree if not args.reverse else reverse_tree(tree), list_all=args.all, show_only=show_only, frozen=args.freeze, exclude=exclude) print(tree) return return_code
def get_dependants(project_name): """Yield dependants of `project_name`.""" for package in get_installed_distributions(user_only=ENABLE_USER_SITE): if is_dependant(package, project_name): yield package.project_name
def autocomplete(): """Command and option completion for the main option parser (and options) and its subcommands (and options). Enable by sourcing one of the completion shell scripts (bash, zsh or fish). """ # Don't complete if user hasn't sourced bash_completion file. if 'PIP_AUTO_COMPLETE' not in os.environ: return cwords = os.environ['COMP_WORDS'].split()[1:] cword = int(os.environ['COMP_CWORD']) try: current = cwords[cword - 1] except IndexError: current = '' subcommands = [cmd for cmd, summary in get_summaries()] options = [] # subcommand try: subcommand_name = [w for w in cwords if w in subcommands][0] except IndexError: subcommand_name = None parser = create_main_parser() # subcommand options if subcommand_name: # special case: 'help' subcommand has no options if subcommand_name == 'help': sys.exit(1) # special case: list locally installed dists for show and uninstall should_list_installed = ( subcommand_name in ['show', 'uninstall'] and not current.startswith('-') ) if should_list_installed: installed = [] lc = current.lower() for dist in get_installed_distributions(local_only=True): if dist.key.startswith(lc) and dist.key not in cwords[1:]: installed.append(dist.key) # if there are no dists installed, fall back to option completion if installed: for dist in installed: print(dist) sys.exit(1) subcommand = commands_dict[subcommand_name]() for opt in subcommand.parser.option_list_all: if opt.help != optparse.SUPPRESS_HELP: for opt_str in opt._long_opts + opt._short_opts: options.append((opt_str, opt.nargs)) # filter out previously specified options from available options prev_opts = [x.split('=')[0] for x in cwords[1:cword - 1]] options = [(x, v) for (x, v) in options if x not in prev_opts] # filter options by current input options = [(k, v) for k, v in options if k.startswith(current)] for option in options: opt_label = option[0] # append '=' to options which require args if option[1] and option[0][:2] == "--": opt_label += '=' print(opt_label) else: # show main parser options only when necessary if current.startswith('-') or current.startswith('--'): opts = [i.option_list for i in parser.option_groups] opts.append(parser.option_list) opts = (o for it in opts for o in it) for opt in opts: if opt.help != optparse.SUPPRESS_HELP: subcommands += opt._long_opts + opt._short_opts print(' '.join([x for x in subcommands if x.startswith(current)])) sys.exit(1)
def main(): default_skip = ['setuptools', 'pip', 'python', 'distribute'] skip = default_skip + ['pipdeptree'] pkgs = get_installed_distributions(local_only=True, skip=skip) pickle.dump(pkgs, sys.stdout) return 0
def freeze( requirement=None, # type: Optional[List[str]] find_links=None, # type: Optional[List[str]] local_only=None, # type: Optional[bool] user_only=None, # type: Optional[bool] skip_regex=None, # type: Optional[str] isolated=False, # type: bool wheel_cache=None, # type: Optional[WheelCache] exclude_editable=False, # type: bool skip=() # type: Container[str] ): # type: (...) -> Iterator[str] find_links = find_links or [] skip_match = None if skip_regex: skip_match = re.compile(skip_regex).search for link in find_links: yield '-f %s' % link installations = {} # type: Dict[str, FrozenRequirement] for dist in get_installed_distributions(local_only=local_only, skip=(), user_only=user_only): try: req = FrozenRequirement.from_dist(dist) except RequirementParseError: logger.warning( "Could not parse requirement: %s", dist.project_name ) continue if exclude_editable and req.editable: continue installations[req.name] = req if requirement: # the options that don't get turned into an InstallRequirement # should only be emitted once, even if the same option is in multiple # requirements files, so we need to keep track of what has been emitted # so that we don't emit it again if it's seen again emitted_options = set() # type: Set[str] # keep track of which files a requirement is in so that we can # give an accurate warning if a requirement appears multiple times. req_files = collections.defaultdict(list) # type: Dict[str, List[str]] for req_file_path in requirement: with open(req_file_path) as req_file: for line in req_file: if (not line.strip() or line.strip().startswith('#') or (skip_match and skip_match(line)) or line.startswith(( '-r', '--requirement', '-Z', '--always-unzip', '-f', '--find-links', '-i', '--index-url', '--pre', '--trusted-host', '--process-dependency-links', '--extra-index-url'))): line = line.rstrip() if line not in emitted_options: emitted_options.add(line) yield line continue if line.startswith('-e') or line.startswith('--editable'): if line.startswith('-e'): line = line[2:].strip() else: line = line[len('--editable'):].strip().lstrip('=') line_req = install_req_from_editable( line, isolated=isolated, wheel_cache=wheel_cache, ) else: line_req = install_req_from_line( COMMENT_RE.sub('', line).strip(), isolated=isolated, wheel_cache=wheel_cache, ) if not line_req.name: logger.info( "Skipping line in requirement file [%s] because " "it's not clear what it would install: %s", req_file_path, line.strip(), ) logger.info( " (add #egg=PackageName to the URL to avoid" " this warning)" ) elif line_req.name not in installations: # either it's not installed, or it is installed # but has been processed already if not req_files[line_req.name]: logger.warning( "Requirement file [%s] contains %s, but " "package %r is not installed", req_file_path, COMMENT_RE.sub('', line).strip(), line_req.name ) else: req_files[line_req.name].append(req_file_path) else: yield str(installations[line_req.name]).rstrip() del installations[line_req.name] req_files[line_req.name].append(req_file_path) # Warn about requirements that were included multiple times (in a # single requirements file or in different requirements files). for name, files in six.iteritems(req_files): if len(files) > 1: logger.warning("Requirement %s included multiple times [%s]", name, ', '.join(sorted(set(files)))) yield( '## The following requirements were added by ' 'pip freeze:' ) for installation in sorted( installations.values(), key=lambda x: x.name.lower()): if canonicalize_name(installation.name) not in skip: yield str(installation).rstrip()
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' @Author : Yuanze @version : @Time : 2019/2/19 17:30 @license : Copyright(C), Your Company ''' import pip # pip V10.0.0以上版本需要导入下面的包 from pip._internal.utils.misc import get_installed_distributions from subprocess import call from time import sleep for dist in get_installed_distributions(): # 执行后,pip默认为Python3版本 # 双版本下需要更新Python2版本的包,使用py2运行,并将pip修改成pip2 call("pip install --upgrade " + dist.project_name, shell=True)