def chugid_and_umask(runas, umask, group=None):
    '''
    Helper method for for subprocess.Popen to initialise uid/gid and umask
    for the new process.
    '''
    set_runas = False
    set_grp = False

    current_user = getpass.getuser()
    if runas and runas != current_user:
        set_runas = True
        runas_user = runas
    else:
        runas_user = current_user

    current_grp = grp.getgrgid(pwd.getpwnam(getpass.getuser()).pw_gid).gr_name
    if group and group != current_grp:
        set_grp = True
        runas_grp = group
    else:
        runas_grp = current_grp

    if set_runas or set_grp:
        chugid(runas_user, runas_grp)
    if umask is not None:
        os.umask(umask)
Helper method for for subprocess.Popen to initialise uid/gid and umask
    for the new process.