def downloadFile(sUrlFile, sDstFile, sLocalPrefix, fnLog, fnError=None, fNoProxies=True): """ Downloads the given file if an URL is given, otherwise assume it's something on the build share and copy it from there. Raises no exceptions, returns log + success indicator instead. Note! This method may use proxies configured on the system and the http_proxy, ftp_proxy, no_proxy environment variables. """ if fnError is None: fnError = fnLog if sUrlFile.startswith('http://') \ or sUrlFile.startswith('https://') \ or sUrlFile.startswith('ftp://'): # Download the file. fnLog('Downloading "%s" to "%s"...' % (sUrlFile, sDstFile)) try: ## @todo We get 404.html content instead of exceptions here, which is confusing and should be addressed. if not fNoProxies: oOpener = urllib_build_opener() else: oOpener = urllib_build_opener( urllib_ProxyHandler(proxies=dict())) oSrc = oOpener.open(sUrlFile) oDst = utils.openNoInherit(sDstFile, 'wb') oDst.write(oSrc.read()) oDst.close() oSrc.close() except Exception as oXcpt: fnError('Error downloading "%s" to "%s": %s' % (sUrlFile, sDstFile, oXcpt)) return False else: # Assumes file from the build share. if sUrlFile.startswith('file:///'): sSrcPath = sUrlFile[7:] elif sUrlFile.startswith('file://'): sSrcPath = sUrlFile[6:] elif os.path.isabs(sUrlFile): sSrcPath = sUrlFile else: sSrcPath = os.path.join(sLocalPrefix, sUrlFile) fnLog('Copying "%s" to "%s"...' % (sSrcPath, sDstFile)) try: utils.copyFileSimple(sSrcPath, sDstFile) except Exception as oXcpt: fnError('Error copying "%s" to "%s": %s' % (sSrcPath, sDstFile, oXcpt)) return False return True
def __get_opener(self): # enable self-signed certificates # https://www.python.org/dev/peps/pep-0476/ # http://bugs.python.org/issue21308 if (hasattr(ssl, "_create_unverified_context") and "context" in inspect.getargspec(urllib_HTTPSHandler.__init__).args): opener = urllib_build_opener( urllib_HTTPSHandler(context=ssl._create_unverified_context()), urllib_HTTPCookieProcessor()) else: opener = urllib_build_opener(urllib_HTTPCookieProcessor()) return opener
def __get_opener(self): # enable self-signed certificates # https://www.python.org/dev/peps/pep-0476/ # http://bugs.python.org/issue21308 if ( hasattr(ssl, "_create_unverified_context") and "context" in inspect.getargspec(urllib_HTTPSHandler.__init__).args ): opener = urllib_build_opener( urllib_HTTPSHandler(context=ssl._create_unverified_context()), urllib_HTTPCookieProcessor() ) else: opener = urllib_build_opener(urllib_HTTPCookieProcessor()) return opener
def downloadFile(sUrlFile, sDstFile, sLocalPrefix, fnLog, fnError = None, fNoProxies=True): """ Downloads the given file if an URL is given, otherwise assume it's something on the build share and copy it from there. Raises no exceptions, returns log + success indicator instead. Note! This method may use proxies configured on the system and the http_proxy, ftp_proxy, no_proxy environment variables. """ if fnError is None: fnError = fnLog; if sUrlFile.startswith('http://') \ or sUrlFile.startswith('https://') \ or sUrlFile.startswith('ftp://'): # Download the file. fnLog('Downloading "%s" to "%s"...' % (sUrlFile, sDstFile)); try: ## @todo We get 404.html content instead of exceptions here, which is confusing and should be addressed. if not fNoProxies: oOpener = urllib_build_opener(); else: oOpener = urllib_build_opener(urllib_ProxyHandler(proxies = dict())); oSrc = oOpener.open(sUrlFile); oDst = utils.openNoInherit(sDstFile, 'wb'); oDst.write(oSrc.read()); oDst.close(); oSrc.close(); except Exception as oXcpt: fnError('Error downloading "%s" to "%s": %s' % (sUrlFile, sDstFile, oXcpt)); return False; else: # Assumes file from the build share. sSrcPath = os.path.join(sLocalPrefix, sUrlFile); fnLog('Copying "%s" to "%s"...' % (sSrcPath, sDstFile)); try: utils.copyFileSimple(sSrcPath, sDstFile); except Exception as oXcpt: fnError('Error copying "%s" to "%s": %s' % (sSrcPath, sDstFile, oXcpt)); return False; return True;