提交 38857031 编写于 作者: S solarhu 提交者: Gitee

!36 解决缺陷和规范扫描的问题

Merge pull request !36 from 薄皮小笼包/master
......@@ -2,15 +2,19 @@
description: Function and class annotation specifications in the project
functions: test
"""
# py文件中,如果全部为函数,顶部信息格式如上,description填写描述信息,functions填写函数名称
# In the PY file, if all are functions, the format of the top information is as above,
# the description information is filled in, and the function name is filled in functions
# Args:
# 列出每个参数的名字, 并在名字后使用一个冒号和一个空格,
# 分隔对该参数的描述.如果描述太长超过了单行80字符,使用2或者4个空格的悬挂缩进(与文件其他部分保持一致).
# 描述应该包括所需的类型和含义.
# List the name of each parameter with a colon and a space after the name,
# Separate the description of this parameter.
# If the description is too long than 80 characters in a single line,
# use a hanging indent of 2 or 4 spaces (consistent with the rest of the file)
# The description should include the type and meaning required
# Returns:
# 描述返回值的类型和语义. 如果函数返回None, 这一部分可以省略.
# Describes the type and semantics of the return value. If the function returns none,
# this part can be omitted
# Raises:
# 可能产生的异常
# Possible anomalies
def test(name, age):
......@@ -31,13 +35,14 @@ def test(name, age):
# description: Function and class annotation specifications in the project
# class: SampleClass
# py文件中,如果全部为类,顶部信息格式如上,description填写描述信息,class填写类名称,用 三引号,不用#
# 类应该在其定义下有一个用于描述该类的文档字符串.
# 如果你的类有公共属性(Attributes),
# 那么文档中应该有一个属性(Attributes)段.
# 并且应该遵守和函数参数相同的格式.
# In the PY file, if all are classes, the top information format is as above,
# description fills in the description information, class fills in the class name,
# uses three quotation marks, does not need#
# Class should have a document string under its definition that describes
# the class
# If your class has attributes,
# Then there should be an attribute section in the document
# And it should follow the same format as function parameters
class SampleClass():
"""
Summary of class here.
......
#!/usr/bin/python3
'''
Initial operation and configuration of the flask project
'''
......
'''
Interception before request
'''
#!/usr/bin/python3
"""
Description: Interception before request
"""
from flask import request
from packageship.application import OPERATION
from packageship import application
from packageship.application.apps.package.url import urls
......@@ -10,14 +11,17 @@ __all__ = ['identity_verification']
def identity_verification():
'''
Requested authentication
'''
"""
Description: Requested authentication
Args:
Returns:
Raises:
"""
if request.url_rule:
url_rule = request.url_rule.rule
for view, url, authentication in urls:
if url == url_rule and OPERATION in authentication.keys():
if request.method not in authentication.get(OPERATION):
if url == url_rule and application.OPERATION in authentication.keys():
if request.method not in authentication.get(application.OPERATION):
return False
break
return True
......
from flask.blueprints import Blueprint
from flask_restful import Api
from packageship.application.apps.package.url import urls
from packageship.application import OPERATION
from packageship import application
package = Blueprint('package', __name__)
......@@ -9,7 +9,7 @@ package = Blueprint('package', __name__)
api = Api()
for view, url, operation in urls:
if OPERATION and OPERATION in operation.keys():
if application.OPERATION and application.OPERATION in operation.keys():
api.add_resource(view, url)
......
# -*- coding:utf-8 -*-
'''
The dependencies of the query package
#!/usr/bin/python3
"""
Description:The dependencies of the query package
Used for package deletion and upgrade scenarios
This includes both install and build dependencies
'''
Class: BeDepend
"""
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.sql import literal_column
......@@ -12,33 +13,50 @@ from packageship.libs.dbutils import DBHelper
from packageship.application.models.package import src_pack
from packageship.application.apps.package.function.constants import ResponseCode
class BeDepend:
'''
Find the dependencies of the source package
'''
class BeDepend():
"""
Description: Find the dependencies of the source package
Attributes:
source_name: source name
db_name: database name
with_sub_pack: with_sub_pack
source_name_set:Source package lookup set
bin_name_set:Bin package lookup set
result_dict:return json
"""
def __init__(self, source_name, db_name, with_sub_pack):
'''
:param source_name: source_name
:param db_name: db_name
:param with_sub_pack: with_sub_pack
'''
"""
init class
"""
self.source_name = source_name
self.db_name = db_name
self.with_sub_pack = with_sub_pack
# Source package lookup set
self.source_name_set = set()
# Bin package lookup set
self.bin_name_set = set()
# return json
self.result_dict = dict()
def main(self):
'''
Map the database, if the source package of the query is not in the database,
"""
Description: Map the database, if the source
package of the query is not in the database,
throw an exception. Directly to the end
:return:
'''
Args:
Returns:
"source name": [
"source",
"version",
"dbname",
[
[
"root",
null
]
]
]
Raises:
"""
with DBHelper(db_name=self.db_name) as data_base:
src_obj = data_base.session.query(
src_pack).filter_by(name=self.source_name).first()
......@@ -57,13 +75,16 @@ class BeDepend:
return self.result_dict
def package_bedepend(self, pkg_id_list, data_base, package_type):
'''
Query the dependent function
:param pkg_id_list:source or binary packages id
:param data_base: database
:param package_type: package type
:return:
'''
"""
Description: Query the dependent function
Args:
pkg_id_list:source or binary packages id
data_base: database
package_type: package type
Returns:
Raises:
SQLAlchemyError: Database connection exception
"""
search_set = set(pkg_id_list)
id_in = literal_column('id').in_(search_set)
# package_type
......@@ -171,14 +192,17 @@ class BeDepend:
self.package_bedepend(bin_id_list, data_base, package_type="bin")
def make_dicts(self, key, source_name, version, parent_node, be_type):
'''
:param key: dependent bin name
:param source_name: source name
:param version: version
:param parent_node: Rely on package name
:param be_type: dependent type
:return:
'''
"""
Description: Splicing dictionary function
Args:
key: dependent bin name
source_name: source name
version: version
parent_node: Rely on package name
be_type: dependent type
Returns:
Raises:
"""
if key not in self.result_dict:
self.result_dict[key] = [
source_name,
......@@ -191,7 +215,7 @@ class BeDepend:
]
]
else:
if [parent_node,be_type] not in self.result_dict[key][-1]:
if [parent_node, be_type] not in self.result_dict[key][-1]:
self.result_dict[key][-1].append([
parent_node,
be_type
......
#!/usr/bin/python3
"""
Find compilation dependency of source package
Description: Find compilation dependency of source package
class: BuildDepend
"""
from packageship.application.apps.package.function.searchdb import SearchDB
from packageship.application.apps.package.function.install_depend import InstallDepend
from packageship.application.apps.package.function.constants import ResponseCode, ListNode
class BuildDepend:
class BuildDepend():
"""
Find compilation dependency of source package
Description: Find compilation dependency of source package
Attributes:
pkg_name_list: List of package names
db_list: List of database names
self_build: Compile dependency conditions
history_dict: Query history dict
search_db:Query an instance of a database class
result_dict:A dictionary to store the data that needs to be echoed
source_dict:A dictionary to store the searched source code package name
"""
def __init__(self, pkg_name_list, db_list, self_build=0, history_dict=None):
"""
init class
"""
self.pkg_name_list = pkg_name_list
self._self_build = self_build
......@@ -25,8 +38,13 @@ class BuildDepend:
def build_depend_main(self):
"""
Entry function
:return: ResponseCode,result_dict,source_dict
Description: Entry function
Args:
Returns:
ResponseCode: response code
result_dict: Dictionary of query results
source_dict: Dictionary of source code package
Raises:
"""
if not self.search_db.db_object_dict:
return ResponseCode.DIS_CONNECTION_DB, None, None
......@@ -52,8 +70,12 @@ class BuildDepend:
def build_depend(self, pkg_list):
"""
@:param pkg_list:You need to find the dependent source package name
:return ResponseCode
Description: Compile dependency query
Args:
pkg_list:You need to find the dependent source package name
Returns:
ResponseCode: response code
Raises:
"""
res_status, build_list = self.search_db.get_build_depend(pkg_list)
......@@ -84,11 +106,14 @@ class BuildDepend:
def _create_node_and_get_search_list(self, build_list, pkg_list):
"""
To create root node in self.result_dict and
Description: To create root node in self.result_dict and
return the name of the source package to be found next time
@:param build_list:List of binary package names
@:param pkg_list: List of binary package names
:return the name of the source package to be found next time
Args:
build_list:List of binary package names
pkg_list: List of binary package names
Returns:
the name of the source package to be found next time
Raises:
"""
search_set = set()
search_list = []
......@@ -150,9 +175,11 @@ class BuildDepend:
def self_build(self, pkg_name_li):
"""
Using recursion to find compilation dependencies
:param pkg_name_li: Source package name list
:return:
Description: Using recursion to find compilation dependencies
Args:
pkg_name_li: Source package name list
Returns:
Raises:
"""
if not pkg_name_li:
return
......
'''
Response contain and code ID
'''
#!/usr/bin/python3
"""
Description: Response contain and code ID
class: ListNode, ResponseCode
"""
class ListNode:
class ListNode():
"""
the structure of dict:
Description: Dethe structure of dict:
{package_name: [source_name,
dbname,
[[parent_node_1, depend_type],[parent_node_2, depend_type],...]],
check_tag]
}
changeLog:
"""
SOURCE_NAME = 0
......@@ -26,8 +29,8 @@ class ListNode:
class ResponseCode():
"""
Description: response code to web
changeLog:
Description: response code to web
changeLog:
"""
# Four digits are common status codes
SUCCESS = "2001"
......@@ -66,7 +69,7 @@ class ResponseCode():
@classmethod
def response_json(cls, code, data=None):
"""
classmethod
Description: classmethod
"""
return {
"code": code,
......
'''
Querying for install dependencies
Querying packages install depend for those package can be installed
'''
#!/usr/bin/python3
"""
Description: Querying for install dependencies
Querying packages install depend for those package can be installed
class: InstallDepend, DictionaryOperations
"""
from packageship.libs.log import Log
from .searchdb import SearchDB
from .constants import ResponseCode
......@@ -11,15 +13,20 @@ LOGGER = Log(__name__)
class InstallDepend():
'''
Description: quert install depend of package
"""
Description: query install depend of package
Attributes:
db_list: A list of Database name to show the priority
__search_list: Contain the binary packages searched in the next loop
binary_dict: Contain all the binary packages info and operation
__search_db: A object of database which would be connected
changeLog:
'''
"""
#pylint: disable = too-few-public-methods
def __init__(self, db_list):
'''
:param db_list: A list of Database name to show the priority
'''
"""
Initialization class
"""
self.binary_dict = DictionaryOperations()
self.__search_list = []
......@@ -27,12 +34,14 @@ class InstallDepend():
self.__search_db = SearchDB(db_list)
def query_install_depend(self, binary_list, history_dicts=None):
'''
"""
Description: init result dict and determint the loop end point
:param binary_list: A list of binary rpm package name
history_dicts: record the searching install depend history,
Args:
binary_list: A list of binary rpm package name
history_dicts: record the searching install depend history,
defualt is None
:return binary_dict.dictionary:
Returns:
binary_dict.dictionary:
{binary_name: [
src,
dbname,
......@@ -42,7 +51,8 @@ class InstallDepend():
'install'
]
]}
'''
Raises:
"""
if not self.__search_db.db_object_dict:
return ResponseCode.DIS_CONNECTION_DB, None
if not binary_list:
......@@ -58,10 +68,12 @@ class InstallDepend():
def __query_single_install_dep(self, history_dicts):
"""
Description: query a package install depend and append to result
:param history_dicts
:return response_code
changeLog:
Description: query a package install depend and append to result
Args:
history_dicts: A list of binary rpm package name
Returns:
response_code: response code
Raises:
"""
result_list = self.__search_db.get_install_depend(self.__search_list)
for search in self.__search_list:
......@@ -98,28 +110,50 @@ class InstallDepend():
parent_node=[[result.search_name, 'install']])
self.__search_list.append(result.depend_name)
class DictionaryOperations():
'''
Related to dictionary operations, creating dictionary, append dictionary
'''
"""
Description: Related to dictionary operations, creating dictionary, append dictionary
Attributes:
dictionary: Contain all the binary packages info after searching
changeLog:
"""
def __init__(self):
"""
init class
"""
self.dictionary = dict()
def init_key(self, key, src=None, version=None, dbname=None, parent_node=None):
'''
Creating dictionary
'''
"""
Description: Creating dictionary
Args:
key: binary_name
src: source_name
version: version
dbname: databases name
parent_node: parent_node
Returns:
dictionary[key]: [src, version, dbname, parent_node]
"""
if dbname:
self.dictionary[key] = [src, version, dbname, [['root', None]]]
else:
self.dictionary[key] = [src, version, dbname, parent_node]
def update_value(self, key, src=None, version=None, dbname=None, parent_node=None):
'''
append dictionary
'''
"""
Description: append dictionary
Args:
key: binary_name
src: source_name
version: version
dbname: database name
parent_node: parent_node
Returns:
Raises:
"""
if src:
self.dictionary[key][ListNode.SOURCE_NAME] = src
if version:
......
#!/usr/bin/python3
"""
docstring:Get package information and modify package information
Description: Get package information and modify package information
functions: get_packages, buildep_packages, sub_packages, get_single_package,
update_single_package, update_maintaniner_info
"""
from flask import current_app
......@@ -13,11 +16,16 @@ from packageship.libs.exception import Error
def get_packages(dbname):
'''
"""
Description: Get all packages info
:param dbname: Database name
:return: package info
'''
Args:
dbname: Database name
Returns:
Package information is returned as a list
Raises:
AttributeError: Object does not have this property
Error: Abnormal error
"""
with DBHelper(db_name=dbname) as db_name:
src_pack_queryset = db_name.session.query(src_pack).all()
resp_list = []
......@@ -39,9 +47,13 @@ def get_packages(dbname):
def buildep_packages(dbname, src_pack_id):
"""
Description: Query package layer 1 compilation dependency
:param dbname: databases name
:param src_pack_id: srcpackage id
:return: buildDep Compile dependencies of source packages
Args:
dbname: databases name
src_pack_id: The ID of the source package
Returns:
buildDep Compile dependencies of source packages
Raises:
AttributeError: Object does not have this property
"""
with DBHelper(db_name=dbname) as db_name:
b_pack_requires_set = db_name.session.query(
......@@ -61,10 +73,14 @@ def buildep_packages(dbname, src_pack_id):
def sub_packages(dbname, src_pack_id):
"""
Description: Query package layer 1 installation dependency
:param dbname: databases name
:param src_pack_id: srcpackage id
:return: subpack Source package to binary package, then find the installation dependencies
Args:
dbname: databases name
src_pack_id: srcpackage id
Returns:
subpack Source package to binary package, then find the installation dependencies
of the binary package
Raises:
AttributeError: Object does not have this property
"""
with DBHelper(db_name=dbname) as db_name:
subpack = {}
......@@ -93,12 +109,16 @@ def sub_packages(dbname, src_pack_id):
def get_single_package(dbname, sourcename):
'''
"""
Description: Get all packages info
:param dbname: Database name
:param sourcename: Source package name
:return: package info
'''
Args:
dbname: Database name
sourcename: Source package name
Returns:
package info
Raises:
AttributeError: Object does not have this property
"""
with DBHelper(db_name=dbname) as db_name:
package = {}
src_pack_obj = db_name.session.query(src_pack).filter_by(
......@@ -126,11 +146,16 @@ def update_single_package(
maintain_level):
"""
Description: change single package management
:param package_name: package name
:param dbname: Database name
:param maintainer: maintainer info
:param maintain_level: maintain_level info
:return: message success or failed
Args:
package_name: package name
dbname: Database name
maintainer: maintainer info
maintain_level: maintain_level info
Returns:
message success or failed
Raises:
AttributeError: Object does not have this property
TypeError: Abnormal error
"""
with DBHelper(db_name=dbname) as db_name:
update_obj = db_name.session.query(
......@@ -144,16 +169,19 @@ def update_maintaniner_info(package_name,
dbname,
maintaniner,
maintainlevel):
'''
"""
update separately maintaniner info
:param package_name: package name
:param dbname: Database name
:param maintainer: maintainer info
:param maintain_level: maintain_level info
:return: message success or failed
Description: update separately maintaniner info
Args:
package_name: package name
dbname: Database name
maintainer: maintainer info
maintain_level: maintain_level info
Returns:
message success or failed
Raises:
AttributeError: Object does not have this property
Error: Abnormal error
"""
'''
with DBHelper(db_name=dbname) as db_name:
src_pack_obj = db_name.session.query(src_pack).filter_by(
name=package_name).first()
......
#!/usr/bin/python3
"""
A set for all query databases function
Description: A set for all query databases function
class: SearchDB
functions: db_priority
"""
from collections import namedtuple
......@@ -22,6 +25,9 @@ LOGGER = Log(__name__)
class SearchDB():
"""
Description: query in database
Attributes:
db_list: Database list
db_object_dict:A dictionary for storing database connection objects
changeLog:
"""
def __new__(cls, *args, **kwargs):
......@@ -31,6 +37,9 @@ class SearchDB():
return cls._instance
def __init__(self, db_list):
"""
init class
"""
self.db_object_dict = dict()
for db_name in db_list:
try:
......@@ -44,9 +53,11 @@ class SearchDB():
Description: get a package install depend from database:
binary_name -> binary_id -> requires_set -> requires_id_set -> provides_set
-> install_depend_binary_id_key_list -> install_depend_binary_name_list
:param binary_lsit: a list of binary package name
:return install depend list
changeLog:
Args:
binary_list: a list of binary package name
Returns:
install depend list
Raises:
"""
result_list = []
get_list = []
......@@ -111,9 +122,15 @@ class SearchDB():
"""
Description: get a package source name from database:
bianry_name ->binary_source_name -> source_name
input: search package's name, database preority list
return: database name, source name
changeLog:
Args:
binary_name: search package's name, database preority list
Returns:
db_name: database name
source_name: source name
source_version: source version
Raises:
AttributeError: The object does not have this property
SQLAlchemyError: sqlalchemy error
"""
for db_name, data_base in self.db_object_dict.items():
try:
......@@ -136,9 +153,13 @@ class SearchDB():
"""
Description: get a subpack list based on source name list:
source_name ->source_name_id -> binary_name
input: search package's name, database preority list
return: subpack tuple
changeLog:
Args:
source_name_list: search package's name, database preority list
Returns:
result_list: subpack tuple
Raises:
AttributeError: The object does not have this property
SQLAlchemyError: sqlalchemy error
"""
if not self.db_object_dict:
return ResponseCode.DIS_CONNECTION_DB, None
......@@ -190,14 +211,19 @@ class SearchDB():
def get_binary_in_other_database(self, not_found_binary, db_):
"""
Binary package name data not found in the current database, go to other databases to try
@:param:not_found_build These data cannot be found in the current database
@:param:db:current database name
return:a list :[(search_name,source_name,bin_name,
Description: Binary package name data not found in
the current database, go to other databases to try
Args:
not_found_binary: not_found_build These data cannot be found in the current database
db_:current database name
Returns:
a list :[(search_name,source_name,bin_name,
bin_version,db_name,search_version,req_name),
(search_name,source_name,bin_name,
bin_version,db_name,search_version,req_name),]
changeLog:new method to look for data in other databases
Raises:
AttributeError: The object does not have this property
SQLAlchemyError: sqlalchemy error
"""
if not not_found_binary:
return []
......@@ -264,13 +290,16 @@ class SearchDB():
def get_build_depend(self, source_name_li):
"""
Description: get a package build depend from database
input:
@:param: search package's name list
return: all source pkg build depend list
structure :[(search_name,source_name,bin_name,bin_version,db_name,search_version),
Args:
source_name_li: search package's name list
Returns:
all source pkg build depend list
structure :[(search_name,source_name,bin_name,bin_version,db_name,search_version),
(search_name,source_name,bin_name,bin_version,db_name,search_version),]
changeLog: Modify SQL logic and modify return content by:zhangtao
Raises:
AttributeError: The object does not have this property
SQLAlchemyError: sqlalchemy error
"""
# pylint: disable=R0914
return_tuple = namedtuple("return_tuple", [
......@@ -372,7 +401,13 @@ class SearchDB():
def db_priority():
"""
return dbprioty
Description: Read yaml file, return database name, according to priority
Args:
Returns:
db_list: database name list
Raises:
FileNotFoundError: file cannot be found
Error: abnormal error
"""
try:
with open(DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context:
......
'''
Querying for self dependencies
#!/usr/bin/python3
"""
Description: Querying for self dependencies
Querying packages install and build depend for those package can be
build and install
'''
class: SelfDepend, DictionaryOperations
"""
import copy
from packageship.libs.log import Log
......@@ -14,13 +16,28 @@ from .build_depend import BuildDepend as build_depend
LOGGER = Log(__name__)
class SelfDepend():
'''
"""
Description:
Querying for self dependencies
Querying packages install and build depend for those package can be
build and install
'''
Attributes:
db_list: list of database names
binary_dict: Contain all the binary packages info and operation
source_dicts: Contain all the source packages info and operation
result_tmp: restore the return result dict
search_install_list: Contain the binary packages searched install dep in the next loop
search_build_list: Contain the source packages searched build dep in the next loop
search_subpack_list: Contain the source packages searched subpack in the next loop
withsubpack: withsubpack
search_db: A object of database which would be connected
"""
def __init__(self, db_list):
"""
init class
"""
self.binary_dict = DictionaryOperations()
self.source_dicts = DictionaryOperations()
self.result_tmp = dict()
......@@ -34,9 +51,15 @@ class SelfDepend():
def query_depend(self, packname, selfbuild, withsubpack, packtype='binary'):
"""
Description: init result dict and determint the loop end point
input: packname, selfbuild, withsubpack, packtype
return: result dicts, source dicts
changeLog:
Args:
packname: Package name
selfbuild: selfbuild
withsubpack: withsubpack
packtype: package type
Returns:
binary_dict.dictionary: Contain all the binary packages info after searching
source_dicts.dictionary: Contain all the source packages info after searching
Raises:
"""
if not self.search_db.db_object_dict:
return ResponseCode.DIS_CONNECTION_DB, None, None
......@@ -67,9 +90,12 @@ class SelfDepend():
def init_dict(self, packname, packtype):
"""
Description: init result dict
input: packname, packtype
return: response_code
changeLog:
Args:
packname: package name
packtype: package type
Returns:
response_code
Raises:
"""
if packtype == 'source':
response_code, subpack_list = self.search_db.get_sub_pack([packname])
......@@ -99,8 +125,9 @@ class SelfDepend():
def query_install(self):
"""
Description: query install depend
return:
changeLog:
Args:
Returns:
Raises:
"""
self.result_tmp.clear()
_, self.result_tmp = \
......@@ -131,8 +158,9 @@ class SelfDepend():
def with_subpack(self):
"""
Description: query subpackage
return:
changeLog:
Args:
Returns:
Raises:
"""
if None in self.search_subpack_list:
LOGGER.logger.warning("There is a NONE in input value:" + \
......@@ -153,8 +181,10 @@ class SelfDepend():
def query_build(self, selfbuild):
"""
Description: query build depend
return:
changeLog:
Args:
selfbuild: selfbuild
Returns:
Raises:
"""
self.result_tmp.clear()
if selfbuild == 0:
......@@ -165,8 +195,9 @@ class SelfDepend():
def query_builddep(self):
"""
Description: for selfbuild == 0, query selfbuild depend
return:
changeLog:
Args:
Returns:
Raises:
"""
_, self.result_tmp, _ = build_depend(
self.search_build_list,
......@@ -197,8 +228,8 @@ class SelfDepend():
def query_selfbuild(self):
"""
Description: for selfbuild == 1, query selfbuild depend
return:
changeLog:
Args:
Returns:
"""
_, self.result_tmp, source_dicts_tmp = build_depend(
self.search_build_list,
......@@ -228,32 +259,55 @@ class SelfDepend():
class DictionaryOperations():
'''
Related to dictionary operations, creating dictionary, append dictionary
'''
"""
Description: Related to dictionary operations, creating dictionary, append dictionary
Attributes:
dictionary: dict
"""
def __init__(self):
"""
init class
"""
self.dictionary = dict()
def append_src(self, key, dbname, version):
'''
Appending source dictionary
'''
"""
Description: Appending source dictionary
Args:
key: bianry name
dbname: database name
version: version
Returns:
Raises:
"""
self.dictionary[key] = [dbname, version]
def append_bin(self, key, src=None, version=None, dbname=None, parent_node=None):
'''
Appending binary dictionary
'''
"""
Description: Appending binary dictionary
Args:
key: binary name
src: source name
version: version
dbname: database name
parent_node: parent node
Returns:
Raises:
"""
if not parent_node:
self.dictionary[key] = [src, version, dbname, [['root', None]]]
else:
self.dictionary[key] = [src, version, dbname, [parent_node]]
def update_value(self, key, parent_list=None):
'''
append dictionary
'''
"""
Args:
key: binary name
parent_list: parent list
Returns:
Raises:
"""
if parent_list:
self.dictionary[key][ListNode.PARENT_LIST].extend(parent_list)
#!/usr/bin/python3
"""
marshmallow serialize
Description: marshmallow serialize
"""
from marshmallow import Schema
from marshmallow import fields
......@@ -9,7 +10,7 @@ from marshmallow import validate
class PackagesSchema(Schema):
"""
PackagesSchema serialize
Description: PackagesSchema serialize
"""
dbName = fields.Str(validate=validate.Length(
max=50), required=False, allow_none=True)
......@@ -17,7 +18,7 @@ class PackagesSchema(Schema):
class GetpackSchema(Schema):
"""
GetpackSchema serialize
Description: GetpackSchema serialize
"""
sourceName = fields.Str(
required=True,
......@@ -32,7 +33,13 @@ class GetpackSchema(Schema):
def validate_maintainlevel(maintainlevel):
"""
Method test
Description: Method test
Args:
maintainlevel: maintainlevel
Returns:
True or failure
Raises:
ValidationError: Test failed
"""
if maintainlevel not in ['1', '2', '3', '4']:
raise ValidationError("maintainLevel is illegal data ")
......@@ -40,7 +47,7 @@ def validate_maintainlevel(maintainlevel):
class PutpackSchema(Schema):
"""
PutpackSchema serialize
Description: PutpackSchema serialize
"""
sourceName = fields.Str(
required=True,
......@@ -62,7 +69,7 @@ class PutpackSchema(Schema):
class InstallDependSchema(Schema):
"""
InstallDependSchema
Description: InstallDependSchema
"""
binaryName = fields.Str(
required=True,
......@@ -73,7 +80,7 @@ class InstallDependSchema(Schema):
class BuildDependSchema(Schema):
"""
BuildDependSchema serialize
Description: BuildDependSchema serialize
"""
sourceName = fields.Str(
required=True,
......@@ -84,7 +91,13 @@ class BuildDependSchema(Schema):
def validate_withsubpack(withsubpack):
"""
Method test
Description: Method test
Args:
withsubpack: withsubpack
Returns:
True or failure
Raises:
ValidationError: Test failed
"""
if withsubpack not in ['0', '1']:
raise ValidationError("withSubpack is illegal data ")
......@@ -92,7 +105,7 @@ def validate_withsubpack(withsubpack):
class BeDependSchema(Schema):
"""
BeDependSchema serialize
Description: BeDependSchema serialize
"""
packagename = fields.Str(
required=True,
......@@ -111,7 +124,7 @@ class BeDependSchema(Schema):
def validate_selfbuild(selfbuild):
"""
Method test
Description: Method test
"""
if selfbuild not in ['0', '1']:
raise ValidationError("selfbuild is illegal data ")
......@@ -119,7 +132,7 @@ def validate_selfbuild(selfbuild):
def validate_packtype(packtype):
"""
Method test
Description: Method test
"""
if packtype not in ['source', 'binary']:
raise ValidationError("packtype is illegal data ")
......@@ -127,7 +140,7 @@ def validate_packtype(packtype):
class SelfDependSchema(Schema):
"""
SelfDependSchema serialize
Description: SelfDependSchema serialize
"""
packagename = fields.Str(
required=True,
......@@ -145,7 +158,7 @@ class SelfDependSchema(Schema):
class DeletedbSchema(Schema):
"""
DeletedbSchema serialize
Description: DeletedbSchema serialize
"""
dbName = fields.Str(
required=True,
......@@ -155,17 +168,21 @@ class DeletedbSchema(Schema):
def have_err_db_name(db_list, db_priority):
'''
@param:db_list db list of inputs
@param:db_priority default list
return:If any element in db_list is no longer in db_priority, return false
'''
"""
Description: have error database name method
Args:
db_list: db_list db list of inputs
db_priority: db_priority default list
Returns:
If any element in db_list is no longer in db_priority, return false
Raises:
"""
return any(filter(lambda db_name: db_name not in db_priority, db_list))
class InitSystemSchema(Schema):
"""
InitSystemSchema serialize
Description: InitSystemSchema serialize
"""
configfile = fields.Str(
validate=validate.Length(
......
#!/usr/bin/python3
"""
url set
Description: url set
"""
from . import view
......
#!/usr/bin/python3
"""
view: Request logic processing Return json format
description: Interface processing
class: BeDepend, BuildDepend, InitSystem, InstallDepend, Packages,
Repodatas, SelfDepend, SinglePack
"""
import yaml
from flask import request
......@@ -41,22 +44,38 @@ LOGGER = Log(__name__)
class Packages(Resource):
'''
"""
Description: interface for package info management
Restful API: get
changeLog:
'''
"""
def get(self):
'''
"""
Description: Get all package info from a database
input:
dbName
return:
json file contain package's info
Exception:
Changelog:
'''
Args:
dbName: Data path name, not required parameter
Returns:
{
"code": "",
"data": [
{
"dbname": "",
"downloadURL": "",
"license": "",
"maintainer": ,
"maintainlevel": ,
"sourceName": "",
"sourceURL": "",
"version": ""
},
"msg": ""
}
Raises:
DisconnectionError: Unable to connect to database exception
AttributeError: Object does not have this property
Error: Abnormal error
"""
# Get verification parameters
schema = PackagesSchema()
data = schema.dump(request.args)
......@@ -96,24 +115,45 @@ class Packages(Resource):
ResponseCode.response_json(
ResponseCode.DIS_CONNECTION_DB))
class SinglePack(Resource):
'''
"""
description: single package management
Restful API: get, put
ChangeLog:
'''
"""
def get(self):
'''
"""
description: Searching a package info
input:
sourceName
dbName
return:
json file contain package's detailed info
exception:
changeLog:
'''
Args:
dbName: Database name, not required parameter
sourceName: Source code package name, must pass
Returns:
{
"code": "",
"data": [
{
"buildDep": [],
"dbname": "",
"downloadURL": "",
"license": "",
"maintainer": "",
"maintainlevel": "",
"sourceName": "",
"sourceURL": "",
"subpack": {},
"version": ""
}
],
"msg": ""
}
Raises:
DisconnectionError: Unable to connect to database exception
AttributeError: Object does not have this property
TypeError: Exception of type
Error: Abnormal error
"""
# Get verification parameters
schema = GetpackSchema()
data = schema.dump(request.args)
......@@ -160,17 +200,25 @@ class SinglePack(Resource):
return jsonify(ResponseCode.response_json(abnormal))
def put(self):
'''
Description: update a package info
input:
packageName
dbName
maintainer
maintainLevel
return:
exception:
changeLog:
'''
"""
Description: update a package info,
Args:
dbName: Database name,Parameters are required
sourceName: The name of the source code package. Parameters are required
maintainer: Maintainer, parameter not required
maintainlevel: Maintenance level, parameter not required
Returns:
{
"code": "",
"data": "",
"msg": ""
}
Raises:
DisconnectionError: Unable to connect to database exception
AttributeError: Object does not have this property
TypeError: Exception of type
Error: Abnormal error
"""
# Get verification parameters
schema = PutpackSchema()
data = schema.dump(request.get_json())
......@@ -215,14 +263,14 @@ class SinglePack(Resource):
class InstallDepend(Resource):
'''
"""
Description: install depend of binary package
Restful API: post
changeLog:
'''
"""
def post(self):
'''
"""
Description: Query a package's install depend(support
querying in one or more databases)
input:
......@@ -244,9 +292,8 @@ class InstallDepend(Resource):
]
]
}
exception:
changeLog:
'''
Raises:
"""
schema = InstallDependSchema()
data = request.get_json()
......@@ -292,35 +339,28 @@ class InstallDepend(Resource):
class BuildDepend(Resource):
'''
"""
Description: build depend of binary package
Restful API: post
changeLog:
'''
"""
def post(self):
'''
"""
Description: Query a package's build depend and
build depend package's install depend
(support querying in one or more databases)
input:
sourceName:
dbPreority: the array for database preority
return:
resultList[
restult[
binaryName:
srcName:
dbName:
type: install or build, which depend
on the function
parentNode: the binary package name which is
the build/install depend for binaryName
]
]
exception:
changeLog:
'''
Args:
sourceName :name of the source package
dbPreority:the array for database preority
Returns:
{
"code": "",
"data": "",
"msg": ""
}
Raises:
"""
schema = BuildDependSchema()
data = request.get_json()
......@@ -360,38 +400,31 @@ class BuildDepend(Resource):
class SelfDepend(Resource):
'''
"""
Description: querying install and build depend for a package
and others which has the same src name
Restful API: post
changeLog:
'''
"""
def post(self):
'''
description: Query a package's all dependencies including install and build depend
"""
Description: Query a package's all dependencies including install and build depend
(support quering a binary or source package in one or more databases)
input:
packageName:
Args:
packageName:package name
packageType: source/binary
selfBuild :0/1
withSubpack: 0/1
dbPreority: the array for database preority
return:
resultList[
restult[
binaryName:
srcName:
dbName:
type: install or build, which depend on the function
parentNode: the binary package name which is the
build/install depend for binaryName
]
]
exception:
changeLog:
'''
dbPreority:the array for database preority
Returns:
{
"code": "",
"data": "",
"msg": ""
}
Raises:
"""
schema = SelfDependSchema()
data = request.get_json()
......@@ -440,22 +473,22 @@ class SelfDepend(Resource):
class BeDepend(Resource):
'''
"""
Description: querying be installed and built depend for a package
and others which has the same src name
Restful API: post
changeLog:
'''
"""
def post(self):
'''
"""
description: Query a package's all dependencies including
be installed and built depend
input:
packageName:
Args:
packageName:package name
withSubpack: 0/1
dbname:
return:
dbname:database name
Returns:
resultList[
restult[
binaryName:
......@@ -468,7 +501,7 @@ class BeDepend(Resource):
]
exception:
changeLog:
'''
"""
schema = BeDependSchema()
data = request.get_json()
validate_err = schema.validate(data)
......@@ -501,19 +534,33 @@ class BeDepend(Resource):
class Repodatas(Resource):
"""API for operating databases"""
"""
description: Get database information and delete database
Restful API: get, delete
ChangeLog:
"""
def get(self):
'''
"""
description: get all database
input:
return:
databasesName
status
priority
exception:
changeLog:
'''
Args:
Returns:
{
"code": "",
"data": [
{
"database_name": "",
"priority": "",
"status": ""
}
],
"msg": ""
}
Raises:
FileNotFoundError: File not found exception
TypeError: Exception of wrong type
Error: abnormal Error
"""
try:
with open(DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context:
init_database_date = yaml.load(
......@@ -535,11 +582,20 @@ class Repodatas(Resource):
)
def delete(self):
'''
"""
description: get all database
input: database name
return: success or failure
'''
Args:
Returns:
{
"code": "",
"data": "",
"msg": ""
}
Raises:
FileNotFoundError: File not found exception,
TypeError: Exception of wrong type
Error: Abnormal error
"""
schema = DeletedbSchema()
data = schema.dump(request.args)
if schema.validate(data):
......@@ -570,15 +626,29 @@ class Repodatas(Resource):
class InitSystem(Resource):
'''InitSystem'''
"""
description: Initialize database
Restful API: post
ChangeLog:
"""
def post(self):
"""
description: InitSystem
input:
return:
exception:
changeLog:
Args:
Returns:
{
"code": "",
"data": "",
"msg": ""
}
Raises:
ContentNoneException: Unable to connect to the exception of the database
DisconnectionError:Exception connecting to database
TypeError:Exception of wrong type
DataMergeException:Exception of merging data
FileNotFoundError:File not found exception
Error: abnormal Error
"""
schema = InitSystemSchema()
......
'''
Integration of multiple sqlite file data, including
reading sqlite database and inserting data
'''
#!/usr/bin/python3
"""
Description: Integration of multiple sqlite file data, including reading
sqlite database and inserting data
Class: MergeData
"""
from sqlalchemy.exc import SQLAlchemyError
from packageship.application.models.temporarydb import src_package
from packageship.application.models.temporarydb import src_requires
......@@ -16,30 +18,56 @@ LOGGER = Log(__name__)
class MergeData():
'''
Load data from sqlite database
'''
"""
Description: Load data from sqlite database
Attributes:
db_file: Database file
db_type: Connected database type
datum_database: Base database name
"""
def __init__(self, db_file):
"""
Description: Class instance initialization
Args:
db_file: Database file
"""
self.db_file = db_file
self.db_type = 'sqlite:///'
self.datum_database = 'maintenance.information'
self.src_requires_dicts = dict()
self.src_package_datas = []
self.bin_provides_dicts = dict()
self.bin_package_datas = []
self.mainter_infos = dict()
@staticmethod
def __columns(cursor):
'''
functional description:Returns all the column names queried by the current cursor
'''
"""
Description: functional description:Returns all the column names
queried by the current cursor
Args:
cursor: Cursor
Returns:
The first columns
Raises:
"""
return [col[0] for col in cursor.description]
def get_package_data(self):
'''
get binary package or source package data
'''
"""
Description: get binary package or source package data
Args:
Returns:
All source package data queried
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True)\
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True) \
as database:
src_packages_data = database.session.execute(
"select pkgKey,name,version,rpm_license,url,rpm_sourcerpm from packages")
......@@ -51,11 +79,17 @@ class MergeData():
return None
def get_requires_data(self):
'''
get dependent package data of binary package or source package
'''
"""
Description: get dependent package data of binary package or source package
Args:
Returns:
All dependent data queried
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True)\
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True) \
as database:
requires = database.session.execute(
"select pkgKey,name from requires")
......@@ -66,11 +100,17 @@ class MergeData():
return None
def get_provides(self):
'''
get the dependency package provided by the binary package
'''
"""
Description: get the dependency package provided by the binary package
Args:
Returns:
Query the component data provided by all binary packages
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True)\
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True) \
as database:
requires = database.session.execute(
"select pkgKey,name from provides")
......@@ -81,9 +121,15 @@ class MergeData():
return None
def get_maintenance_info(self):
'''
Obtain the information of the maintainer
'''
"""
Description: Obtain the information of the maintainer
Args:
Returns:
Maintainer related information
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
if not hasattr(self, 'mainter_infos'):
self.mainter_infos = dict()
......@@ -99,9 +145,17 @@ class MergeData():
LOGGER.logger.error(sql_error)
def src_file_merge(self, src_package_key, db_file):
'''
Source code related data integration
'''
"""
Description: Source code related data integration
Args:
src_package_key: The relevant key value of the source package
db_file: Database file
Returns:
Key value after successful data combination
(0, False) or (src_package_key, True)
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
self.get_maintenance_info()
self.__compose_src_package()
......@@ -131,9 +185,15 @@ class MergeData():
return (src_package_key, True)
def __compose_src_package(self):
'''
Combine source package data
'''
"""
Description: Combine source package data
Args:
Returns:
Raises:
"""
if getattr(self, 'src_package_datas', None) is None:
self.src_package_datas = []
......@@ -142,27 +202,36 @@ class MergeData():
if src_package_name:
# Find the maintainer information of the current data
maintenance_infos = self.mainter_infos.get(src_package_name)
maintenance = None
maintenance = []
version = src_package_item.get('version')
if self.mainter_infos.get(src_package_name):
maintenance = list(filter(lambda x: x.get(
'version') == src_package_item.get('version'), maintenance_infos))
for maintenance_item in maintenance_infos:
if maintenance_item.get('version') == version:
maintenance.append(maintenance_item)
self.src_package_datas.append(
{
"name": src_package_item.get('name'),
"version": src_package_item.get('version'),
"version": version,
"rpm_license": src_package_item.get('rpm_license'),
"url": src_package_item.get('url'),
"pkgKey": src_package_item.get('pkgKey'),
'maintaniner':
maintenance[0].get('maintaniner') if maintenance and len(
maintenance) > 0 else None
maintenance[0].get('maintaniner') if maintenance and len(
maintenance) > 0 else None
}
)
def __compose_src_rquires(self):
'''
Combine source package dependent package data
'''
"""
Description: Combine source package dependent package data
Args:
Returns:
Raises:
"""
if getattr(self, 'src_requires_dicts', None) is None:
self.src_requires_dicts = dict()
......@@ -179,9 +248,15 @@ class MergeData():
)
def __compose_bin_package(self):
'''
Combine binary package data
'''
"""
Description: Combine binary package data
Args:
Returns:
Raises:
AttributeError
"""
if getattr(self, 'bin_package_datas', None) is None:
self.bin_package_datas = []
......@@ -205,9 +280,14 @@ class MergeData():
)
def __compose_bin_requires(self):
'''
Combining binary dependent package data
'''
"""
Description: Combining binary dependent package data
Args:
Returns:
Raises:
"""
if getattr(self, 'bin_requires_dicts', None) is None:
self.bin_requires_dicts = dict()
......@@ -222,9 +302,15 @@ class MergeData():
})
def __compose_bin_provides(self):
'''
Combine binary package data
'''
"""
Description: Combine binary package data
Args:
Returns:
Raises:
"""
if getattr(self, 'bin_provides_dicts', None) is None:
self.bin_provides_dicts = dict()
......@@ -239,10 +325,17 @@ class MergeData():
})
def bin_file_merge(self, bin_package_key, db_file):
'''
Binary package related data integration
'''
"""
Description: Binary package related data integration
Args:
bin_package_key: Primary key of binary package
db_file: Database file
Returns:
Key value after successful data combination
(0, False) or (bin_package_key, True)
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
self.__compose_bin_package()
# binary package dependent package integration
......
'''
Database entity model mapping
'''
#!/usr/bin/python3
"""
Description: Database entity model mapping
"""
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from packageship.libs.dbutils.sqlalchemy_helper import DBHelper
class src_pack(DBHelper.BASE): # pylint: disable=C0103,R0903
'''
functional description:Source package model
"""
Description: functional description:Source package model
modify record:
'''
"""
__tablename__ = 'src_pack'
......@@ -32,10 +33,10 @@ class src_pack(DBHelper.BASE): # pylint: disable=C0103,R0903
class bin_pack(DBHelper.BASE): # pylint: disable=C0103,R0903
'''
functional description:Binary package data
"""
Description: functional description:Binary package data
modify record:
'''
"""
__tablename__ = 'bin_pack'
id = Column(Integer, primary_key=True)
......
'''
Database entity model mapping
'''
#!/usr/bin/python3
"""
Description: Database entity model mapping
"""
from sqlalchemy import Column, Integer, String
from packageship.libs.dbutils.sqlalchemy_helper import DBHelper
class src_package(DBHelper.BASE): # pylint: disable=C0103,R0903
'''
Temporary source package model
'''
"""
Description: Temporary source package model
"""
__tablename__ = 'src_package'
......@@ -26,9 +27,9 @@ class src_package(DBHelper.BASE): # pylint: disable=C0103,R0903
class bin_package(DBHelper.BASE): # pylint: disable=C0103,R0903
'''
Temporary binary package model
'''
"""
Description: Temporary binary package model
"""
__tablename__ = 'bin_package'
pkgKey = Column(Integer, primary_key=True)
......@@ -47,9 +48,9 @@ class bin_package(DBHelper.BASE): # pylint: disable=C0103,R0903
class src_requires(DBHelper.BASE): # pylint: disable=C0103,R0903
'''
Temporary source package depends on package model
'''
"""
Description: Temporary source package depends on package model
"""
__tablename__ = 'src_requires'
id = Column(Integer, primary_key=True)
......@@ -60,9 +61,9 @@ class src_requires(DBHelper.BASE): # pylint: disable=C0103,R0903
class bin_requiresment(DBHelper.BASE): # pylint: disable=C0103,R0903
'''
Dependency package model for temporary binary packages
'''
"""
Description: Dependency package model for temporary binary packages
"""
__tablename__ = 'bin_requiresment'
id = Column(Integer, primary_key=True)
......@@ -73,9 +74,9 @@ class bin_requiresment(DBHelper.BASE): # pylint: disable=C0103,R0903
class bin_provides(DBHelper.BASE): # pylint: disable=C0103,R0903
'''
Provided package model for temporary binary packages
'''
"""
Description: Provided package model for temporary binary packages
"""
__tablename__ = 'bin_provides'
id = Column(Integer, primary_key=True)
......
'''
Basic configuration of flask framework
'''
#!/usr/bin/python3
"""
Description: Basic configuration of flask framework
"""
import random
from packageship.libs.configutils.readconfig import ReadConfig
class Config():
'''
Configuration items in a formal environment
'''
"""
Description: Configuration items in a formal environment
Attributes:
_read_config: read config
_set_config_val: Set the value of the configuration item
"""
SECRET_KEY = None
DEBUG = False
......@@ -23,31 +27,34 @@ class Config():
@classmethod
def _random_secret_key(cls, random_len=32):
'''
Generate random strings
'''
"""
Description: Generate random strings
"""
cls.SECRET_KEY = ''.join(
[random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()') for index in range(random_len)])
@classmethod
def _set_debug(cls, debug):
'''
Set the debugging mode
'''
"""
Description: Set the debugging mode
"""
if debug == 'true':
cls.DEBUG = True
@classmethod
def _set_log_level(cls, log_level):
'''
Set the log level
'''
"""
Description: Set the log level
"""
cls.LOG_LEVEL = log_level
def set_config_val(self):
'''
Set the value of the configuration item
'''
"""
Description: Set the value of the configuration item
Args:
Returns:
Raises:
"""
Config._random_secret_key()
debug = self._read_config.get_system('debug')
......
'''
Read the base class of the configuration file in the system
which mainly includes obtaining specific node values
and obtaining arbitrary node values
'''
#!/usr/bin/python3
"""
Description:Read the base class of the configuration file in the system
which mainly includes obtaining specific node values
and obtaining arbitrary node values
Class:ReadConfig
"""
import configparser
from configparser import NoSectionError
from configparser import NoOptionError
from packageship.system_config import SYS_CONFIG_PATH
class ReadConfig:
'''
Read the configuration file base class in the system
'''
class ReadConfig():
"""
Description: Read the configuration file base class in the system
Attributes:
conf:Configuration file for the system
conf.read:Read the system configuration file
"""
def __init__(self):
self.conf = configparser.ConfigParser()
self.conf.read(SYS_CONFIG_PATH)
def get_system(self, param):
'''
Get any data value under the system configuration node
'''
"""
Description: Get any data value under the system configuration node
Args:
param:The node parameters that need to be obtained
Returns:
Raises:
"""
if param:
try:
return self.conf.get("SYSTEM", param)
......@@ -32,9 +41,13 @@ class ReadConfig:
return None
def get_database(self, param):
'''
Get any data value under the database configuration node
'''
"""
Description: Get any data value under the database configuration node
Args:
param:The node parameters that need to be obtained
Returns:
Raises:
"""
if param:
try:
return self.conf.get("DATABASE", param)
......@@ -45,9 +58,14 @@ class ReadConfig:
return None
def get_config(self, node, param):
'''
Get configuration data under any node
'''
"""
Description: Get configuration data under any node
Args:
node:node
param:The node parameters that need to be obtained
Returns:
Raises:
"""
if all([node, param]):
try:
return self.conf.get(node, param)
......
'''
Simple encapsulation of sqlalchemy orm framework operation database
'''
#!/usr/bin/python3
"""
Description: Simple encapsulation of sqlalchemy orm framework operation database
Class: DBHelper
"""
import os
from sqlalchemy import create_engine
from sqlalchemy import MetaData
......@@ -18,22 +19,34 @@ from packageship import system_config
class DBHelper():
'''
Database connection, operation public class
'''
"""
Description: Database connection, operation public class
Attributes:
user_name: Username
password: Password
ip_address: Ip address
port: Port
db_name: Database name
db_type: Database type
session: Session
"""
# The base class inherited by the data model
BASE = declarative_base()
def __init__(self, user_name=None, passwrod=None, ip_address=None, # pylint: disable=R0913
def __init__(self, user_name=None, password=None, ip_address=None, # pylint: disable=R0913
port=None, db_name=None, db_type=None, **kwargs):
"""
Description: Class instance initialization
"""
self.user_name = user_name
self._readconfig = ReadConfig()
if self.user_name is None:
self.user_name = self._readconfig.get_database('user_name')
self.passwrod = passwrod
if self.passwrod is None:
self.passwrod = self._readconfig.get_database('password')
self.password = password
if self.password is None:
self.password = self._readconfig.get_database('password')
self.ip_address = ip_address
......@@ -67,9 +80,15 @@ class DBHelper():
self.session = None
def _create_engine(self):
'''
Create a database connection object
'''
"""
Description: Create a database connection object
Args:
Returns:
Raises:
DisconnectionError: A disconnect is detected on a raw DB-API connection.
"""
if self.db_type.startswith('sqlite'):
if not self.db_name:
raise DbnameNoneException(
......@@ -78,11 +97,11 @@ class DBHelper():
self.db_type + self.db_name, encoding='utf-8', convert_unicode=True,
connect_args={'check_same_thread': False})
else:
if all([self.user_name, self.passwrod, self.ip_address, self.port, self.db_name]):
if all([self.user_name, self.password, self.ip_address, self.port, self.db_name]):
# create connection object
self.engine = create_engine(URL(**{'database': self.db_name,
'username': self.user_name,
'password': self.passwrod,
'password': self.password,
'host': self.ip_address,
'port': self.port,
'drivername': self.db_type}),
......@@ -93,9 +112,14 @@ class DBHelper():
'A disconnect is detected on a raw DB-API connection')
def _db_file_path(self):
'''
load the path stored in the sqlite database
'''
"""
Description: load the path stored in the sqlite database
Args:
Returns:
Raises:
"""
self.database_file_path = self._readconfig.get_system(
'data_base_path')
if not self.database_file_path:
......@@ -104,12 +128,18 @@ class DBHelper():
os.makedirs(self.database_file_path)
def __enter__(self):
'''
functional description:Create a context manager for the database connection
'''
"""
Description: functional description:Create a context manager for the database connection
Args:
Returns:
Class instance
Raises:
"""
session = sessionmaker()
if getattr(self, 'engine') is None:
if not hasattr(self, 'engine'):
raise DisconnectionError('Abnormal database connection')
session.configure(bind=self.engine)
......@@ -117,28 +147,43 @@ class DBHelper():
return self
def __exit__(self, exc_type, exc_val, exc_tb):
'''
functional description:Release the database connection pool and close the connection
'''
"""
Description: functional description:Release the database connection pool
and close the connection
Args:
Returns:
exc_type: Abnormal type
exc_val: Abnormal value
exc_tb: Abnormal table
Raises:
"""
self.session.close()
@classmethod
def create_all(cls, db_name=None):
'''
functional description:Create all database tables
parameter:
return value:
exception description:
modify record:
'''
"""
Description: functional description:Create all database tables
Args:
db_name: Database name
Returns:
Raises:
"""
cls.BASE.metadata.create_all(bind=cls(db_name=db_name).engine)
def create_table(self, tables):
'''
Create a single table
'''
"""
Description: Create a single table
Args:
tables: Table
Returns:
Raises:
"""
meta = MetaData(self.engine)
for table_name in DBHelper.BASE.metadata.tables.keys():
if table_name in tables:
......@@ -147,12 +192,16 @@ class DBHelper():
table.create()
def add(self, entity):
'''
functional description:Insert a single data entity
parameter:
return value:
"""
Description: Insert a single data entity
Args:
entity: Data entity
Return:
If the addition is successful, return the corresponding entity, otherwise return None
'''
Raises:
ContentNoneException: An exception occurred while content is none
SQLAlchemyError: An exception occurred while creating the database
"""
if entity is None:
raise ContentNoneException(
......@@ -168,12 +217,17 @@ class DBHelper():
return entity
def batch_add(self, dicts, model):
'''
functional description:tables for adding databases in bulk
parameter:
:param dicts:Entity dictionary data to be added
:param model:Solid model class
'''
"""
Description:tables for adding databases in bulk
Args:
dicts:Entity dictionary data to be added
model:Solid model class
Returns:
Raises:
TypeError: An exception occurred while incoming type does not meet expectations
SQLAlchemyError: An exception occurred while creating the database
"""
if model is None:
raise ContentNoneException('solid model must be specified')
......
'''
System exception information
'''
#!/usr/bin/python3
"""
Description:System exception information
Class:Error,ContentNoneException,DbnameNoneException,
DatabaseRepeatException,DataMergeException
"""
class Error(Exception):
"""
Base class for ConfigParser exceptions
Description: Read the configuration file base class in the system
Attributes:
message:Exception information
"""
def __init__(self, msg=''):
......@@ -19,36 +25,40 @@ class Error(Exception):
class ContentNoneException(Error):
'''
Content is empty exception
'''
"""
Description: Content is empty exception
Attributes:
"""
def __init__(self, message):
Error.__init__(self, 'No content: %r' % (message,))
class DbnameNoneException(ContentNoneException):
'''
Exception with empty database name
'''
"""
Description: Exception with empty database name
Attributes:
"""
def __init__(self, message):
ContentNoneException.__init__(self, '%r' % (message,))
class DatabaseRepeatException(Error):
'''
There are duplicate exceptions in the database
'''
"""
Description: There are duplicate exceptions in the database
Attributes:
"""
def __init__(self, message):
Error.__init__(self, 'Database repeat: %r' % (message,))
class DataMergeException(Error):
'''
abnormal integration data
'''
"""
Description: abnormal integration data
Attributes:
"""
def __init__(self, message):
Error.__init__(self, 'DataMerge exception: %r' % (message,))
#!/usr/bin/python3
'''
Logging related
'''
......@@ -53,6 +54,7 @@ class Log():
def __init__(self, name=__name__, path=None):
self.__name = name
self.__path = path
self.__file_handler = None
if self.__path is None:
self.__path = READCONFIG.get_system('log_path')
log_name = READCONFIG.get_config('LOG', 'log_name')
......
'''
Entry for project initialization and service startupc
'''
#!/usr/bin/python3
"""
Description: Entry for project initialization and service startupc
"""
import os
from flask_script import Manager
from packageship.libs.exception import Error
try:
from packageship.system_config import SYS_CONFIG_PATH
......@@ -28,6 +28,9 @@ else:
@app.before_request
def before_request():
"""
Description: Global request interception
"""
if not identity_verification():
return 'No right to perform operation'
......
此差异已折叠。
'''
Entry for project initialization and service startupc
'''
#!/usr/bin/python3
"""
Description: Entry for project initialization and service startupc
"""
import os
from flask_script import Manager
from packageship.libs.exception import Error
......@@ -28,6 +29,9 @@ else:
@app.before_request
def before_request():
"""
Description: Global request interception
"""
if not identity_verification():
return 'No right to perform operation'
......
'''
System-level file configuration, mainly configure
#!/usr/bin/python3
"""
Description:System-level file configuration, mainly configure
the address of the operating environment, commonly used variables, etc.
'''
"""
import os
import sys
......
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
Compare the values in two Python data types for equality, ignoring the order of values
......@@ -38,7 +39,7 @@ def get_correct_json_by_filename(filename):
"test",
"common_files",
"correct_test_result_json",
f"{filename}.json")
"{}.json".format(filename))
try:
with open(json_path, "r") as json_fp:
correct_list = json.loads(json_fp.read())
......
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
InitConf
"""
import os
from configparser import ConfigParser
from packageship import system_config
......@@ -7,6 +10,10 @@ import yaml
class InitConf:
"""
InitConf
"""
def __init__(self):
base_path = os.path.join(os.path.dirname(system_config.BASE_PATH),
"test",
......@@ -23,8 +30,10 @@ class InitConf:
origin_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
for index, obj in enumerate(origin_yaml, 1):
src_path = os.path.join(base_path, "db_origin", f"data_{str(index)}_src.sqlite")
bin_path = os.path.join(base_path, "db_origin", f"data_{str(index)}_bin.sqlite")
src_path = os.path.join(base_path, "db_origin",
"data_{}_src.sqlite".format(str(index)))
bin_path = os.path.join(base_path, "db_origin",
"data_{}_bin.sqlite".format(str(index)))
obj["src_db_file"] = [src_path]
obj["bin_db_file"] = [bin_path]
with open(conf_path, 'w', encoding='utf-8') as w_f:
......
#!/usr/bin/python3
"""
Inherited from unittest.TestResult,
The simple statistical function is realized.
"""
import sys
import unittest
class MyTestResult(unittest.TestResult):
"""
Inherited from unittest.TestResult,
The simple statistical function is realized.
"""
def __init__(self, verbosity=0):
super(MyTestResult, self).__init__()
self.success_case_count = 0
self.err_case_count = 0
self.failure_case_count = 0
self.verbosity = verbosity
def addSuccess(self, test):
"""When the use case is executed successfully"""
self.success_case_count += 1
super(MyTestResult, self).addSuccess(test)
if self.verbosity > 0:
sys.stderr.write('Success ')
sys.stderr.write(str(test))
sys.stderr.write('\n')
def addError(self, test, err):
"""When a code error causes a use case to fail"""
self.err_case_count += 1
super(MyTestResult, self).addError(test, err)
if self.verbosity > 0:
sys.stderr.write('Error ')
sys.stderr.write(str(test)+'\n')
_,err_info = self.errors[-1]
sys.stderr.write(err_info)
sys.stderr.write('\n')
def addFailure(self, test, err):
"""When the assertion is false"""
self.failure_case_count += 1
super(MyTestResult, self).addFailure(test, err)
if self.verbosity > 0:
sys.stderr.write('Failure ')
sys.stderr.write(str(test)+'\n')
_, err_info = self.failures[-1]
sys.stderr.write(err_info)
sys.stderr.write('\n')
class MyTestRunner():
"""
Run All TestCases
"""
def __init__(self, verbosity=0):
self.verbosity = verbosity
def run(self, test):
"""run MyTestResult and return result"""
result = MyTestResult(self.verbosity)
test(result)
return result
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
OperateTestBase
"""
import os
import unittest
from packageship.libs.exception import Error
try:
from packageship import system_config
......@@ -21,14 +26,14 @@ try:
from test.base_code.init_config_path import init_config
from packageship.manage import app
except Exception as e:
except Error:
raise
class OperateTestBase(unittest.TestCase):
"""
OperateTestBase
"""
def setUp(self) -> None:
def setUp(self):
self.client = app.test_client()
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import os
import unittest
from packageship.libs.exception import Error
try:
from packageship import system_config
......@@ -22,10 +24,10 @@ try:
from test.base_code.init_config_path import init_config
from packageship.selfpkg import app
except Exception as e:
except Error:
raise
class ReadTestBase(unittest.TestCase):
def setUp(self) -> None:
def setUp(self):
self.client = app.test_client()
[
{
"input": {
"binaryName": "A1"
},
"output": {
"code": "2001",
"data": {
"A1": [
"A",
"0.0.23b",
"openEuler-20.03-LTS",
[
[
"root",
null
],
[
"D1",
"install"
]
]
],
"A2": [
"A",
"0.0.23b",
"openEuler-20.03-LTS",
[
[
"A1",
"install"
],
[
"C1",
"install"
]
]
],
"C1": [
"C",
"0.1",
"openEuler-20.03-LTS",
[
[
"A2",
"install"
]
]
],
"D1": [
"D",
"0.11",
"openEuler-20.03-LTS",
[
[
"A2",
"install"
]
]
]
},
"msg": "Successful Operation!"
}
},
{
"input": {
"binaryName": "D2"
},
"output": {
"code": "2001",
"data": {
"A1": [
"A",
"0.0.23b",
"openEuler-20.03-LTS",
[
[
"D1",
"install"
]
]
],
"A2": [
"A",
"0.0.23b",
"openEuler-20.03-LTS",
[
[
"A1",
"install"
],
[
"C1",
"install"
]
]
],
"C1": [
"C",
"0.1",
"openEuler-20.03-LTS",
[
[
"A2",
"install"
]
]
],
"D1": [
"D",
"0.11",
"openEuler-20.03-LTS",
[
[
"D2",
"install"
],
[
"A2",
"install"
]
]
],
"D2": [
"D",
"0.11",
"openEuler-20.03-LTS",
[
[
"root",
null
]
]
]
},
"msg": "Successful Operation!"
}
},
{
"input": {
"binaryName": "C2"
},
"output": {
"code": "2001",
"data": {
"C2": [
"C",
"0.1",
"openEuler-20.03-LTS",
[
[
"root",
null
]
]
]
},
"msg": "Successful Operation!"
}
}
]
\ No newline at end of file
# -*- coding:utf-8 -*-
"""Test case of init data"""
from test.test_module.init_system_tests.test_importdata import test_import_data_suit
def execute_init():
"""Test case of init data"""
test_import_data_suit()
if __name__ == '__main__':
execute_init()
# -*- coding:utf-8 -*-
"""
Test case of reading data
"""
from test.test_module.dependent_query_tests.test_build_depend import test_build_depend_suit
from test.test_module.packages_tests.test_packages import test_packages_suit
from test.test_module.single_package_tests.test_get_singlepack import test_get_single_package_suit
from test.test_module.repodatas_test.test_get_repodatas import test_get_repodatas_suit
from test.test_module.dependent_query_tests.test_be_depend import test_be_depend_suit
def execute_read():
"""Test case of reading data"""
test_build_depend_suit()
test_packages_suit()
test_get_single_package_suit()
test_get_repodatas_suit()
test_be_depend_suit()
if __name__ == '__main__':
execute_read()
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
Execute all test cases
"""
import unittest
import datetime
from test.base_code.my_test_runner import MyTestRunner
RUNNER = MyTestRunner(verbosity=1)
def import_data_tests():
"""Initialize related test cases"""
from test.test_module.init_system_tests.test_importdata import ImportData
suite = unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(ImportData))
return RUNNER.run(suite)
def read_data_tests():
"""Test cases with read operations on data"""
from test.test_module.dependent_query_tests.test_install_depend import TestInstallDepend
from test.test_module.dependent_query_tests.test_self_depend import TestSelfDepend
from test.test_module.dependent_query_tests.test_be_depend import TestBeDepend
from test.test_module.repodatas_test.test_get_repodatas import TestGetRepodatas
from test.test_module.dependent_query_tests.test_build_depend import TestBuildDepend
from test.test_module.packages_tests.test_packages import TestPackages
from test.test_module.single_package_tests.test_get_singlepack import TestGetSinglePack
suite = unittest.TestSuite()
classes = [TestInstallDepend, TestSelfDepend, TestBeDepend,
TestGetRepodatas, TestBuildDepend, TestPackages, TestGetSinglePack]
for cls in classes:
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(cls))
return RUNNER.run(suite)
def write_data_tests():
"""Test cases with write operations on data"""
from test.test_module.repodatas_test.test_delete_repodatas import TestDeleteRepodatas
from test.test_module.single_package_tests.test_update_singlepack import TestUpdatePackage
suite = unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestDeleteRepodatas))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestUpdatePackage))
return RUNNER.run(suite)
def main():
"""Test case execution entry function"""
start_time = datetime.datetime.now()
result_4_import = import_data_tests()
result_4_read = read_data_tests()
result_4_write = write_data_tests()
stop_time = datetime.datetime.now()
print('\nA total of %s test cases were run: \nsuccessful:%s\tfailed:%s\terror:%s\n' % (
int(result_4_import.testsRun) + int(result_4_read.testsRun) + int(result_4_write.testsRun),
int(
result_4_import.success_case_count
) + int(result_4_read.success_case_count) + int(result_4_write.success_case_count),
int(
result_4_import.failure_case_count
) + int(result_4_read.failure_case_count) + int(result_4_write.failure_case_count),
int(
result_4_import.err_case_count
) + int(result_4_read.err_case_count) + int(result_4_write.err_case_count)
))
print('Total Time: %s' % (stop_time - start_time))
main()
# -*- coding:utf-8 -*-
"""Test case of writing data"""
from test.test_module.repodatas_test.test_delete_repodatas import test_delete_repodatas_suit
from test.test_module.single_package_tests.test_update_singlepack import test_updata_single_package_suit
def execute_operate():
"""Test case of writing data"""
test_updata_single_package_suit()
test_delete_repodatas_suit()
if __name__ == '__main__':
execute_operate()
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
Less transmission is always parameter transmission
......@@ -8,10 +9,11 @@ from test.base_code.read_data_base import ReadTestBase
from test.base_code.common_test_code import compare_two_values, get_correct_json_by_filename
from packageship.application.apps.package.function.constants import ResponseCode
class TestBeDepend(ReadTestBase):
'''
"""
The dependencies of the package are tested
'''
"""
def test_lack_parameter(self):
"""
......@@ -355,18 +357,3 @@ class TestBeDepend(ReadTestBase):
resp_dict = json.loads(resp.data)
self.assertTrue(compare_two_values(output_for_input, resp_dict),
msg="The answer is not correct")
def test_be_depend_suit():
"""
Start the test case function
"""
print("---TestBeDepend START---")
suite = unittest.TestSuite()
suite.addTest(TestBeDepend("test_lack_parameter"))
suite.addTest(TestBeDepend("test_wrong_parameter"))
suite.addTest(TestBeDepend("test_true_params_result"))
unittest.TextTestRunner().run(suite)
# if __name__ == '__main__':
# unittest.main()
#!/usr/bin/python3
# -*- coding:utf-8 -*-
'''
"""
build_depend unittest
'''
"""
import json
import unittest
......@@ -11,15 +12,15 @@ from packageship.application.apps.package.function.constants import ResponseCode
class TestBuildDepend(ReadTestBase):
'''
"""
class for test build_depend
'''
"""
def test_empty_source_name_dblist(self):
'''
"""
test empty parameters:sourceName,dbList
:return:
'''
"""
resp = self.client.post("/packages/findBuildDepend",
data="{}",
content_type="application/json")
......@@ -57,10 +58,10 @@ class TestBuildDepend(ReadTestBase):
self.assertIsNotNone(resp_dict.get("data"), msg="Error in data information return")
def test_wrong_source_name_dblist(self):
'''
"""
test wrong parameters:sourceName,dbList
:return: None
'''
"""
resp = self.client.post("/packages/findBuildDepend",
data=json.dumps({"sourceName": 0}),
content_type="application/json")
......@@ -136,6 +137,11 @@ class TestBuildDepend(ReadTestBase):
self.assertIsNone(resp_dict.get("data"), msg="Error in data information return")
def test_true_params_result(self):
"""
test_true_params_result
Returns:
"""
correct_list = get_correct_json_by_filename("build_depend")
self.assertNotEqual([], correct_list, msg="Error reading JSON file")
......@@ -151,14 +157,5 @@ class TestBuildDepend(ReadTestBase):
msg="The answer is not correct")
def test_build_depend_suit():
print("---TestBuildDepend START---")
suite = unittest.TestSuite()
suite.addTest(TestBuildDepend("test_empty_source_name_dblist"))
suite.addTest(TestBuildDepend("test_wrong_source_name_dblist"))
suite.addTest(TestBuildDepend("test_true_params_result"))
unittest.TextTestRunner().run(suite)
if __name__ == '__main__':
unittest.main()
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
TestInstallDepend
"""
import unittest
import json
from test.base_code.common_test_code import get_correct_json_by_filename, compare_two_values
from test.base_code.read_data_base import ReadTestBase
from packageship.application.apps.package.function.constants import ResponseCode
class TestInstallDepend(ReadTestBase):
"""
TestInstallDepend
"""
def test_empty_binaryName_dbList(self):
"""
test_empty_binaryName_dbList
Returns:
"""
resp = self.client.post("/packages/findInstallDepend",
data="{}",
content_type="application/json")
resp_dict = json.loads(resp.data)
self.assertIn("code", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.PARAM_ERROR,
resp_dict.get("code"),
msg="Error in status code return")
self.assertIn("msg", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.CODE_MSG_MAP.get(ResponseCode.PARAM_ERROR),
resp_dict.get("msg"),
msg="Error in status prompt return")
self.assertIn("data", resp_dict, msg="Error in data format return")
self.assertIsNone(resp_dict.get("data"), msg="Error in data information return")
resp = self.client.post("/packages/findInstallDepend",
data=json.dumps({"binaryName": "A1"}),
content_type="application/json")
resp_dict = json.loads(resp.data)
self.assertIn("code", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.SUCCESS,
resp_dict.get("code"),
msg="Error in status code return")
self.assertIn("msg", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.CODE_MSG_MAP.get(ResponseCode.SUCCESS),
resp_dict.get("msg"),
msg="Error in status prompt return")
self.assertIn("data", resp_dict, msg="Error in data format return")
self.assertIsNotNone(resp_dict.get("data"), msg="Error in data information return")
def test_wrong_binaryName_dbList(self):
"""
test_empty_binaryName_dbList
Returns:
"""
resp = self.client.post("/packages/findInstallDepend",
data=json.dumps({"binaryName": 0}),
content_type="application/json")
resp_dict = json.loads(resp.data)
self.assertIn("code", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.PARAM_ERROR,
resp_dict.get("code"),
msg="Error in status code return")
self.assertIn("msg", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.CODE_MSG_MAP.get(ResponseCode.PARAM_ERROR),
resp_dict.get("msg"),
msg="Error in status prompt return")
self.assertIn("data", resp_dict, msg="Error in data format return")
self.assertIsNone(resp_dict.get("data"), msg="Error in data information return")
resp = self.client.post("/packages/findInstallDepend",
data=json.dumps(
{"binaryName": "qitiandasheng"}),
content_type="application/json")
resp_dict = json.loads(resp.data)
self.assertIn("code", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.SUCCESS,
resp_dict.get("code"),
msg="Error in status code return")
self.assertIn("msg", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.CODE_MSG_MAP.get(ResponseCode.SUCCESS),
resp_dict.get("msg"),
msg="Error in status prompt return")
self.assertIn("data", resp_dict, msg="Error in data format return")
self.assertIsNotNone(resp_dict.get("data"), msg="Error in data information return")
resp = self.client.post("/packages/findInstallDepend",
data=json.dumps({"binaryName": "A1",
"db_list": [12, 3, 4]}),
content_type="application/json")
resp_dict = json.loads(resp.data)
self.assertIn("code", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.PARAM_ERROR,
resp_dict.get("code"),
msg="Error in status code return")
self.assertIn("msg", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.CODE_MSG_MAP.get(ResponseCode.PARAM_ERROR),
resp_dict.get("msg"),
msg="Error in status prompt return")
self.assertIn("data", resp_dict, msg="Error in data format return")
self.assertIsNone(resp_dict.get("data"), msg="Error in data information return")
resp = self.client.post("/packages/findInstallDepend",
data=json.dumps({"binaryName": "A1",
"db_list": ["shifu", "bajie"]
}), content_type="application/json")
resp_dict = json.loads(resp.data)
self.assertIn("code", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.DB_NAME_ERROR,
resp_dict.get("code"),
msg="Error in status code return")
self.assertIn("msg", resp_dict, msg="Error in data format return")
self.assertEqual(ResponseCode.CODE_MSG_MAP.get(ResponseCode.DB_NAME_ERROR),
resp_dict.get("msg"),
msg="Error in status prompt return")
self.assertIn("data", resp_dict, msg="Error in data format return")
self.assertIsNone(resp_dict.get("data"), msg="Error in data information return")
def test_true_params_result(self):
"""
test_empty_binaryName_dbList
Returns:
"""
correct_list = get_correct_json_by_filename("install_depend")
self.assertNotEqual([], correct_list, msg="Error reading JSON file")
for correct_data in correct_list:
input_value = correct_data["input"]
resp = self.client.post("/packages/findInstallDepend",
data=json.dumps(input_value),
content_type="application/json")
output_for_input = correct_data["output"]
resp_dict = json.loads(resp.data)
self.assertTrue(compare_two_values(output_for_input, resp_dict),
msg="The answer is not correct")
if __name__ == '__main__':
unittest.main()
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
test import_databases
......@@ -5,8 +6,11 @@ test import_databases
import os
import shutil
import unittest
import warnings
from configparser import ConfigParser
import yaml
from packageship import system_config
from packageship.libs.exception import Error
try:
......@@ -29,13 +33,12 @@ try:
from test.base_code.init_config_path import init_config
except Exception:
raise
import warnings
import yaml
except Error:
raise Error
from packageship.application.initsystem.data_import import InitDataBase
from packageship.libs.exception import ContentNoneException, DatabaseRepeatException
from packageship.libs.exception import ContentNoneException
from packageship.libs.exception import DatabaseRepeatException
from packageship.libs.configutils.readconfig import ReadConfig
......@@ -49,13 +52,12 @@ class ImportData(unittest.TestCase):
warnings.filterwarnings("ignore")
def test_empty_param(self):
# If init is not obtained_ conf_ Path parameter
"""If init is not obtained_ conf_ Path parameter"""
try:
InitDataBase(config_file_path=None).init_data()
except Exception as e:
except ContentNoneException as error:
self.assertEqual(
e.__class__,
error.__class__,
ContentNoneException,
msg="No init in package_ conf_ Path parameter, wrong exception type thrown")
......@@ -69,9 +71,9 @@ class ImportData(unittest.TestCase):
w_f.write("")
InitDataBase(config_file_path=_config_path).init_data()
except Exception as e:
except ContentNoneException as error:
self.assertEqual(
e.__class__,
error.__class__,
ContentNoneException,
msg="Yaml file exists, but the content is empty. The exception type is wrong")
finally:
......@@ -83,18 +85,18 @@ class ImportData(unittest.TestCase):
try:
_config_path = ReadConfig().get_system('init_conf_path')
shutil.copyfile(_config_path, _config_path + '.bak')
with open(_config_path, 'r', encoding='utf-8') as f:
origin_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
with open(_config_path, 'r', encoding='utf-8') as file:
origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader)
for obj in origin_yaml:
obj["dbname"] = "openEuler"
with open(_config_path, 'w', encoding='utf-8') as w_f:
yaml.dump(origin_yaml, w_f)
InitDataBase(config_file_path=_config_path).init_data()
except Exception as e:
except DatabaseRepeatException as error:
self.assertEqual(
e.__class__,
error.__class__,
DatabaseRepeatException,
msg="Yaml file exists but DB_ Name duplicate exception type is wrong")
finally:
......@@ -103,8 +105,8 @@ class ImportData(unittest.TestCase):
os.rename(_config_path + '.bak', _config_path)
def test_wrong_param(self):
# If the corresponding current init_ conf_ The directory specified by
# path is incorrect
"""If the corresponding current init_ conf_ The directory
specified by path is incorrect"""
try:
# Back up source files
shutil.copyfile(
......@@ -118,9 +120,9 @@ class ImportData(unittest.TestCase):
_config_path = ReadConfig().get_system('init_conf_path')
InitDataBase(config_file_path=_config_path).init_data()
except Exception as e:
except FileNotFoundError as error:
self.assertEqual(
e.__class__,
error.__class__,
FileNotFoundError,
msg="init_ conf_ Path specified directory is empty exception type is wrong")
finally:
......@@ -144,10 +146,10 @@ class ImportData(unittest.TestCase):
_config_path = ReadConfig().get_system('init_conf_path')
InitDataBase(config_file_path=None).init_data()
except Exception as e:
except Error as error:
self.assertEqual(
e.__class__,
Exception,
error.__class__,
Error,
msg="Wrong exception type thrown when dbtype is wrong")
finally:
# To restore a file, delete the file first and then rename it back
......@@ -157,61 +159,60 @@ class ImportData(unittest.TestCase):
system_config.SYS_CONFIG_PATH)
def test_dbname(self):
"""test dbname"""
try:
_config_path = ReadConfig().get_system('init_conf_path')
shutil.copyfile(_config_path, _config_path + '.bak')
with open(_config_path, 'r', encoding='utf-8') as f:
origin_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
with open(_config_path, 'r', encoding='utf-8') as file:
origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader)
for obj in origin_yaml:
obj["dbname"] = ""
with open(_config_path, 'w', encoding='utf-8') as w_f:
yaml.dump(origin_yaml, w_f)
InitDataBase(config_file_path=_config_path).init_data()
with open(system_config.DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context:
init_database_date = yaml.load(
file_context.read(), Loader=yaml.FullLoader)
except DatabaseRepeatException as error:
self.assertEqual(
init_database_date,
None,
msg=" Priority must be a positive integer between 0 and 100 ")
except Exception as e:
return
error.__class__,
DatabaseRepeatException,
msg="Yaml file exists but DB_ Name duplicate exception type is wrong")
finally:
# Restore files
os.remove(_config_path)
os.rename(_config_path + '.bak', _config_path)
def test_src_db_file(self):
"""test src db file"""
try:
_config_path = ReadConfig().get_system('init_conf_path')
shutil.copyfile(_config_path, _config_path + '.bak')
with open(_config_path, 'r', encoding='utf-8') as f:
origin_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
with open(_config_path, 'r', encoding='utf-8') as file:
origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader)
for obj in origin_yaml:
obj["src_db_file"] = ""
with open(_config_path, 'w', encoding='utf-8') as w_f:
yaml.dump(origin_yaml, w_f)
InitDataBase(config_file_path=_config_path).init_data()
with open(system_config.DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context:
init_database_date = yaml.load(
file_context.read(), Loader=yaml.FullLoader)
except TypeError as error:
self.assertEqual(
init_database_date,
None,
msg=" Priority must be a positive integer between 0 and 100 ")
except Exception as e:
return
error.__class__,
TypeError,
msg="Yaml file exists but DB_ Name duplicate exception type is wrong")
finally:
# Restore files
os.remove(_config_path)
os.rename(_config_path + '.bak', _config_path)
def test_priority(self):
"""test priority"""
try:
_config_path = ReadConfig().get_system('init_conf_path')
shutil.copyfile(_config_path, _config_path + '.bak')
with open(_config_path, 'r', encoding='utf-8') as f:
origin_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
with open(_config_path, 'r', encoding='utf-8') as file:
origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader)
for obj in origin_yaml:
obj["priority"] = "-1"
with open(_config_path, 'w', encoding='utf-8') as w_f:
......@@ -224,7 +225,7 @@ class ImportData(unittest.TestCase):
init_database_date,
None,
msg=" Priority must be a positive integer between 0 and 100 ")
except Exception as e:
except FileNotFoundError:
return
finally:
# Restore files
......@@ -232,9 +233,9 @@ class ImportData(unittest.TestCase):
os.rename(_config_path + '.bak', _config_path)
def test_true_init_data(self):
'''
"""
Initialization of system data
'''
"""
# Normal configuration
_config_path = ReadConfig().get_system('init_conf_path')
InitDataBase(config_file_path=_config_path).init_data()
......@@ -250,11 +251,11 @@ class ImportData(unittest.TestCase):
# And there is data in this file, and it comes from the yaml file of
# conf
with open(_config_path, 'r', encoding='utf-8') as f:
yaml_config = yaml.load(f.read(), Loader=yaml.FullLoader)
with open(_config_path, 'r', encoding='utf-8') as file:
yaml_config = yaml.load(file.read(), Loader=yaml.FullLoader)
with open(path, 'r', encoding='utf-8') as f:
yaml_success = yaml.load(f.read(), Loader=yaml.FullLoader)
with open(path, 'r', encoding='utf-8') as files:
yaml_success = yaml.load(files.read(), Loader=yaml.FullLoader)
self.assertEqual(
len(yaml_config),
......@@ -264,7 +265,8 @@ class ImportData(unittest.TestCase):
# Compare name and priority
success_name_priority = dict()
config_name_priority = dict()
for i in range(len(yaml_config)):
len_con = len(yaml_config)
for i in range(len_con):
success_name_priority[yaml_success[i]["database_name"]] = \
yaml_success[i]["priority"]
config_name_priority[yaml_config[i]["dbname"]] = \
......@@ -273,17 +275,5 @@ class ImportData(unittest.TestCase):
self.assertEqual(
success_name_priority,
config_name_priority,
msg="The database and priority after initialization are inconsistent with the original file")
def test_import_data_suit():
"""test_import_data_suit"""
print("-----ImportData START----")
suite = unittest.TestSuite()
suite.addTest(ImportData("test_empty_param"))
suite.addTest(ImportData("test_wrong_param"))
suite.addTest(ImportData("test_dbname"))
suite.addTest(ImportData("test_src_db_file"))
suite.addTest(ImportData("test_priority"))
suite.addTest(ImportData("test_true_init_data"))
unittest.TextTestRunner().run(suite)
msg="The database and priority after initialization are"
"inconsistent with the original file")
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册