Exemple #1
0
 def DownloadFileFromURLToPath(cls, url_address, file_path):
     request = comp_urlreq.Request(url_address)
     request.add_header("User-Agent", AOSD_VERSION)
     if config.getVerboseLogging() == True:
         logging_helper.getLogger().info('Starting download from  "'+url_address+'" -> "'+file_path+'"...')
     response = None
     try:
         response = comp_urlreq.urlopen(request)
     except comp_urlerr.HTTPError as e:
         logging_helper.getLogger().error('HTTPError = '+str(e.code)+' on '+url_address)
         response = None
     except comp_urlerr.URLError as e:
         logging_helper.getLogger().error('URLError = '+ str(e.reason)+' on '+url_address)
         response = None
     except compat_http.HTTPException as e:
         logging_helper.getLogger().error('HTTPException on '+url_address)
         response = None
     except Exception:
         logging_helper.getLogger().error(':Exception :( on '+url_address)
         response = None
     if response != None:
         output = open(file_path, 'wb')
         output.write(response.read())
         output.close()
         if config.getVerboseLogging() == True:
             logging_helper.getLogger().info('Download Complete!')
         return True
     else:
         return False
Exemple #2
0
    def DownloadPackageTarball(cls, release_type, package_name, build_number, \
            release_version = '', dest_dir = '', check_hash = True):
        
        downloaded_directory_path = ''
        tarball_address = cls.CreateTarballURL(release_type, package_name, build_number)
        package_file_name = os.path.basename(tarball_address)

        if dest_dir != '':
            output_directory = dest_dir
        else:
            output_directory = os.path.expanduser(config.getDownloadDir())

        if release_version != '':
            output_directory = output_directory + '/' + release_version
        
        try:
            os.makedirs(output_directory)
        except OSError:
            pass

        output_file = os.path.join(output_directory, package_file_name)
        download_successful = cls.DownloadFileFromURLToPath(tarball_address, output_file)
        tar_name = os.path.splitext(package_file_name)[0]
        file_name = os.path.splitext(tar_name)[0]
        if check_hash == True:
            logging_helper.getLogger().info('Downloaded "'+file_name+'" to "'+output_file+'"')
            # check the downloaded file against the stored hash
            hash_result = Hashes.ValidateDownloadedFileByHash(output_file, release_type, package_name, build_number, False)
            if hash_result[0] == True:
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Decompressing "'+output_file+'" -> "'+tar_name+'"...')
                gz_archive = gzip.open(output_file, 'rb')
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Reading file contents...')
                file_content = gz_archive.read()
                tar_path = os.path.join(output_directory, tar_name)
                dump_tar = open(tar_path, 'wb')
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Dumping tar file...')
                dump_tar.write(file_content)
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Closing files...')
                dump_tar.close()
                gz_archive.close()
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Removing archive...')
                os.remove(output_file)
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Opening tar file...')
                tar_archive = tarfile.open(tar_path)
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Decompressing tar file...')
                tar_archive.extractall(output_directory)
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Closing tar file...')
                tar_archive.close()
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Removing tar file...')
                os.remove(tar_path)
                if config.getVerboseLogging() == True:
                    logging_helper.getLogger().info('Decompression Complete!')
                logging_helper.getLogger().info('The package "'+file_name+'" has been downloaded to "'+output_directory+'".')
                downloaded_directory_path = os.path.join(output_directory, file_name)
            else:
                logging_helper.getLogger().info('The package "'+file_name+'" has hash of "'+hash_result[1]+'" which doesn\'t match the hash on record ('+hash_result[2]+')')
        else:
            if download_successful == True:
                downloaded_directory_path = output_file
        return downloaded_directory_path