Display progress when downloading Selenium
authorFrederic Massart <fred@moodle.com>
Mon, 9 Sep 2013 08:13:32 +0000 (16:13 +0800)
committerFrederic Massart <fred@moodle.com>
Mon, 9 Sep 2013 08:13:32 +0000 (16:13 +0800)
lib/commands/behat.py
lib/tools.py

index 7d759b4..e76fcd9 100644 (file)
@@ -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')
 
index 37e5e2b..3d85ab8 100644 (file)
@@ -22,6 +22,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 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'