Refactor wo_download

This commit is contained in:
VirtuBox
2019-10-23 14:08:54 +02:00
parent e84399540b
commit cadad5b733
2 changed files with 9 additions and 18 deletions

View File

@@ -50,6 +50,8 @@ class WOUpdateController(CementBaseController):
if pargs.preserve: if pargs.preserve:
install_args = install_args + "--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/" WODownload.download(self, [["https://raw.githubusercontent.com/"
"WordOps/WordOps/{0}/install" "WordOps/WordOps/{0}/install"
.format(wo_branch), .format(wo_branch),

View File

@@ -1,7 +1,6 @@
"""WordOps download core classes.""" """WordOps download core classes."""
import os import os
import urllib.error from requests import get, RequestException
import urllib.request
from wo.core.logging import Log from wo.core.logging import Log
@@ -23,25 +22,15 @@ class WODownload():
if not os.path.exists(directory): if not os.path.exists(directory):
os.makedirs(directory) os.makedirs(directory)
Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ') Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ')
req = urllib.request.Request( with open(filename, "wb") as out_file:
url, headers={'User-Agent': 'Mozilla/5.0'}) req = get(url, timeout=15)
with urllib.request.urlopen(req) as response, open(filename, 'wb') as out_file: if req.encoding is None:
out_file.write(response.read()) req.encoding = 'utf-8'
out_file.write(req.content)
Log.info(self, "{0}".format("[" + Log.ENDC + "Done" Log.info(self, "{0}".format("[" + Log.ENDC + "Done"
+ Log.OKBLUE + "]")) + Log.OKBLUE + "]"))
except urllib.error.URLError as e: except RequestException as e:
Log.debug(self, "[{err}]".format(err=str(e.reason))) Log.debug(self, "[{err}]".format(err=str(e.reason)))
Log.error(self, "Unable to download file, {0}" Log.error(self, "Unable to download file, {0}"
.format(filename)) .format(filename))
return False 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