cli: add c.close() in download_file and move inner function outside
This commit is contained in:
parent
25c36a8b56
commit
2d7ceb2992
1 changed files with 14 additions and 11 deletions
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue