Esempio n. 1
0
    def get_files_from_packagefile(cls, file_fullpathname):
        """
        Extract all information of a .pkg.tar.xz package.
        - List of all files
        - List of all Configuration-files

        This Method extract all the information in a temporary directory. The Debian package
        is extracted to the temporary directory, this because the files are needed to compute the File-Hash.

        :param file_pathname: Path to the .pkg.tar.xz package
        :return: Lexicographical sorted List of FileInfo()-Objects (Conffiles and normal Files)
        """
        all_files = []

        save_options = create_temp_folder(file_fullpathname)

        command_args_extract_package = [
            'tar', '-xf', save_options['absolute_package_path']
        ]
        command_args_files = [cls.executable, '-Qlp', file_fullpathname]

        files_output = CM.run_command_check_output(command_args_files)

        lines = files_output.split('\n')
        lines = filter(lambda l: len(l) > 0, lines)

        CM.run_command(command_args_extract_package,
                       working_directory=save_options['save_location'])

        for line in lines:
            path = line.split(' ')[1]
            temporary_path = save_options['save_location'] + path
            if cls._is_file(temporary_path):
                file_info = FileInfo(path, actual_path=False)
                file_info.set_actual_path(temporary_path)
                # With the assumption that files in the '/etc'-Folders are mostly Configuration-Files
                if path.startswith("/etc/"):
                    file_info.mutable = True
                all_files.append(file_info)

        return all_files
    def get_files_from_packagefile(cls, file_fullpathname):
        """
        Extract all information of a .pkg.tar.xz package.
        - List of all files
        - List of all Configuration-files

        This Method extract all the information in a temporary directory. The Debian package
        is extracted to the temporary directory, this because the files are needed to compute the File-Hash.

        :param file_pathname: Path to the .pkg.tar.xz package
        :return: Lexicographical sorted List of FileInfo()-Objects (Conffiles and normal Files)
        """
        all_files = []

        save_options = create_temp_folder(file_fullpathname)

        command_args_extract_package = ['tar', '-xf', save_options['absolute_package_path']]
        command_args_files = [cls.executable, '-Qlp', file_fullpathname]

        files_output = CM.run_command_check_output(command_args_files)

        lines = files_output.split('\n')
        lines = filter(lambda l: len(l) > 0, lines)

        CM.run_command(command_args_extract_package, working_directory=save_options['save_location'])

        for line in lines:
            path = line.split(' ')[1]
            temporary_path = save_options['save_location'] + path
            if cls._is_file(temporary_path):
                file_info = FileInfo(path, actual_path=False)
                file_info.set_actual_path(temporary_path)
                # With the assumption that files in the '/etc'-Folders are mostly Configuration-Files
                if path.startswith("/etc/"):
                    file_info.mutable = True
                all_files.append(file_info)

        return all_files
Esempio n. 3
0
    def get_files_from_packagefile(cls, file_pathname):
        """
        Extract all information of a .deb package.
        - List of all files
        - List of all Configuration-files

        This Method extract all the information in a temporary directory. The Debian package
        is extracted to the temporary directory, this because the files are needed to compute the File-Hash.

        :param file_pathname: Path to the .deb package
        :return: Lexicographical sorted List of FileInfo()-Objects (Conffiles and normal Files)
        """
        save_options = create_temp_folder(file_pathname)

        result = []
        result_help_list = []  # needed to check duplications

        command_args_unpack_package = [
            cls.executable, '-x', save_options['absolute_package_path'],
            save_options['save_location']
        ]
        command_args_extract_controlpackage = [
            "ar", "x", save_options['absolute_package_path'],
            cls.control_archive
        ]
        command_args_extract_conffile = [
            "tar", "-zxf", "/".join(
                (save_options['save_location'], cls.control_archive)),
            cls.conffile_file_name
        ]
        command_args_file_list = [cls.executable, '-c', file_pathname]

        def _add_to_result_list(file_path, actual_path, mutable=False):
            result_help_list.append(file_path)
            file_info = FileInfo(file_path, actual_path=False)
            file_info.set_actual_path(actual_path)
            file_info.mutable = mutable
            result.append(file_info)

        # Extraction of all needed Files (Store Files into tmp folder), extract Configuration-Files entries
        try:
            CM.run_command(command_args_unpack_package)
            CM.run_command(command_args_extract_controlpackage,
                           working_directory=save_options['save_location'])
            CM.run_command(command_args_extract_conffile,
                           working_directory=save_options['save_location'])

            temp_conffile_save_location = "/".join(
                (save_options['save_location'], cls.conffile_file_name))

            with open(temp_conffile_save_location, 'rb') as afile:
                file_content = afile.read().encode('utf-8')

        except (IOError, subprocess.CalledProcessError):
            # If no file extracted from command -> no .conffile-File exists
            file_content = None
            config_file_paths = []

        # Extract Configuration-Files-Entries from output
        if file_content is not None:
            config_file_paths = filter(lambda path: len(path) > 0,
                                       file_content.split('\n'))

            for config_file_path in config_file_paths:
                _add_to_result_list(config_file_path,
                                    save_options['save_location'] +
                                    config_file_path,
                                    mutable=True)

        # Extraction of file-list
        output_file_list = CM.run_command_check_output(command_args_file_list)
        line_list = output_file_list.split('\n')

        for line in line_list:

            splitted_line = line.split(' ')
            directory_or_file_path = splitted_line[-1]

            if "->" in splitted_line:
                # symbol-link
                symbol_link = (splitted_line[-3])[1:]
                temp_save_location_symbol_link = "/".join(
                    (save_options['save_location'], symbol_link))
                head, _ = ntpath.split(symbol_link)

                if "../" in directory_or_file_path:
                    root, _ = ntpath.split(head)
                    directory_or_file_path = root + directory_or_file_path[2:]
                else:
                    directory_or_file_path = "/".join(
                        (head, directory_or_file_path))

                if cls._is_file(temp_save_location_symbol_link):
                    if symbol_link not in config_file_paths and symbol_link not in result_help_list:
                        _add_to_result_list(symbol_link,
                                            temp_save_location_symbol_link)
            else:
                directory_or_file_path = directory_or_file_path[1:]

            temp_save_location_file = "/".join(
                (save_options['save_location'], directory_or_file_path))

            if cls._is_file(temp_save_location_file):
                if directory_or_file_path not in config_file_paths and directory_or_file_path not in result_help_list:
                    _add_to_result_list(directory_or_file_path,
                                        temp_save_location_file)

        return sorted(result, key=lambda f: f.full_pathname)
    def get_files_from_packagefile(cls, file_pathname):
        """
        Extract all information of a .deb package.
        - List of all files
        - List of all Configuration-files

        This Method extract all the information in a temporary directory. The Debian package
        is extracted to the temporary directory, this because the files are needed to compute the File-Hash.

        :param file_pathname: Path to the .deb package
        :return: Lexicographical sorted List of FileInfo()-Objects (Conffiles and normal Files)
        """
        save_options = create_temp_folder(file_pathname)

        result = []
        result_help_list = []  # needed to check duplications

        command_args_unpack_package = [cls.executable, '-x', save_options['absolute_package_path'], save_options['save_location']]
        command_args_extract_controlpackage = ["ar", "x", save_options['absolute_package_path'], cls.control_archive]
        command_args_extract_conffile = ["tar", "-zxf", "/".join((save_options['save_location'], cls.control_archive)), cls.conffile_file_name]
        command_args_file_list = [cls.executable, '-c', file_pathname]

        def _add_to_result_list(file_path, actual_path, mutable=False):
            result_help_list.append(file_path)
            file_info = FileInfo(file_path, actual_path=False)
            file_info.set_actual_path(actual_path)
            file_info.mutable = mutable
            result.append(file_info)

        # Extraction of all needed Files (Store Files into tmp folder), extract Configuration-Files entries
        try:
            CM.run_command(command_args_unpack_package)
            CM.run_command(command_args_extract_controlpackage, working_directory=save_options['save_location'])
            CM.run_command(command_args_extract_conffile, working_directory=save_options['save_location'])

            temp_conffile_save_location = "/".join((save_options['save_location'], cls.conffile_file_name))

            with open(temp_conffile_save_location, 'rb') as afile:
                file_content = afile.read().encode('utf-8')

        except(IOError, subprocess.CalledProcessError):
            # If no file extracted from command -> no .conffile-File exists
            file_content = None
            config_file_paths = []

        # Extract Configuration-Files-Entries from output
        if file_content is not None:
            config_file_paths = filter(lambda path: len(path) > 0, file_content.split('\n'))

            for config_file_path in config_file_paths:
                _add_to_result_list(config_file_path, save_options['save_location'] + config_file_path, mutable=True)

        # Extraction of file-list
        output_file_list = CM.run_command_check_output(command_args_file_list)
        line_list = output_file_list.split('\n')

        for line in line_list:

            splitted_line = line.split(' ')
            directory_or_file_path = splitted_line[-1]

            if "->" in splitted_line:
                # symbol-link
                symbol_link = (splitted_line[-3])[1:]
                temp_save_location_symbol_link = "/".join((save_options['save_location'], symbol_link))
                head, _ = ntpath.split(symbol_link)

                if "../" in directory_or_file_path:
                    root, _ = ntpath.split(head)
                    directory_or_file_path = root + directory_or_file_path[2:]
                else:
                    directory_or_file_path = "/".join((head, directory_or_file_path))

                if cls._is_file(temp_save_location_symbol_link):
                    if symbol_link not in config_file_paths and symbol_link not in result_help_list:
                        _add_to_result_list(symbol_link, temp_save_location_symbol_link)
            else:
                directory_or_file_path = directory_or_file_path[1:]

            temp_save_location_file = "/".join((save_options['save_location'], directory_or_file_path))

            if cls._is_file(temp_save_location_file):
                if directory_or_file_path not in config_file_paths and directory_or_file_path not in result_help_list:
                    _add_to_result_list(directory_or_file_path, temp_save_location_file)

        return sorted(result, key=lambda f: f.full_pathname)