bootstrap.py 文件源码

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

项目:dcos 作者: dcos 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号