示例#1
0
    def cleanup_tmp_folder(self):
        """Clean up temporary folder

        Args:
            None

        Returns:
            None
        """
        lose = set()
        # do not modify tmp_directories during the loop
        for tmp_dir in self.tmp_directories:
            if exists(tmp_dir):
                try:
                    shutil.rmtree(tmp_dir)
                except:
                    # directories that cannot be searched cause problems
                    try:
                        FileLister.set_tree_perms(tmp_dir)
                        shutil.rmtree(tmp_dir)
                    except Exception as e:
                        Output.print_warning(
                            "Temp directory %s was not removed (%s)" %
                            (tmp_dir, str(e)))
                        continue
            lose.add(tmp_dir)

        FileLister.all_temp_dirs -= lose
        self.tmp_directories -= lose
        Output.print_information(
            "Temp dir count is %s %s" %
            (len(self.tmp_directories), len(FileLister.all_temp_dirs)))
示例#2
0
    def get_directory_filelist(self, path, tmp_root_path, current_path):
        """Recursively list all the files in a directory, extracting all the archives inside.

        Args:
            path: (string) path of the directory
            tmp_root_path: (string) if the directory is inside of a tmp directory, this is the
                address of that directory, otherwise null.
            current_path: (string) current address within the temporary directory. If we are not in
                a tmp directory, this is also null. This is used to compute the display path.

        Returns:
            (list) a list of files, where each file is a dict with two keys "display_path" and
            "physical_path". "display_path" is the path that's shown to the user and "physical_path"
            is where file can be accessed.
        """
        file_list = []

        for dirpath, _, filenames in walk(path, followlinks=False):
            for filename in filenames:
                full_path = abspath(join(dirpath, filename))
                if islink(full_path):
                    Output.print_warning("Skipping symbolic link: " + full_path)
                    continue

                archive_type = FileLister.archive_type(full_path)

                if archive_type:
                    tmp_dir = self.create_tmp_directory(full_path)

                    if tmp_root_path:
                        display_path = join(current_path, relpath(full_path, tmp_root_path))
                    else:
                        display_path = full_path

                    try:
                        FileLister.extract_archive(archive_type, full_path, display_path, tmp_dir)
                    except ExtractError as expn:
                        Output.print_error(str(expn))
                        continue

                    file_list.extend(self.get_directory_filelist(tmp_dir, \
                        tmp_root_path=tmp_dir, current_path=display_path))
                else:
                    if tmp_root_path:
                        file_list.append({
                            "display_path": join(current_path, relpath(full_path, tmp_root_path)),
                            "physical_path": full_path
                        })
                    else:
                        file_list.append({"display_path": full_path, "physical_path": full_path})

        return file_list