def test_decompress(self): """We decompress a valid .tgz file successfully""" filepath = os.path.join(self.compressfiles_dir, "valid.tgz") self.tempdir = tempfile.mkdtemp() Decompressor( { open(filepath, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir=None) }, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEquals(len(results), 1, str(results)) for fd in results: self.assertIsNone(results[fd].error) self.assertTrue( os.path.isdir(os.path.join(self.tempdir, 'server-content'))) self.assertTrue( os.path.isfile( os.path.join(self.tempdir, 'server-content', 'simplefile'))) self.assertTrue( os.path.isdir( os.path.join(self.tempdir, 'server-content', 'subdir'))) self.assertTrue( os.path.isfile( os.path.join(self.tempdir, 'server-content', 'subdir', 'otherfile')))
def decompress_and_install(self, fds): UI.display(DisplayMessage("Installing {}".format(self.name))) # empty destination directory if reinstall for dir_to_remove in self._paths_to_clean: with suppress(FileNotFoundError): shutil.rmtree(dir_to_remove) # marked them as cleaned self._paths_to_clean = [] os.makedirs(self.install_path, exist_ok=True) decompress_fds = {} for fd in fds: direct_copy = False for ext in self.DIRECT_COPY_EXT: if fd.name.endswith(ext): direct_copy = True break if direct_copy: shutil.copy2( fd.name, os.path.join(self.install_path, os.path.basename(fd.name))) else: decompress_fds[fd] = Decompressor.DecompressOrder( dir=self.dir_to_decompress_in_tarball, dest=self.install_path) Decompressor(decompress_fds, self.decompress_and_install_done) UI.display(UnknownProgress(self.iterate_until_install_done))
def test_decompress_file_with_archive(self): """We decompress a .sh file containing an archive successfully""" filepath = os.path.join(self.compressfiles_dir, "script_with_archive.sh") with open(filepath, 'rb') as fd: for line in fd: if line.startswith(b"== ARCHIVE TAG =="): break Decompressor( { fd: Decompressor.DecompressOrder(dest=self.tempdir, dir=None) }, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEqual(len(results), 1, str(results)) for fd in results: self.assertIsNone(results[fd].error) self.assertTrue( os.path.isdir(os.path.join(self.tempdir, 'server-content'))) self.assertTrue( os.path.isfile( os.path.join(self.tempdir, 'server-content', 'simplefile'))) self.assertTrue( os.path.isdir( os.path.join(self.tempdir, 'server-content', 'subdir'))) self.assertTrue( os.path.isfile( os.path.join(self.tempdir, 'server-content', 'subdir', 'otherfile')))
def test_decompress_zip_good_permission(self): """We decompress a valid zip file successfully, retaining the right permissions""" filepath = os.path.join(self.compressfiles_dir, "valid.zip") self.tempdir = tempfile.mkdtemp() Decompressor( { open(filepath, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir=None) }, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEquals(len(results), 1, str(results)) for fd in results: self.assertIsNone(results[fd].error) self.assertTrue( os.path.isdir(os.path.join(self.tempdir, 'server-content'))) simplefile = os.path.join(self.tempdir, 'server-content', 'simplefile') self.assertTrue(os.path.isfile(simplefile)) execfile = os.path.join(self.tempdir, 'server-content', 'executablefile') self.assertTrue(os.path.isfile(execfile)) self.assertEquals(oct(stat.S_IMODE(os.lstat(simplefile).st_mode)), '0o664') self.assertEquals(oct(stat.S_IMODE(os.lstat(execfile).st_mode)), '0o775')
def decompress_and_install(self, fd): UI.display(DisplayMessage("Installing {}".format(self.name))) # empty destination directory if reinstall for dir_to_remove in self._paths_to_clean: with suppress(FileNotFoundError): shutil.rmtree(dir_to_remove) Decompressor({fd: Decompressor.DecompressOrder(dir=self.dir_to_decompress_in_tarball, dest=self.install_path)}, self.decompress_and_install_done) UI.display(UnknownProgress(self.iterate_until_install_done))
def test_decompress_invalid_file(self): """We return an error if the compressed file is invalid""" self.expect_warn_error = True filepath = os.path.join(self.compressfiles_dir, "invalid.tgz") Decompressor({open(filepath, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir=None)}, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEqual(len(results), 1, str(results)) for fd in results: self.assertIsNotNone(results[fd].error)
def test_decompress_wrong_dir_content(self): """We decompress a valid file, but the selected subdir isn't valid""" self.expect_warn_error = True filepath = os.path.join(self.compressfiles_dir, "valid.tgz") Decompressor({open(filepath, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir='doesnt-exists')}, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEqual(len(results), 1, str(results)) for fd in results: self.assertIsNotNone(results[fd].error)
def test_decompress_exec(self): """We decompress a valid executable file successfully""" filepath = os.path.join(self.compressfiles_dir, "simple.bin") Decompressor({open(filepath, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir=None)}, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEqual(len(results), 1, str(results)) for fd in results: self.assertIsNone(results[fd].error) self.assertTrue(os.path.isdir(os.path.join(self.tempdir, 'android-ndk-foo'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'android-ndk-foo', 'ndk-which'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'android-ndk-foo', 'ndk-build')))
def test_decompress_move_dir_content(self): """We decompress a valid file decompressing one subdir content (other files in root are kept in place)""" filepath = os.path.join(self.compressfiles_dir, "valid.tgz") Decompressor({open(filepath, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir='server-content')}, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEqual(len(results), 1, str(results)) for fd in results: self.assertIsNone(results[fd].error) self.assertTrue(os.path.isdir(self.tempdir)) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'simplefile'))) self.assertTrue(os.path.isdir(os.path.join(self.tempdir, 'subdir'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'subdir', 'otherfile')))
def test_decompress_move_dir_content_keep_existing_files(self): """We decompress a valid file changing dir in a directory which already have some content. This one is left.""" filepath = os.path.join(self.compressfiles_dir, "valid.tgz") open(os.path.join(self.tempdir, "foo"), 'w').write('') Decompressor({open(filepath, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir='server-content')}, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEqual(len(results), 1, str(results)) for fd in results: self.assertIsNone(results[fd].error) self.assertTrue(os.path.isdir(self.tempdir)) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'simplefile'))) self.assertTrue(os.path.isdir(os.path.join(self.tempdir, 'subdir'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'subdir', 'otherfile'))) # the original file is there here self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'foo')))
def test_decompress_multiple_with_dir(self): """We decompress multiple valid .tgz file successfully with different dir to extract""" filepath1 = os.path.join(self.compressfiles_dir, "valid.tgz") filepath2 = os.path.join(self.compressfiles_dir, "valid2.tgz") Decompressor({open(filepath1, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir='server-content'), open(filepath2, 'rb'): Decompressor.DecompressOrder(dest=self.tempdir, dir='server-content2')}, self.on_done) self.wait_for_callback(self.on_done) results = self.on_done.call_args[0][0] self.assertEqual(len(results), 2, str(results)) for fd in results: self.assertIsNone(results[fd].error) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'simplefile'))) self.assertTrue(os.path.isdir(os.path.join(self.tempdir, 'subdir'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'subdir', 'otherfile'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'simplefile2'))) self.assertTrue(os.path.isdir(os.path.join(self.tempdir, 'subdir2'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'subdir2', 'otherfile'))) self.assertEqual(self.on_done.call_count, 1, "Global done callback is only called once")