<commit_before>'''
Support for poudriere
'''

# Import python libs
import os
import logging

# Import salt libs
import salt.utils

log = logging.getLogger(__name__)


def __virtual__():
    '''
    Module load on freebsd only and if poudriere installed
    '''
    if __grains__['os'] == 'FreeBSD' and salt.utils.which('poudriere'):
        return 'poudriere'
    else:
        return False


def _config_file():
    '''
    Return the config file location to use
    '''
    return __salt__['config.option']('poudriere.config')


def _config_dir():
    '''
    Return the configuration directory to use
    '''
    return __salt__['config.option']('poudriere.config_dir')


def _check_config_exists(config_file=None):
    '''
    Verify the config file is present
    '''
    if config_file is None:
        config_file = _config_file()
    if not os.path.isfile(config_file):
        return False
    return True


def is_jail(name):
    '''
    Return True if jail exists False if not

    CLI Example::

        salt '*' poudriere.is_jail <jail name>
    '''
    jails = list_jails()
    for jail in jails:
        if jail.split()[0] == name:
            return True
    return False


def make_pkgng_aware(jname):
    '''
    Make jail ``jname`` pkgng aware

    CLI Example::

        salt '*' poudriere.make_pkgng_aware <jail name>
    '''
    ret = {'changes': {}}
    cdir = _config_dir()
    # ensure cdir is there
    if not os.path.isdir(cdir):
        os.makedirs(cdir)
        if os.path.isdir(cdir):
            ret['changes'] = 'Created poudriere make file dir {0}'.format(cdir)
        else:
            return 'Could not create or find required directory {0}'.format(
                    cdir)

    # Added args to file
    cmd = 'echo "WITH_PKGNG=yes" > {0}-make.conf'.format(
            os.path.join(cdir, jname))

    __salt__['cmd.run'](cmd)

    if os.path.isfile(os.path.join(cdir, jname) + '-make.conf'):
        ret['changes'] = 'Created {0}'.format(
                os.path.join(cdir, '{0}-make.conf'.format(jname))
                )
        return ret
    else:
        return 'Looks like file {0} could not be created'.format(
                os.path.join(cdir, jname + '-make.conf')
                )


def parse_config(config_file=None):
    '''
    Returns a dict of poudriere main configuration defintions

    CLI Example::

        salt '*' poudriere.parse_config
    '''
    if config_file is None:
        config_file = _config_file()
    ret = {}
    if _check_config_exists(config_file):
        with salt.utils.fopen(config_file) as f:
            for line in f:
                k, y = line.split('=')
                ret[k] = y
        return ret
    else:
        return 'Could not find {0} on file system'.format(config_file)


def version():
    '''
    Return poudriere version

    CLI Example::

        salt '*' poudriere.version
    '''
    cmd = "poudriere version"
    return __salt__['cmd.run'](cmd)


def list_jails():<commit_msg>Fix PyLint C0103 on `salt.modules.poudriere`. Refs #1775.<commit_after>'''
Support for poudriere
'''

# Import python libs
import os
import logging

# Import salt libs
import salt.utils

log = logging.getLogger(__name__)


def __virtual__():
    '''
    Module load on freebsd only and if poudriere installed
    '''
    if __grains__['os'] == 'FreeBSD' and salt.utils.which('poudriere'):
        return 'poudriere'
    else:
        return False


def _config_file():
    '''
    Return the config file location to use
    '''
    return __salt__['config.option']('poudriere.config')


def _config_dir():
    '''
    Return the configuration directory to use
    '''
    return __salt__['config.option']('poudriere.config_dir')


def _check_config_exists(config_file=None):
    '''
    Verify the config file is present
    '''
    if config_file is None:
        config_file = _config_file()
    if not os.path.isfile(config_file):
        return False
    return True


def is_jail(name):
    '''
    Return True if jail exists False if not

    CLI Example::

        salt '*' poudriere.is_jail <jail name>
    '''
    jails = list_jails()
    for jail in jails:
        if jail.split()[0] == name:
            return True
    return False


def make_pkgng_aware(jname):
    '''
    Make jail ``jname`` pkgng aware

    CLI Example::

        salt '*' poudriere.make_pkgng_aware <jail name>
    '''
    ret = {'changes': {}}
    cdir = _config_dir()
    # ensure cdir is there
    if not os.path.isdir(cdir):
        os.makedirs(cdir)
        if os.path.isdir(cdir):
            ret['changes'] = 'Created poudriere make file dir {0}'.format(cdir)
        else:
            return 'Could not create or find required directory {0}'.format(
                    cdir)

    # Added args to file
    cmd = 'echo "WITH_PKGNG=yes" > {0}-make.conf'.format(
            os.path.join(cdir, jname))

    __salt__['cmd.run'](cmd)

    if os.path.isfile(os.path.join(cdir, jname) + '-make.conf'):
        ret['changes'] = 'Created {0}'.format(
                os.path.join(cdir, '{0}-make.conf'.format(jname))
                )
        return ret
    else:
        return 'Looks like file {0} could not be created'.format(
                os.path.join(cdir, jname + '-make.conf')
                )


def parse_config(config_file=None):
    '''
    Returns a dict of poudriere main configuration defintions

    CLI Example::

        salt '*' poudriere.parse_config
    '''
    if config_file is None:
        config_file = _config_file()
    ret = {}
    if _check_config_exists(config_file):
        with salt.utils.fopen(config_file) as ifile:
            for line in ifile:
                key, val = line.split('=')
                ret[key] = val
        return ret

    return 'Could not find {0} on file system'.format(config_file)


def version():
    '''
    Return poudriere version

    CLI Example::

        salt '*' poudriere.version
    '''
    cmd = "poudriere version"
    return __salt__['cmd.run'](cmd)


def list_jails():
{'fraction_non_alphanumeric': 0.12069825436408978, 'fraction_numerical': 0.00399002493765586, 'mean_word_length': 2.2607046070460703, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5292809', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1933, 'n_words': 510}