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

View File

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