def setUp(self):
        """
        set up function
        Returns:

        """
        self.query_package = QueryPackage()
        self.session = self.query_package._db_session
    def setUp(self):
        """
        set up function
        Returns:

        """
        self.query_package = QueryPackage(database_list=['os_version_1', 'os_version_2'])
        self.session = self.query_package._db_session
Beispiel #3
0
    def src_package_info(self, src_name_list, database_list=None):
        """
        get a source package info (provides, requires, etc)
        Args:
            src_name_list: source package list
            database_list: database list

        Returns:
            src_package_info_res

        Attributes:
            AttributeError: Cannot find the attribute of the corresponding object
            IndexError: list index out of range
            TypeError: object does not support this property or method

        Raises:
            ElasticSearchQueryException: dataBase connect failed
            DatabaseConfigException: dataBase config error
        """
        try:
            src_package_info_res = {}
            query_package = QueryPackage()
            for database in database_list:
                single_db_src_info = query_package.get_src_info(
                    src_name_list, database, 1, 20).get("data")
                if not single_db_src_info:
                    return {}

                database_src_info_list = []
                for pkg_info in single_db_src_info:
                    pkgname = list(pkg_info.keys())[0]
                    build_package_info = self.get_build_info(pkgname, database)
                    subpacks = self.get_subpack_info(pkgname, database,
                                                     pkg_info)

                    database_src_info_list.append({
                        "src_name":
                        pkgname,
                        "license":
                        pkg_info[pkgname].get("license"),
                        "version":
                        pkg_info[pkgname].get("version"),
                        "url":
                        pkg_info[pkgname].get("url"),
                        "summary":
                        pkg_info[pkgname].get("summary"),
                        "description":
                        pkg_info[pkgname].get("description"),
                        "build_dep":
                        build_package_info,
                        "subpacks":
                        subpacks
                    })
                src_package_info_res[database] = database_src_info_list
            return src_package_info_res
        except (AttributeError, IndexError, TypeError) as e:
            LOGGER.error(e)
            return {}
Beispiel #4
0
    def all_bin_packages(self,
                         database,
                         page_num=1,
                         page_size=20,
                         package_list=None,
                         command_line=False):
        """
        get all binary package info
        Args:
            database: database
            page_num: paging query index
            page_size: paging query size
            package_list: package list name
            command_line: command_line or UI
        Returns:
            all_bin_dict: all binary package information dict
            for example::
                {
                "total": "",
                "data": ""
                }
        Attributes:
            AttributeError: Cannot find the attribute of the corresponding object
            KeyError: key not exists
            TypeError: object does not support this property or method
        Raises:
            ElasticSearchQueryException: dataBase connect failed
            DatabaseConfigException: dataBase config error

        """
        try:
            # query all binary package info from database
            query_package = QueryPackage()
            all_bin_info = query_package.get_bin_info(package_list, database,
                                                      page_num, page_size,
                                                      command_line)

            if not all_bin_info["data"]:
                _msg = "An error occurred when getting bianry package info."
                raise PackageInfoGettingError(_msg)

            parsed_all_bin_info = []
            total_num = all_bin_info.get("total")
            for pkg_info in all_bin_info.get("data"):
                single_pkg = []
                for pkgname, info_values in pkg_info.items():
                    single_pkg = self.__parse_pkg_info(info_values, pkgname,
                                                       database)
                    single_pkg["source_name"] = info_values["src_name"]
                parsed_all_bin_info.append(single_pkg)
            all_bin_dict = {"total": total_num, "data": parsed_all_bin_info}
            return all_bin_dict
        except (AttributeError, KeyError, TypeError) as e:
            LOGGER.error(e)
            return {}
class TestQuerySourcePkgInfo(TestCase):
    """
    Test query source packages' detail
    """
    def setUp(self):
        """
        set up function
        Returns:

        """
        self.query_package = QueryPackage()
        self.session = self.query_package._db_session

    def test_query_specify(self):
        """
        Test query specify source package
        Returns:

        """
        self.session.query = MagicMock(
            return_value=ObtainMockData.get_data("JudySource.json"))
        result = self.query_package.get_src_info(src_list=['Judy'],
                                                 database='os_version_1',
                                                 page_num=1,
                                                 page_size=20,
                                                 command_line=False)

        self.assertIsNotNone(result['data'][0]['Judy'])
class TestQueryBinName(TestCase):
    """
    Test query binary packages‘ sources package
    """

    def setUp(self):
        """
        set up function
        Returns:

        """
        self.query_package = QueryPackage(database_list=['os_version_1', 'os_version_2'])
        self.session = self.query_package._db_session

    def test_query_specify_rpm(self):
        """
        Test query specify binary package
        Returns:
        """
        self.session.query = MagicMock(
            return_value=ObtainMockData.get_data('JudySource.json'))
        source_list = ['Judy']
        result = self.query_package.get_bin_name(source_list=source_list)
        expect_value = ObtainMockData.get_data('returnJudyResult.json')

        self.assertEqual(result[0], expect_value)

    def test_src_list_empty(self):
        """
        Test input a empty binary packages list
        Returns:
        """
        source_list = []
        result = self.query_package.get_bin_name(source_list=source_list)

        self.assertListEqual(result, [])
Beispiel #7
0
    def __init__(self, db_list):
        """
        Args:
            db_list: database priority list
        """
        if db_list:
            self.db_list = db_list
        else:
            raise AttributeError("the input of db_list is none")

        super(SelfDepend, self).__init__()
        self._init_search_dict(self.search_install_dict, self.db_list)
        self._init_search_dict(self.search_build_dict, self.db_list)
        self._init_search_dict(self.search_subpack_dict, self.db_list)

        self.__query_pkg = QueryPackage(self.db_list)
        self.subpack = False
 def __init__(self):
     """
     init
     """
     self.result = list()
     self.query_pkg = QueryPackage()
class QueryDepend(object):
    """
    Query dependency information of all packages
    """
    SOURCE_NAME = "source_name"
    BINARY_NAME = "binary_name"

    def __init__(self):
        """
        init
        """
        self.result = list()
        self.query_pkg = QueryPackage()

    def all_depend_info(self, depend_type, dbs):
        """
        Get all rpm info ,include source rpm(query build depend) and binary rpm (query install rpm)
        :param depend_type:
        :param dbs:
        :return:
        """
        if depend_type == BUILD_DEPEND_TYPE:
            LOGGER.info("Start to query all source rpm dependency information")
            self._get_source_rpm_depend(dbs)
        elif depend_type == INSTALL_DEPEND_TYPE:
            LOGGER.info("Start to query all binary rpm dependency information")
            self._get_bin_rpm_depend(dbs)

        return self.result

    def _get_source_rpm_depend(self, dbs):
        """
        Get one-level compilation dependency of all source packages in the database
        :param dbs: Database to be queried
        :return: None
        """
        for database in dbs:
            # First,query all source rpm name of specify database.
            all_source_rpm = self.query_pkg.get_src_info(
                src_list=None,
                database=database,
                page_num=DEFAULT_PAGE_NUM,
                page_size=MAXIMUM_PAGE_SIZE,
                command_line=True)
            all_source_rpm_name = list()
            for source_rpm in all_source_rpm.get('data'):
                all_source_rpm_name.extend(source_rpm.keys())
            LOGGER.info(
                f'The number of source packages in the {database} database is {len(all_source_rpm_name)}'
            )
            # Second, query the one-level build dependencies of all packages based on the package name.
            build_query_engine = BuildRequires([database])
            all_source_rpm_depend = build_query_engine.get_build_req(
                source_list=all_source_rpm_name)
            # Third, format build dependencies
            format_source_rpm_depend = self._format_depend(
                all_source_rpm_depend, key_word=self.SOURCE_NAME)

            self.result.append({database: format_source_rpm_depend})

    def _get_bin_rpm_depend(self, dbs):
        """
         Get one-level install dependency of all binary packages in the database
        :param dbs: Database to be queried
        :return:
        """
        for database in dbs:
            # First,query all binary rpm name of specify database.
            all_bin_rpm = self.query_pkg.get_bin_info(
                binary_list=None,
                database=database,
                page_num=DEFAULT_PAGE_NUM,
                page_size=MAXIMUM_PAGE_SIZE,
                command_line=True)
            all_bin_rpm_name = list()
            for bin_rpm in all_bin_rpm.get('data'):
                all_bin_rpm_name.extend(bin_rpm.keys())
            LOGGER.info(
                f'The number of binary packages in the {database} database is {len(all_bin_rpm_name)}'
            )
            # Second, query the one-level install dependencies of all packages based on the package name.
            install_query_engine = InstallRequires([database])
            all_binary_rpm_depend = install_query_engine.get_install_req(
                binary_list=all_bin_rpm_name)
            # Third, format build dependencies
            format_binary_rpm_depend = self._format_depend(
                all_binary_rpm_depend, key_word=self.BINARY_NAME)

            self.result.append({database: format_binary_rpm_depend})

    @staticmethod
    def _format_depend(all_rpm_depend, key_word):
        """
        Format the query dependent information.
        :param all_rpm_depend: depend info
        :param key_word: build or install
        :return: Formatted dependency information
        """
        format_rpm_depend = list()
        for rpm_depend_info in all_rpm_depend:
            format_rpm_depend.append(
                {rpm_depend_info.get(key_word): rpm_depend_info})
        return format_rpm_depend
Beispiel #10
0
    def bin_package_info(self, bin_name_list, database_list=None):
        """
        Query for binary package details
        Args:
            bin_name_list: binary package name list
            database_list: database list

        Returns:
                binary package detail info

        Attributes:
            AttributeError: Cannot find the attribute of the corresponding object
            IndexError: list index out of range
            TypeError: object does not support this property or method
        Raises:
            ElasticSearchQueryException: dataBase connect failed
            DatabaseConfigException: dataBase config error

        """
        try:
            query_package = QueryPackage()
            bin_package_info_res = {}
            for database in database_list:

                single_db_bin_info = query_package.get_bin_info(
                    bin_name_list, database, 1, 20).get("data")
                if not single_db_bin_info:
                    return {}

                database_bin_info_list = []
                for pkg_info in single_db_bin_info:
                    pkgname = list(pkg_info.keys())[0]

                    # get provides from bedepend info
                    provides_info = self.get_provides(pkgname, database)
                    # get requires from install info
                    requires_info = self.get_requires(pkgname, database)

                    database_bin_info_list.append({
                        "bin_name":
                        pkgname,
                        "version":
                        pkg_info[pkgname].get("version"),
                        "url":
                        pkg_info[pkgname].get("url"),
                        "license":
                        pkg_info[pkgname].get("license"),
                        "release":
                        pkg_info[pkgname].get("release"),
                        "summary":
                        pkg_info[pkgname].get("summary"),
                        "description":
                        pkg_info[pkgname].get("description"),
                        "src_name":
                        pkg_info[pkgname].get("src_name"),
                        "provides":
                        provides_info,
                        "requires":
                        requires_info,
                        "filelist":
                        self.parse_filelist_info(
                            pkg_info[pkgname].get("file_list"))
                    })
                bin_package_info_res[database] = database_bin_info_list
            return bin_package_info_res
        except (AttributeError, IndexError, TypeError) as e:
            LOGGER.error(e)
            return {}
class TestQuerySrcName(TestCase):
    """
    Test query sources packages' binary packages
    """
    def setUp(self):
        """
        set up function
        Returns:

        """
        self.query_package = QueryPackage(
            database_list=['os_version_1', 'os_version_2'])
        self.session = self.query_package._db_session

    def test_query_specify_rpm(self):
        """
        Test query specify binary package
        Returns:
        """
        self.session.query = MagicMock(
            return_value=ObtainMockData.get_data('JudyBinary.json'))
        binary_list = ['Judy']
        result = self.query_package.get_src_name(binary_list=binary_list)
        expect_value = {
            'binary_name': 'Judy',
            'bin_version': '1.0.5',
            'database': 'os_version_1',
            'src_name': 'Judy',
            'src_version': '1.0.5'
        }

        self.assertEqual(result[0], expect_value)

    def test_binary_list_empty(self):
        """
        Test input a empty binary packages list
        Returns:

        """
        binary_list = []
        result = self.query_package.get_src_name(binary_list=binary_list)

        self.assertListEqual(result, [])

    def test_query_no_data(self):
        """
        Test query a not exist binary package
        Returns:

        """
        self.session.query = MagicMock(return_value={})
        binary_list = ["Test123"]
        result = self.query_package.get_src_name(binary_list=binary_list)

        self.assertListEqual(result, [])

    def test_query_specify_database(self):
        """
        Test query binary package and specify database
        Returns:

        """
        self.session.query = MagicMock(return_value={})
        binary_list = ["Test123"]
        result = self.query_package.get_src_name(binary_list=binary_list,
                                                 specify_db='os_version_1')

        self.assertListEqual(result, [])
Beispiel #12
0
 def __init__(self, **kwargs):
     super(BeDepend, self).__init__()
     self.__dict__.update(kwargs)
     self.database = self.parameter["db_priority"][0]
     self.provide = BeDependRequires()
     self.query_pkg = QueryPackage()
Beispiel #13
0
class BeDepend(BaseDepend):
    def __init__(self, **kwargs):
        super(BeDepend, self).__init__()
        self.__dict__.update(kwargs)
        self.database = self.parameter["db_priority"][0]
        self.provide = BeDependRequires()
        self.query_pkg = QueryPackage()

    def __get_subpacks(self, pkg_name_lst, is_init=False):
        """get source packages's subpacks

        Args:
            pkg_name_lst ([list]): [source packagenames list]

        Returns:
            [set]: [source package's subpacks]
        """
        binary_pkgs = set()
        if not pkg_name_lst:
            return binary_pkgs
        searched_pkg = set()
        for pkg_dict in self.query_pkg.get_src_info(pkg_name_lst,
                                                    self.database, 1,
                                                    len(pkg_name_lst))["data"]:
            for _, pkg_info in pkg_dict.items():
                searched_pkg.add(pkg_info.get("src_name"))
                binary_pkgs.update(set(pkg_info.get("subpacks", [])))

        self._search_set.update(searched_pkg)
        if is_init:
            not_found_pkg = str(set(pkg_name_lst) - searched_pkg)
            self.log_msg = f"source packages {not_found_pkg} not found in {self.database}"
            LOGGER.warning(self.log_msg)
        return binary_pkgs

    def __update_binary_dict(self, dep_info):
        """update binary dict content

        Args:
            dep_info (dict): package depend info

        Returns:
            set: packages to search for next
        """
        next_search_binary = set()
        to_search_subpacks = set()
        bin_name = dep_info.get("binary_name")
        if not bin_name:
            return next_search_binary
        if bin_name not in self.binary_dict:
            self.binary_dict[bin_name] = {
                "name": bin_name,
                "version": dep_info.get("bin_version"),
                "source_name": dep_info.get("src_name"),
                "database": self.database,
                "install": [],
            }
        for pro in dep_info.get("provides", []):
            for req_bin_info in pro.get("install_require", []):
                next_search, next_src_names = self.__process_binary_data(
                    bin_name, req_bin_info)
                next_search_binary.update(next_search)
                to_search_subpacks.update(next_src_names)
        next_search_binary.update(self.__get_subpacks(to_search_subpacks))
        return next_search_binary

    def __update_source_dict(self, dep_info):
        """update source dict content

        Args:
            dep_info (dict): package depend info

        Returns:
            set: packages to search for next
        """
        next_search_pkgs = set()
        bin_name = dep_info.get("binary_name")
        if not bin_name:
            return next_search_pkgs

        src_name = dep_info.get("src_name")
        if src_name and src_name not in self.source_dict:
            self.source_dict[src_name] = {
                "name": src_name,
                "version": dep_info["src_version"],
                "database": self.database,
                "build": [],
            }

        for pro in dep_info.get("provides", []):
            for req_src_info in pro.get("build_require", []):
                next_search_pkgs.update(
                    self.__process_source_data(bin_name, req_src_info))
        return next_search_pkgs

    def __process_binary_data(self, dep_name, req_bin_info):
        """[update binary data method]

        Args:
            dep_name ([str]): [deepend name]
            req_bin_info ([dict]): [package's install depend info ]

        Returns:
            [set]: [packages to search for next ]
        """
        with_subpack_src_names = set()
        next_search_binary = set()
        bin_key = req_bin_info.get("req_bin_name")
        src_name = req_bin_info.get("req_src_name")
        if not bin_key:
            return next_search_binary, with_subpack_src_names

        if bin_key not in self.binary_dict:
            self.binary_dict[bin_key] = {
                "name": bin_key,
                "version": req_bin_info["req_bin_version"],
                "source_name": src_name,
                "database": self.database,
                "install": [dep_name],
            }
            next_search_binary.add(bin_key)
            if self.parameter["with_subpack"] and src_name:
                with_subpack_src_names.add(src_name)
        else:
            if dep_name not in self.binary_dict[bin_key]["install"]:
                self.binary_dict[bin_key]["install"].append(dep_name)
        return next_search_binary, with_subpack_src_names

    def __process_source_data(self, dep_name, req_src_info):
        """[update source data method]

        Args:
            dep_name ([str]): [deepend name]
            req_src_info ([dict]): [package's build depend info ]

        Returns:
            [set]: [packages to search for next ]
        """
        next_search_pkgs = set()
        src_key = req_src_info["req_src_name"]
        if src_key not in self._search_set:
            next_search_pkgs.update(self.__get_subpacks([src_key]))
        if src_key not in self.source_dict:
            self.source_dict[src_key] = {
                "name": src_key,
                "version": req_src_info["req_src_version"],
                "database": self.database,
                "build": [dep_name],
            }
        else:
            if dep_name not in self.source_dict[src_key]["build"]:
                self.source_dict[src_key]["build"].append(dep_name)
        return next_search_pkgs

    def __both_update(self, dep_info):
        """update bianry data ,source data at the same time

        Args:
            dep_info (dict): package depend info

        Returns:
            set: packages to serach for next
        """
        next_search_pkgs = self.__update_binary_dict(dep_info)
        next_search_pkgs.update(self.__update_source_dict(dep_info))
        return next_search_pkgs

    def __get_update_data_method(self):
        """get update data method

        Returns:
            func: update_binary,update_source,both_upte
        """
        search_type = self.parameter.get("search_type")
        if not search_type:
            return self.__both_update
        if search_type == "install":
            return self.__update_binary_dict
        else:
            return self.__update_source_dict

    def __init_to_serach_pkgs(self):
        """init_to_serach_pkgs

        Returns:
            set: first to search binary pkgs
        """
        if self.parameter.get("packtype") == "source":
            return set(self.__get_subpacks(self.packagename, is_init=True))

        return set(self.packagename)

    def be_depend(self):
        """
        get source(binary) rpm package(s) bedepend relation
        """
        searched_pkgs = set()
        update_meth = self.__get_update_data_method()

        to_search = self.__init_to_serach_pkgs()
        is_init = True
        while to_search:
            resp = self.provide.get_be_req(to_search, self.database)
            if not resp:
                break

            next_search = set()

            for bedep_info in resp:
                searched_pkgs.add(bedep_info.get("binary_name"))
                next_pkgs = update_meth(bedep_info)
                next_search.update(next_pkgs)

            if is_init and self.parameter.get("packtype") == "binary":
                not_found_pkg = str(to_search - searched_pkgs)
                self.log_msg = f"binary packages {not_found_pkg} not found in {self.database}"
                LOGGER.warning(self.log_msg)
                is_init = False

            to_search = next_search - searched_pkgs

    def __call__(self, **kwargs):
        self.__dict__.update(dict(dependency_type="bedep"))

        @buffer_cache(depend=self)
        def _depends(**kwargs):
            self.be_depend()

        _depends(**kwargs)