Add error handler for http connection

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura
2019-03-15 08:29:12 +09:00
parent 4c1bde3d7a
commit 4f830a92bb
3 changed files with 66 additions and 41 deletions

View File

@@ -20,8 +20,11 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import xml.etree.ElementTree as ElementTree
import logging
import requests
import traceback
import xml.etree.ElementTree as ElementTree
from six import StringIO
class QtPackage:
@@ -65,32 +68,40 @@ class QtArchives:
# Get packages index
update_xml_url = "{0}Updates.xml".format(archive_url)
r = requests.get(update_xml_url)
self.update_xml = ElementTree.fromstring(r.text)
for packageupdate in self.update_xml.iter("PackageUpdate"):
name = packageupdate.find("Name").text
if name.split(".")[-1] != arch:
continue
if name.split(".")[-2] == "debug_info":
continue
if packageupdate.find("DownloadableArchives").text is None:
continue
if name == "qt.qt5.{}.{}".format(qt_ver_num, arch) or name == "qt.{}.{}".format(qt_ver_num, arch):
# basic packages
pass
else:
# optional packages: FIXME: check option whether install or not
pass
downloadable_archives = packageupdate.find("DownloadableArchives").text.split(", ")
full_version = packageupdate.find("Version").text
package_desc = packageupdate.find("Description").text
for archive in downloadable_archives:
package_url = archive_url + name + "/" + full_version + archive
self.archives.append(QtPackage(name, package_url, archive, package_desc))
try:
r = requests.get(update_xml_url)
except requests.exceptions.ConnectionError as e:
print("Caught download error: %s" % e.args)
exc_buffer = StringIO()
traceback.print_exc(file=exc_buffer)
logging.error('Download error:\n%s', exc_buffer.getvalue())
raise e
else:
self.update_xml = ElementTree.fromstring(r.text)
for packageupdate in self.update_xml.iter("PackageUpdate"):
name = packageupdate.find("Name").text
if name.split(".")[-1] != arch:
continue
if name.split(".")[-2] == "debug_info":
continue
if packageupdate.find("DownloadableArchives").text is None:
continue
if name == "qt.qt5.{}.{}".format(qt_ver_num, arch) or name == "qt.{}.{}".format(qt_ver_num, arch):
# basic packages
pass
else:
# optional packages: FIXME: check option whether install or not
pass
downloadable_archives = packageupdate.find("DownloadableArchives").text.split(", ")
full_version = packageupdate.find("Version").text
package_desc = packageupdate.find("Description").text
for archive in downloadable_archives:
package_url = archive_url + name + "/" + full_version + archive
self.archives.append(QtPackage(name, package_url, archive, package_desc))
if len(self.archives) == 0:
print("Error while parsing package information!")
exit(1)
if len(self.archives) == 0:
print("Error while parsing package information!")
exit(1)
def get_archives(self):
return self.archives

View File

@@ -20,17 +20,19 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import logging
import os
import platform
import sys
from multiprocessing.dummy import Pool
import traceback
import requests
from six import StringIO
from multiprocessing.dummy import Pool
if sys.version_info.major == 3:
from subprocess import run
else:
from subprocess import call as run
NUM_PROCESS = 3
@@ -44,17 +46,25 @@ class QtInstaller:
url = package.get_url()
sys.stdout.write("\033[K")
print("-Downloading {}...".format(url))
r = requests.get(url, stream=True)
with open(archive, 'wb') as fd:
for chunk in r.iter_content(chunk_size=8196):
fd.write(chunk)
sys.stdout.write("\033[K")
print("-Extracting {}...".format(archive))
if platform.system() == 'Windows':
run([r'C:\Program Files\7-Zip\7z.exe', 'x', '-aoa', '-y', archive])
try:
r = requests.get(url, stream=True)
except requests.exceptions.ConnectionError as e:
print("Caught download error: %s" % e.args)
exc_buffer = StringIO()
traceback.print_exc(file=exc_buffer)
logging.error('Uncaught exception in worker process:\n%s', exc_buffer.getvalue())
raise e
else:
run([r'7zr', 'x', '-aoa', '-y', archive])
os.unlink(archive)
with open(archive, 'wb') as fd:
for chunk in r.iter_content(chunk_size=8196):
fd.write(chunk)
sys.stdout.write("\033[K")
print("-Extracting {}...".format(archive))
if platform.system() == 'Windows':
run([r'C:\Program Files\7-Zip\7z.exe', 'x', '-aoa', '-y', archive])
else:
run([r'7zr', 'x', '-aoa', '-y', archive])
os.unlink(archive)
@staticmethod
def get_base_dir(qt_version):
@@ -92,5 +102,9 @@ class QtInstaller:
if 'QT_EDITION' in line:
line = 'QT_EDITION = OpenSource'
f.write(line)
except IOError:
pass
except IOError as e:
print("Configuration file generation error: %s" % e.args)
exc_buffer = StringIO()
traceback.print_exc(file=exc_buffer)
logging.error('Error happened when writing configuration files:\n%s', exc_buffer.getvalue())
raise e

View File

@@ -20,7 +20,7 @@ setup(name='aqtinstall',
author='Hioshi Miura',
author_email='miurahr@linux.com',
packages=["aqt"],
install_requires=['requests'],
install_requires=['requests', 'six'],
extras_require={
'dev': [
'pytest',