def download_from_github(self): # download Logger.i('Downloading repository: {0}...'.format(self.get_name())) download_url = self.get_download_url() download_filename = self.get_download_filename() download_dest_dir = Constants.TEMP_DIR download_dest_path = os.path.join(Constants.TEMP_DIR, download_filename) unpacked_dir = self.get_temp_dir() unpack_dir = Constants.TEMP_DIR force_download = False _, git_data_type, git_data_version = self.get_git_data() if git_data_type == Repository.GIT_TYPE_BRANCH: force_download = True # skip if exists if not force_download and os.path.isfile(download_dest_path): Logger.i('Repository already downloaded: {0}'.format( self.get_name())) else: FileUtil.remove_file(download_dest_path) DownloadUtil.download_file(download_url, download_dest_dir, download_filename) # check if file was downloaded if os.path.isfile(download_dest_path): Logger.i('Repository downloaded: {0}'.format(self.get_name())) else: Logger.f('Problems when download repository: {0}'.format( self.get_name())) # unpack Logger.i('Unpacking repository: {0}...'.format(self.get_name())) if not force_download and os.path.isdir(unpacked_dir): Logger.i('Repository already unpacked: {0}...'.format( self.get_name())) else: FileUtil.remove_dir(unpacked_dir) # untar file FileUtil.create_dir(unpack_dir) tar = tarfile.open(download_dest_path) tar.extractall(path=unpack_dir) tar.close() if os.path.isdir(unpacked_dir): Logger.i('Repository unpacked: {0}'.format(self.get_name())) else: Logger.f('Problems when unpack repository: {0}'.format( self.get_name()))
def test_download_file_with_custom_filename(self, d): os.chdir(d.path) download_url = 'https://raw.githubusercontent.com/ezored/ezored/master/extras/images/jetbrains-logo.png' download_filename = 'file.png' DownloadUtil.download_file(download_url, filename=download_filename) self.assertTrue(os.path.isfile(download_filename)) self.assertEqual(os.path.getsize(download_filename), 5627)
def test_download_file_with_auto_create_dest_dir(self, d): os.chdir(d.path) download_url = 'https://raw.githubusercontent.com/ezored/ezored/master/extras/images/jetbrains-logo.png' download_dest = 'download' download_filename = DownloadUtil.get_filename_from_url(download_url) DownloadUtil.download_file(download_url, download_dest) self.assertTrue(os.path.isfile(os.path.join(download_dest, download_filename))) self.assertEqual(os.path.getsize(os.path.join(download_dest, download_filename)), 5627)
def get_download_filename(self): if self.rep_type == Constants.REPOSITORY_TYPE_GIT: return GitUtil.get_repository_name(self.rep_path) elif self.rep_type == Constants.REPOSITORY_TYPE_LOCAL: _, filename = os.path.split(self.rep_path) return slugify(filename) elif self.rep_type == Constants.REPOSITORY_TYPE_ZIP: filename = DownloadUtil.get_filename_from_url(self.rep_path) return filename elif self.rep_type == Constants.REPOSITORY_TYPE_TAR: filename = DownloadUtil.get_filename_from_url(self.rep_path) return filename else: return ''
def download_from_zip(self): # download Logger.i('Downloading repository: {0}...'.format(self.get_name())) download_url = self.get_download_url() download_filename = self.get_download_filename() download_dest_dir = Constants.TEMP_DIR download_dest_path = os.path.join(download_dest_dir, download_filename) unpacked_dir = self.get_temp_dir() unpack_dir = download_dest_dir # skip if exists if os.path.isfile(download_dest_path): Logger.i('Repository already downloaded: {0}'.format( self.get_name())) else: FileUtil.remove_file(download_dest_path) DownloadUtil.download_file(download_url, download_dest_dir, download_filename) # check if file was downloaded if os.path.isfile(download_dest_path): Logger.i('Repository downloaded: {0}'.format(self.get_name())) else: Logger.f('Problems when download repository: {0}'.format( self.get_name())) # unpack Logger.i('Unpacking repository: {0}...'.format(self.get_name())) if os.path.isdir(unpacked_dir): Logger.i('Repository already unpacked: {0}...'.format( self.get_name())) else: FileUtil.remove_dir(unpacked_dir) # unpack file FileUtil.create_dir(unpack_dir) zipref = zipfile.ZipFile(download_dest_path, 'r') zipref.extractall(path=unpack_dir) zipref.close() if os.path.isdir(unpacked_dir): Logger.i('Repository unpacked: {0}'.format(self.get_name())) else: Logger.f('Problems when unpack repository: {0}'.format( self.get_name()))
def get_name(self): if self.rep_type == Constants.REPOSITORY_TYPE_GIT: filename = self.get_download_filename() return slugify(filename) elif self.rep_type == Constants.REPOSITORY_TYPE_LOCAL: rep_path, rep_file = os.path.split(self.rep_path) return slugify(rep_file) elif self.rep_type == Constants.REPOSITORY_TYPE_ZIP: filename = DownloadUtil.get_filename_from_url_without_extension( self.rep_path) return slugify(filename) elif self.rep_type == Constants.REPOSITORY_TYPE_TAR: filename = DownloadUtil.get_filename_from_url_without_extension( self.rep_path) return slugify(filename) else: return ''
def test_get_filename_from_url(self): download_url = 'https://raw.githubusercontent.com/ezored/ezored/master/extras/images/jetbrains-logo.png' download_filename = DownloadUtil.get_filename_from_url(download_url) self.assertEqual(download_filename, 'jetbrains-logo.png')