2

我怎样才能从命令的标准输出中读取单个文件的内容而不敲击磁盘?

我想出了这样的事情:

def get_files_from(sha, files):
    from subprocess import Popen, PIPE
    import tarfile
    p = Popen(["git", "archive", sha], bufsize=10240, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    tar = tarfile.open(fileobj=p.stdout, mode='r|')
    p.communicate()
    members = tar.getmembers()
    names = tar.getnames()
    contents = {}
    for fname in files:
        if fname not in names:
            contents[fname] = None
            continue
        else:
            idx = names.index(fname)
            contents[fname] = members[idx].tobuf()
            contents[fname] = tar.extractfile(members[idx]) #<--- HERE

    tar.close()
    return contents

问题是.read()在标记的行上添加呼叫

            contents[fname] = tar.extractfile(members[idx]) #<--- HERE

将给出错误:

tarfile.StreamError:不允许向后搜索

那么如何获取文件的内容呢?

4

2 回答 2

4

您拼错了mode=参数,而是写了more=

tar = tarfile.open(fileobj=p.stdout, mode='r|')

.tell()如果您正确指定模式,将不会被调用。:-)

然后,您必须遍历tarfile 对象以提取成员,您不能从 tarfile 中读取任意文件:

for entry in tar:
    # test if this is a file you want.
    if entry.name in files:
        f = tar.extractfile(entry) 

您不能使用任何.getnames().getmember().getmembers()方法,因为这些方法需要对文件进行全面扫描,将文件指针放在末尾,让您无法读取条目数据本身。

于 2012-12-02T09:08:54.840 回答
0

对于任何有兴趣的人:

def get_files_from(sha, files):
    from subprocess import Popen, PIPE
    import tarfile
    p = Popen(["git", "archive", sha], bufsize=10240, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    tar = tarfile.open(fileobj=p.stdout, mode='r|')
    p.communicate()
    contents = {}
    doall = files == '*'
    if not doall:
        files = set(files)
    for entry in tar:
        if (isinstance(files, set) and entry.name in files) or doall:
            tf = tar.extractfile(entry)
            contents[entry.name] = tf.read()
            if not doall:
                files.discard(entry.name)

    if not doall:
        for fname in files:
            contents[fname] = None

    tar.close()
    return contents

print get_files_from("a8c11fcee68881dfb86095aa36290fb304047cf1", ['README.MD', 'foo'])
print get_files_from("a8c11fcee68881dfb86095aa36290fb304047cf1", '*')

欢迎补丁!

于 2012-12-02T10:39:33.643 回答