def askCopyUploadImage(self, parent, source_path, server, node_type): """ Ask user for copying the image to the default directory or upload it to remote server. :param parent: Parent window :param path: File path on computer :param server: The server where the images should be located :param node_type: Remote upload endpoint :returns path: Final path """ if (server and server != "local") or Controller.instance().isRemote(): return self._uploadImageToRemoteServer(source_path, server, node_type) else: destination_directory = self.getDirectoryForType(node_type) destination_path = os.path.join(destination_directory, os.path.basename(source_path)) source_filename = os.path.basename(source_path) destination_filename = os.path.basename(destination_path) if os.path.normpath(os.path.dirname(source_path)) != destination_directory: # the image is not in the default images directory if source_filename == destination_filename: # the filename already exists in the default images directory source_image = Image(node_type, source_path, filename=source_filename) destination_image = Image(node_type, destination_path, filename=destination_filename) try: if source_image.md5sum == destination_image.md5sum: # the source and destination images are identical return source_path except OSError as e: QtWidgets.QMessageBox.critical(parent, 'Image', 'Cannot compare image file {} with {}: {}.'.format(source_path, destination_path, str(e))) return source_path # find a new unique path to avoid overwriting existing destination file destination_path = self._getUniqueDestinationPath(source_image, node_type, destination_path) reply = QtWidgets.QMessageBox.question(parent, 'Image', 'Would you like to copy {} to the default images directory'.format(source_filename), QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No) if reply == QtWidgets.QMessageBox.Yes: try: os.makedirs(destination_directory, exist_ok=True) except OSError as e: QtWidgets.QMessageBox.critical(parent, 'Image', 'Could not create destination directory {}: {}'.format(destination_directory, str(e))) return source_path worker = FileCopyWorker(source_path, destination_path) progress_dialog = ProgressDialog(worker, 'Image', 'Copying {}'.format(source_filename), 'Cancel', busy=True, parent=parent) progress_dialog.show() progress_dialog.exec_() errors = progress_dialog.errors() if errors: QtWidgets.QMessageBox.critical(parent, 'Image', '{}'.format(''.join(errors))) return source_path else: source_path = destination_path return source_path
def test_md5sum_ova(tmpdir): """ An OVA can't have a md5sum computed. We can only use the disk cache. """ path = str(tmpdir / "test.ova") os.makedirs(path) image = Image(path) assert image.md5sum is None with open(path + ".md5sum", "w+", encoding="utf-8") as f: f.write("56f46611dfa80d0eead602cbb3f6dcee") image = Image(path) assert image.md5sum == "56f46611dfa80d0eead602cbb3f6dcee"
def test_md5sum_from_file_cache(tmpdir): path = str(tmpdir / "test.img") open(path, "w+").close() with open(path + ".md5sum", "w+", encoding="utf-8") as f: f.write("56f46611dfa80d0eead602cbb3f6dcee") image = Image(path) assert image.md5sum == "56f46611dfa80d0eead602cbb3f6dcee"
def test_copy(images_dir, tmpdir): with open(str(tmpdir / "a.img"), "w+") as f: f.write("a") Image(str(tmpdir / "a.img")).copy(os.path.join(images_dir, "QEMU"), "a.img") assert os.path.exists(os.path.join(images_dir, "QEMU", "a.img")) assert os.path.exists(os.path.join(images_dir, "QEMU", "a.img.md5sum")) with open(os.path.join(images_dir, "QEMU", "a.img.md5sum")) as f: c = f.read() assert c == "0cc175b9c0f1b6a831c399e269772661"
def test_copy_ova(images_dir, tmpdir): tar = tarfile.open(str(tmpdir / "a.ova"), "w:gz") string = io.StringIO("a") info = tarfile.TarInfo(name="a.vmdk") info.size = 0 tar.addfile(tarinfo=info, fileobj=string) tar.close() Image(str(tmpdir / "a.ova")).copy(os.path.join(images_dir, "QEMU"), "a.ova") assert os.path.exists(os.path.join(images_dir, "QEMU", "a.ova", "a.vmdk")) assert os.path.exists(os.path.join(images_dir, "QEMU", "a.ova.md5sum")) with open(os.path.join(images_dir, "QEMU", "a.ova.md5sum")) as f: assert len(f.read()) == 32
def _getUniqueDestinationPath(self, source_image, node_type, path): """ Get a unique destination path (with counter). """ if not os.path.exists(path): return path path, extension = os.path.splitext(path) counter = 1 new_path = "{}-{}{}".format(path, counter, extension) while os.path.exists(new_path): destination_image = Image(node_type, new_path, filename=os.path.basename(new_path)) try: if source_image.md5sum == destination_image.md5sum: # the source and destination images are identical return new_path except OSError: continue counter += 1 new_path = "{}-{}{}".format(path, counter, extension) return new_path
def image(): return Image('QEMU', 'test.img')
def test_md5sum_from_memory_cache(linux_microcore_img): Image._cache[linux_microcore_img] = "4d41402abc4b2a76b9719d911017c591" image = Image(linux_microcore_img) assert image.md5sum == "4d41402abc4b2a76b9719d911017c591"
def test_filesize(linux_microcore_img): image = Image(linux_microcore_img) assert image.filesize == 5
def test_md5sum(linux_microcore_img): image = Image(linux_microcore_img) assert image.md5sum == "5d41402abc4b2a76b9719d911017c592" assert image._cache[ linux_microcore_img] == "5d41402abc4b2a76b9719d911017c592"
def test_filename(linux_microcore_img): image = Image(linux_microcore_img) assert image.filename == "linux-microcore-3.4.1.img"
def image(): (fd, path) = tempfile.mkstemp(suffix=".img") return Image('QEMU', path)