From ba087236dd4bf4cf0a95ab5836ec1cdc7fbd16a8 Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Aug 08 2017 16:26:22 +0000 Subject: cli: using PROGRESSFUNCTION before libcurl 7.32.0 in download_file --- diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py index 58f0340..4b4a504 100644 --- a/cli/koji_cli/lib.py +++ b/cli/koji_cli/lib.py @@ -487,8 +487,15 @@ def download_file(url, relpath, quiet=False, noprogress=False, size=None, num=No c.setopt(pycurl.FOLLOWLOCATION, 1) c.setopt(c.WRITEDATA, open(relpath, 'wb')) if not (quiet or noprogress): - c.setopt(c.NOPROGRESS, False) - c.setopt(c.XFERINFOFUNCTION, _download_progress) + proc_func_param = getattr(c, 'XFERINFOFUNCTION', None) + if proc_func_param is None: + proc_func_param = getattr(c, 'PROGRESSFUNCTION', None) + if proc_func_param is not None: + c.setopt(c.NOPROGRESS, False) + c.setopt(proc_func_param, _download_progress) + else: + c.close() + error(_('Error: XFERINFOFUNCTION and PROGRESSFUNCTION are not supported by pyCurl. Quit download progress')) c.perform() c.close() if not (quiet or noprogress): diff --git a/tests/test_cli/test_download_file.py b/tests/test_cli/test_download_file.py index 8b8cfdb..e2b5783 100644 --- a/tests/test_cli/test_download_file.py +++ b/tests/test_cli/test_download_file.py @@ -1,5 +1,6 @@ from __future__ import absolute_import import mock +from mock import call import six import shutil import tempfile @@ -15,6 +16,8 @@ class TestDownloadFile(unittest.TestCase): def reset_mock(self): self.stdout.seek(0) self.stdout.truncate() + self.stderr.seek(0) + self.stderr.truncate() # self.curl.reset_mock() self.curlClass.reset_mock() @@ -22,6 +25,7 @@ class TestDownloadFile(unittest.TestCase): self.tempdir = tempfile.mkdtemp() self.filename = self.tempdir + "/filename" self.stdout = mock.patch('sys.stdout', new_callable=six.StringIO).start() + self.stderr = mock.patch('sys.stderr', new_callable=six.StringIO).start() self.curlClass = mock.patch('pycurl.Curl', create=True).start() self.curl = self.curlClass.return_value @@ -83,6 +87,29 @@ class TestDownloadFile(unittest.TestCase): self.assertMultiLineEqual(actual, expected) self.assertEqual(self.curl.setopt.call_count, 3) + def test_handle_download_file_curl_version(self): + self.curl.XFERINFOFUNCTION = None + download_file("http://url", self.filename, quiet=False, noprogress=False) + actual = self.stdout.getvalue() + expected = 'Downloading: %s\n\n' % self.filename + self.assertMultiLineEqual(actual, expected) + self.assertEqual(self.curl.setopt.call_count, 5) + self.curl.setopt.assert_has_calls([call(self.curl.PROGRESSFUNCTION, _download_progress)]) + + self.reset_mock() + self.curl.PROGRESSFUNCTION = None + with self.assertRaises(SystemExit) as cm: + download_file("http://url", self.filename, quiet=False, noprogress=False) + actual = self.stdout.getvalue() + expected = 'Downloading: %s\n' % self.filename + self.assertMultiLineEqual(actual, expected) + actual = self.stderr.getvalue() + expected = 'Error: XFERINFOFUNCTION and PROGRESSFUNCTION are not supported by pyCurl. Quit download progress\n' + self.assertMultiLineEqual(actual, expected) + self.assertEqual(self.curl.setopt.call_count, 3) + self.assertEqual(cm.exception.code, 1) + + class TestDownloadProgress(unittest.TestCase): # Show long diffs in error output...