def install_libraries(libraries):
"""
Install libraries that are not already installed.
Arguments:
libraries (iterable):
Returns:
None
"""
# Get currently installed libraries
libraries_installed = [lib.key for lib in get_installed_distributions()]
# Install libraries not found in the currently installed libraries
for lib in libraries:
if lib not in libraries_installed:
print('Installing {} ...'.format(lib))
main(['install', lib])
else:
print('{} is already installed.'.format(lib))
python类get_installed_distributions()的实例源码
def collect_environment(self):
import socket
import os
import pip
import platform
env = {}
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)
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)
def get_installed_packages(site_packages, site_packages_64):
"""
Returns a dict of installed packages that Zappa cares about.
"""
import pip # this is to avoid 'funkiness' with global import
package_to_keep = []
if os.path.isdir(site_packages):
package_to_keep += os.listdir(site_packages)
if os.path.isdir(site_packages_64):
package_to_keep += os.listdir(site_packages_64)
package_to_keep = [x.lower() for x in package_to_keep]
installed_packages = {package.project_name.lower(): package.version for package in
pip.get_installed_distributions()
if package.project_name.lower() in package_to_keep
or package.location in [site_packages, site_packages_64]}
return installed_packages
def get_package_tree(ignore_list=None, include_only=None):
"""Returns dependency package tree
:param ignore_list: list of dependencies to exclude from tree
:param include_only: list of dependencies to include if
:return: dictionary of top level packages with their dependencies
"""
ignore_list = [i.lower() for i in ignore_list] if ignore_list else []
include_only = [i.lower() for i in include_only] if include_only else []
packages = [
package for package in
pip.get_installed_distributions()
if package.key not in ignore_list
]
# if include_only is set, remove other packages
if include_only:
packages = [
package for package in packages
if package.key in include_only
]
dist_index = pipdeptree.build_dist_index(pkgs=packages)
tree = pipdeptree.construct_tree(index=dist_index)
return tree
def get_remote_installed_packages(ip_address):
'''
This method queries a remote python installation about the installed packages.
All necessary information is extracted from ~/.artemisrc
:param address: Ip address of remote server
:return:
'''
python_executable = get_artemis_config_value(section=ip_address, option="python")
function = "%s -c 'import pip; import json; print json.dumps({i.key: i.version for i in pip.get_installed_distributions() })' "%python_executable
ssh_conn = get_ssh_connection(ip_address)
stdin , stdout, stderr = ssh_conn.exec_command(function)
err = stderr.read()
if err:
msg="Quering %s python installation at %s sent a message on stderr. If you are confident that the error can be ignored, catch this RuntimeError" \
"accordingly. The error is: %s"%(ip_address, python_executable, err)
raise RuntimeError(msg)
installed_packages = json.loads(stdout.read())
ssh_conn.close()
return installed_packages
def chill(show_all=False):
if show_all:
ignored_packages = ()
else:
ignored_packages = ('pip', 'pip-chill', 'wheel', 'setuptools',
'pkg-resources')
# Gather all packages that are requirements and will be auto-installed.
dependencies = set()
for distribution in pip.get_installed_distributions():
for requirement in distribution.requires():
dependencies.add(requirement.key)
# List all packages and versions installed, excluding the auto-installed.
return [
(distribution.key, distribution.version)
for distribution in pip.get_installed_distributions()
if distribution.key not in dependencies
and distribution.key not in ignored_packages
]
def _load():
global GPIO, _DRIVER, HAL_DRIVER_ID
if not _DRIVER:
installed_packages = pip.get_installed_distributions()
flat_installed_packages = [package.project_name for package in installed_packages]
known_drivers = [
("kervi-hal-win", "kervi.platforms.windows"),
("kervi-hal-linux", "kervi.platforms.linux"),
("kervi-hal-rpi", "kervi.platforms.raspberry"),
("kervi-hal-generic", "kervi.platforms.generic")
]
for driver_name, module_name in known_drivers:
if driver_name in flat_installed_packages:
_DRIVER = importlib.import_module(module_name)
HAL_DRIVER_ID = module_name
GPIO = get_gpio()
return driver_name
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()]))
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()]))
def check_requirements(self, show_warning=False):
req_path = self.get_requirements() or self.path
req_file = 'requirements.txt'
missing = []
try:
with open(os.path.join(req_path, req_file), 'r') as f:
import pip
installed_packages = [re.sub(r'-', '_', package.project_name.lower()) for package in pip.get_installed_distributions(local_only=True)]
for line in f.read().splitlines():
pkg = re.sub(r'-', '_', re.sub(r'^([\w-]+).*$', r'\1', line).lower())
if not pkg in installed_packages:
missing.append(pkg)
if missing and install_requirements:
try:
action("Auto-installing missing Python modules...")
pquery(['pip', 'install', '-q', '-r', os.path.join(req_path, req_file)])
missing = []
except ProcessException:
warning("Unable to auto-install required Python modules.")
except (IOError, ImportError, OSError):
pass
if missing:
err = (
"-----------------------------------------------------------------\n"
"The mbed OS tools in this program require the following Python modules: %s\n"
"You can install all missing modules by running \"pip install -r %s\" in \"%s\"" % (', '.join(missing), req_file, req_path))
if os.name == 'posix':
err += "\nOn Posix systems (Linux, Mac, etc) you might have to switch to superuser account or use \"sudo\""
if show_warning:
warning(err)
else:
error(err, 1)
# Routines after cloning mbed-os
def pip():
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
def check_scm_installed(self):
dists = set([di.key for di in pip.get_installed_distributions()])
assert 'scm' in dists
def check_installed_packages():
logger.debug("Gathering napalm packages")
installed_packages = pip.get_installed_distributions()
napalm_packages = sorted(["{}=={}".format(i.key, i.version)
for i in installed_packages if i.key.startswith("napalm")])
for n in napalm_packages:
logger.debug(n)
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()]))
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()]))
def _verify_dependencies(self):
# These will always be initialized.
logging.up('Verifying dependencies')
installed_packages_list = sorted([i.key for i in pip.get_installed_distributions()])
for plugin_id, plugin_info in self.available_plugins.items():
if not plugin_info['_is_core']:
if 'dependencies' not in plugin_info:
continue
if 'os' in plugin_info['dependencies']:
if get_general_os() in plugin_info['dependencies']['os']:
plugin_info['dependencies'] = data_merge(plugin_info['dependencies'], plugin_info['dependencies']['os'][get_general_os()])
if 'plugin' in plugin_info['dependencies']:
for depend_name in plugin_info['dependencies']['plugin']:
installed = 'prism_' + depend_name in self.available_plugins
if not installed:
plugin_info['_is_satisfied'] = False
plugin_info['_dependencies'].append(('plugin', depend_name, installed))
if 'binary' in plugin_info['dependencies']:
for depend_name in plugin_info['dependencies']['binary']:
installed = is_package_installed(depend_name)
if not installed:
plugin_info['_is_satisfied'] = False
plugin_info['_dependencies'].append(('binary', depend_name, installed))
if 'module' in plugin_info['dependencies']:
for depend_name in plugin_info['dependencies']['module']:
installed = (depend_name in installed_packages_list)
if not installed:
plugin_info['_is_satisfied'] = False
plugin_info['_dependencies'].append(('module', depend_name, installed))
if not plugin_info['_is_satisfied']:
# Create a dummy plugin container
self._insert_dummy_plugin(plugin_info)
logging.error('Dependency unsatisfied. Offender: %s' % plugin_id)
logging.down()
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()]))
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()]))
def _echo_installed_packages():
import pip
# This code was derived from an example provide here:
# http://stackoverflow.com/a/23885252/3424666
installed_packages = sorted(["%s==%s" % (i.key, i.version)
for i in pip.get_installed_distributions()])
for e in installed_packages:
click.echo(e)
def _requirements(self, package_name):
def _get_package(_package_name):
candidates = [p for p in pip.get_installed_distributions() if p.project_name == _package_name]
if not candidates:
raise ValueError('No package "{}"'.format(package_name))
return candidates[0]
package = _get_package(package_name)
result = set(name for name in package._get_metadata("top_level.txt") if '/' not in name)
for requirement in package.requires():
result |= self._requirements(requirement.project_name)
return result
nvme_cli_selftests.py 文件源码
项目:avocado-misc-tests
作者: avocado-framework-tests
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def setUp(self):
"""
Download 'nvme-cli'.
"""
self.device = self.params.get('device', default='/dev/nvme0')
self.disk = self.params.get('disk', default='/dev/nvme0n1')
cmd = 'ls %s' % self.device
if process.system(cmd, ignore_status=True) is not 0:
self.skip("%s does not exist" % self.device)
smm = SoftwareManager()
if not smm.check_installed("nvme-cli") and not \
smm.install("nvme-cli"):
self.skip('nvme-cli is needed for the test to be run')
python_packages = pip.get_installed_distributions()
python_packages_list = [i.key for i in python_packages]
python_pkgs = ['nose', 'nose2', 'pep8', 'flake8', 'pylint', 'epydoc']
for py_pkg in python_pkgs:
if py_pkg not in python_packages_list:
self.skip("python package %s not installed" % py_pkg)
url = 'https://codeload.github.com/linux-nvme/nvme-cli/zip/master'
tarball = self.fetch_asset("nvme-cli-master.zip", locations=[url],
expire='7d')
archive.extract(tarball, self.teststmpdir)
self.nvme_dir = os.path.join(self.teststmpdir, "nvme-cli-master")
print os.listdir(self.nvme_dir)
os.chdir(os.path.join(self.nvme_dir, 'tests'))
msg = ['{']
msg.append(' \"controller\": \"%s\",' % self.device)
msg.append(' \"ns1\": \"%s\",' % self.disk)
msg.append(' \"log_dir\": \"%s\"' % self.outputdir)
msg.append('}')
with open('config.json', 'w') as config_file:
config_file.write("\n".join(msg))
process.system("cat config.json")
def get_modules(**kwargs):
modules = OrderedDict()
for i in sorted(pip.get_installed_distributions(local_only=True),
key=lambda i: i.project_name.lower()):
modules[i.project_name.lower()] = i.version
return modules
def get_function_source(func):
"""
Determine the source file of a function
Parameters
----------
func : function
Returns
-------
str
the module name
list of str
a list of filenames necessary to be copied
"""
installed_packages = pip.get_installed_distributions()
inpip = func.__module__.split('.')[0] in [p.key for p in installed_packages]
insubdir = os.path.realpath(
func.__code__.co_filename).startswith(os.path.realpath(os.getcwd()))
is_local = not inpip and insubdir
if not is_local:
return func.__module__, []
else:
return func.__module__.split('.')[-1], \
[os.path.realpath(func.__code__.co_filename)]
def check_dependency(self):
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:
# Why this package is not in get_installed_distributions ?
if str(req_dep) == "argparse":
pass
else:
missing_deps.append(req_dep)
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()
def installed_python_packages():
"""
This function ...
:return:
"""
# Initialize dictionary to contain the package names and version numbers
packages = dict()
# Get all python distributions
distributions = pip.get_installed_distributions()
# Loop over the distributions
for distribution in distributions:
# Get name and version
top_level_meta_data = list(distribution._get_metadata('top_level.txt'))
import_name = top_level_meta_data[0] if len(top_level_meta_data) > 0 else distribution.project_name
version = str(distribution.parsed_version)
# possible other interesting properties of an entry in the distributions list:
# .egg_name()
# .as_requirement()
# .parsed_version
# .has_version()
# .project_name
# .py_version
# .requires()
# Add entry to the dictionary
packages[import_name] = version
# Return the dictionary
return packages
# -----------------------------------------------------------------
def installed_python_packages():
"""
This function ...
:return:
"""
# Initialize dictionary to contain the package names and version numbers
packages = dict()
# Get all python distributions
distributions = pip.get_installed_distributions()
# Loop over the distributions
for distribution in distributions:
# Get name and version
top_level_meta_data = list(distribution._get_metadata('top_level.txt'))
import_name = top_level_meta_data[0] if len(top_level_meta_data) > 0 else distribution.project_name
version = str(distribution.parsed_version)
# possible other interesting properties of an entry in the distributions list:
# .egg_name()
# .as_requirement()
# .parsed_version
# .has_version()
# .project_name
# .py_version
# .requires()
# Add entry to the dictionary
packages[import_name] = version
# Return the dictionary
return packages
# -----------------------------------------------------------------
def is_gpu():
packages = [str(i) for i in pip.get_installed_distributions()]
for item in packages:
if "tensorflow-gpu" in item:
return True
return False
def get_deps_list(self, pkg_name, installed_distros=None):
"""
For a given package, returns a list of required packages. Recursive.
"""
import pip
deps = []
if not installed_distros:
installed_distros = pip.get_installed_distributions()
for package in installed_distros:
if package.project_name.lower() == pkg_name.lower():
deps = [(package.project_name, package.version)]
for req in package.requires():
deps += self.get_deps_list(pkg_name=req.project_name, installed_distros=installed_distros)
return list(set(deps)) # de-dupe before returning
def check(key, db, json, full_report, bare, stdin, files, cache, ignore):
if files and stdin:
click.secho("Can't read from --stdin and --file at the same time, exiting", fg="red")
sys.exit(-1)
if files:
packages = list(itertools.chain.from_iterable(read_requirements(f, resolve=True) for f in files))
elif stdin:
packages = list(read_requirements(sys.stdin))
else:
packages = pip.get_installed_distributions()
try:
vulns = safety.check(packages=packages, key=key, db_mirror=db, cached=cache, ignore_ids=ignore)
click.secho(report(
vulns=vulns,
full=full_report,
json_report=json,
bare_report=bare,
checked_packages=len(packages),
db=db,
key=key
)
)
sys.exit(-1 if vulns else 0)
except InvalidKeyError:
click.secho("Your API Key '{key}' is invalid. See {link}".format(
key=key, link='https://goo.gl/O7Y1rS'),
fg="red")
sys.exit(-1)
except DatabaseFileNotFoundError:
click.secho("Unable to load vulnerability database from {db}".format(db=db), fg="red")
sys.exit(-1)
except DatabaseFetchError:
click.secho("Unable to load vulnerability database", fg="red")
sys.exit(-1)
def is_butterfly_installed():
installed_packages = pip.get_installed_distributions()
flat_installed_packages = [package.project_name for package in installed_packages]
return 'butterfly' in flat_installed_packages
# create a machine object and write a new entry(status Unknown) to MongoDB