I'm working on a simple GUI Python script to do some simple tasks on a system. Some of that work involves apt-get install to install some packages.

While this is going on, I want to display a progress bar that should update with the progress of the download, using the little percentage shown in apt-get's interface in the terminal.

BUT! I can't find a way to get the progress info. Piping or redirecting the output of apt-get just gives static lines that show the "completed download" message for each package, and same for reading via subprocess.Popen() in my script.

How can I read from apt-get's output to get the percentages of the file downloaded?

share|improve this question
up vote 3 down vote accepted

As I've often said, use pexpect, not subprocess etc, to run sub-processes when you need to get their continuous output. pexpect fools the subprocess into believing it's running on a terminal, so the subprocess will provide just the kind of output it would give on a real terminal... and you can catch it and transform it into any kind of fancy output you want!-)

share|improve this answer
    
Any chance this could work in my C port as well using the posix popen()? Or is there a similar method? – Grant Paul Nov 7 '09 at 6:05
2  
popen will let the subprocess know its output is a pipe, so you're probably out of luck except with commands that are nice enough to let you ask them for "full unbuffered realtime output anyway pls!" and I don't think apt-get is in that elite group. To implement a "fake terminal" in C under Linux, start with the pty (7) man page, e.g. at linux.die.net/man/7/pty - and, good luck (lots of work will be needed!). – Alex Martelli Nov 7 '09 at 6:12

Instead of parsing the output of the apt-get, you can use python-apt to install packages. AFAIK it also has modules for reporting the progress.

share|improve this answer
    
Look at /usr/share/doc/python-apt/examples/progress.py – unutbu Nov 7 '09 at 19:16
    
Any way to do this without Python and without the huge mess of libapt? – Grant Paul Nov 8 '09 at 6:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.