def _write_file(path, data, mode, owner='root'):
dirpath = os.path.dirname(os.path.abspath(path))
log.info('Opening {} for locking'.format(dirpath))
with utils.Directory(dirpath) as d:
log.info('Taking exclusive lock on {}'.format(dirpath))
with d.lock():
umask_original = os.umask(0)
try:
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
log.info('Writing {} with mode {:o}'.format(path, mode))
tmppath = path + '.tmp'
with os.fdopen(os.open(tmppath, flags, mode), 'wb') as f:
f.write(data)
os.rename(tmppath, path)
user = pwd.getpwnam(owner)
os.chown(path, user.pw_uid, user.pw_gid)
finally:
os.umask(umask_original)
评论列表
文章目录