diff --git a/external_libraries.py b/external_libraries.py index e49a146f1..9f0849527 100644 --- a/external_libraries.py +++ b/external_libraries.py @@ -1,10 +1,17 @@ +import logging +import logging.config import urllib2 import re import os from _init import STATIC_FONTS_DIR, STATIC_LDN_DIR +from util.log import logfile_path + + LOCAL_PATH = '/static/ldn/' +MAX_RETRY_COUNT = 3 + EXTERNAL_JS = [ 'code.jquery.com/jquery.js', 'netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js', @@ -56,6 +63,9 @@ EXTERNAL_CSS_FONTS = [ ] +logger = logging.getLogger(__name__) + + def get_external_javascript(local=False): if local: return [LOCAL_PATH + format_local_name(src) for src in EXTERNAL_JS] @@ -82,11 +92,24 @@ def format_local_name(url): return filename +def _download_url(url): + for index in range(0, MAX_RETRY_COUNT): + try: + response = urllib2.urlopen(url) + return response.read() + except urllib2.URLError: + logger.exception('Got exception when trying to download URL %s (try #%s)', url, index + 1) + + raise Exception('Aborted due to maximum retries reached') + + if __name__ == '__main__': + logging.config.fileConfig(logfile_path(debug=False), disable_existing_loggers=False) + + for url in EXTERNAL_JS + EXTERNAL_CSS: print 'Downloading %s' % url - response = urllib2.urlopen('https://' + url) - contents = response.read() + contents = _download_url('https://' + url) filename = format_local_name(url) print 'Writing %s' % filename @@ -95,16 +118,16 @@ if __name__ == '__main__': for url in EXTERNAL_CSS_FONTS: print 'Downloading %s' % url - response = urllib2.urlopen('https://' + url) + contents = _download_url('https://' + url) filename = os.path.basename(url).split('?')[0] with open(STATIC_LDN_DIR + filename, "wb") as local_file: - local_file.write(response.read()) + local_file.write(contents) for url in EXTERNAL_FONTS: print 'Downloading %s' % url - response = urllib2.urlopen('https://' + url) + contents = _download_url('https://' + url) filename = os.path.basename(url).split('?')[0] with open(STATIC_FONTS_DIR + filename, "wb") as local_file: - local_file.write(response.read()) + local_file.write(contents)