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 ...
评论列表
文章目录