misc.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:librarian 作者: HERA-Team 项目源码 文件源码
def copyfiletree(src, dst):
    """Something like shutil.copytree that just copies data, not mode
    bits, and that will accept either a file or a directory as input.

    *dst* may not be the name of containing directory. It must be the
    name where the data in *src* are intended to land.

    As a special hack, we make the new files and directories
    group-writeable, since this function is used at NRAO when staging
    data to Lustre and otherwise users can't actually modify the
    files/dirs created for them, which is super annoying.

    """
    import os.path
    from shutil import copyfile
    import stat

    try:
        items = os.listdir(src)
    except OSError as e:
        if e.errno == 20:  # not a directory?
            copyfile(src, dst)
            st = os.stat(dst)  # NOTE! not src; we explicitly do not preserve perms
            mode = stat.S_IMODE(st.st_mode)
            mode |= (stat.S_IWUSR | stat.S_IWGRP)
            os.chmod(dst, mode)
            return
        raise

    os.mkdir(dst)
    st = os.stat(dst)  # NOTE! not src; we explicitly do not preserve perms
    mode = stat.S_IMODE(st.st_mode)
    mode |= (stat.S_IWUSR | stat.S_IWGRP | stat.S_IXUSR | stat.S_IXGRP | stat.S_ISGID)
    os.chmod(dst, mode)

    for item in items:
        copyfiletree(
            os.path.join(src, item),
            os.path.join(dst, item)
        )


# Misc ...
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号