[Tutor] pipes, lots of 'em

Michael P. Reilly arcege@shore.net
Wed, 14 Mar 2001 12:25:07 -0500 (EST)


> On Tue, Mar 13, 2001 at 11:35:24PM -0800, Sean 'Shaleh' Perry wrote:
> | On Tue, Mar 13, 2001 at 10:39:44PM -0800, Daniel Yoo wrote:
> | 
> | ar p foo.ar bar|tar xfO - baz
> | 
> | is the basic command line I need.  This get bar out of the ar formatted foo.ar
> | and sends it to tar which extracts baz.  I want to be able to parse baz
> | without it getting written to disk.  
> 
> I think the problem here is the command line.  Can you request tar to
> output on stdout, rather than writing to disk?  I know that you can
> for the tarfile itself with the "f -" options.  I just briefly looked
> at the tar manpage but didn't find any obvious examples.  Perhaps a
> closer look at the manpage would help.
> 
> Alternatively, maybe you can dig into tar's sources a little and find
> the functions that do the actual work.  Maybe you can turn it into a
> library without too much trouble?

I have a form of tar (<URL: http://www.shore.net/~arcege/python/tar.py>)
available.  It only handles reading tarfiles.

The "O" option is not standard, and probably only exists for GNU tar.

I have to read tarfiles a lot in my code at times, I usually create a
temporary directory, change into that directory, extract the files there,
then remove the directory tree when I'm done.

>>> import os, shutil, tempfile
>>> tempdir = tempfile.mktemp()
>>> os.mkdir(tempdir)
>>> os.chdir(tempdir)
>>> if os.path.splitext(tarfile)[1] == '.gz':
...   preop = 'gzip -cd'
... else:
...   preop = 'cat'
...
>>> f = os.popen('%(preop)s %(tarfile) | tar xvf -' % locals(), 'r')
>>> files_extracted = f.readlines()
>>> tar_status = f.close()
>>> for extracted_file in files_extracted:
...   # use extracted_file in current directory to operate of files
...   pass
...
>>> os.chdir('/')
>>> shutil.rmtree(tempdir)

Or something like that. :)

  -Arcege


-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------