export.py 文件源码

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

项目:Marty 作者: NaPs 项目源码 文件源码
def export_directory(tree, storage, output):
    """ Export a tree in a directory.
    """

    os.mkdir(output)

    for fullname, item in walk_tree(storage, tree):
        outfullname = os.path.join(output.encode('utf-8'), fullname.lstrip(b'/'))

        if item.type == 'blob':
            blob = storage.get_blob(item.ref).blob
            with open(outfullname, 'wb') as fout:
                shutil.copyfileobj(blob, fout)
            printer.verbose('Exporting to {out}: <b>{fn}</b> ({size})',
                            out=output,
                            fn=fullname.decode('utf-8', errors='replace'),
                            size=humanize.naturalsize(item['size'], binary=True))
        elif item.type == 'tree':
            os.mkdir(outfullname)
            printer.verbose('Exporting to {out}: <b>{fn}</b> (directory)',
                            out=output,
                            fn=fullname.decode('utf-8', errors='replace'))
        else:
            if item['filetype'] == 'link':
                os.symlink(item['link'], outfullname)
                printer.verbose('Exporting to {out}: <b>{fn}</b> (link to {link})',
                                out=output,
                                fn=fullname.decode('utf-8', errors='replace'),
                                link=item['link'].decode('utf-8', errors='replace'))

            elif item['filetype'] == 'fifo':
                os.mkfifo(outfullname)
                printer.verbose('Exporting to {out}: <b>{fn}</b> (fifo)',
                                out=output,
                                fn=fullname.decode('utf-8', errors='replace'))
            else:
                continue  # Ignore unknown file types

        try:
            if 'mode' in item:
                try:
                    os.chmod(outfullname, item['mode'], follow_symlinks=False)
                except SystemError:
                    pass  # Workaround follow_symlinks not implemented in Python 3.5 (bug?)
            if 'uid' in item or 'gid' in item:
                os.chown(outfullname, item.get('uid', -1), item.get('gid', -1), follow_symlinks=False)
        except PermissionError:
            printer.p('<color fg=yellow><b>Warning:</b> unable to set attributes on {fn}</color>',
                      fn=fullname.decode('utf-8', errors='replace'))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号