Best Python code snippet using slash
composer.py
Source:composer.py
1# -*- coding: utf-8 -*-2'''3Use composer to install PHP dependencies for a directory4'''5from __future__ import absolute_import, print_function, unicode_literals6# Import python libs7import logging8import os.path9# Import salt libs10import salt.utils.args11import salt.utils.path12from salt.exceptions import (13 CommandExecutionError,14 CommandNotFoundError,15 SaltInvocationError16)17log = logging.getLogger(__name__)18# Function alias to make sure not to shadow built-in's19__func_alias__ = {20 'list_': 'list'21}22def __virtual__():23 '''24 Always load25 '''26 return True27def _valid_composer(composer):28 '''29 Validate the composer file is indeed there.30 '''31 if salt.utils.path.which(composer):32 return True33 return False34def did_composer_install(dir):35 '''36 Test to see if the vendor directory exists in this directory37 dir38 Directory location of the composer.json file39 CLI Example:40 .. code-block:: bash41 salt '*' composer.did_composer_install /var/www/application42 '''43 lockFile = "{0}/vendor".format(dir)44 if os.path.exists(lockFile):45 return True46 return False47def _run_composer(action,48 directory=None,49 composer=None,50 php=None,51 runas=None,52 prefer_source=None,53 prefer_dist=None,54 no_scripts=None,55 no_plugins=None,56 optimize=None,57 no_dev=None,58 quiet=False,59 composer_home='/root',60 extra_flags=None,61 env=None):62 '''63 Run PHP's composer with a specific action.64 If composer has not been installed globally making it available in the65 system PATH & making it executable, the ``composer`` and ``php`` parameters66 will need to be set to the location of the executables.67 action68 The action to pass to composer ('install', 'update', 'selfupdate', etc).69 directory70 Directory location of the composer.json file. Required except when71 action='selfupdate'72 composer73 Location of the composer.phar file. If not set composer will74 just execute "composer" as if it is installed globally.75 (i.e. /path/to/composer.phar)76 php77 Location of the php executable to use with composer.78 (i.e. /usr/bin/php)79 runas80 Which system user to run composer as.81 prefer_source82 --prefer-source option of composer.83 prefer_dist84 --prefer-dist option of composer.85 no_scripts86 --no-scripts option of composer.87 no_plugins88 --no-plugins option of composer.89 optimize90 --optimize-autoloader option of composer. Recommended for production.91 no_dev92 --no-dev option for composer. Recommended for production.93 quiet94 --quiet option for composer. Whether or not to return output from composer.95 composer_home96 $COMPOSER_HOME environment variable97 extra_flags98 None, or a string containing extra flags to pass to composer.99 env100 A list of environment variables to be set prior to execution.101 '''102 if composer is not None:103 if php is None:104 php = 'php'105 else:106 composer = 'composer'107 # Validate Composer is there108 if not _valid_composer(composer):109 raise CommandNotFoundError(110 '\'composer.{0}\' is not available. Couldn\'t find \'{1}\'.'111 .format(action, composer)112 )113 if action is None:114 raise SaltInvocationError('The \'action\' argument is required')115 # Don't need a dir for the 'selfupdate' action; all other actions do need a dir116 if directory is None and action != 'selfupdate':117 raise SaltInvocationError(118 'The \'directory\' argument is required for composer.{0}'.format(action)119 )120 # Base Settings121 cmd = [composer, action, '--no-interaction', '--no-ansi']122 if extra_flags is not None:123 cmd.extend(salt.utils.args.shlex_split(extra_flags))124 # If php is set, prepend it125 if php is not None:126 cmd = [php] + cmd127 # Add Working Dir128 if directory is not None:129 cmd.extend(['--working-dir', directory])130 # Other Settings131 if quiet is True:132 cmd.append('--quiet')133 if no_dev is True:134 cmd.append('--no-dev')135 if prefer_source is True:136 cmd.append('--prefer-source')137 if prefer_dist is True:138 cmd.append('--prefer-dist')139 if no_scripts is True:140 cmd.append('--no-scripts')141 if no_plugins is True:142 cmd.append('--no-plugins')143 if optimize is True:144 cmd.append('--optimize-autoloader')145 if env is not None:146 env = salt.utils.data.repack_dictlist(env)147 env['COMPOSER_HOME'] = composer_home148 else:149 env = {'COMPOSER_HOME': composer_home}150 result = __salt__['cmd.run_all'](cmd,151 runas=runas,152 env=env,153 python_shell=False)154 if result['retcode'] != 0:155 raise CommandExecutionError(result['stderr'])156 if quiet is True:157 return True158 return result159def install(directory,160 composer=None,161 php=None,162 runas=None,163 prefer_source=None,164 prefer_dist=None,165 no_scripts=None,166 no_plugins=None,167 optimize=None,168 no_dev=None,169 quiet=False,170 composer_home='/root',171 env=None):172 '''173 Install composer dependencies for a directory.174 If composer has not been installed globally making it available in the175 system PATH & making it executable, the ``composer`` and ``php`` parameters176 will need to be set to the location of the executables.177 directory178 Directory location of the composer.json file.179 composer180 Location of the composer.phar file. If not set composer will181 just execute "composer" as if it is installed globally.182 (i.e. /path/to/composer.phar)183 php184 Location of the php executable to use with composer.185 (i.e. /usr/bin/php)186 runas187 Which system user to run composer as.188 prefer_source189 --prefer-source option of composer.190 prefer_dist191 --prefer-dist option of composer.192 no_scripts193 --no-scripts option of composer.194 no_plugins195 --no-plugins option of composer.196 optimize197 --optimize-autoloader option of composer. Recommended for production.198 no_dev199 --no-dev option for composer. Recommended for production.200 quiet201 --quiet option for composer. Whether or not to return output from composer.202 composer_home203 $COMPOSER_HOME environment variable204 env205 A list of environment variables to be set prior to execution.206 CLI Example:207 .. code-block:: bash208 salt '*' composer.install /var/www/application209 salt '*' composer.install /var/www/application \210 no_dev=True optimize=True211 '''212 result = _run_composer('install',213 directory=directory,214 composer=composer,215 php=php,216 runas=runas,217 prefer_source=prefer_source,218 prefer_dist=prefer_dist,219 no_scripts=no_scripts,220 no_plugins=no_plugins,221 optimize=optimize,222 no_dev=no_dev,223 quiet=quiet,224 composer_home=composer_home,225 env=env)226 return result227def update(directory,228 composer=None,229 php=None,230 runas=None,231 prefer_source=None,232 prefer_dist=None,233 no_scripts=None,234 no_plugins=None,235 optimize=None,236 no_dev=None,237 quiet=False,238 composer_home='/root',239 env=None):240 '''241 Update composer dependencies for a directory.242 If `composer install` has not yet been run, this runs `composer install`243 instead.244 If composer has not been installed globally making it available in the245 system PATH & making it executable, the ``composer`` and ``php`` parameters246 will need to be set to the location of the executables.247 directory248 Directory location of the composer.json file.249 composer250 Location of the composer.phar file. If not set composer will251 just execute "composer" as if it is installed globally.252 (i.e. /path/to/composer.phar)253 php254 Location of the php executable to use with composer.255 (i.e. /usr/bin/php)256 runas257 Which system user to run composer as.258 prefer_source259 --prefer-source option of composer.260 prefer_dist261 --prefer-dist option of composer.262 no_scripts263 --no-scripts option of composer.264 no_plugins265 --no-plugins option of composer.266 optimize267 --optimize-autoloader option of composer. Recommended for production.268 no_dev269 --no-dev option for composer. Recommended for production.270 quiet271 --quiet option for composer. Whether or not to return output from composer.272 composer_home273 $COMPOSER_HOME environment variable274 env275 A list of environment variables to be set prior to execution.276 CLI Example:277 .. code-block:: bash278 salt '*' composer.update /var/www/application279 salt '*' composer.update /var/www/application \280 no_dev=True optimize=True281 '''282 result = _run_composer('update',283 directory=directory,284 extra_flags='--no-progress',285 composer=composer,286 php=php,287 runas=runas,288 prefer_source=prefer_source,289 prefer_dist=prefer_dist,290 no_scripts=no_scripts,291 no_plugins=no_plugins,292 optimize=optimize,293 no_dev=no_dev,294 quiet=quiet,295 composer_home=composer_home,296 env=env)297 return result298def selfupdate(composer=None,299 php=None,300 runas=None,301 quiet=False,302 composer_home='/root'):303 '''304 Update composer itself.305 If composer has not been installed globally making it available in the306 system PATH & making it executable, the ``composer`` and ``php`` parameters307 will need to be set to the location of the executables.308 composer309 Location of the composer.phar file. If not set composer will310 just execute "composer" as if it is installed globally.311 (i.e. /path/to/composer.phar)312 php313 Location of the php executable to use with composer.314 (i.e. /usr/bin/php)315 runas316 Which system user to run composer as.317 quiet318 --quiet option for composer. Whether or not to return output from composer.319 composer_home320 $COMPOSER_HOME environment variable321 CLI Example:322 .. code-block:: bash323 salt '*' composer.selfupdate324 '''325 result = _run_composer('selfupdate',326 extra_flags='--no-progress',327 composer=composer,328 php=php,329 runas=runas,330 quiet=quiet,331 composer_home=composer_home)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!