def copyFile(filePath, fileLocalPath, checksumMode): """Copies file from the given path to local path if the path does not exist yet.""" if os.path.exists(fileLocalPath): logging.debug("Artifact already copy: " + filePath) else: if os.path.exists(filePath): shutil.copyfile(filePath, fileLocalPath) if checksumMode in (_ChecksumMode.download, _ChecksumMode.check): if os.path.exists(filePath + ".md5"): shutil.copyfile(filePath + ".md5", fileLocalPath + ".md5") if os.path.exists(filePath + ".sha1"): shutil.copyfile(filePath + ".sha1", fileLocalPath + ".sha1") if checksumMode == _ChecksumMode.check: if not maven_repo_util.checkChecksum(filePath): logging.error('Checksum problem with copy of %s. Exiting', filePath) sys.exit(1) else: logging.warning("Source file not found: %s", filePath)
def download(url, checksumMode, filePath=None): """Download the given url to a local file""" logging.debug('Attempting download: %s', url) if filePath: if os.path.exists(filePath): logging.debug('Local file already exists, skipping: %s', filePath) return localdir = os.path.dirname(filePath) if not os.path.exists(localdir): os.makedirs(localdir) def getFileName(url, openUrl): if 'Content-Disposition' in openUrl.info(): # If the response has Content-Disposition, try to get filename from it cd = dict(map( lambda x: x.strip().split('=') if '=' in x else (x.strip(), ''), openUrl.info()['Content-Disposition'].split(';'))) if 'filename' in cd: filename = cd['filename'].strip("\"'") if filename: return filename # if no filename was found above, parse it out of the final URL. return os.path.basename(urlparse.urlsplit(openUrl.url)[2]) try: retries = 2 checksumsOk = False while retries > 0 and not checksumsOk: httpResponse = urllib2.urlopen(urllib2.Request(url)) if (httpResponse.code == 200): filePath = filePath or getFileName(url, httpResponse) with open(filePath, 'wb') as localfile: shutil.copyfileobj(httpResponse, localfile) if checksumMode in (_ChecksumMode.download, _ChecksumMode.check): logging.debug('Downloading MD5 checksum from %s', url + ".md5") csHttpResponse = urllib2.urlopen(urllib2.Request(url + ".md5")) checksumFilePath = filePath + ".md5" with open(checksumFilePath, 'wb') as localfile: shutil.copyfileobj(csHttpResponse, localfile) if (csHttpResponse.code != 200): logging.warning('Unable to download MD5 checksum, http code: %s', csHttpResponse.code) logging.debug('Downloading SHA1 checksum from %s', url + ".sha1") csHttpResponse = urllib2.urlopen(urllib2.Request(url + ".sha1")) checksumFilePath = filePath + ".sha1" with open(checksumFilePath, 'wb') as localfile: shutil.copyfileobj(csHttpResponse, localfile) if (csHttpResponse.code != 200): logging.warning('Unable to download SHA1 checksum, http code: %s', csHttpResponse.code) if checksumMode == _ChecksumMode.check: if maven_repo_util.checkChecksum(filePath): checksumsOk = True else: checksumsOk = True if checksumsOk: logging.debug('Download complete') elif retries > 0: logging.warning('Checksum problem with %s, retrying download...', url) retries -= 1 else: logging.error('Checksum problem with %s. No chance to download the file correctly. Exiting', url) sys.exit(1) else: logging.warning('Unable to download, http code: %s', httpResponse.code) httpResponse.close() return httpResponse.code except urllib2.HTTPError as e: logging.debug('Unable to download, HTTP Response code = %s', e.code) return e.code except urllib2.URLError as e: logging.error('Unable to download, URLError: %s', e.reason) except httplib.HTTPException as e: logging.exception('Unable to download, HTTPException: %s', e.message) except ValueError as e: logging.error('ValueError: %s', e.message)