<commit_before># -*- coding: utf-8 -*-
"""Proof of concept to use saltstack's pure modules and states with fabric::

    fab list_packages print_specs -H <ip> --user=<user>

On my vagrant environment:

    fab list_packages print_specs -H <ip> --password=<PASSWORD> --user=vagrant
"""

import os
import sys
import subprocess
from salt.modules.cmdmod import DEFAULT_SHELL

sys.path.append(os.path.dirname(__file__))
from salt_python import salt


try:
    import fabric
except ImportError:
    raise Exception("""
        You must install the latest version of salt:
        pip install fabric
    """)


def salt_run_fabric(cmd,
                    cwd=None,
                    stdin=None,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    output_loglevel='debug',
                    quiet=False,
                    runas=None,
                    shell=DEFAULT_SHELL,
                    python_shell=True,
                    env=None,
                    clean_env=False,
                    rstrip=True,
                    template=None,
                    umask=None,
                    timeout=None,
                    with_communicate=True,
                    reset_system_locale=True,
                    ignore_retcode=False,
                    saltenv='base',
                    use_vt=False):

    """Adapter to fit salt's argument signature into into fabric's
    subprocess wrapper."""

    return fabric.operations.sudo(
        cmd,
        shell=shell,
        user=runas,
        timeout=timeout,
        warn_only=ignore_retcode,
        stdout=stdout,
        stderr=stderr,
        env=env
    )

def salt_run_fabric_quiet(cmd,
                        cwd=None,
                        stdin=None,
                        runas=None,
                        shell=DEFAULT_SHELL,
                        python_shell=True,
                        env=None,
                        template=None,
                        umask=None,
                        timeout=None,
                        reset_system_locale=True,
                        saltenv='base'):
    '''
    Helper for running commands quietly for minion startup
    '''
    return salt_run_fabric(cmd,
                runas=runas,
                cwd=cwd,
                stdin=stdin,
                stderr=subprocess.STDOUT,
                output_loglevel='quiet',
                shell=shell,
                python_shell=python_shell,
                env=env,
                template=template,
                umask=umask,
                timeout=timeout,
                reset_system_locale=reset_system_locale,
                saltenv=saltenv)['stdout']

def bootstrap_fabric():
    from salt.config import DEFAULT_MASTER_OPTS
    from salt.grains.core import os_data

    __builtins__['__salt__'] = {
        'cmd.run': salt_run_fabric,
        'cmd.run_all': salt.modules.cmdmod.run_all
    }

    __builtins__['__context__'] = {}
    __builtins__['__env__'] = {}
    __builtins__['__opts__'] = DEFAULT_MASTER_OPTS
    __builtins__['__grains__'] = salt.loader.grains(__opts__)

    __builtins__['__salt__'].update(
        salt.loader.minion_mods(
            __opts__
        )
    )
    print(__salt__.keys())


bootstrap_fabric()


@fabric.api.task
def list_packages():
    # from salt.modules import aptpkg
    # if __grains__['os'] == 'MacOS' and sources:
    # causes a race condition
    #print(aptpkg.install('vim'))

    from salt.modules import aptpkg
    print(aptpkg.list_pkgs())

@fabric.api.task
def print_specs():
    from pprint import PrettyPrinter
    pp = PrettyPrinter(indent=4).pprint
    pp(__grains__)<commit_msg>update comment about fabfile race condition<commit_after># -*- coding: utf-8 -*-
"""Proof of concept to use saltstack's pure modules and states with fabric::

    fab list_packages print_specs -H <ip> --user=<user>

On my vagrant environment:

    fab list_packages print_specs -H <ip> --password=<PASSWORD> --user=vagrant
"""

import os
import sys
import subprocess
from salt.modules.cmdmod import DEFAULT_SHELL

sys.path.append(os.path.dirname(__file__))
from salt_python import salt


try:
    import fabric
except ImportError:
    raise Exception("""
        You must install the latest version of salt:
        pip install fabric
    """)


def salt_run_fabric(cmd,
                    cwd=None,
                    stdin=None,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    output_loglevel='debug',
                    quiet=False,
                    runas=None,
                    shell=DEFAULT_SHELL,
                    python_shell=True,
                    env=None,
                    clean_env=False,
                    rstrip=True,
                    template=None,
                    umask=None,
                    timeout=None,
                    with_communicate=True,
                    reset_system_locale=True,
                    ignore_retcode=False,
                    saltenv='base',
                    use_vt=False):

    """Adapter to fit salt's argument signature into into fabric's
    subprocess wrapper."""

    return fabric.operations.sudo(
        cmd,
        shell=shell,
        user=runas,
        timeout=timeout,
        warn_only=ignore_retcode,
        stdout=stdout,
        stderr=stderr,
        env=env
    )

def salt_run_fabric_quiet(cmd,
                        cwd=None,
                        stdin=None,
                        runas=None,
                        shell=DEFAULT_SHELL,
                        python_shell=True,
                        env=None,
                        template=None,
                        umask=None,
                        timeout=None,
                        reset_system_locale=True,
                        saltenv='base'):
    '''
    Helper for running commands quietly for minion startup
    '''
    return salt_run_fabric(cmd,
                runas=runas,
                cwd=cwd,
                stdin=stdin,
                stderr=subprocess.STDOUT,
                output_loglevel='quiet',
                shell=shell,
                python_shell=python_shell,
                env=env,
                template=template,
                umask=umask,
                timeout=timeout,
                reset_system_locale=reset_system_locale,
                saltenv=saltenv)['stdout']

def bootstrap_fabric():
    from salt.config import DEFAULT_MASTER_OPTS
    from salt.grains.core import os_data

    __builtins__['__salt__'] = {
        'cmd.run': salt_run_fabric,
        'cmd.run_all': salt.modules.cmdmod.run_all
    }

    __builtins__['__context__'] = {}
    __builtins__['__env__'] = {}
    __builtins__['__opts__'] = DEFAULT_MASTER_OPTS
    __builtins__['__grains__'] = salt.loader.grains(__opts__)

    __builtins__['__salt__'].update(
        salt.loader.minion_mods(
            __opts__
        )
    )
    print(__salt__.keys())


bootstrap_fabric()


@fabric.api.task
def list_packages():
    # from salt.modules import aptpkg
    # if __grains__['os'] == 'MacOS' and sources:
    # causes a race condition. virtual modules must avoid dunder race
    # conditions.
    #print(aptpkg.install('vim'))

    from salt.modules import aptpkg
    print(aptpkg.list_pkgs())

@fabric.api.task
def print_specs():
    from pprint import PrettyPrinter
    pp = PrettyPrinter(indent=4).pprint
    pp(__grains__)
{'fraction_non_alphanumeric': 0.08669516430670585, 'fraction_numerical': 0.0005343307507347048, 'mean_word_length': 1.6141759776536313, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6829704', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2009, 'n_words': 415}