How to use installed_pkgs method in avocado

Best Python code snippet using avocado_python Github


Full Screen

1# -*- coding: utf-8 -*-2'''3Installation of NPM Packages4============================5These states manage the installed packages for node.js using the Node Package6Manager (npm). Note that npm must be installed for these states to be7available, so npm states should include a requisite to a pkg.installed state8for the package which provides npm (simply ``npm`` in most cases). Example:9.. code-block:: yaml10 npm:11 pkg.installed12 yaml:13 npm.installed:14 - require:15 - pkg: npm16'''17from __future__ import absolute_import18# Import salt libs19from salt.exceptions import CommandExecutionError, CommandNotFoundError20def __virtual__():21 '''22 Only load if the npm module is available in __salt__23 '''24 return 'npm' if 'npm.list' in __salt__ else False, '\'npm\' binary not found on system'25def installed(name,26 pkgs=None,27 dir=None,28 user=None,29 force_reinstall=False,30 registry=None,31 env=None):32 '''33 Verify that the given package is installed and is at the correct version34 (if specified).35 .. code-block:: yaml36 coffee-script:37 npm.installed:38 - user: someuser39 coffee-script@1.0.1:40 npm.installed: []41 name42 The package to install43 .. versionchanged:: 2014.7.244 This parameter is no longer lowercased by salt so that45 case-sensitive NPM package names will work.46 pkgs47 A list of packages to install with a single npm invocation; specifying48 this argument will ignore the ``name`` argument49 .. versionadded:: 2014.7.050 dir51 The target directory in which to install the package, or None for52 global installation53 user54 The user to run NPM with55 .. versionadded:: 0.17.056 registry57 The NPM registry from which to install the package58 .. versionadded:: 2014.7.059 env60 A list of environment variables to be set prior to execution. The61 format is the same as the :py:func:` <>`.62 state function.63 .. versionadded:: 2014.7.064 force_reinstall65 Install the package even if it is already installed66 '''67 ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}68 if pkgs is not None:69 pkg_list = pkgs70 else:71 pkg_list = [name]72 try:73 installed_pkgs = __salt__['npm.list'](dir=dir, runas=user, env=env)74 except (CommandNotFoundError, CommandExecutionError) as err:75 ret['result'] = False76 ret['comment'] = 'Error looking up {0!r}: {1}'.format(name, err)77 return ret78 else:79 installed_pkgs = dict((p, info)80 for p, info in installed_pkgs.items())81 pkgs_satisfied = []82 pkgs_to_install = []83 def _pkg_is_installed(pkg, installed_pkgs):84 '''85 Helper function to determine if a package is installed86 This performs more complex comparison than just checking87 keys, such as examining source repos to see if the package88 was installed by a different name from the same repo89 :pkg str: The package to compare90 :installed_pkgs: A dictionary produced by npm list --json91 '''92 if (pkg_name in installed_pkgs and93 'version' in installed_pkgs[pkg_name]):94 return True95 # Check to see if we are trying to install from a URI96 elif '://' in pkg_name: # TODO Better way?97 for pkg_details in installed_pkgs.values():98 pkg_from = pkg_details.get('from', '').split('://')[1]99 if pkg_name.split('://')[1] == pkg_from:100 return True101 return False102 for pkg in pkg_list:103 pkg_name, _, pkg_ver = pkg.partition('@')104 pkg_name = pkg_name.strip()105 if force_reinstall is True:106 pkgs_to_install.append(pkg)107 continue108 if not _pkg_is_installed(pkg, installed_pkgs):109 pkgs_to_install.append(pkg)110 continue111 installed_name_ver = '{0}@{1}'.format(pkg_name,112 installed_pkgs[pkg_name]['version'])113 # If given an explicit version check the installed version matches.114 if pkg_ver:115 if installed_pkgs[pkg_name].get('version') != pkg_ver:116 pkgs_to_install.append(pkg)117 else:118 pkgs_satisfied.append(installed_name_ver)119 continue120 else:121 pkgs_satisfied.append(installed_name_ver)122 continue123 if __opts__['test']:124 ret['result'] = None125 comment_msg = []126 if pkgs_to_install:127 comment_msg.append('NPM package(s) {0!r} are set to be installed'128 .format(', '.join(pkgs_to_install)))129 ret['changes'] = {'old': [], 'new': pkgs_to_install}130 if pkgs_satisfied:131 comment_msg.append('Package(s) {0!r} satisfied by {1}'132 .format(', '.join(pkg_list), ', '.join(pkgs_satisfied)))133 ret['result'] = True134 ret['comment'] = '. '.join(comment_msg)135 return ret136 if not pkgs_to_install:137 ret['result'] = True138 ret['comment'] = ('Package(s) {0!r} satisfied by {1}'139 .format(', '.join(pkg_list), ', '.join(pkgs_satisfied)))140 return ret141 try:142 cmd_args = {143 'dir': dir,144 'runas': user,145 'registry': registry,146 'env': env,147 }148 if pkgs is not None:149 cmd_args['pkgs'] = pkgs150 else:151 cmd_args['pkg'] = pkg_name152 call = __salt__['npm.install'](**cmd_args)153 except (CommandNotFoundError, CommandExecutionError) as err:154 ret['result'] = False155 ret['comment'] = 'Error installing {0!r}: {1}'.format(156 ', '.join(pkg_list), err)157 return ret158 if call and (isinstance(call, list) or isinstance(call, dict)):159 ret['result'] = True160 ret['changes'] = {'old': [], 'new': pkgs_to_install}161 ret['comment'] = 'Package(s) {0!r} successfully installed'.format(162 ', '.join(pkgs_to_install))163 else:164 ret['result'] = False165 ret['comment'] = 'Could not install package(s) {0!r}'.format(166 ', '.join(pkg_list))167 return ret168def removed(name,169 dir=None,170 user=None):171 '''172 Verify that the given package is not installed.173 dir174 The target directory in which to install the package, or None for175 global installation176 user177 The user to run NPM with178 .. versionadded:: 0.17.0179 '''180 ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}181 try:182 installed_pkgs = __salt__['npm.list'](dir=dir)183 except (CommandExecutionError, CommandNotFoundError) as err:184 ret['result'] = False185 ret['comment'] = 'Error uninstalling {0!r}: {1}'.format(name, err)186 return ret187 if name not in installed_pkgs:188 ret['result'] = True189 ret['comment'] = 'Package {0!r} is not installed'.format(name)190 return ret191 if __opts__['test']:192 ret['result'] = None193 ret['comment'] = 'Package {0!r} is set to be removed'.format(name)194 return ret195 if __salt__['npm.uninstall'](pkg=name, dir=dir, runas=user):196 ret['result'] = True197 ret['changes'][name] = 'Removed'198 ret['comment'] = 'Package {0!r} was successfully removed'.format(name)199 else:200 ret['result'] = False201 ret['comment'] = 'Error removing package {0!r}'.format(name)202 return ret203def bootstrap(name,204 user=None):205 '''206 Bootstraps a node.js application.207 Will execute 'npm install --json' on the specified directory.208 user209 The user to run NPM with210 .. versionadded:: 0.17.0211 '''212 ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}213 try:214 call = __salt__['npm.install'](dir=name, runas=user, pkg=None)215 except (CommandNotFoundError, CommandExecutionError) as err:216 ret['result'] = False217 ret['comment'] = 'Error Bootstrapping {0!r}: {1}'.format(name, err)218 return ret219 if not call:220 ret['result'] = True221 ret['comment'] = 'Directory is already bootstrapped'222 return ret223 # npm.install will return a string if it can't parse a JSON result224 if isinstance(call, str):225 ret['result'] = False226 ret['comment'] = 'Could not bootstrap directory'227 else:228 ret['result'] = True229 ret['changes'] = {name: 'Bootstrapped'}230 ret['comment'] = 'Directory was successfully bootstrapped'...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?