def find_packages_latests_versions(self, options): index_urls = [options.index_url] + options.extra_index_urls if options.no_index: logger.notify('Ignoring indexes: %s' % ','.join(index_urls)) index_urls = [] if options.use_mirrors: logger.deprecated("1.7", "--use-mirrors has been deprecated and will be removed" " in the future. Explicit uses of --index-url and/or " "--extra-index-url is suggested.") if options.mirrors: logger.deprecated("1.7", "--mirrors has been deprecated and will be removed in " " the future. Explicit uses of --index-url and/or " "--extra-index-url is suggested.") index_urls += options.mirrors dependency_links = [] for dist in get_installed_distributions(local_only=options.local, skip=self.skip): if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt'), ) session = self._build_session(options) finder = self._build_package_finder(options, index_urls, session) finder.add_dependency_links(dependency_links) installed_packages = get_installed_distributions(local_only=options.local, include_editables=False, skip=self.skip) for dist in installed_packages: req = InstallRequirement.from_line(dist.key, None) try: link = finder.find_requirement(req, True) # If link is None, means installed version is most up-to-date if link is None: continue except DistributionNotFound: continue except BestVersionAlreadyInstalled: remote_version = req.installed_version else: # It might be a good idea that link or finder had a public method # that returned version remote_version = finder._link_package_versions(link, req.name)[0] remote_version_raw = remote_version[2] remote_version_parsed = remote_version[0] yield dist, remote_version_raw, remote_version_parsed
def test_exclude_editables( self, mock_dist_is_editable, mock_dist_is_local): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local dists = get_installed_distributions(include_editables=False) assert len(dists) == 1 assert dists[0].test_name == "normal"
def load_installed_distributions(self): import pip from pip.util import get_installed_distributions for dist in get_installed_distributions(local_only=True): pd = PackageData.from_dist(pip.FrozenRequirement.from_dist(dist, [], find_tags=True), pre_installed=True) if pd.editable and pd.location is not None: self.repo_up_to_date[pd.location] = False self.pre_installed[pd.name] = pd
def export_env(self): """ This should wrap pip freeze in the future iterates over pip installed packages, local and not local returns tuples version, name """ for each_distribution in get_installed_distributions(): yield each_distribution.version, each_distribution.key
def freezer(local=False, skip=excludes): """ Return a dict of installed python package versions keyed by name. """ installations = {} for dist in get_installed_distributions(local_only=local, skip=skip): req = pip.FrozenRequirement.from_dist(dist, []) installations[req.name] = dist.version return installations
def get_package_information(): ret = {} reload(pkg_resources) # gather new package information distributions = get_installed_distributions(local_only=False) for distribution in distributions: project_name = distribution.project_name version = distribution.version ret[project_name] = version return ret
def run(self, options, args): self.installed_packages = [p.project_name for p in pkg_resources.working_set] self.pypi = xmlrpclib.ServerProxy(index_url, pip.download.xmlrpclib_transport) packages = set([]) for dist in get_installed_distributions(local_only=True): pypi_hits = self.search(dist.key) hits = transform_hits(pypi_hits) data = [(i['name'], highest_version(i['versions'])) for i in hits] packages = packages.union(set(data)) self.print_results(packages)
def do_version_info(options): """ List all installed packages with his versions and version info from PyPi """ local_only = True # exclude globally-installed packages in a virtualenv dependency_links = [] for dist in pkg_resources.working_set: if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt'), ) installed_info = [] for dist in get_installed_distributions(local_only=local_only): req = dist.as_requirement() # pkg_resources.Requirement() instance project_name = req.project_name specs = req.specs version = specs[0][1] installed_info.append((project_name, version)) max_name_len = max([len(i[0]) for i in installed_info]) max_ver_len = max([len(i[1]) for i in installed_info]) if max_name_len<20: max_name_len = 20 if max_ver_len<20: max_ver_len = 20 max_len_others = get_terminal_size()[0] - max_name_len - max_ver_len - 7 table_header = " package name".ljust(max_name_len) table_header += " installed version".ljust(max_ver_len) table_header += " versions on PyPi".ljust(max_len_others) print c.colorize(table_header, opts=("underscore",)) for project_name, version in installed_info: print project_name.ljust(max_name_len), print version.ljust(max_ver_len), # http://wiki.python.org/moin/PyPiXmlRpc client = xmlrpclib.ServerProxy(INDEX_URL) versions = client.package_releases(project_name) if not versions: print c.colorize("No version found at PyPi!", foreground="yellow") else: latest = highest_version(versions) older_versions = [v for v in versions if latest!=v] pypi_info = "%s" % latest if older_versions: pypi_info += " - %s" % ", ".join(older_versions) if len(pypi_info)>max_len_others: pypi_info = pypi_info[:max_len_others] + " ..." print pypi_info
def pip_get_installed(): """Code extracted from the middle of the pip freeze command. """ from pip.util import get_installed_distributions installed = [] for dist in get_installed_distributions(local_only=True): req = dist_to_req(dist) installed.append(req) return installed
def load_installed_distributions(self): import pip from pip.util import get_installed_distributions for dist in get_installed_distributions(local_only=True, skip=[]): dist_as_req = dist.as_requirement() # if pip patches an earlier version of setuptools as distribute, skip it if (dist_as_req.project_name == 'distribute' and dist_as_req.specs == []): continue pd = PackageData.from_dist(pip.FrozenRequirement.from_dist(dist, [], find_tags=True), pre_installed=True) if pd.editable and pd.location is not None: self.repo_up_to_date[pd.location] = False self.pre_installed[pd.name] = pd
def find_packages_latests_versions(self, options): index_urls = [options.index_url] + options.extra_index_urls if options.no_index: logger.notify('Ignoring indexes: %s' % ','.join(index_urls)) index_urls = [] if options.use_mirrors: logger.deprecated( "1.7", "--use-mirrors has been deprecated and will be removed" " in the future. Explicit uses of --index-url and/or " "--extra-index-url is suggested." ) if options.mirrors: logger.deprecated( "1.7", "--mirrors has been deprecated and will be removed in " " the future. Explicit uses of --index-url and/or " "--extra-index-url is suggested." ) index_urls += options.mirrors session = self._build_session(options) finder = self._build_package_finder(options, index_urls, session) installed_packages = get_installed_distributions( local_only=options.local, include_editables=False, ) for dist in installed_packages: req = InstallRequirement.from_line(dist.key, None) try: link = finder.find_requirement(req, True) # If link is None, means installed version is most up-to-date if link is None: continue except DistributionNotFound: continue except BestVersionAlreadyInstalled: remote_version = req.installed_version else: # It might be a good idea that link or finder had a public # method that returned version remote_version = finder._link_package_versions( link, req.name )[0] remote_version_raw = remote_version[2] remote_version_parsed = remote_version[0] yield dist, remote_version_raw, remote_version_parsed
def get_upgradeable(): dependency_links = [] for dist in pkg_resources.working_set: if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt')) print("\ndependency_links: %r\n" % dependency_links) packages = [] editables = [] for dist in get_installed_distributions(local_only=True): req = pip.FrozenRequirement.from_dist( dist, dependency_links=dependency_links) if not req.editable: packages.append(req.name) else: # FIXME: How can we get this needes information easier? raw_cmd = str(req) full_url = raw_cmd.split()[1] url, full_version = full_url.rsplit("@", 1) rev = full_version.rsplit("-", 1)[1] if rev != "dev": pip_url = "%s@%s#egg=%s" % (url, rev, req.name) else: pip_url = "%s#egg=%s" % (url, req.name) editables.append(pip_url) if not packages: print( c.colorize("Found no local packages.", foreground="blue", opts=("underscore", ))) else: print(c.colorize("Found theses local packages:", foreground="green")) for package in packages: print("\t %s" % package) if not editables: print( c.colorize("Found no local editables.", foreground="blue", opts=("underscore", ))) else: print(c.colorize("Found theses local editables:", foreground="green")) for editable in editables: print("\t %s" % editable) return packages, editables
def browse(request): import pkg_resources from pip.commands.search import highest_version, transform_hits from pip.util import get_installed_distributions import xmlrpclib pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi') installed_packages = [p.project_name for p in pkg_resources.working_set] results = [] for p in get_installed_distributions(): result = {} hits = pypi.search({ 'name': p.project_name, 'summary': p.project_name }, 'or') hits = transform_hits(hits) for hit in hits: try: if hit['name'] in installed_packages: dist = pkg_resources.get_distribution(hit['name']) result['name'] = hit['name'] result['version'] = dist.version result['summary'] = hit['summary'] or '' try: result['latest'] = highest_version(hit['versions']) finally: results.append(result) except UnicodeEncodeError: pass query = request.GET.copy() return render_to_response( 'djip/index.html', { 'results': results, #'dir': path, #'p': p, #'q': q, #'page': page, #'results_var': results_var, #'counter': counter, 'query': query, 'title': _(u'PIP browser'), #'settings_var': get_settings_var(), #'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': "" }, context_instance=RequestContext(request))
def find_packages_latests_versions(self, options): index_urls = [options.index_url] + options.extra_index_urls if options.no_index: logger.notify('Ignoring indexes: %s' % ','.join(index_urls)) index_urls = [] dependency_links = [] for dist in get_installed_distributions(local_only=options.local, skip=self.skip): if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt'), ) finder = self._build_package_finder(options, index_urls) finder.add_dependency_links(dependency_links) installed_packages = get_installed_distributions( local_only=options.local, include_editables=False, skip=self.skip) for dist in installed_packages: req = InstallRequirement.from_line(dist.key, None) try: link = finder.find_requirement(req, True) # If link is None, means installed version is most up-to-date if link is None: continue except DistributionNotFound: continue except BestVersionAlreadyInstalled: remote_version = req.installed_version else: # It might be a good idea that link or finder had a public method # that returned version remote_version = finder._link_package_versions(link, req.name)[0] remote_version_raw = remote_version[2] remote_version_parsed = remote_version[0] yield dist, remote_version_raw, remote_version_parsed
def find_packages_latests_versions(self, options): index_urls = [options.index_url] + options.extra_index_urls if options.no_index: logger.notify('Ignoring indexes: %s' % ','.join(index_urls)) index_urls = [] dependency_links = [] for dist in get_installed_distributions(local_only=options.local): if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt'), ) finder = self._build_package_finder(options, index_urls) finder.add_dependency_links(dependency_links) installed_packages = get_installed_distributions(local_only=options.local, include_editables=False) for dist in installed_packages: req = InstallRequirement.from_line(dist.key, None) try: link = finder.find_requirement(req, True) # If link is None, means installed version is most up-to-date if link is None: continue except DistributionNotFound: continue except BestVersionAlreadyInstalled: remote_version = req.installed_version else: # It might be a good idea that link or finder had a public method # that returned version remote_version = finder._link_package_versions(link, req.name)[0] remote_version_raw = remote_version[2] remote_version_parsed = remote_version[0] yield dist, remote_version_raw, remote_version_parsed
def info(self, **options): from djip import get_version from pip.commands.search import transform_hits from pip.util import get_installed_distributions import xmlrpclib pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi') print "DjIP version %s\n" % get_version() for p in get_installed_distributions(): hits = pypi.search({'name': p.project_name, 'summary': p.project_name}, 'or') hits = transform_hits(hits) self.print_results(hits)
def difio_register(data, useragent, excluded_names=[]): """ Register to Difio @data = { 'user_id' : int, 'app_name' : '', 'app_uuid' : '', 'app_type' : '', 'app_url' : '', 'app_vendor' : int, 'pkg_type' : int, 'installed' : [{'n: 'name', 'v' : 'version' : 't' : int (optional)}], } """ if not data.has_key('pkg_type'): data['pkg_type'] = 0 # Python if not data.has_key('app_type'): data['app_type'] = 'python-%d.%d.%d' % ( sys.version_info[0], sys.version_info[1], sys.version_info[2]) # make a list of package names for dist in get_installed_distributions(local_only=True): # skip some packages if told to if dist.project_name.lower() not in excluded_names: data['installed'].append({ 'n': dist.project_name, 'v': dist.version }) json_data = json.dumps(data) params = urllib.urlencode({'json_data': json_data}) headers = {"User-agent": "%s" % useragent} conn = httplib.HTTPSConnection('difio-otb.rhcloud.com') conn.request('POST', '/application/register/', params, headers) response = conn.getresponse() if (response.status != 200) or (not response.getheader( 'Content-type').startswith('application/json')): raise BaseException('Communication failed - %s' % response.read()) result = json.loads(response.read()) print result['message'] sys.exit(result['exit_code'])
def main(argv=None): import sys from argparse import ArgumentParser argv = argv or sys.argv parser = ArgumentParser( description='show download urls for installed packages') parser.add_argument('--unknown', help='show unknown urls', action='store_true', default=False) args = parser.parse_args(argv[1:]) for dist in get_installed_distributions(): url = download_url(dist) if url or args.unknown: print('{}: {}'.format(dist.project_name, url or 'UNKNOWN'))
def browse(request): import pkg_resources from pip.commands.search import highest_version, transform_hits from pip.util import get_installed_distributions import xmlrpclib pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi') installed_packages = [p.project_name for p in pkg_resources.working_set] results = [] for p in get_installed_distributions(): result = {} hits = pypi.search({'name': p.project_name, 'summary': p.project_name}, 'or') hits = transform_hits(hits) for hit in hits: try: if hit['name'] in installed_packages: dist = pkg_resources.get_distribution(hit['name']) result['name'] = hit['name'] result['version'] = dist.version result['summary'] = hit['summary'] or '' try: result['latest'] = highest_version(hit['versions']) finally: results.append(result) except UnicodeEncodeError: pass query = request.GET.copy() return render_to_response('djip/index.html', { 'results': results, #'dir': path, #'p': p, #'q': q, #'page': page, #'results_var': results_var, #'counter': counter, 'query': query, 'title': _(u'PIP browser'), #'settings_var': get_settings_var(), #'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': "" }, context_instance=RequestContext(request))
def remove_armstrong(): """Remove all Armstrong components (except for Dev) from this environment""" from pip.util import get_installed_distributions pkgs = get_installed_distributions(local_only=True, include_editables=True) apps = [pkg for pkg in pkgs if pkg.key.startswith('armstrong') and pkg.key != 'armstrong.dev'] for app in apps: run("pip uninstall -y %s" % app.key) if apps: print( "Note: this hasn't removed other dependencies installed by " "these components. There's no substitute for a fresh virtualenv.") else: print("No Armstrong components to remove.")
def difio_register(data, useragent, excluded_names = []): """ Register to Difio @data = { 'user_id' : int, 'app_name' : '', 'app_uuid' : '', 'app_type' : '', 'app_url' : '', 'app_vendor' : int, 'pkg_type' : int, 'installed' : [{'n: 'name', 'v' : 'version' : 't' : int (optional)}], } """ if not data.has_key('pkg_type'): data['pkg_type'] = 0 # Python if not data.has_key('app_type'): data['app_type'] = 'python-%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2]) # make a list of package names for dist in get_installed_distributions(local_only=True): # skip some packages if told to if dist.project_name.lower() not in excluded_names: data['installed'].append({'n' : dist.project_name, 'v' : dist.version}) json_data = json.dumps(data) params = urllib.urlencode({'json_data' : json_data}) headers = {"User-agent": "%s" % useragent} conn = httplib.HTTPSConnection('difio-otb.rhcloud.com') conn.request('POST', '/application/register/', params, headers) response = conn.getresponse() if (response.status != 200) or (not response.getheader('Content-type').startswith('application/json')): raise BaseException('Communication failed - %s' % response.read()) result = json.loads(response.read()) print result['message'] sys.exit(result['exit_code'])
def get_upgradeable(): dependency_links = [] for dist in pkg_resources.working_set: if dist.has_metadata('dependency_links.txt'): dependency_links.extend(dist.get_metadata_lines('dependency_links.txt')) print(("\ndependency_links: %r\n" % dependency_links)) packages = [] editables = [] for dist in get_installed_distributions(local_only=True): req = pip.FrozenRequirement.from_dist(dist, dependency_links=dependency_links) if not req.editable: packages.append(req.name) else: # FIXME: How can we get this needed information easier? raw_cmd = str(req) full_url = raw_cmd.split()[1] url, full_version = full_url.rsplit("@", 1) rev = full_version.rsplit("-", 1)[1] if rev != "dev": pip_url = "%s@%s#egg=%s" % (url, rev, req.name) else: pip_url = "%s#egg=%s" % (url, req.name) editables.append(pip_url) if not packages: print((c.colorize("Found no local packages.", foreground="blue", opts=("underscore",)))) else: print((c.colorize("Found theses local packages:", foreground="green"))) for package in packages: print(("\t %s" % package)) if not editables: print((c.colorize("Found no local editables.", foreground="blue", opts=("underscore",)))) else: print((c.colorize("Found theses local editables:", foreground="green"))) for editable in editables: print(("\t %s" % editable)) return packages, editables
def remove_armstrong(): """Remove all Armstrong components (except for Dev) from this environment""" from pip.util import get_installed_distributions pkgs = get_installed_distributions(local_only=True, include_editables=True) apps = [ pkg for pkg in pkgs if pkg.key.startswith('armstrong') and pkg.key != 'armstrong.dev' ] for app in apps: run("pip uninstall -y %s" % app.key) if apps: print( "Note: this hasn't removed other dependencies installed by " "these components. There's no substitute for a fresh virtualenv.") else: print("No Armstrong components to remove.")
def main(): parser = argparse.ArgumentParser(description="Read all installed packages from sys.path and list licenses.") args = parser.parse_args() meta_files_to_check = ['PKG-INFO', 'METADATA'] for installed_distribution in get_installed_distributions(): found_license = False for metafile in meta_files_to_check: if not installed_distribution.has_metadata(metafile): continue for line in installed_distribution.get_metadata_lines(metafile): if 'License: ' in line: (k, v) = line.split(': ', 1) sys.stdout.write("{project_name}: {license}\n".format( project_name=installed_distribution.project_name, license=v)) found_license = True if not found_license: sys.stdout.write("{project_name}: Found no license information.\n".format( project_name=installed_distribution.project_name))
def run(self, options, args): local_only = options.local index_url = options.index installations = {} dependency_links = [] find_tags = False f = sys.stdout for dist in pkg_resources.working_set: if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt'), ) for dist in get_installed_distributions(local_only=local_only): req = pip.FrozenRequirement.from_dist( dist, dependency_links, find_tags=find_tags, ) installations[req.name] = req pypi_hits = self.search( [i.name for i in installations.values()], index_url, ) hits = transform_hits(pypi_hits) for hit in hits: name = hit['name'] try: if name in installations: req = installations[name].req latest = highest_version(hit['versions']) if req.specs[0][1] != latest: f.write('%s (LATEST: %s)\n' % (str(req), latest)) except UnicodeEncodeError: pass
def run(logger, include_paths, directory, module_name): if include_paths: versions = get_versions_from_installed(include_paths) versions = dict([(module, {'module': {'name': module}, 'version': version}) for module, version in versions.items()]) else: versions = {} dist_versions = get_version_from_distributions( get_installed_distributions(), logger) versions.update(dist_versions) rep_info = get_repository_info(logger, directory) if rep_info: if not module_name: module_name = get_default_module_name(directory) versions[module_name] = {'module': {'name': module_name}, 'vcs': rep_info} # Versions are returned as a dict of "module":"version" # We convert it here. Just ditch the keys. list_versions = [v for k, v in versions.items()] # Quick fix for the new modprobes structure releases = [] _vcs = None for ver in list_versions: mod = Module(ver['module']['name']) if (ver.get('vcs')): _vcs = Vcs( ver['vcs'].get('type'), ver['vcs'].get('revision'), ver['vcs'].get('repository'), ver['vcs'].get('branch')) releases.append(Release(mod, ver['version'], _vcs)) return releases
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 or zsh). """ # 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 uninstall command if subcommand_name == 'uninstall' and not current.startswith('-'): 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[subcommand_name]() options += [(opt.get_opt_string(), opt.nargs) for opt in subcommand.parser.option_list_all if opt.help != optparse.SUPPRESS_HELP] # 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]: 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) subcommands += [i.get_opt_string() for i in opts if i.help != optparse.SUPPRESS_HELP] print(' '.join([x for x in subcommands if x.startswith(current)])) sys.exit(1)
def run(self, options, args): requirement = options.requirement find_links = options.find_links or [] local_only = options.local ## FIXME: Obviously this should be settable: find_tags = False skip_match = None skip_regex = options.skip_requirements_regex if skip_regex: skip_match = re.compile(skip_regex) dependency_links = [] f = sys.stdout for dist in pkg_resources.working_set: if dist.has_metadata('dependency_links.txt'): dependency_links.extend( dist.get_metadata_lines('dependency_links.txt')) for link in find_links: if '#egg=' in link: dependency_links.append(link) for link in find_links: f.write('-f %s\n' % link) installations = {} for dist in get_installed_distributions(local_only=local_only): req = pip.FrozenRequirement.from_dist(dist, dependency_links, find_tags=find_tags) installations[req.name] = req if requirement: req_f = open(requirement) for line in req_f: if not line.strip() or line.strip().startswith('#'): f.write(line) continue if skip_match and skip_match.search(line): f.write(line) continue elif line.startswith('-e') or line.startswith('--editable'): if line.startswith('-e'): line = line[2:].strip() else: line = line[len('--editable'):].strip().lstrip('=') line_req = InstallRequirement.from_editable( line, default_vcs=options.default_vcs) elif (line.startswith('-r') or line.startswith('--requirement') or line.startswith('-Z') or line.startswith('--always-unzip') or line.startswith('-f') or line.startswith('-i') or line.startswith('--extra-index-url') or line.startswith('--find-links') or line.startswith('--index-url')): f.write(line) continue else: line_req = InstallRequirement.from_line(line) if not line_req.name: logger.notify( "Skipping line because it's not clear what it would install: %s" % line.strip()) logger.notify( " (add #egg=PackageName to the URL to avoid this warning)" ) continue if line_req.name not in installations: logger.warn( "Requirement file contains %s, but that package is not installed" % line.strip()) continue f.write(str(installations[line_req.name])) del installations[line_req.name] f.write( '## The following requirements were added by pip --freeze:\n') for installation in sorted(installations.values(), key=lambda x: x.name): f.write(str(installation))
def test_py26_excludes(self, mock_dist_is_editable, mock_dist_is_local): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local dists = get_installed_distributions() assert len(dists) == 1 assert dists[0].key == 'argparse'
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 or zsh). """ # 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 = "" load_all_commands() subcommands = [cmd for cmd, cls in command_dict.items() if not cls.hidden] options = [] # subcommand try: subcommand_name = [w for w in cwords if w in subcommands][0] except IndexError: subcommand_name = None # 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 uninstall command if subcommand_name == "uninstall" and not current.startswith("-"): 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 = command_dict.get(subcommand_name) options += [ (opt.get_opt_string(), opt.nargs) for opt in subcommand.parser.option_list if opt.help != optparse.SUPPRESS_HELP ] # 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]: opt_label += "=" print(opt_label) else: # show options of main parser only when necessary if current.startswith("-") or current.startswith("--"): subcommands += [opt.get_opt_string() for opt in parser.option_list if opt.help != optparse.SUPPRESS_HELP] print(" ".join([x for x in subcommands if x.startswith(current)])) sys.exit(1)
def test_freeze_excludes(self, mock_dist_is_editable, mock_dist_is_local): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local dists = get_installed_distributions(skip=freeze_excludes) assert len(dists) == 0
#!/usr/bin/env python """ A Python script to upgrade all install Python modules Coded by Steve Moss (gawbul [at] gmail [dot] com) http://about.me/gawbul """ # import required modules from pip.util import get_installed_distributions import xmlrpclib # setup pypi xmlrpc proxy pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi') # traverse installed distributions for dist in get_installed_distributions(local_only=True): # check for available packages available = pypi.package_releases(dist.project_name) # check by capitalising pkg name, if not found in first instance if not available: available = pypi.package_releases(dist.project_name.capitalize()) # check by converting to lowercase if not available: available = pypi.package_releases(dist.project_name.capitalize()) # if still not available then let user know no release if not available: msg = 'no releases at pypi' # if release versions are different then report as available elif available[0] >= dist.version: msg = '{} available'.format(available[0])
import psutil from .common import checks from .common import converters from .common import utils try: from pip._internal.utils import misc as pip_utils_misc discord_version = pip_utils_misc.get_installed_version("discord.py") except ImportError: try: from pip import util as pip_utils except ImportError: from pip import utils as pip_utils discord_version = discord.utils.get( pip_utils.get_installed_distributions(), project_name="discord.py").version SOURCE_URL = "https://github.com/khazhyk/dango.py/tree/master" DPY_SOURCE_URL = "https://github.com/Rapptz/discord.py/tree/rewrite/discord" class NoSuchCommand(Exception): """Command does not exist.""" def __init__(self, cmd_name): super().__init__(cmd_name) self.cmd_name = cmd_name class NoSuchSubCommand(Exception): """SubCommand does not exist."""
def run_listing(self, options): installed_packages = get_installed_distributions( local_only=options.local, skip=self.skip) self.output_package_listing(installed_packages)
def run_listing(self, options): installed_packages = get_installed_distributions(local_only=options.local, skip=self.skip) self.output_package_listing(installed_packages)
def test_editables_only(self, mock_dist_is_editable, mock_dist_is_local): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local dists = get_installed_distributions(editables_only=True) assert len(dists) == 1, dists assert dists[0].test_name == "editable"
from discord.ext.commands import group import psutil from .common import checks from .common import utils try: from pip._internal.utils import misc as pip_utils_misc discord_version = pip_utils_misc.get_installed_version("discord.py") except ImportError: try: from pip import util as pip_utils except ImportError: from pip import utils as pip_utils discord_version = discord.utils.get( pip_utils.get_installed_distributions(), project_name="discord.py").version SOURCE_URL = "https://github.com/khazhyk/dango.py/tree/master" DPY_SOURCE_URL = "https://github.com/Rapptz/discord.py/tree/rewrite/discord" class NoSuchCommand(Exception): """Command does not exist.""" def __init__(self, cmd_name): super().__init__(cmd_name) self.cmd_name = cmd_name class NoSuchSubCommand(Exception):
from pip.util import get_installed_distributions for d in get_installed_distributions(): for ln in d._get_metadata("PKG-INFO"): if ln.startswith("Home-page: "): home_page_url = ln.split(" ", 1)[1].strip() if home_page_url == "UNKNOWN": home_page_url = None if home_page_url is not None: print home_page_url
def test_include_globals(self, mock_dist_is_editable, mock_dist_is_local): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local dists = get_installed_distributions(local_only=False) assert len(dists) == 3
def run_editables(self, options): installed_packages = get_installed_distributions( local_only=options.local, editables_only=True) self.output_package_listing(installed_packages)
def run_editables(self, options): installed_packages = get_installed_distributions(local_only=options.local, editables_only=True) self.output_package_listing(installed_packages)
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 or zsh). """ # 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 uninstall command if subcommand_name == 'uninstall' and not current.startswith('-'): 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[subcommand_name]() options += [(opt.get_opt_string(), opt.nargs) for opt in subcommand.parser.option_list_all if opt.help != optparse.SUPPRESS_HELP] # 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]: 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) subcommands += [ i.get_opt_string() for i in opts if i.help != optparse.SUPPRESS_HELP ] print(' '.join([x for x in subcommands if x.startswith(current)])) sys.exit(1)
def run(self, options, args): requirement = options.requirement find_links = options.find_links or [] local_only = options.local # # FIXME: Obviously this should be settable: find_tags = False skip_match = None skip_regex = options.skip_requirements_regex if skip_regex: skip_match = re.compile(skip_regex) dependency_links = [] f = sys.stdout for dist in pkg_resources.working_set: if dist.has_metadata('dependency_links.txt'): dependency_links.extend(dist.get_metadata_lines('dependency_links.txt')) for link in find_links: if '#egg=' in link: dependency_links.append(link) for link in find_links: f.write('-f %s\n' % link) installations = {} for dist in get_installed_distributions(local_only=local_only): req = pip.FrozenRequirement.from_dist(dist, dependency_links, find_tags=find_tags) installations[req.name] = req if requirement: req_f = open(requirement) for line in req_f: if not line.strip() or line.strip().startswith('#'): f.write(line) continue if skip_match and skip_match.search(line): f.write(line) continue elif line.startswith('-e') or line.startswith('--editable'): if line.startswith('-e'): line = line[2:].strip() else: line = line[len('--editable'):].strip().lstrip('=') line_req = InstallRequirement.from_editable(line, default_vcs=options.default_vcs) elif (line.startswith('-r') or line.startswith('--requirement') or line.startswith('-Z') or line.startswith('--always-unzip') or line.startswith('-f') or line.startswith('-i') or line.startswith('--extra-index-url') or line.startswith('--find-links') or line.startswith('--index-url')): f.write(line) continue else: line_req = InstallRequirement.from_line(line) if not line_req.name: logger.notify("Skipping line because it's not clear what it would install: %s" % line.strip()) logger.notify(" (add #egg=PackageName to the URL to avoid this warning)") continue if line_req.name not in installations: logger.warn("Requirement file contains %s, but that package is not installed" % line.strip()) continue f.write(str(installations[line_req.name])) del installations[line_req.name] f.write('## The following requirements were added by pip --freeze:\n') for installation in sorted(installations.values(), key=lambda x: x.name): f.write(str(installation))
def test_exclude_editables(self, mock_dist_is_editable, mock_dist_is_local): mock_dist_is_editable.side_effect = self.dist_is_editable mock_dist_is_local.side_effect = self.dist_is_local dists = get_installed_distributions(include_editables=False) assert len(dists) == 1 assert dists[0].test_name == "normal"
def get_dist(name): from pip.util import get_installed_distributions for dist in get_installed_distributions(): if dist.key == name: return dist
def installed_packages(self): return get_installed_distributions()
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 or zsh). """ # Don't complete if user hasn't sourced bash_completion file. if not os.environ.has_key('PIP_AUTO_COMPLETE'): return cwords = os.environ['COMP_WORDS'].split()[1:] cword = int(os.environ['COMP_CWORD']) try: current = cwords[cword-1] except IndexError: current = '' load_all_commands() subcommands = [cmd for cmd, cls in command_dict.items() if not cls.hidden] options = [] # subcommand try: subcommand_name = [w for w in cwords if w in subcommands][0] except IndexError: subcommand_name = None # 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 uninstall command if subcommand_name == 'uninstall' and not current.startswith('-'): 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 = command_dict.get(subcommand_name) options += [(opt.get_opt_string(), opt.nargs) for opt in subcommand.parser.option_list if opt.help != optparse.SUPPRESS_HELP] # filter out previously specified options from available options prev_opts = [x.split('=')[0] for x in cwords[1:cword-1]] options = filter(lambda (x, v): x not in prev_opts, options) # 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]: opt_label += '=' print opt_label else: # show options of main parser only when necessary if current.startswith('-') or current.startswith('--'): subcommands += [opt.get_opt_string() for opt in parser.option_list if opt.help != optparse.SUPPRESS_HELP] print ' '.join(filter(lambda x: x.startswith(current), subcommands)) sys.exit(1)