Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import subprocess
  2.  
  3. def install_package(package):
  4. percent = 0
  5. # Alpine apk provides machine-readable progress in bytes_downloaded/bytes_total format output to file descriptor of choice, so create a pipe for it
  6. pipe_rfd, pipe_wfd = os.pipe()
  7. with os.fdopen(pipe_rfd) as pipe_rf:
  8. with subprocess.Popen(['apk', '--progress-fd', str(pipe_wfd), '--no-cache', 'add', package], pass_fds=[pipe_wfd]) as p:
  9. # Close write pipe for vmmgr to not block the pipe once apk finishes
  10. os.close(pipe_wfd)
  11. while p.poll() == None:
  12. # Wait for line end or EOF in read pipe and process it
  13. data = pipe_rf.readline()
  14. if data:
  15. progress = data.rstrip().split('/')
  16. percent = math.floor(int(progress[0]) / int(progress[1]) * 100)
  17. # If the apk command didn't finish with returncode 0, raise an exception
  18. if p.returncode:
  19. raise subprocess.CalledProcessError(p.returncode, p.args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement