From cadad5b73356d243bb738a9d4a27186b1a755260 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Wed, 23 Oct 2019 14:08:54 +0200 Subject: [PATCH] Refactor wo_download --- wo/cli/plugins/update.py | 2 ++ wo/core/download.py | 25 +++++++------------------ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/wo/cli/plugins/update.py b/wo/cli/plugins/update.py index 28f8287..f386c29 100644 --- a/wo/cli/plugins/update.py +++ b/wo/cli/plugins/update.py @@ -50,6 +50,8 @@ class WOUpdateController(CementBaseController): if pargs.preserve: install_args = install_args + "--preserve " + if not os.path.isdir('/var/lib/wo/tmp'): + os.makedirs('/var/lib/wo/tmp') WODownload.download(self, [["https://raw.githubusercontent.com/" "WordOps/WordOps/{0}/install" .format(wo_branch), diff --git a/wo/core/download.py b/wo/core/download.py index 53abbfd..38d0ede 100644 --- a/wo/core/download.py +++ b/wo/core/download.py @@ -1,7 +1,6 @@ """WordOps download core classes.""" import os -import urllib.error -import urllib.request +from requests import get, RequestException from wo.core.logging import Log @@ -23,25 +22,15 @@ class WODownload(): if not os.path.exists(directory): os.makedirs(directory) Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ') - req = urllib.request.Request( - url, headers={'User-Agent': 'Mozilla/5.0'}) - with urllib.request.urlopen(req) as response, open(filename, 'wb') as out_file: - out_file.write(response.read()) + with open(filename, "wb") as out_file: + req = get(url, timeout=15) + if req.encoding is None: + req.encoding = 'utf-8' + out_file.write(req.content) Log.info(self, "{0}".format("[" + Log.ENDC + "Done" + Log.OKBLUE + "]")) - except urllib.error.URLError as e: + except RequestException as e: Log.debug(self, "[{err}]".format(err=str(e.reason))) Log.error(self, "Unable to download file, {0}" .format(filename)) return False - except urllib.HTTPError.error as e: - Log.error(self, "Package download failed. {0}" - .format(pkg_name)) - Log.debug(self, "[{err}]".format(err=str(e.reason))) - return False - except urllib.ContentTooShortError.error as e: - Log.debug(self, "{0}{1}".format(e.errno, e.strerror)) - Log.error(self, "Package download failed. The amount of the" - " downloaded data is less than " - "the expected amount \{0} ".format(pkg_name)) - return False