cli: add c.close() in download_file and move inner function outside

This commit is contained in:
Yuming Zhu 2017-07-01 07:17:49 +00:00 committed by Mike McLean
parent 25c36a8b56
commit 2d7ceb2992

View file

@ -473,16 +473,6 @@ def linked_upload(localfile, path, name=None):
def download_file(url, relpath, quiet=False, noprogress=False, size=None, num=None):
"""Download files from remote"""
def _progress(download_t, download_d, upload_t, upload_d):
if download_t == 0:
percent_done = 0.0
else:
percent_done = float(download_d) / float(download_t)
percent_done_str = "%02d%%" % (percent_done * 100)
data_done = _format_size(download_d)
sys.stdout.write("[% -36s] % 4s % 10s\r" % ('=' * (int(percent_done * 36)), percent_done_str, data_done))
sys.stdout.flush()
if '/' in relpath:
koji.ensuredir(os.path.dirname(relpath))
@ -498,12 +488,25 @@ def download_file(url, relpath, quiet=False, noprogress=False, size=None, num=No
c.setopt(c.WRITEDATA, open(relpath, 'wb'))
if not (quiet or noprogress):
c.setopt(c.NOPROGRESS, False)
c.setopt(c.XFERINFOFUNCTION, _progress)
c.setopt(c.XFERINFOFUNCTION, _download_progress)
c.perform()
c.close()
if not quiet:
print('')
def _download_progress(download_t, download_d, upload_t, upload_d):
if download_t == 0:
percent_done = 0.0
else:
percent_done = float(download_d) / float(download_t)
percent_done_str = "%02d%%" % (percent_done * 100)
data_done = _format_size(download_d)
sys.stdout.write("[% -36s] % 4s % 10s\r" % ('=' * (int(percent_done * 36)), percent_done_str, data_done))
sys.stdout.flush()
def error(msg=None, code=1):
if msg:
sys.stderr.write(msg + "\n")