提交 bcedab22 编写于 作者: W wuzewu

do not print tips in downloader

上级 d15bd206
......@@ -59,11 +59,13 @@ class DownloadCommand(BaseCommand):
self.print_args()
if self.args.uncompress:
default_downloader.download_file_and_uncompress(
result, tips, file = default_downloader.download_file_and_uncompress(
url=url, save_path=self.args.output_path)
else:
default_downloader.download_file(
result, tips, file = default_downloader.download_file(
url=url, save_path=self.args.output_path)
print(tips)
return result
command = DownloadCommand.instance()
......@@ -74,8 +74,10 @@ class HubServer:
def request(self):
file_url = self.server_url + MODULE_LIST_FILE
self.module_list_file = default_downloader.download_file(
result, tips, self.module_list_file = default_downloader.download_file(
file_url, save_path=hub.CACHE_HOME)
if not result:
return False
self.module_list_file = csv_reader.read(self.module_list_file)
return True
......
......@@ -41,7 +41,12 @@ def md5file(fname):
class Downloader:
def download_file(self, url, save_path, save_name=None, retry_limit=3):
def download_file(self,
url,
save_path,
save_name=None,
retry_limit=3,
print_progress=False):
if not os.path.exists(save_path):
utils.mkdir(save_path)
save_name = url.split('/')[-1] if save_name is None else save_name
......@@ -53,11 +58,9 @@ class Downloader:
if retry_times < retry_limit:
retry_times += 1
else:
raise RuntimeError(
"Cannot download {0} within retry limit {1}".format(
url, retry_limit))
logger.info(
"Cache file %s not found, downloading %s" % (file_name, url))
tips = "Cannot download {0} within retry limit {1}".format(
url, retry_limit)
return False, tips, None
r = requests.get(url, stream=True)
total_length = r.headers.get('content-length')
......@@ -72,13 +75,14 @@ class Downloader:
for data in r.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
if print_progress:
done = int(50 * dl / total_length)
sys.stdout.write(
"\r[%s%s]" % ('=' * done, ' ' * (50 - done)))
sys.stdout.flush()
logger.info("file %s download completed!" % (file_name))
return file_name
tips = "file %s download completed!" % (file_name)
return True, tips, file_name
def uncompress(self, file, dirname=None, delete_file=False):
dirname = os.path.dirname(file) if dirname is None else dirname
......@@ -91,7 +95,7 @@ class Downloader:
if delete_file:
os.remove(file)
return module_dir
return True, "file %s uncompress completed!" % file, module_dir
def download_file_and_uncompress(self,
url,
......@@ -99,17 +103,21 @@ class Downloader:
save_name=None,
retry_limit=3,
delete_file=True):
file = self.download_file(
result, tips_1, file = self.download_file(
url=url,
save_path=save_path,
save_name=save_name,
retry_limit=retry_limit)
file = self.uncompress(file, delete_file=delete_file)
if not result:
return result, tips_1, file
result, tips_2, file = self.uncompress(file, delete_file=delete_file)
if not result:
return result, tips_2, file
if save_name:
save_name = os.path.join(save_path, save_name)
shutil.move(file, save_name)
return save_name
return file
return result, "%s\n%s" % (tips_1, tips_2), save_name
return result, "%s\n%s" % (tips_1, tips_2), file
default_downloader = Downloader()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册