简体   繁体   English

在tgz文件上运行python subprocess.call以解压缩并输出流

[英]Running python subprocess.call on tgz file to untar and stream output

I'm using a subprocess call to untar a file in the command line, I need to use the output of that call to stream into a temp file so I can read the contents of the "+CONTENTS" folder with in the tgz file. 我正在使用子过程调用来在命令行中解压缩文件,我需要使用该调用的输出将其流式传输到临时文件中,这样我才能读取tgz文件中“ + CONTENTS”文件夹的内容。

My failed output is: 我失败的输出是:

./streamContents.py rsh: ftp: No address associated with hostname tar (child): ftp://myftpserver.com/pkgsrc/doxygen_pkgs/test. ./streamContents.py rsh:ftp:没有与主机名tar(子项)关联的地址:ftp://myftpserver.com/pkgsrc/doxygen_pkgs/test。 tgz: Cannot open: Input/output error tar (child): Error is not recoverable: exiting now tgz:无法打开:输入/输出错误tar(子级):错误不可恢复:现在退出

gzip: stdin: unexpected end of file tar: Child returned status 2 tar: Error exit delayed from previous errors Traceback (most recent call last): File "./streamContents.py", line 29, in stream = proc.stdout.read(8196) AttributeError: 'int' object has no attribute 'stdout' gzip:stdin:文件tar意外结束:子级返回状态2 tar:错误退出由于先前的错误而延迟追溯(最近一次调用为最新):文件“ ./streamContents.py”,第29行,流= proc.stdout.read (8196)AttributeError:“ int”对象没有属性“ stdout”

#!/usr/bin/python

from io import BytesIO
import urllib2
import tarfile
import ftplib
import socket
import threading
import subprocess

tarfile_url = "ftp://myftpserver.com/pkgsrc/doxygen_pkgs/test.tg
z"

try:
    ftpstream = urllib2.urlopen(tarfile_url)
except URLerror, e:
    print "URL timeout"
except socket.timeout:
    print "Socket timeout"


# BytesIO creates an in-memory temporary file.
tmpfile = BytesIO()
last_size = 0
tfile_extract = ""

while True:
    proc = subprocess.call(['tar','-xzvf', tarfile_url], stdout=subprocess.PIPE)
    # Download a piece of the file from the ftp connection
    stream = proc.stdout.read(8196)
    if not stream: break
    tmpfile.write(bytes(stream))
    # Seeking back to the beginning of the temporary file.
    tmpfile.seek(0)
    # r|gz forbids seeking backward; r:gz allows seeking backward
    try:
       tfile = tarfile.open(fileobj=tmpfile, mode="r:gz")
       print tfile.extractfile("+CONTENTS")
       tfile_extract_text = tfile_extract.read()
       print tfile_extract.tell()
       tfile.close()
       if tfile_extract.tell() > 0 and tfile_extract.tell() == last_size:
          print tfile_extract_text
          break
       else:
          last_size = tfile_extract.tell()
    except Exception:
       tfile.close()
       pass


tfile_extract_text = tfile_extract.read()
print tfile_extract_text

# When you're done:
tfile.close()
tmpfile.close()

Expanding on my comment above, you need to do download the tar file using urllib2 and tempfile to a temporary file and then open this temporary file using tarfile . 扩展上面的评论,您需要使用urllib2tempfile将tar文件下载到一个临时文件,然后使用tarfile打开此临时文件。

Here's some code to get started: 这是一些入门代码:

import urllib2
import tarfile
from tempfile import TemporaryFile

f_url = 'url_of_your_tar_archive'
ftpstream = urllib2.urlopen(f_url)
tmpfile = TemporaryFile()

# Download contents of tar to a temporary file
while True:
    s = ftpstream.read(16384)
    if not s:
        break
    tmpfile.write(s)
ftpstream.close()

# Access the temporary file to extract the file you need
tmpfile.seek(0)
tfile = tarfile.open(fileobj=tmpfile, mode='r:gz')
print tfile.getnames()
contents = tfile.extractfile("+CONTENTS").read()
print contents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM