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 initialize(self): from ezored.models.logger import Logger from ezored.models.constants import Constants from ezored.models.util.file_util import FileUtil Logger.i('Cleaning...') FileUtil.remove_dir(Constants.TEMP_DIR) FileUtil.remove_dir(Constants.VENDOR_DIR) Logger.i('Finished')
def test_remove_dir(self, d): os.chdir(d.path) dir_name_1 = 'new-dir-1' dir_name_2 = 'new-dir-2' FileUtil.create_dir(dir_name_1) FileUtil.create_dir(os.path.join(dir_name_1, dir_name_2)) FileUtil.remove_dir(dir_name_1) self.assertFalse(os.path.isdir(dir_name_1)) self.assertFalse(os.path.isdir(os.path.join(dir_name_1, dir_name_2)))
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 download_from_git(self): # download Logger.i('Downloading repository: {0}...'.format(self.get_name())) force_download = False rep_path, rep_type, rep_version = self.get_git_data() download_filename = self.get_download_filename() download_dest_dir = Constants.TEMP_DIR download_dest_path = os.path.join(download_dest_dir, download_filename) downloaded_version = GitUtil.get_current_downloaded_repository_version( download_dest_path) if rep_type == Constants.GIT_TYPE_BRANCH: force_download = True if downloaded_version is not None: if downloaded_version != rep_version: Logger.i( 'Repository downloaded version ({0}) is different from configured version ({1}), ' 'downloading configured version...'.format( downloaded_version.strip(), rep_version.strip())) force_download = True # skip if exists if not force_download and os.path.isdir(download_dest_path): Logger.i('Repository already downloaded: {0}'.format( self.get_name())) else: FileUtil.remove_dir(download_dest_path) GitUtil.download(rep_path, rep_type, rep_version, download_dest_path) # check if file was downloaded if os.path.isdir(download_dest_path): Logger.i('Repository downloaded: {0}'.format(self.get_name())) else: Logger.f('Problems when download repository: {0}'.format( self.get_name()))
def remove(self): Logger.d('Removing files for target: {0}...'.format(self.get_name())) vendor_dir = self.repository.get_vendor_dir() FileUtil.remove_dir(vendor_dir)