Atomically install a queue

def install(trg_queue, is_down=False, is_triggered=False, user=None,
            group=None, mode=None, item_user=None, item_group=None,
            item_mode=None, hosts=None, is_host_triggered=False):
    '''Atomically install a queue'''
    mode, user, group, item_user, item_group, item_mode =\
        _def_mode(mode, user, group, item_user, item_group, item_mode)
    if hosts and not hasattr(hosts, '__iter__'):
        raise TypeError('Hosts must be an interable')
    # validate here, so that we don't throw an odd exception on the tmp name
    trg_queue = fsq_path.valid_name(trg_queue)
    # uid_gid makes calls to the pw db and|or gr db, in addition to
    # potentially stat'ing, as such, we want to avoid calling it unless we
    # absoultely have to
    uid, gid = uid_gid(user, group)
    tmp_full, tmp_queue = _tmp_trg(trg_queue, _c.FSQ_ROOT)
    try:
        # open once to cut down on stat/open for chown/chmod combo
        fd = os.open(tmp_full, os.O_RDONLY)
        try:
            # always fchmod here as mkdtemp is different than normal mkdir
            os.fchmod(fd, mode)
            if -1 != uid or -1 != gid:
                os.fchown(fd, uid, gid)
        finally:
            os.close(fd)

        # bless our queue with its children
        _instdir(fsq_path.tmp(tmp_queue), mode, uid, gid)
        _instdir(fsq_path.queue(tmp_queue), mode, uid, gid)
        _instdir(fsq_path.done(tmp_queue), mode, uid, gid)
        _instdir(fsq_path.fail(tmp_queue), mode, uid, gid)

        # down via configure.down if necessary
        if is_down:
            down(tmp_queue, user=item_user, group=item_group, mode=item_mode)
        if is_triggered or _c.FSQ_USE_TRIGGER:
            trigger(tmp_queue, user=item_user, group=item_group,
                    mode=item_mode)

        # atomic commit -- by rename
        os.rename(tmp_full, fsq_path.base(trg_queue))
    except (OSError, IOError, ), e:
        shutil.rmtree(tmp_full)
        if e.errno == errno.ENOTEMPTY:
            raise FSQInstallError(e.errno, u'queue exists: {0}'.format(
                                  trg_queue))
        if isinstance(e, FSQError):
            raise e
        raise FSQInstallError(e.errno, wrap_io_os_err(e))

    if hosts:
        install_host(trg_queue, *hosts, is_down=is_down, user=user,
                     group=group, mode=mode, item_user=item_user,
                     item_group=item_group, item_mode=item_mode)
    if is_host_triggered:
       host_trigger(trg_queue, user=user, group=group, mode=mode)