Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11180

Using a zipped file as a shell stdin

I'm trying to write a short script that will recover a repository. The backup script generates dump files that are gzipped.

To apply a dump I need to call this command:

svnadmin load < myfile

but since myfile is a gzipped one, I need to unzip it for the command to work.

Now here comes my question, is the command on top same as

subprocess.call(['svnadmin','load', myfilecontents])

This way I will avoid the need to unzip the file, to a temporary location. or should I be using

subprocess.call(['svnadmin','load'],stdin=gzip.open(myfile))

Upvotes: 0

Views: 199

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

You can't point stdin at a GzipFile, but you can copy the data yourself

In [5]: cmd=subprocess.Popen(["od", "-cx"], stdin=subprocess.PIPE)
In [6]: data=gzip.open("/tmp/hello.gz")
In [8]: cmd.stdin.write(data.read())
In [9]: cmd.stdin.close()
0000000   h   i  \n
           6968    000a
0000003

Alternatively, you could use Popen.communicate():

In [11]: cmd=subprocess.Popen(["od", "-cx"], 
                               stdin=subprocess.PIPE, 
                               stdout=subprocess.PIPE, 
                               stderr=subprocess.PIPE)
In [12]: data=gzip.open("/tmp/hello.gz")
In [13]: cmd.communicate(data.read())
Out[13]: ('0000000   h   i  \\n\n           6968    000a\n0000003\n', '')

Upvotes: 1

Related Questions