From f5c65d4b605ee467a3464e62c3b743d32e2ef199 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 9 Sep 2013 16:13:32 +0800 Subject: [PATCH] Display progress when downloading Selenium --- lib/commands/behat.py | 9 +++++++-- lib/tools.py | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/commands/behat.py b/lib/commands/behat.py index 7d759b4..e76fcd9 100644 --- a/lib/commands/behat.py +++ b/lib/commands/behat.py @@ -29,7 +29,7 @@ import logging import gzip from time import sleep from lib.command import Command -from lib.tools import process, ProcessInThread +from lib.tools import process, ProcessInThread, downloadProcessHook class BehatCommand(Command): @@ -170,7 +170,12 @@ class BehatCommand(Command): selenium = re.search(r'http:[a-z0-9/._-]+selenium-server-standalone-[0-9.]+\.jar', content, re.I) if selenium: logging.info('Downloading Selenium from %s' % (selenium.group(0))) - urllib.urlretrieve(selenium.group(0), seleniumPath) + if (logging.getLogger().level <= logging.INFO): + urllib.urlretrieve(selenium.group(0), seleniumPath, downloadProcessHook) + # Force a new line after the hook display + logging.info('') + else: + urllib.urlretrieve(selenium.group(0), seleniumPath) else: logging.warning('Could not locate Selenium server to download') diff --git a/lib/tools.py b/lib/tools.py index 37e5e2b..3d85ab8 100644 --- a/lib/tools.py +++ b/lib/tools.py @@ -22,6 +22,7 @@ along with this program. If not, see . http://github.com/FMCorz/mdk """ +import sys import os import signal import subprocess @@ -111,6 +112,14 @@ def process(cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE): return (proc.returncode, out, err) +def downloadProcessHook(count, size, total): + """Hook to report the downloading a file using urllib.urlretrieve""" + downloaded = int((count * size) / (1024)) + total = int(total / (1024)) if total != 0 else '?' + sys.stderr.write("\r %sKB / %sKB" % (downloaded, total)) + sys.stderr.flush() + + def stableBranch(version): if version == 'master': return 'master' -- 2.11.0