def install(name=None,
            fromrepo=None,
            pkgs=None,
            sources=None,
            jail=None,
            chroot=None,
            orphan=False,
            force=False,
            glob=False,
            local=False,
            dryrun=False,
            quiet=False,
            reinstall_requires=False,
            regex=False,
            pcre=False,
            **kwargs):
    try:
        pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
            name, pkgs, sources, **kwargs
        )
    except MinionError as exc:
        raise CommandExecutionError(exc)

    if pkg_params is None or len(pkg_params) == 0:
        return {}

    opts = 'y'
    if salt.utils.is_true(orphan):
        opts += 'A'
    if salt.utils.is_true(force):
        opts += 'f'
    if salt.utils.is_true(glob):
        opts += 'g'
    if salt.utils.is_true(local):
        opts += 'U'
    if salt.utils.is_true(dryrun):
        opts += 'n'
    if salt.utils.is_true(quiet):
        opts += 'q'
    if salt.utils.is_true(reinstall_requires):
        opts += 'R'
    if salt.utils.is_true(regex):
        opts += 'x'
    if salt.utils.is_true(pcre):
        opts += 'X'

    old = list_pkgs(jail=jail, chroot=chroot)

    if pkg_type == 'file':
        pkg_cmd = 'add'
        # pkg add has smaller set of options (i.e. no -y or -n), filter below
        opts = ''.join([opt for opt in opts if opt in 'AfIMq'])
        targets = pkg_params
    elif pkg_type == 'repository':
        pkg_cmd = 'install'
        if pkgs is None and kwargs.get('version') and len(pkg_params) == 1:
            # Only use the 'version' param if 'name' was not specified as a
            # comma-separated list
            pkg_params = {name: kwargs.get('version')}
        targets = []
        for param, version_num in six.iteritems(pkg_params):
            if version_num is None:
                targets.append(param)
            else:
                targets.append('{0}-{1}'.format(param, version_num))

    cmd = _pkg(jail, chroot)
    cmd.append(pkg_cmd)
    if fromrepo:
        cmd.extend(['-r', fromrepo])
    if opts:
        cmd.append('-' + opts)
    cmd.extend(targets)

    if pkg_cmd == 'add' and salt.utils.is_true(dryrun):
        # pkg add doesn't have a dryrun mode, so echo out what will be run
        return ' '.join(cmd)

    out = __salt__['cmd.run_all'](
        cmd,
        output_loglevel='trace',
        python_shell=False
    )

    if out['retcode'] != 0 and out['stderr']:
        errors = [out['stderr']]
    else:
        errors = []

    __context__.pop(_contextkey(jail, chroot), None)
    __context__.pop(_contextkey(jail, chroot, prefix='pkg.origin'), None)
    new = list_pkgs(jail=jail, chroot=chroot)
    ret = salt.utils.compare_dicts(old, new)

    if errors:
        raise CommandExecutionError(
            'Problem encountered installing package(s)',
            info={'errors': errors, 'changes': ret}
        )

    return ret
Install package(s) from a repository
dataset/ETHPy150Open saltstack/salt/salt/modules/pkgng.py/install