def extract(self, archive_file_path, output_file_path, progress):
        def progress_func(pos, size):
            progress.value = int(pos * 100 / size)

        logging.info("Extracting {0}".format(self.__url))

        progress.value = 0
        progress.job = "Extracting"
        #output_file_path = u"\\\\?\\" + os.path.abspath(output_file_path)
        if os.path.isdir(output_file_path):
            shutil.rmtree(output_file_path,False)
        try:
            os.makedirs(output_file_path)
        except Exception:
            # doesn't matter if the directory already exists.
            pass

        with on_failure(lambda: shutil.rmtree(output_file_path)):
            filename, extension = os.path.splitext(self.__file_name)
            if extension == ".gz" or extension == ".tgz":
                archive_file = ProgressFile(archive_file_path, progress_func)
                with tarfile.open(fileobj=archive_file, mode='r:gz') as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".bz2":
                archive_file = ProgressFile(archive_file_path, progress_func)
                with tarfile.open(fileobj=archive_file, mode='r:bz2') as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".zip":
                archive_file = ProgressFile(archive_file_path, progress_func)
                with zipfile.ZipFile(archive_file) as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".7z":
                os_utils.ensureDirExists(output_file_path)
                proc = subprocess.Popen([config['paths']['7z'], "x", '-aoa', os_utils.cygpath(os.path.abspath(archive_file_path)), "-o{}".format(os_utils.cygpath(os.path.abspath(output_file_path)))])
                if proc.wait() != 0:
                    return False
            elif extension in [".exe", ".msi"]:
                # installers need to be handled by the caller
                return True
            else:
                logging.error("unsupported file extension {0}".format(extension))
                return False

            for i in range(self.__tree_depth):
                sub_dirs = os.listdir(output_file_path)
                if len(sub_dirs) != 1:
                    raise ValueError("unexpected archive structure,"
                                    " expected exactly one directory in {}".format(output_file_path))
                source_dir = os.path.join(output_file_path, sub_dirs[0])

                for src in os.listdir(source_dir):
                    shutil.move(os.path.join(source_dir, src), output_file_path)

                shutil.rmtree(source_dir)
        return True
    def extract(self, archive_file_path, output_file_path, progress):
        def progress_func(pos, size):
            progress.value = int(pos * 100 / size)

        try:
            os.makedirs(output_file_path)
        except Exception:
            # it does matter if the directory already exists otherwise downloads with tree_depth will fail if the are
            # extracted a second time on a dirty build environment
            sub_dirs = os.listdir(output_file_path)
            if not len(sub_dirs) == 0 and self.__clean:
                logging.info("Cleaning {}".format(output_file_path))
                for ls in sub_dirs:
                    try:
                        shutil.rmtree(os.path.join(output_file_path, ls))
                    except Exception:
                        os.remove(os.path.join(output_file_path, ls))

        logging.info("Extracting {}".format(self.__file_path))
        output_file_path = "\\\\?\\" + os.path.abspath(output_file_path)

        def extractProgress():
            progress.value = 0
            progress.job = "Extracting"

        with on_failure(lambda: shutil.rmtree(output_file_path)):
            extractProgress()
            archive_file = ProgressFile(archive_file_path, progress_func)
            with tarfile.open(fileobj=archive_file, mode='r:gz') as arch:
                arch.extractall(output_file_path)
            archive_file.close()

            for i in range(self.__tree_depth):
                sub_dirs = os.listdir(output_file_path)
                if len(sub_dirs) != 1:
                    raise ValueError(
                        "unexpected archive structure,"
                        " expected exactly one directory in {}".format(
                            output_file_path))
                source_dir = os.path.join(output_file_path, sub_dirs[0])

                for src in os.listdir(source_dir):
                    shutil.move(os.path.join(source_dir, src),
                                output_file_path)

                shutil.rmtree(source_dir)
        return True
Example #3
0
    def extract(self, archive_file_path, output_file_path, progress):
        def progress_func(pos, size):
            progress.value = int(pos * 100 / size)

        try:
            os.makedirs(output_file_path)
        except Exception:
            # it does matter if the directory already exists otherwise downloads with tree_depth will fail if the are
            # extracted a second time on a dirty build environment
            sub_dirs = os.listdir(output_file_path)
            if not len(sub_dirs) == 0 and self.__clean:
                logging.info("Cleaning {}".format(output_file_path))
                for ls in sub_dirs:
                    try:
                        shutil.rmtree(os.path.join(output_file_path, ls))
                    except Exception:
                        os.remove(os.path.join(output_file_path, ls))

        logging.info("Extracting {}".format(self.__file_path))
        output_file_path = "\\\\?\\" + os.path.abspath(output_file_path)

        def extractProgress():
            progress.value = 0
            progress.job = "Extracting"

        with on_failure(lambda: shutil.rmtree(output_file_path)):
            filename, extension = os.path.splitext(self.__file_name)
            if extension == ".gz" or extension == ".tgz":
                extractProgress()
                archive_file = ProgressFile(archive_file_path, progress_func)
                with tarfile.open(fileobj=archive_file, mode='r:gz') as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".bz2":
                extractProgress()
                archive_file = ProgressFile(archive_file_path, progress_func)
                with tarfile.open(fileobj=archive_file, mode='r:bz2') as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".zip":
                extractProgress()
                archive_file = ProgressFile(archive_file_path, progress_func)
                with zipfile.ZipFile(archive_file) as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".7z":
                proc = subprocess.Popen([
                    config['paths']['7z'], "x", archive_file_path,
                    "-o{}".format(output_file_path)
                ])
                if proc.wait() != 0:
                    return False
            elif extension in [".exe", ".msi"]:
                # installers need to be handled by the caller
                return True
            elif extension in [".md", ".txt"]:
                # we don't need todo anything
                return True
            else:
                logging.error("unsupported file extension %s", extension)
                return False

            for i in range(self.__tree_depth):
                sub_dirs = os.listdir(output_file_path)
                if len(sub_dirs) != 1:
                    raise ValueError(
                        "unexpected archive structure,"
                        " expected exactly one directory in {}".format(
                            output_file_path))
                source_dir = os.path.join(output_file_path, sub_dirs[0])

                for src in os.listdir(source_dir):
                    shutil.move(os.path.join(source_dir, src),
                                output_file_path)

                shutil.rmtree(source_dir)
        return True
    def extract(self, archive_file_path, output_file_path, progress):
        def progress_func(pos, size):
            progress.value = int(pos * 100 / size)

        logging.info("Extracting {0}".format(self.__url))

        progress.value = 0
        progress.job = "Extracting"
        output_file_path = u"\\\\?\\" + os.path.abspath(output_file_path)

        try:
            os.makedirs(output_file_path)
        except Exception:
            # doesn't matter if the directory already exists.
            pass

        with on_failure(lambda: shutil.rmtree(output_file_path)):
            filename, extension = os.path.splitext(self.__file_name)
            if extension == ".gz" or extension == ".tgz":
                archive_file = ProgressFile(archive_file_path, progress_func)
                with tarfile.open(fileobj=archive_file, mode='r:gz') as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".bz2":
                archive_file = ProgressFile(archive_file_path, progress_func)
                with tarfile.open(fileobj=archive_file, mode='r:bz2') as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".zip":
                archive_file = ProgressFile(archive_file_path, progress_func)
                with zipfile.ZipFile(archive_file) as arch:
                    arch.extractall(output_file_path)
                archive_file.close()
            elif extension == ".7z":
                proc = subprocess.Popen([
                    config['paths']['7z'], "x", archive_file_path,
                    "-o{}".format(output_file_path)
                ])
                if proc.wait() != 0:
                    return False
            elif extension in [".exe", ".msi"]:
                # installers need to be handled by the caller
                return True
            else:
                logging.error(
                    "unsupported file extension {0}".format(extension))
                return False

            for i in range(self.__tree_depth):
                sub_dirs = os.listdir(output_file_path)
                if len(sub_dirs) != 1:
                    raise ValueError(
                        "unexpected archive structure,"
                        " expected exactly one directory in {}".format(
                            output_file_path))
                source_dir = os.path.join(output_file_path, sub_dirs[0])

                for src in os.listdir(source_dir):
                    shutil.move(os.path.join(source_dir, src),
                                output_file_path)

                shutil.rmtree(source_dir)
        return True