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 _hardenedCopyFile(self, sSrc, sDst, iMode): """ Copies a file. """ reporter.log('_hardenedCopyFile: %s -> %s (mode: %o)' % (sSrc, sDst, iMode,)); if utils.getHostOs() in [ 'win', 'os2' ]: utils.copyFileSimple(sSrc, sDst); os.chmod(sDst, iMode); else: fRc = self._sudoExecuteSync(['/bin/cp', sSrc, sDst]); if fRc is not True: raise Exception('Failed to copy "%s" to "%s".' % (sSrc, sDst,)); fRc = self._sudoExecuteSync(['/bin/chmod', '%o' % (iMode,), sDst]); if fRc is not True: raise Exception('Failed to chmod "%s".' % (sDst,)); return True;
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;
oSrc = urllib_urlopen(sUrlFile); else: oSrc = urllib_urlopen(sUrlFile, proxies = dict()); oDst = utils.openNoInherit(sDstFile, 'wb'); oDst.write(oSrc.read()); oDst.close(); oSrc.close(); except Exception, 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, oXcpt: fnError('Error copying "%s" to "%s": %s' % (sSrcPath, sDstFile, oXcpt)); return False; return True; # # Unit testing. # # pylint: disable=C0111 class CommonUtilsTestCase(unittest.TestCase): def testHasSchema(self):