Ensure that the named package is built and exists in the named directory

    name
        The name to track the build, the name value is otherwise unused

    runas
        The user to run the build process as

    dest_dir
        The directory on the minion to place the built package(s)

    spec
        The location of the spec file (used for rpms)

    sources
        The list of package sources

    tgt
        The target platform to run the build on

    template
        Run the spec file through a templating engine

        .. versionchanged:: 2015.8.2

            This argument is now optional, allowing for no templating engine to
            be used if none is desired.

    deps
        Packages required to ensure that the named package is built
        can be hosted on either the salt master server or on an HTTP
        or FTP server.  Both HTTPS and HTTP are supported as well as
        downloading directly from Amazon S3 compatible URLs with both
        pre-configured and automatic IAM credentials

    env
        A dictionary of environment variables to be set prior to execution.
        Example:

        .. code-block:: yaml

            - env:
                DEB_BUILD_OPTIONS: 'nocheck'

        .. warning::

            The above illustrates a common PyYAML pitfall, that **yes**,
            **no**, **on**, **off**, **true**, and **false** are all loaded as
            boolean ``True`` and ``False`` values, and must be enclosed in
            quotes to be used as strings. More info on this (and other) PyYAML
            idiosyncrasies can be found :ref:`here <yaml-idiosyncrasies>`.

    results
        The names of the expected rpms that will be built

    force : False
        If ``True``, packages will be built even if they already exist in the
        ``dest_dir``. This is useful when building a package for continuous or
        nightly package builds.

        .. versionadded:: 2015.8.2

    saltenv
        The saltenv to use for files downloaded from the salt filesever

    log_dir : /var/log/salt/rpmbuild
        Root directory for log files created from the build. Logs will be
        organized by package name, version, OS release, and CPU architecture
        under this directory.

        .. versionadded:: 2015.8.2

def built(name,
          runas,
          dest_dir,
          spec,
          sources,
          tgt,
          template=None,
          deps=None,
          env=None,
          results=None,
          force=False,
          saltenv='base',
          log_dir='/var/log/salt/pkgbuild'):
    '''
    Ensure that the named package is built and exists in the named directory

    name
        The name to track the build, the name value is otherwise unused

    runas
        The user to run the build process as

    dest_dir
        The directory on the minion to place the built package(s)

    spec
        The location of the spec file (used for rpms)

    sources
        The list of package sources

    tgt
        The target platform to run the build on

    template
        Run the spec file through a templating engine

        .. versionchanged:: 2015.8.2

            This argument is now optional, allowing for no templating engine to
            be used if none is desired.

    deps
        Packages required to ensure that the named package is built
        can be hosted on either the salt master server or on an HTTP
        or FTP server.  Both HTTPS and HTTP are supported as well as
        downloading directly from Amazon S3 compatible URLs with both
        pre-configured and automatic IAM credentials

    env
        A dictionary of environment variables to be set prior to execution.
        Example:

        .. code-block:: yaml

            - env:
                DEB_BUILD_OPTIONS: 'nocheck'

        .. warning::

            The above illustrates a common PyYAML pitfall, that **yes**,
            **no**, **on**, **off**, **true**, and **false** are all loaded as
            boolean ``True`` and ``False`` values, and must be enclosed in
            quotes to be used as strings. More info on this (and other) PyYAML
            idiosyncrasies can be found :ref:`here <yaml-idiosyncrasies>`.

    results
        The names of the expected rpms that will be built

    force : False
        If ``True``, packages will be built even if they already exist in the
        ``dest_dir``. This is useful when building a package for continuous or
        nightly package builds.

        .. versionadded:: 2015.8.2

    saltenv
        The saltenv to use for files downloaded from the salt filesever

    log_dir : /var/log/salt/rpmbuild
        Root directory for log files created from the build. Logs will be
        organized by package name, version, OS release, and CPU architecture
        under this directory.

        .. versionadded:: 2015.8.2
    '''
    ret = {'name': name,
           'changes': {},
           'comment': '',
           'result': True}

    if not results:
        ret['comment'] = '\'results\' argument is required'
        ret['result'] = False
        return ret

    if isinstance(results, six.string_types):
        results = results.split(',')

    needed = _get_missing_results(results, dest_dir)
    if not force and not needed:
        ret['comment'] = 'All needed packages exist'
        return ret

    if __opts__['test']:
        ret['result'] = None
        if force:
            ret['comment'] = 'Packages will be force-built'
        else:
            ret['comment'] = 'The following packages need to be built: '
            ret['comment'] += ', '.join(needed)
        return ret

    # Need the check for None here, if env is not provided then it falls back
    # to None and it is assumed that the environment is not being overridden.
    if env is not None and not isinstance(env, dict):
        ret['comment'] = ('Invalidly-formatted \'env\' parameter. See '
                          'documentation.')
        ret['result'] = False
        return ret

    func = 'pkgbuild.build'
    if __grains__.get('os_family', False) not in ('RedHat', 'Suse'):
        for res in results:
            if res.endswith('.rpm'):
                func = 'rpmbuild.build'
                break

    ret['changes'] = __salt__[func](
        runas,
        tgt,
        dest_dir,
        spec,
        sources,
        deps,
        env,
        template,
        saltenv,
        log_dir)

    needed = _get_missing_results(results, dest_dir)
    if needed:
        ret['comment'] = 'The following packages were not built: '
        ret['comment'] += ', '.join(needed)
        ret['result'] = False
    else:
        ret['comment'] = 'All needed packages were built'
    return ret