Given the package destination directory, the tarball containing debian files (e.g. control)
    and package sources, use pbuilder to safely build the platform package

    CLI Example:

    **Debian**

    .. code-block:: bash

        salt '*' pkgbuild.make_src_pkg deb-8-x86_64 /var/www/html
                https://raw.githubusercontent.com/saltstack/libnacl/master/pkg/deb/python-libnacl.control
                https://pypi.python.org/packages/source/l/libnacl/libnacl-1.3.5.tar.gz

    This example command should build the libnacl package for Debian using pbuilder
    and place it in /var/www/html/ on the minion

def build(runas,
          tgt,
          dest_dir,
          spec,
          sources,
          deps,
          env,
          template,
          saltenv='base',
          log_dir='/var/log/salt/pkgbuild'):  # pylint: disable=unused-argument
    '''
    Given the package destination directory, the tarball containing debian files (e.g. control)
    and package sources, use pbuilder to safely build the platform package

    CLI Example:

    **Debian**

    .. code-block:: bash

        salt '*' pkgbuild.make_src_pkg deb-8-x86_64 /var/www/html
                https://raw.githubusercontent.com/saltstack/libnacl/master/pkg/deb/python-libnacl.control
                https://pypi.python.org/packages/source/l/libnacl/libnacl-1.3.5.tar.gz

    This example command should build the libnacl package for Debian using pbuilder
    and place it in /var/www/html/ on the minion
    '''
    ret = {}
    retrc = 0
    try:
        os.makedirs(dest_dir)
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise
    dsc_dir = tempfile.mkdtemp()
    try:
        dscs = make_src_pkg(dsc_dir, spec, sources, env, saltenv, runas)
    except Exception as exc:
        shutil.rmtree(dsc_dir)
        log.error('Failed to make src package, exception \'%s\'', exc)
        return ret

    root_user = 'root'

    #ensure pbuilder setup from runas if other than root
    if runas != root_user:
        user_home = os.path.expanduser('~{0}'.format(runas))
        root_home = os.path.expanduser('~root')
        cmd = 'cp {0}/.pbuilderrc {1}/'.format(user_home, root_home)
        retrc = __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)
        cmd = 'cp -R {0}/.pbuilder-hooks {1}/'.format(user_home, root_home)
        retrc = __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)
        if retrc != 0:
            raise SaltInvocationError(
                 "build copy pbuilder files from \'{0}\' to \'{1}\' returned error \'{2}\', "
                 "check logs for further details".format(user_home, root_home, retrc))

    cmd = '/usr/sbin/pbuilder --create'
    retrc = __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)
    if retrc != 0:
        raise SaltInvocationError(
             'pbuilder create failed with return error \'{0}\', '
             'check logs for further details'.format(retrc))

    # use default /var/cache/pbuilder/result
    results_dir = '/var/cache/pbuilder/result'

    # ensure clean
    cmd = 'rm -fR {0}'.format(results_dir)
    retrc |= __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)

    # dscs should only contain salt orig and debian tarballs and dsc file
    for dsc in dscs:
        afile = os.path.basename(dsc)
        os.path.join(dest_dir, afile)

        if dsc.endswith('.dsc'):
            dbase = os.path.dirname(dsc)
            try:
                cmd = 'chown {0}:{0} -R {1}'.format(runas, dbase)
                retrc |= __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)
                cmd = '/usr/sbin/pbuilder update --override-config'
                retrc |= __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)
                cmd = '/usr/sbin/pbuilder build --debbuildopts "-sa" {0}'.format(dsc)
                retrc |= __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)
                if retrc != 0:
                    raise SaltInvocationError(
                         'pbuilder build or update failed with return error {0}, '
                         'check logs for further details'.format(retrc)
                    )

                # ignore local deps generated package file
                for bfile in os.listdir(results_dir):
                    if bfile != 'Packages':
                        full = os.path.join(results_dir, bfile)
                        bdist = os.path.join(dest_dir, bfile)
                        shutil.copy(full, bdist)
                        ret.setdefault('Packages', []).append(bdist)

            except Exception:
                log.exception('Error building from %s', dsc)

    # remove any Packages file created for local dependency processing
    for pkgzfile in os.listdir(dest_dir):
        if pkgzfile == 'Packages':
            pkgzabsfile = os.path.join(dest_dir, pkgzfile)
            os.remove(pkgzabsfile)

    cmd = 'chown {0}:{0} -R {1}'.format(runas, dest_dir)
    __salt__['cmd.retcode'](cmd, runas=root_user, python_shell=True, env=env)

    shutil.rmtree(dsc_dir)
    return ret