Python源码示例:pip.get_installed_distributions()
示例1
def collect_loaded_packages() -> List[Tuple[str, str]]:
"""
Return the currently loaded package names and their versions.
"""
dists = get_installed_distributions()
get_dist_files = DistFilesFinder()
file_table = {}
for dist in dists:
for file in get_dist_files(dist):
file_table[file] = dist
used_dists = set()
# we greedily load all values to a list to avoid weird
# "dictionary changed size during iteration" errors
for module in list(sys.modules.values()):
try:
dist = file_table[module.__file__]
except (AttributeError, KeyError):
continue
used_dists.add(dist)
return sorted((dist.project_name, dist.version) for dist in used_dists)
示例2
def collect_environment(self, overwrite_variables=None):
import socket
import os
import pip
import platform
env = {}
if not overwrite_variables:
overwrite_variables = {}
import aetros
env['aetros_version'] = aetros.__version__
env['python_version'] = platform.python_version()
env['python_executable'] = sys.executable
env['hostname'] = socket.gethostname()
env['variables'] = dict(os.environ)
env['variables'].update(overwrite_variables)
if 'AETROS_SSH_KEY' in env['variables']: del env['variables']['AETROS_SSH_KEY']
if 'AETROS_SSH_KEY_BASE64' in env['variables']: del env['variables']['AETROS_SSH_KEY_BASE64']
env['pip_packages'] = sorted([[i.key, i.version] for i in pip.get_installed_distributions()])
self.set_system_info('environment', env)
示例3
def _save_pip_list(self, tar):
'''Dump a pip list containing packages and versions.
:param dest: unicode -- directory of dumped pip list
:param tar: tarfile object -- tar where pip list dump will be added
'''
pkgs = pip.get_installed_distributions()
sorted_pkgs = sorted(pkgs, key=lambda pkg: pkg._key)
sorted_pkg_names = [str(pkg) for pkg in sorted_pkgs]
pip_list_file = os.path.join(self.tar_dest, 'pip_list.txt')
with open(pip_list_file, 'w') as pip_file:
pip_file.write('\n'.join(sorted_pkg_names))
self._add_file_to_tar(self.tar_dest, 'pip_list.txt', tar)
os.remove(pip_list_file)
return sorted_pkgs
示例4
def _save_pip_list(self, tar):
'''Dump a pip list, containing packages and versions.
:param dest: unicode -- directory of dumped pip list
:param tar: tarfile object -- tar where pip list dump will be added
'''
pkgs = pip.get_installed_distributions()
sorted_pkgs = sorted(pkgs, key=lambda pkg: pkg._key)
sorted_pkg_names = [str(pkg) for pkg in sorted_pkgs]
pip_list_file = os.path.join(self.tar_dest, 'pip_list.txt')
with open(pip_list_file, 'w') as pip_file:
pip_file.write('\n'.join(sorted_pkg_names))
self._add_file_to_tar(self.tar_dest, 'pip_list.txt', tar)
os.remove(pip_list_file)
return sorted_pkgs
示例5
def check_dependency():
list_deps = []
missing_deps = []
with open('requirements.txt') as f:
list_deps = f.read().splitlines()
pip_list = sorted([(i.key) for i in pip.get_installed_distributions()])
for req_dep in list_deps:
if req_dep not in pip_list:
missing_deps.append(req_dep)
if missing_deps:
print "You are missing a module for Datasploit. Please install them using: "
print "pip install -r requirements.txt"
sys.exit()
示例6
def can_use_gpu():
# Check that 'tensorflow-gpu' is installed on the current code-env
import pip
installed_packages = pip.get_installed_distributions()
return "tensorflow-gpu" in [p.project_name for p in installed_packages]
###################################################################################################################
## FILES LOGIC
###################################################################################################################
示例7
def can_use_gpu():
# Check that 'tensorflow-gpu' is installed on the current code-env
import pip
installed_packages = pip.get_installed_distributions()
return "tensorflow-gpu" in [p.project_name for p in installed_packages]
###################################################################################################################
## FILES LOGIC
###################################################################################################################
示例8
def _generate_software_info(self):
os_version = self._prepare_for_json(platform.platform())
installed_packages = [self._prepare_for_json(pack) for pack in pip.get_installed_distributions()]
output = self._run_external_cmd("dpkg-query --list | grep indy")
indy_packages = output.split(os.linesep)
return {
"Software": {
"OS_version": os_version,
"Installed_packages": installed_packages,
# TODO add this field
"Indy_packages": self._prepare_for_json(indy_packages),
}
}
示例9
def get_uninstalled_project_names():
try:
import pip
pkg_set = set(KNOWN_PKG_LIST)
pathmeta_list = pip.get_installed_distributions()
installed_set = set([meta.project_name for meta in pathmeta_list])
uninstalled_set = pkg_set.difference(installed_set)
uninstalled_list = list(uninstalled_set)
except Exception as ex:
print(ex)
uninstalled_list = KNOWN_PKG_LIST
return uninstalled_list
示例10
def get_pip_installed():
try:
import pip
pypkg_list = [item.key for item in pip.get_installed_distributions()]
return pypkg_list
except ImportError:
#out, err, ret = shell('pip list')
#if ret == 0:
# pypkg_list = [_.split(' ')[0] for _ in out.split('\n')]
return []
示例11
def show_version(scoring_version):
''' Python version and library versions '''
swrite('\n=== VERSIONS ===\n\n')
# Scoring program version
swrite("Scoring program version: " + str(scoring_version) + "\n\n")
# Python version
swrite("Python version: " + version + "\n\n")
# Give information on the version installed
swrite("Versions of libraries installed:\n")
map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()]))
示例12
def show_version():
# Python version and library versions
swrite('\n=== VERSIONS ===\n\n')
# Python version
swrite("Python version: " + version + "\n\n")
# Give information on the version installed
swrite("Versions of libraries installed:\n")
map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()]))
示例13
def _safe_dist():
for p in pip.get_installed_distributions():
try:
yield p.key, p.version
except Exception: # pragma nocover
pass
示例14
def log_running_python_versions():
logging.info("Python version: " + str(sys.version) + ", " + str(sys.version_info)) # () required in Python 3.
installed_packages = pip.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))
示例15
def check_deps():
# Fail if the 'co3' module is installed, this supersedes it
packages = get_installed_distributions(local_only=True)
# For each EggInfoDistribution, find its metadata
for pkg in packages:
try:
distro = get_distribution(pkg.project_name)
if distro.project_name == 'co3':
print("This package replaces the 'co3' distribution. Please 'pip uninstall co3' first.")
sys.exit(1)
except DistributionNotFound:
pass
示例16
def show_version(scoring_version):
''' Python version and library versions '''
swrite('\n=== VERSIONS ===\n\n')
# Scoring program version
swrite("Scoring program version: " + str(scoring_version) + "\n\n")
# Python version
swrite("Python version: " + version + "\n\n")
# Give information on the version installed
swrite("Versions of libraries installed:\n")
map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()]))
示例17
def show_version():
# Python version and library versions
swrite('\n=== VERSIONS ===\n\n')
# Python version
swrite("Python version: " + version + "\n\n")
# Give information on the version installed
swrite("Versions of libraries installed:\n")
map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()]))
示例18
def show_version(scoring_version):
''' Python version and library versions '''
swrite('\n=== VERSIONS ===\n\n')
# Scoring program version
swrite("Scoring program version: " + str(scoring_version) + "\n\n")
# Python version
swrite("Python version: " + version + "\n\n")
# Give information on the version installed
swrite("Versions of libraries installed:\n")
map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()]))
示例19
def show_version():
# Python version and library versions
swrite('\n=== VERSIONS ===\n\n')
# Python version
swrite("Python version: " + version + "\n\n")
# Give information on the version installed
swrite("Versions of libraries installed:\n")
map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()]))
示例20
def check_depdendencies():
"""
Makes sure that all required dependencies are installed in the exact version
(as specified in requirements.txt)
"""
# Get installed packages
installed_packages = pip.get_installed_distributions(local_only=False)
installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])
# Now check if each package specified in requirements.txt is actually installed
deps_filename = os.path.join(ROOT_INSTALL_PATH, "requirements.txt")
with open(deps_filename, "r") as f:
for dep in f.read().split():
if not dep.lower() in installed_packages_list:
raise SystemCheckError("Required dependency %s is not installed. Please run 'pip install -e .'" % dep)
示例21
def main():
"""Use pip to find pip installed packages in a given prefix."""
pip_packages = {}
for package in pip.get_installed_distributions():
name = package.project_name
version = package.version
full_name = "{0}-{1}-pip".format(name.lower(), version)
pip_packages[full_name] = {'version': version}
data = json.dumps(pip_packages)
print(data)
示例22
def Check(self):
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
module = __import__(self.module)
except ImportError:
raise MissingDependency
if self.attr and hasattr(module, self.attr):
version = getattr(module, self.attr)
elif hasattr(module, "__version__"):
version = module.__version__
elif hasattr(module, "VERSION"):
version = module.VERSION
elif hasattr(module, "version"):
version = module.version
else:
result = [
p.version
for p in pip.get_installed_distributions()
if str(p).startswith(self.name + " ")
]
if result:
version = result[0]
else:
raise Exception("Can't get version information")
if not isinstance(version, basestring):
version = ".".join(str(x) for x in version)
if CompareVersion(version, self.version) < 0:
raise WrongVersion
示例23
def system():
pywebdriver_info = []
pywebdriver_info.append({
'name': _('CORS allowed origins'),
'value': config.get('flask','cors_origins')
})
system_info = []
system_info.append({
'name': _('OS - System'), 'value': platform.system()})
system_info.append({
'name': _('OS - Distribution'),
'value': platform.linux_distribution()})
system_info.append({
'name': _('OS - Release'), 'value': platform.release()})
system_info.append({
'name': _('OS - Version'), 'value': platform.version()})
system_info.append({
'name': _('Machine'), 'value': platform.machine()})
system_info.append({
'name': _('Python Version'), 'value': platform.python_version()})
installed_python_packages = get_installed_distributions()
installed_python_packages = sorted(
installed_python_packages, key=lambda package: package.key)
return render_template(
'system.html',
pywebdriver_info=pywebdriver_info,
system_info=system_info,
installed_python_packages=installed_python_packages
)
示例24
def step_cli_installed(context):
"""
Make sure wharfee is in installed packages.
"""
dists = set([di.key for di in pip.get_installed_distributions()])
assert 'wharfee' in dists
示例25
def is_latest(package_name):
pypi = xmlrpc_client.ServerProxy('https://pypi.python.org/pypi')
for dist in pip.get_installed_distributions():
if dist.project_name == package_name:
available = pypi.package_releases(dist.project_name)
if available[0] != dist.version:
return False
return True
示例26
def deps(ctx):
repos = get_repos_from_requirements(ctx.obj['base_path'])
required_versions = get_tag_from_repo_url(repos)
latest_versions = get_latest_tags_from_repos(repos)
installed_pkgs = pip.get_installed_distributions()
click.echo('')
for repo in repos:
status = 'outdated'
required = required_versions[repo]
latest = latest_versions[repo]
try:
repo_small = repo.split('@')[1]
pkg_name = repo.split('egg=')[1]
except IndexError:
continue
pkg_name_normalized = pkg_name.lower().replace('_', '-')
installed = 'unknown'
installed_list = [
pkg.version
for pkg in installed_pkgs
if pkg.key in [pkg_name_normalized, pkg_name_normalized + '-lib']
]
if installed_list:
installed = 'v{}'.format(installed_list[0])
if latest is None or installed is None:
continue
if LooseVersion(installed) > LooseVersion(latest):
status = 'develop'
elif LooseVersion(installed) < LooseVersion(required):
status = 'up-to-date (old version installed)'
elif required == latest:
status = 'up-to-date'
msg = '{pkg_name}: {status} (required: {required}, installed: {installed}, latest: {latest})'.format(
repo=repo_small, pkg_name=pkg_name_normalized, status=status, required=required, installed=installed, latest=latest)
if status == 'up-to-date' or (status == 'develop' and installed == required):
color = 'green'
elif status in ('develop', 'up-to-date (old version installed)') or installed == latest:
color = 'yellow'
else:
color = 'red'
click.echo(click.style(msg, fg=color))
示例27
def update(ctx, install, install_all):
base_path = ctx.obj['base_path']
repos = get_repos_from_requirements(base_path)
required_versions = get_tag_from_repo_url(repos)
latest_versions = get_latest_tags_from_repos(repos)
installed_pkgs = pip.get_installed_distributions()
install_list = ['-e .']
click.echo('')
for repo in repos:
latest = latest_versions[repo]
required = required_versions[repo]
try:
pkg_name = repo.split('egg=')[1]
except IndexError:
continue
pkg_name_normalized = pkg_name.lower().replace('_', '-')
installed = 'unknown'
installed_list = [
pkg.version
for pkg in installed_pkgs
if pkg.key in [pkg_name_normalized, pkg_name_normalized + '-lib']
]
if installed_list:
installed = 'v{}'.format(installed_list[0])
if LooseVersion(required) < LooseVersion(latest):
click.echo('Updating {} from {} to {}...'.format(pkg_name, required, latest))
new_repo = update_repo_tag(repo, latest, path=base_path)
if LooseVersion(installed) < LooseVersion(latest):
install_list.append(new_repo)
elif LooseVersion(installed) < LooseVersion(required):
install_list.append(repo)
if install_all:
install = True
install_list = ['-r requirements.txt']
if install:
for new_repo in install_list:
new_repo = new_repo.strip()
click.echo('')
click.echo('Running `pip install -U {}` ...'.format(new_repo))
command = ['pip', 'install', '-U'] + new_repo.split(' ')
exitcode = subprocess.call(command, cwd=base_path)
if exitcode == 0:
click.echo('Done.')
else:
click.echo('Failed.')
sys.exit(exitcode)
示例28
def check_dependency(self):
list_deps = []
missing_deps = []
with open('requirements.txt') as f:
list_deps = f.read().splitlines()
dists = [d for d in pkg_resources.working_set]
pip_list = sorted([(i.key) for i in dists])
#pip_list = sorted([(i.key) for i in pip.get_installed_distributions()])
for req_dep in list_deps:
compare_char = ["==", ">=", "<=", ">", "<", "!="]
for c in compare_char:
if c in req_dep:
pkg = req_dep.split(c)
if pkg[0] not in pip_list:
missing_deps.append(req_dep)
break
else:
installed_ver = pkg_resources.get_distribution(pkg[0]).version
if self.get_truth(installed_ver, c, pkg[1]):
break
else:
missing_deps.append(req_dep)
else:
if req_dep not in pip_list:
# Why this package is not in get_installed_distributions ?
if str(req_dep) == "argparse":
pass
else:
missing_deps.append(req_dep)
missing_deps = set(missing_deps)
if missing_deps:
missing_deps_warning ="""
You are missing a module required for Belati. In order to continue using Belati, please install them with:
{}`pip install --upgrade --force-reinstall -r requirements.txt`{}
or manually install missing modules with:
{}`pip install --upgrade --force-reinstall {}`{}
"""
log.console_log(missing_deps_warning.format(Y, W, Y, ' '.join(missing_deps), W))
sys.exit()
示例29
def main():
args = _get_args()
pkgs = get_installed_distributions(local_only=args.local_only,
user_only=args.user_only)
tree = PackageDAG.from_pkgs(pkgs)
is_text_output = not any([args.json, args.json_tree, args.output_format])
return_code = 0
# Before any reversing or filtering, show warnings to console
# about possibly conflicting or cyclic deps if found and warnings
# are enabled (ie. only if output is to be printed to console)
if is_text_output and args.warn != 'silence':
conflicts = conflicting_deps(tree)
if conflicts:
render_conflicts_text(conflicts)
print('-'*72, file=sys.stderr)
cycles = cyclic_deps(tree)
if cycles:
render_cycles_text(cycles)
print('-'*72, file=sys.stderr)
if args.warn == 'fail' and (conflicts or cycles):
return_code = 1
# Reverse the tree (if applicable) before filtering, thus ensuring
# that the filter will be applied on ReverseTree
if args.reverse:
tree = tree.reverse()
show_only = set(args.packages.split(',')) if args.packages else None
exclude = set(args.exclude.split(',')) if args.exclude else None
if show_only is not None or exclude is not None:
tree = tree.filter(show_only, exclude)
if args.json:
print(render_json(tree, indent=4))
elif args.json_tree:
print(render_json_tree(tree, indent=4))
elif args.output_format:
output = dump_graphviz(tree,
output_format=args.output_format,
is_reverse=args.reverse)
print_graphviz(output)
else:
render_text(tree, args.all, args.freeze)
return return_code
示例30
def getpackages(self):
"""
returns a list with the installed python packages and its versions
"""
# check if pypi service is reachable
if self.pypi_timeout <= 0:
pypi_available = False
# pypi_unavailable_message = translate('PyPI Prüfung deaktiviert')
pypi_unavailable_message = 'PyPI Prüfung deaktiviert'
else:
pypi_available = True
try:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self.pypi_timeout)
# sock.connect(('pypi.python.org', 443))
sock.connect(('pypi.org', 443))
sock.close()
except:
pypi_available = False
# pypi_unavailable_message = translate('PyPI nicht erreichbar')
pypi_unavailable_message = 'PyPI nicht erreichbar'
import pip
import xmlrpc
installed_packages = pip.get_installed_distributions()
# pypi = xmlrpc.client.ServerProxy('https://pypi.python.org/pypi')
pypi = xmlrpc.client.ServerProxy('https://pypi.org/pypi')
packages = []
for dist in installed_packages:
package = {}
package['key'] = dist.key
package['version_installed'] = dist.version
if pypi_available:
try:
available = pypi.package_releases(dist.project_name)
try:
package['version_available'] = available[0]
except:
package['version_available'] = '-'
except:
# package['version_available'] = [translate('Keine Antwort von PyPI')]
package['version_available'] = ['Keine Antwort von PyPI']
else:
package['version_available'] = pypi_unavailable_message
packages.append(package)
sorted_packages = sorted([(i['key'], i['version_installed'], i['version_available']) for i in packages])
return sorted_packages