
import kindred

import socket
import zipfile
import hashlib
import os
import sys
import wget
import shutil
import six

if sys.version_info >= (3, 0):
	import urllib.request
else:
	import urllib

def _calcSHA256(filename):
	return hashlib.sha256(open(filename, 'rb').read()).hexdigest()

def _isDirEmpty(path):
	files = os.listdir(path)
	return files == []

def _findDir(name, path):
	if os.path.isdir(path):
		for root, dirs, files in os.walk(path):
			if name in dirs:
				return os.path.abspath(os.path.join(root, name))
	return None

def _downloadFiles(files,downloadDirectory):
	oldTimeout = socket.getdefaulttimeout()
	socket.setdefaulttimeout(180)

	if not os.path.isdir(downloadDirectory):
		os.mkdir(downloadDirectory)

	for url,shortName,expectedSHA256 in files:
		downloadedPath = os.path.join(downloadDirectory,shortName)
		
		if os.path.isfile(downloadedPath):
			downloadedSHA256 = _calcSHA256(downloadedPath)
			if not downloadedSHA256 == expectedSHA256:
				os.remove(downloadedPath)

		if not os.path.isfile(downloadedPath):
			wget.download(url,out=downloadedPath,bar=None)
			
			downloadedSHA256 = _calcSHA256(downloadedPath)
			assert downloadedSHA256 == expectedSHA256, "SHA256 mismatch with downloaded file: %s" % shortName
			
			if shortName.endswith('.zip'):
				zip_ref = zipfile.ZipFile(downloadedPath, 'r')
				zip_ref.extractall(path=downloadDirectory)
				zip_ref.close()
	
	socket.setdefaulttimeout(oldTimeout)

@@ -24,16 +24,27 @@
  socket%0A
+import ssl%0A
 import z
@@ -122,16 +122,32 @@
 port six
+%0Aimport requests
 %0A%0Aif sys
@@ -558,16 +558,710 @@
 n None%0A%0A
+# From: https://stackoverflow.com/questions/32763720/timeout-a-file-download-with-python-urllib%0Adef _downloadFile(url,filename,timeout=180):%0A%09# Make the actual request, set the timeout for no data to 10 seconds and enable streaming responses so we don't have to keep the large files in memory%0A%09request = requests.get(url, timeout=10, stream=True)%0A%0A%09# Open the output file and make sure we write in binary mode%0A%09with open(filename, 'wb') as fh:%0A%09%09# Walk through the request response in chunks of 1024 * 1024 bytes, so 1MiB%0A%09%09for chunk in request.iter_content(1024 * 1024):%0A%09%09%09# Write the chunk to the file%0A%09%09%09fh.write(chunk)%0A%09%09%09# Optionally we can check here if the download is taking too long%0A%0A
 def _dow
@@ -1298,16 +1298,17 @@
 tory):%0A%09
+#
 oldTimeo
@@ -1340,17 +1340,25 @@
 eout()%0A%09
-s
+#ssl.SSLS
 ocket.se
@@ -1350,39 +1350,32 @@
 sl.SSLSocket.set
-default
 timeout(180)%0A%0A%09i
@@ -1761,16 +1761,17 @@
 th):%0A%09%09%09
+#
 wget.dow
@@ -1809,16 +1809,53 @@
 r=None)%0A
+%09%09%09_downloadFile(url,downloadedPath)%0A
 %09%09%09%0A%09%09%09d
@@ -2160,16 +2160,17 @@
 se()%0A%09%0A%09
+#
 socket.s

Use custom download code instead of wget