def unzip_plugin(target_file, target_folder): """ Unzip a plugin that is packaged in a zip. """ with ZipFile(target_file, 'r') as zipped: folders = [x for x in zipped.namelist() if x.endswith('/')] jars = [x for x in zipped.namelist() if x.endswith('.jar')] strip_folder = False if len(jars) == 0 and len(folders) > 0: folder = folders[0] # All jars in folder jars = [x for x in zipped.namelist() if x.endswith('.jar') and x.startswith(folder)] strip_folder = folder for jar in jars: destination = target_folder if strip_folder: destination += jar.split(strip_folder, 1)[1] else: destination += jar common.extract_file(zipped, jar, destination)
def test_extract_sub_file(self): """ Test common.extract_file with file in subfolder. """ common.extract_file(self.zipfile, 'herp/bar/baz.txt', self.test_folder + 'herp/bar/baz.txt') assert os.path.isdir(self.test_folder + 'herp') assert os.path.isdir(self.test_folder + 'herp/bar') with open(self.test_folder + 'herp/bar/baz.txt', 'r') as file: assert file.readline() == 'baz\n'
def unzip_plugin(target_file, target_folder): """ Unzip a plugin that is packaged in a zip. """ with ZipFile(target_file, 'r') as zipped: folders = [x for x in zipped.namelist() if x.endswith('/')] jars = [x for x in zipped.namelist() if x.endswith('.jar')] strip_folder = False if len(jars) == 0 and len(folders) > 0: folder = folders[0] # All jars in folder jars = [ x for x in zipped.namelist() if x.endswith('.jar') and x.startswith(folder) ] strip_folder = folder for jar in jars: destination = target_folder if strip_folder: destination += jar.split(strip_folder, 1)[1] else: destination += jar common.extract_file(zipped, jar, destination)
def test_extract_sub_folder(self): """ Test common.extract_file with folder in subfolder. """ common.extract_file(self.zipfile, 'herp/bar/', self.test_folder + 'herp/bar/') assert os.path.isdir(self.test_folder + 'herp') assert os.path.isdir(self.test_folder + 'herp/bar')
def test_extract_base_folder(self): """ Test common.extract_file with folder in base of zip. """ common.extract_file(self.zipfile, 'herp/', self.test_folder + 'herp/') assert os.path.isdir(self.test_folder + 'herp')
def test_extract_base_file(self): """ Test common.extract_file with file in base of zip. """ common.extract_file(self.zipfile, 'foo.txt', self.test_folder + 'foo.txt') with open(self.test_folder + 'foo.txt', 'r') as file: assert file.readline() == 'foo\n'