0

I'm trying to add progressbar to my install script. I tried to use the module progressbar but it depends on range and I don't have this number, also every file that I want to install takes a different time. I have a list of the files that I want to install and I want to monitor it by progressbar. This is the process line:

p2 = subprocess.Popen(["dpkg", "--force-all", "-i", "-R", pwd.strip() + "/archives"], stdout=out, stderr=err)

I thought to work with the output file, but It grows dynamically and I didn't succeed monitor it. An example for the out file:

In the beginning of the file, there is line of "Unpacking" and line of "Preparing" for each package:

Unpacking openvswitch-switch (2.0.2-0ubuntu0.14.04.2) over (2.0.2-0ubuntu0.14.04.2) ...
Preparing to unpack .../libllvm3.6_3.6-2ubuntu1_amd64.deb ...
Unpacking libllvm3.6:amd64 (1:3.6-2ubuntu1) over (1:3.6-2ubuntu1) ...
Preparing to unpack .../kpartx_0.4.9-3ubuntu7.4_amd64.deb ...
Unpacking kpartx (0.4.9-3ubuntu7.4) over (0.4.9-3ubuntu7.4) ...
.
.
.

And after that there are lines of "Setting up" and sometimes starting the process of the package, like this:

Setting up openvswitch-switch (2.0.2-0ubuntu0.14.04.2) ...
openvswitch-switch stop/waiting
openvswitch-switch start/running
Setting up libllvm3.6:amd64 (1:3.6-2ubuntu1) ...
Setting up libxen-4.4 (4.4.2-0ubuntu0.14.04.1) ...
Setting up sg3-utils (1.36-1ubuntu1) ...
.
.
.

I have 294 files and the numbers of lines that include 'Unpacking' 294, 'Preparing' 294 and 'Setting up' 293 and the total number of lines is 915.

Anyone have an idea how to do this?

2
  • "it depends on range and I don't have this number, also evry file that I want to install takes a different time" - that's why you have to estimate, same as any other progress bar rendered in any other application. Commented Nov 5, 2015 at 9:28
  • IMHO the problem is not Python itself but the progress bar itself. You can use graphic frameworks such as pytk or even Qt or WxWindows that have python interfaces. Or you can use ASCII art by outputting \b spaces and = to draw and update a line like========> 20%. Once that's specified, remaining should be easier... Commented Nov 5, 2015 at 9:34

1 Answer 1

1

There is no for loop, recursion or some other method where you could insert a progress update function in your posted code, so unless you can expand on it you are out of luck really. The typical way I do a progress bar is something like:

niter = 10000

# Setup the progress counters
increment = int(0.01 * niter)
percent = 0
countdown = increment
nbars = 0
totalbars = 20
barincrement = int(100/totalbars)

sys.stdout.write("{0}0%\r".format(" " * totalbars))

for i in range(niter):
    doSomeWork()
    countdown -= 1

    # Update and print out progress bar and % indicator if required
    if countdown == 0:
      percent += 1
      countdown = increment
      if percent % barcinrement == 0:
        nbars += 1
      bars = "#" * nbars
      space = " " * (totalbars - nbars)

      sys.stdout.write("{0}{1}{2}%\r".format(bars, space, percent))

sys.stdout.write("\nOperation Complete!\n")

I would often move the setup & update of the progress bar to a separate class to make the actual logic more readable, so that you have something like this:

niter = 10000

prog = ProgressReporter(niter)

for i in range(niter):
    doSomeWork()
    prog.update()
prog.finish()

To print out a progress bar you need some kind of iterative procedure by which you can actually update the progress.

In your case, instead of making a subprocess call, you probably need to implement it yourself. Or perhaps make a separate call for each file. But either way - no form of iteration, no progress bar.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.