diff --git a/packageship/packageship/__init__.py b/packageship/packageship/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8d516bd6c4deacca55ea3ce12069eb68a522d75d 100644 --- a/packageship/packageship/__init__.py +++ b/packageship/packageship/__init__.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 +""" +The root path of the project +""" +import os +import sys + +if "SETTINGS_FILE_PATH" not in os.environ: + os.environ["SETTINGS_FILE_PATH"] = '/etc/pkgship/package.ini' + + +# The root directory where the system is running +if getattr(sys, 'frozen', False): + BASE_PATH = os.path.dirname(os.path.realpath(sys.argv[0])) +else: + BASE_PATH = os.path.abspath(os.path.dirname(__file__)) diff --git a/packageship/packageship/application/__init__.py b/packageship/packageship/application/__init__.py index 810d6ce59ab6936a9c982d1fb8e8a3dcdfe778f4..ef52e65f979b33139d75da6c8d250fb66eacf0da 100644 --- a/packageship/packageship/application/__init__.py +++ b/packageship/packageship/application/__init__.py @@ -7,12 +7,9 @@ import threading from flask import Flask from flask_session import Session from flask_apscheduler import APScheduler -from packageship import system_config from packageship.application.settings import Config from packageship.libs.log import setup_log -from packageship.libs.configutils.readconfig import ReadConfig - -OPERATION = None +from packageship.libs.conf import configuration def _timed_task(app): @@ -20,13 +17,10 @@ def _timed_task(app): """ from .apps.lifecycle.function.download_yaml import update_pkg_info # pylint: disable=import-outside-toplevel - - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - - _hour = _readconfig.get_config('TIMEDTASK', 'hour') + _hour = configuration.HOUR if not _hour or not isinstance(_hour, int) or _hour < 0 or _hour > 23: _hour = 3 - _minute = _readconfig.get_config('TIMEDTASK', 'minute') + _minute = configuration.MINUTE if not _hour or not isinstance(_hour, int) or _hour < 0 or _hour > 59: _minute = 0 app.apscheduler.add_job( # pylint: disable=no-member @@ -40,7 +34,7 @@ def _timed_task(app): args=(False,)) -def init_app(operation): +def init_app(): """ Project initialization function """ @@ -61,9 +55,6 @@ def init_app(operation): # Open session function Session(app) - global OPERATION # pylint: disable=global-statement - OPERATION = operation - # Register Blueprint from packageship.application import apps # pylint: disable=import-outside-toplevel for blue, api in apps.blue_point: diff --git a/packageship/packageship/application/app_global.py b/packageship/packageship/application/app_global.py index 611f309e9cab2895f7462fd45acacbff727ff468..8ad15088c7dd3c5bc81d89deb965621b08f84a57 100644 --- a/packageship/packageship/application/app_global.py +++ b/packageship/packageship/application/app_global.py @@ -2,13 +2,15 @@ """ Description: Interception before request """ +import os from flask import request -from packageship import application from packageship.application.apps.package.url import urls __all__ = ['identity_verification'] +PERMISSION_SERVICE = os.environ.get('PERMISSION_SERVICE') + def identity_verification(): """ @@ -20,8 +22,8 @@ def identity_verification(): if request.url_rule: url_rule = request.url_rule.rule for _view, url, authentication in urls: - if url.lower() == url_rule.lower() and application.OPERATION in authentication.keys(): - if request.method not in authentication.get(application.OPERATION): + if url.lower() == url_rule.lower() and PERMISSION_SERVICE in authentication.keys(): + if request.method not in authentication.get(PERMISSION_SERVICE): return False break return True diff --git a/packageship/packageship/application/apps/lifecycle/__init__.py b/packageship/packageship/application/apps/lifecycle/__init__.py index d17a06a54ae6322f651c9e9f125b645e31696989..8783b8a0f61205a1f10194e84ee4cf174fee5a9d 100644 --- a/packageship/packageship/application/apps/lifecycle/__init__.py +++ b/packageship/packageship/application/apps/lifecycle/__init__.py @@ -2,18 +2,19 @@ """ Blueprint registration for life cycle """ +import os from flask.blueprints import Blueprint from flask_restful import Api from packageship.application.apps.lifecycle.url import urls -from packageship import application lifecycle = Blueprint('lifecycle', __name__) +PERMISSION_SERVICE = os.environ.get('PERMISSION_SERVICE') # init restapi api = Api() for view, url, operation in urls: - if application.OPERATION and application.OPERATION in operation.keys(): + if PERMISSION_SERVICE and PERMISSION_SERVICE in operation.keys(): api.add_resource(view, url) diff --git a/packageship/packageship/application/apps/lifecycle/function/base.py b/packageship/packageship/application/apps/lifecycle/function/base.py deleted file mode 100644 index 3631dde4aae9dba270c91c60f08d4e66511fb7e1..0000000000000000000000000000000000000000 --- a/packageship/packageship/application/apps/lifecycle/function/base.py +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/python3 -""" -General approach to version control tools -""" -from packageship.libs.log import Log - - -class Base(): - """ - Public method to get project tags and download yaml file - """ - - def __init__(self): - self.headers = { - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW 64; rv:50.0) Gecko/20100101 \ - Firefox / 50.0 '} - self.log = Log(__name__) diff --git a/packageship/packageship/application/apps/lifecycle/function/download_yaml.py b/packageship/packageship/application/apps/lifecycle/function/download_yaml.py index 9e50ba3f4a36cf8dbe9e396348d998aa1e7ae8c3..d2204f36495e65a45c5c33d203667e638aeb97b0 100644 --- a/packageship/packageship/application/apps/lifecycle/function/download_yaml.py +++ b/packageship/packageship/application/apps/lifecycle/function/download_yaml.py @@ -12,13 +12,12 @@ import yaml from retrying import retry from sqlalchemy.exc import SQLAlchemyError from requests.exceptions import HTTPError -from packageship import system_config +from packageship.libs.log import LOGGER +from packageship.libs.conf import configuration from packageship.application.models.package import Packages from packageship.application.models.package import PackagesMaintainer from packageship.libs.dbutils import DBHelper from packageship.libs.exception import Error, ContentNoneException -from packageship.libs.configutils.readconfig import ReadConfig -from .base import Base from .gitee import Gitee from .concurrent import ProducerConsumer @@ -30,46 +29,24 @@ class ParseYaml(): relevant information into the database Attributes: - base: base class instance pkg: Specific package data _table_name: The name of the data table to be operated openeuler_advisor_url: Get the warehouse address of the yaml file _yaml_content: The content of the yaml file """ - def __init__(self, pkg_info, base, table_name): - self.base = base + def __init__(self, pkg_info, table_name): + self.headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW 64; rv:50.0) Gecko/20100101 \ + Firefox / 50.0 '} self.pkg = pkg_info self._table_name = table_name - self.openeuler_advisor_url = self._path_stitching(pkg_info.name) + self.openeuler_advisor_url = configuration.WAREHOUSE_REMOTE + \ + '{pkg_name}.yaml'.format(pkg_name=pkg_info.name) self._yaml_content = None - self.timed_task_open = self._timed_task_status() + self.timed_task_open = configuration.OPEN self.producer_consumer = ProducerConsumer() - def _timed_task_status(self): - """ - The open state of information such as the maintainer in the scheduled task - """ - _timed_task_status = True - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - open_status = _readconfig.get_config('TIMEDTASK', 'open') - if open_status not in ('True', 'False'): - self.base.log.logger.error( - 'Wrong setting of the open state value of the scheduled task') - if open_status == 'False': - self.timed_task_open = False - return _timed_task_status - - def _path_stitching(self, pkg_name): - """ - The path of the remote service call - """ - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - _remote_url = _readconfig.get_config('LIFECYCLE', 'warehouse_remote') - if _remote_url is None: - _remote_url = 'https://gitee.com/openeuler/openEuler-Advisor/raw/master/upstream-info/' - return _remote_url + '{pkg_name}.yaml'.format(pkg_name=pkg_name) - def update_database(self): """ For the current package, determine whether the specific yaml file exists, parse @@ -81,8 +58,8 @@ class ParseYaml(): self._save_to_database() else: msg = "The yaml information of the %s package has not been\ - obtained yet" % self.pkg.name - self.base.log.logger.warning(msg) + obtained yet " % self.pkg.name + LOGGER.logger.warning(msg) def _get_yaml_content(self, url): """ @@ -90,12 +67,12 @@ class ParseYaml(): """ try: response = requests.get( - url, headers=self.base.headers) + url, headers=self.headers) if response.status_code == 200: self._yaml_content = yaml.safe_load(response.content) except HTTPError as error: - self.base.log.logger.error(error) + LOGGER.logger.error(error) def _openeuler_advisor_exists_yaml(self): """ @@ -145,7 +122,7 @@ class ParseYaml(): copy.deepcopy(_packages_maintainer)) _save_maintainer_info() except (Error, ContentNoneException, SQLAlchemyError) as error: - self.base.log.logger.error(error) + LOGGER.logger.error(error) def _parse_warehouse_info(self): """ @@ -179,7 +156,7 @@ class ParseYaml(): self.pkg.used_time = (date.datetime.now() - _end_time).days except (IndexError, Error) as index_error: - self.base.log.logger.error(index_error) + LOGGER.logger.error(index_error) def update_pkg_info(pkg_info_update=True): @@ -188,12 +165,7 @@ def update_pkg_info(pkg_info_update=True): """ try: - base_control = Base() - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - pool_workers = _readconfig.get_config('LIFECYCLE', 'pool_workers') - _warehouse = _readconfig.get_config('LIFECYCLE', 'warehouse') - if _warehouse is None: - _warehouse = 'src-openeuler' + pool_workers = configuration.POOL_WORKERS if not isinstance(pool_workers, int): pool_workers = 10 # Open thread pool @@ -208,14 +180,13 @@ def update_pkg_info(pkg_info_update=True): if pkg_info_update: parse_yaml = ParseYaml( pkg_info=copy.deepcopy(package_item), - base=base_control, table_name=table_name) pool.submit(parse_yaml.update_database) else: # Get the issue of each warehouse and save it gitee_issue = Gitee( - package_item, _warehouse, package_item.name, table_name) + package_item, configuration.WAREHOUSE, package_item.name, table_name) pool.submit(gitee_issue.query_issues_info) pool.shutdown() except SQLAlchemyError as error_msg: - base_control.log.logger.error(error_msg) + LOGGER.logger.error(error_msg) diff --git a/packageship/packageship/application/apps/lifecycle/function/gitee.py b/packageship/packageship/application/apps/lifecycle/function/gitee.py index 5add8671078276baa569ac4fc6c52402b402ccba..7dc08e386510806a707b982c413c1f1d4ac46188 100644 --- a/packageship/packageship/application/apps/lifecycle/function/gitee.py +++ b/packageship/packageship/application/apps/lifecycle/function/gitee.py @@ -9,16 +9,12 @@ from retrying import retry import requests from requests.exceptions import HTTPError from sqlalchemy.exc import SQLAlchemyError +from packageship.libs.log import LOGGER from packageship.libs.dbutils import DBHelper -from packageship.libs.configutils.readconfig import ReadConfig from packageship.libs.exception import Error, ContentNoneException from packageship.application.models.package import PackagesIssue -from packageship import system_config -from packageship.libs.log import Log from .concurrent import ProducerConsumer -LOGGER = Log(__name__) - class Gitee(): """ @@ -30,7 +26,6 @@ class Gitee(): self.pkg_info = pkg_info self.owner = owner self.repo = repo - self._read_config = ReadConfig(system_config.SYS_CONFIG_PATH) self.url = "https://gitee.com/" self.api_url = "https://gitee.com/api/v5/repos" self.pool = None @@ -38,8 +33,6 @@ class Gitee(): self.defect = 0 self.feature = 0 self.cve = 0 - self.patch_files_path = self._read_config.get_system( - "patch_files_path") self.table_name = table_name self.producer_consumer = ProducerConsumer() @@ -54,7 +47,7 @@ class Gitee(): """ issue_url = self.api_url + \ - "/{}/{}/issues/{}".format(self.owner, self.repo, issue_id) + "/{}/{}/issues/{}".format(self.owner, self.repo, issue_id) try: response = requests.get( issue_url, params={"state": "all", "per_page": 100}) @@ -215,10 +208,10 @@ class Gitee(): issue_info_list.append(issue_content) if self.feature != 0: self.defect, self.feature, self.cve = self.pkg_info.defect, self.pkg_info.feature + \ - 1, self.pkg_info.cve + 1, self.pkg_info.cve if self.defect != 0: self.defect, self.feature, self.cve = self.pkg_info.defect + \ - 1, self.pkg_info.feature, self.pkg_info.cve + 1, self.pkg_info.feature, self.pkg_info.cve if self.cve != 0: self.defect, self.feature, self.cve = self.pkg_info.defect, self.pkg_info.feature, self.pkg_info.cve + 1 self._save_issues(issue_info_list) diff --git a/packageship/packageship/application/apps/lifecycle/url.py b/packageship/packageship/application/apps/lifecycle/url.py index 71750d89b2a40fcd4104da0c44b0be1fcd1ae79c..7b19965d0691e68dfc710b5939dfaa3bcba8d52d 100644 --- a/packageship/packageship/application/apps/lifecycle/url.py +++ b/packageship/packageship/application/apps/lifecycle/url.py @@ -17,6 +17,6 @@ urls = [ # pylint: disable=invalid-name (view.IssueType, '/lifeCycle/issuetype', {'query': ('GET')}), (view.IssueStatus, '/lifeCycle/issuestatus', {'query': ('GET')}), (view.IssueCatch, '/lifeCycle/issuecatch', {'write': ('POST')}), -# update a package info + # update a package info (view.UpdatePackages, '/lifeCycle/updatePkgInfo', {'write': ('PUT')}) ] diff --git a/packageship/packageship/application/apps/lifecycle/view.py b/packageship/packageship/application/apps/lifecycle/view.py index efce4bb4216ab1175af99eca635f89cc6f3ff02c..c8a4ae5f7d67099ad5fd07adc7ce7dec8a95d83a 100644 --- a/packageship/packageship/application/apps/lifecycle/view.py +++ b/packageship/packageship/application/apps/lifecycle/view.py @@ -19,21 +19,18 @@ from marshmallow import ValidationError from sqlalchemy.exc import DisconnectionError, SQLAlchemyError -from packageship import system_config -from packageship.libs.configutils.readconfig import ReadConfig +from packageship.libs.log import LOGGER +from packageship.libs.conf import configuration from packageship.libs.exception import Error from packageship.application.apps.package.function.constants import ResponseCode from packageship.libs.dbutils.sqlalchemy_helper import DBHelper from packageship.application.models.package import PackagesIssue from packageship.application.models.package import Packages from packageship.application.models.package import PackagesMaintainer -from packageship.libs.log import Log from .serialize import IssueDownloadSchema, PackagesDownloadSchema, IssuePageSchema, IssueSchema from ..package.serialize import DataFormatVerfi, UpdatePackagesSchema from .function.gitee import Gitee as gitee -LOGGER = Log(__name__) - # pylint: disable = no-self-use @@ -449,11 +446,8 @@ class IssueCatch(Resource): ResponseCode.response_json(ResponseCode.PARAM_ERROR)) pkg_name = data["repository"]["path"] try: - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - pool_workers = _readconfig.get_config('LIFECYCLE', 'pool_workers') - _warehouse = _readconfig.get_config('LIFECYCLE', 'warehouse') - if _warehouse is None: - _warehouse = 'src-openeuler' + pool_workers = configuration.POOL_WORKERS + _warehouse = configuration.WAREHOUSE if not isinstance(pool_workers, int): pool_workers = 10 pool = ThreadPoolExecutor(max_workers=pool_workers) @@ -550,8 +544,7 @@ class UpdatePackages(Resource): return None try: yaml_data_list = list() - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - pool_workers = _readconfig.get_config('LIFECYCLE', 'pool_workers') + pool_workers = configuration.POOL_WORKERS if not isinstance(pool_workers, int): pool_workers = 10 with ThreadPoolExecutor(max_workers=pool_workers) as pool: diff --git a/packageship/packageship/application/apps/package/__init__.py b/packageship/packageship/application/apps/package/__init__.py index 987ad6145195cd94d044464c03e277aa73bf270a..38161fe672cc55f86eae086cbed90d15bcdb0bdb 100644 --- a/packageship/packageship/application/apps/package/__init__.py +++ b/packageship/packageship/application/apps/package/__init__.py @@ -1,3 +1,4 @@ +import os from flask.blueprints import Blueprint from flask_restful import Api from packageship.application.apps.package.url import urls @@ -7,9 +8,10 @@ package = Blueprint('package', __name__) # init restapi api = Api() +PERMISSION_SERVICE = os.environ.get('PERMISSION_SERVICE') for view, url, operation in urls: - if application.OPERATION and application.OPERATION in operation.keys(): + if PERMISSION_SERVICE and PERMISSION_SERVICE in operation.keys(): api.add_resource(view, url) diff --git a/packageship/packageship/application/initsystem/data_import.py b/packageship/packageship/application/initsystem/data_import.py index cda86a5c1016af7eb271a4f848b13f68f0e18693..b895fde0bb634349eefeb429edca360ebeaceb10 100644 --- a/packageship/packageship/application/initsystem/data_import.py +++ b/packageship/packageship/application/initsystem/data_import.py @@ -8,21 +8,18 @@ import os import pathlib import yaml from sqlalchemy.exc import SQLAlchemyError, InternalError +from packageship.libs.conf import configuration from packageship.libs.dbutils.sqlalchemy_helper import DBHelper from packageship.libs.exception import ContentNoneException from packageship.libs.exception import DatabaseRepeatException from packageship.libs.exception import Error -from packageship.libs.configutils.readconfig import ReadConfig -from packageship.libs.log import Log from packageship.application.models.package import SrcPack from packageship.application.models.package import BinPack from packageship.application.models.package import BinRequires from packageship.application.models.package import SrcRequires from packageship.application.models.package import BinProvides from packageship.application.models.package import Packages -from packageship import system_config - -LOGGER = Log(__name__) +from packageship.libs.log import LOGGER class InitDataBase(): @@ -48,8 +45,7 @@ class InitDataBase(): if self.config_file_path: # yaml configuration file content self.config_file_datas = self.__read_config_file() - self._read_config = ReadConfig(system_config.SYS_CONFIG_PATH) - self.db_type = 'sqlite' + self.db_type = configuration.DATABASE_ENGINE_TYPE self.sql = None self._database = None self._sqlite_db = None @@ -491,9 +487,9 @@ class InitDataBase(): IOError: File or network operation io abnormal """ try: - if not os.path.exists(system_config.DATABASE_FILE_INFO): - pathlib.Path(system_config.DATABASE_FILE_INFO).touch() - with open(system_config.DATABASE_FILE_INFO, 'a+', encoding='utf8') as file_context: + if not os.path.exists(configuration.DATABASE_FILE_INFO): + pathlib.Path(configuration.DATABASE_FILE_INFO).touch() + with open(configuration.DATABASE_FILE_INFO, 'a+', encoding='utf8') as file_context: setting_content = [] if 'database_content' in Kwargs.keys(): content = Kwargs.get('database_content') @@ -520,8 +516,8 @@ class InitDataBase(): """ try: - if os.path.exists(system_config.DATABASE_FILE_INFO): - os.remove(system_config.DATABASE_FILE_INFO) + if os.path.exists(configuration.DATABASE_FILE_INFO): + os.remove(configuration.DATABASE_FILE_INFO) except (IOError, Error) as exception_msg: LOGGER.logger.error(exception_msg) return False @@ -542,14 +538,14 @@ class InitDataBase(): try: del_result = True file_read = open( - system_config.DATABASE_FILE_INFO, 'r', encoding='utf-8') + configuration.DATABASE_FILE_INFO, 'r', encoding='utf-8') _databases = yaml.load( file_read.read(), Loader=yaml.FullLoader) for database in _databases: if database.get('database_name') == db_name: _databases.remove(database) # Delete the successfully imported database configuration node - with open(system_config.DATABASE_FILE_INFO, 'w+', encoding='utf-8') as file_context: + with open(configuration.DATABASE_FILE_INFO, 'w+', encoding='utf-8') as file_context: yaml.safe_dump(_databases, file_context) except (IOError, Error) as del_config_error: LOGGER.logger.error(del_config_error) @@ -694,7 +690,6 @@ class SqliteDatabaseOperations(): kwargs: data related to configuration file nodes """ self.db_name = db_name - self._read_config = ReadConfig(system_config.SYS_CONFIG_PATH) if getattr(kwargs, 'database_path', None) is None: self._database_file_path() else: @@ -711,11 +706,7 @@ class SqliteDatabaseOperations(): Raises: IOError: File or network operation io abnormal """ - self.database_file_folder = self._read_config.get_system( - 'data_base_path') - if not self.database_file_folder: - self.database_file_folder = system_config.DATABASE_FOLDER_PATH - + self.database_file_folder = configuration.DATABASE_FOLDER_PATH if not os.path.exists(self.database_file_folder): try: os.makedirs(self.database_file_folder) diff --git a/packageship/packageship/application/settings.py b/packageship/packageship/application/settings.py index 12d561cbf097d750018acc3f08fa5adf94676d68..373512de892b4c062633b72460ccc9ceeebd4c59 100644 --- a/packageship/packageship/application/settings.py +++ b/packageship/packageship/application/settings.py @@ -3,29 +3,25 @@ Description: Basic configuration of flask framework """ import random -from packageship import system_config -from packageship.libs.configutils.readconfig import ReadConfig +from packageship.libs.conf import configuration class Config(): """ 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 - LOG_LEVEL = 'INFO' + LOG_LEVEL = configuration.LOG_LEVEL SCHEDULER_API_ENABLED = True def __init__(self): - self._read_config = ReadConfig(system_config.SYS_CONFIG_PATH) - self.set_config_val() @classmethod @@ -36,13 +32,6 @@ class Config(): cls.SECRET_KEY = ''.join( [random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()') for index in range(random_len)]) - @classmethod - def _set_log_level(cls, log_level): - """ - Description: Set the log level - """ - cls.LOG_LEVEL = log_level - def set_config_val(self): """ Description: Set the value of the configuration item @@ -51,8 +40,3 @@ class Config(): Raises: """ Config._random_secret_key() - - log_level = self._read_config.get_config('LOG', 'log_level') - - if log_level: - Config._set_log_level(log_level) diff --git a/packageship/packageship/libs/conf/__init__.py b/packageship/packageship/libs/conf/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..54781daaab09da08a90b48e91273730d45d72d50 --- /dev/null +++ b/packageship/packageship/libs/conf/__init__.py @@ -0,0 +1,109 @@ +#!/usr/bin/python3 +""" +System configuration file and default configuration file integration +""" +import os +import configparser + +from packageship.libs.exception import Error +from . import global_config + + +USER_SETTINGS_FILE_PATH = 'SETTINGS_FILE_PATH' + + +class PreloadingSettings(): + """ + The system default configuration file and the configuration + file changed by the user are lazily loaded. + """ + _setting_container = None + + def _preloading(self): + """ + Load the default configuration in the system and the related configuration + of the user, and overwrite the default configuration items of the system + with the user's configuration data + """ + settings_file = os.environ.get(USER_SETTINGS_FILE_PATH) + if not settings_file: + raise Error( + "The system does not specify the user configuration" + "that needs to be loaded:" % USER_SETTINGS_FILE_PATH) + + self._setting_container = Configs(settings_file) + + def __getattr__(self, name): + """ + Return the value of a setting and cache it in self.__dict__ + """ + if self._setting_container is None: + self._preloading() + value = getattr(self._setting_container, name, None) + self.__dict__[name] = value + return value + + def __setattr__(self, name, value): + """ + Set the configured value and re-copy the value cached in __dict__ + """ + if name is None: + raise KeyError("The set configuration key value cannot be empty") + if name == '_setting_container': + self.__dict__.clear() + self.__dict__["_setting_container"] = value + else: + self.__dict__.pop(name, None) + if self._setting_container is None: + self._preloading() + setattr(self._setting_container, name, value) + + def __delattr__(self, name): + """ + Delete a setting and clear it from cache if needed + """ + if name is None: + raise KeyError("The set configuration key value cannot be empty") + + if self._setting_container is None: + self._preloading() + delattr(self._setting_container, name) + self.__dict__.pop(name, None) + + @property + def config_ready(self): + """ + Return True if the settings have already been configured + """ + return self._setting_container is not None + + +class Configs(): + """ + The system's default configuration items and the user's + configuration items are integrated + """ + + def __init__(self, settings_file): + for config in dir(global_config): + if not config.startswith('_'): + setattr(self, config, getattr(global_config, config)) + + # Load user's configuration + self._conf_parser = configparser.ConfigParser() + self._conf_parser.read(settings_file) + + for section in self._conf_parser.sections(): + for option in self._conf_parser.items(section): + try: + _config_value = option[1] + _key = option[0] + except IndexError: + pass + else: + if not isinstance(_config_value, (int, bool)) and not _config_value: + continue + setattr(self, _key.upper(), _config_value) + + +configuration = PreloadingSettings() # pylint: disable=invalid-name diff --git a/packageship/packageship/libs/conf/global_config.py b/packageship/packageship/libs/conf/global_config.py new file mode 100644 index 0000000000000000000000000000000000000000..a470d9877ccfbca4d80152f50dee6c3ff7120d6a --- /dev/null +++ b/packageship/packageship/libs/conf/global_config.py @@ -0,0 +1,95 @@ +#!/usr/bin/python3 +""" +Global environment variable value when the system is running +""" + +import os + +# Configuration file path for data initialization +INIT_CONF_PATH = os.path.join('/', 'etc', 'pkgship', 'conf.yaml') + + +# data file after successful data import +DATABASE_FILE_INFO = os.path.join( + '/', 'var', 'run', 'database_file_info.yaml') + +# If the path of the imported database is not specified in the configuration file, the +# configuration in the system is used by default +DATABASE_FOLDER_PATH = os.path.join('/', 'var', 'run', 'pkgship_dbs') + + +DATABASE_ENGINE_TYPE = 'sqlite' + +# If the directory of log storage is not configured, +# it will be stored in the following directory specified by the system by default +LOG_FOLDER_PATH = os.path.join('/', 'var', 'log', 'logs') + + +# Port managed by the administrator, with write permission + +WRITE_PORT = 8080 + +# Ordinary user query port, only the right to query data, no permission to write data + +QUERY_PORT = 8090 + +# IP address path with write permission + +WRITE_HOST = '127.0.0.1' + +# IP address path with permission to query data + +QUERY_HOST = '127.0.0.1' + +# The address of the remote service, the command line can directly +# call the remote service to complete the data request +REMOTE_HOST = 'https://api.openeuler.org/pkgmanage' + +# Custom log storage path +LOG_PATH = os.path.join('/', 'var', 'log', 'pkgship') + +# Logging level +# The log level option value can only be as follows +# INFO DEBUG WARNING ERROR CRITICAL +LOG_LEVEL = 'INFO' + +# logging name +LOG_NAME = 'log_info.log' + +# The number of dynamically created logs +# after the log file size reaches the upper limit. The default is 10 dynamically created logs +BACKUP_COUNT = 10 + +# The size of each log file, in bytes, the default size of a single log file is 300M +MAX_BYTES = 314572800 + +# Execution frequency and switch of timing tasks +# Whether to execute the switch for batch update of information such +# as the maintainer during initialization. When set to True, the maintainer +# and other information will be updated when the scheduled task starts +# to execute. When it is set to False, it will be updated when the scheduled +# task is executed. , Does not update information such as maintainers and maintenance levels + +OPEN = True + +# The time point at which the timing task is executed in a cycle. +# Every day is a cycle, and the time value can only be any integer period between 0 and 23 +HOUR = 3 + +# Recurring timing task starts to execute the task at the current time point. +# The value range of this configuration item is an integer between 0 and 59 +MINUTE = 0 + +# Configuration during the life cycle for tag information, issue and other information acquisition +# The yaml address of each package is stored in the remote address, which can be +# a remote warehouse address or the address of a static resource service +WAREHOUSE_REMOTE = 'https://gitee.com/wu_fengguang/openEuler-Advisor/raw/master/upstream-info/' + +# When performing timing tasks, you can open multi-threaded execution, and you can set +# the number of threads in the thread pool according to the configuration of the server + +POOL_WORKERS = 10 + +# The main body of the warehouse, the owner of the warehouse +# When this value is not set, the system will default to src-openeuler +WAREHOUSE = 'src-openeuler' diff --git a/packageship/packageship/libs/dbutils/sqlalchemy_helper.py b/packageship/packageship/libs/dbutils/sqlalchemy_helper.py index a0b22e2f2adc0c616b390c0a0da4447d94240ba1..6291a49438f1dcef7d3553fe30612774e01a089a 100644 --- a/packageship/packageship/libs/dbutils/sqlalchemy_helper.py +++ b/packageship/packageship/libs/dbutils/sqlalchemy_helper.py @@ -14,8 +14,7 @@ from sqlalchemy.engine.url import URL from packageship.libs.exception.ext import Error from packageship.libs.exception.ext import DbnameNoneException from packageship.libs.exception.ext import ContentNoneException -from packageship.libs.configutils.readconfig import ReadConfig -from packageship import system_config +from packageship.libs.conf import configuration class BaseHelper(): @@ -24,7 +23,6 @@ class BaseHelper(): """ def __init__(self): - self.readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) self.engine = None @@ -43,12 +41,11 @@ class MysqlHelper(BaseHelper): def __init__(self, user_name=None, password=None, host=None, # pylint: disable=unused-argument port=None, database=None, **kwargs): super(MysqlHelper, self).__init__() - self.user_name = user_name or self.readconfig.get_database( - 'user_name') - self.password = password or self.readconfig.get_database('password') - self.host = host or self.readconfig.get_database('host') - self.port = port or self.readconfig.get_database('port') - self.database = database or self.readconfig.get_database('database') + self.user_name = user_name or configuration.USER_NAME + self.password = password or configuration.PASSWORD + self.host = host or configuration.HOST + self.port = port or configuration.PORT + self.database = database or configuration.DATABASE self.connection_type = 'mysql+pymysql' def create_database_engine(self): @@ -100,10 +97,7 @@ class SqliteHlper(BaseHelper): Raises: """ - _database_folder_path = self.readconfig.get_system( - 'data_base_path') - if not _database_folder_path: - _database_folder_path = system_config.DATABASE_FOLDER_PATH + _database_folder_path = configuration.DATABASE_FOLDER_PATH try: if not os.path.exists(_database_folder_path): os.makedirs(_database_folder_path) @@ -143,6 +137,7 @@ class DBHelper(BaseHelper): """ # The base class inherited by the data model BASE = declarative_base() + ENGINE_CONTAINER = dict() def __init__(self, user_name=None, password=None, host=None, # pylint: disable=R0913 port=None, db_name=None, connection_type=None, **kwargs): @@ -155,18 +150,39 @@ class DBHelper(BaseHelper): 'mysql': MysqlHelper, 'sqlite': SqliteHlper } + if connection_type is None: - connection_type = self.readconfig.get_database( - 'dbtype') or 'sqlite' + connection_type = configuration.DATABASE_ENGINE_TYPE + self._engine_pool = connection_type + '_' + db_name _database_engine = self._database_engine.get(connection_type) if _database_engine is None: - raise DisconnectionError('') - _engine = _database_engine(user_name=user_name, password=password, - host=host, port=port, database=db_name, **kwargs) - _engine.create_database_engine() - self.engine = _engine.engine + raise DisconnectionError( + 'Database engine connection failed' + 'Not currently supported %s database' % connection_type) + _engine = self.ENGINE_CONTAINER.get(self._engine_pool) + if _engine: + self.engine = _engine + else: + _engine = _database_engine(user_name=user_name, password=password, + host=host, port=port, database=db_name, **kwargs) + _engine.create_database_engine() + self.engine = _engine.engine + self.ENGINE_CONTAINER[self._engine_pool] = self.engine self.session = None + def create_engine(self): + """ + Create related database engine connections + """ + session = sessionmaker() + try: + session.configure(bind=self.engine) + except DisconnectionError: + self.ENGINE_CONTAINER.pop(self._engine_pool) + else: + self.session = session() + return self + def __enter__(self): """ Description: functional description:Create a context manager for the database connection @@ -178,13 +194,8 @@ class DBHelper(BaseHelper): """ - session = sessionmaker() - if not hasattr(self, 'engine'): - raise DisconnectionError('Abnormal database connection') - session.configure(bind=self.engine) - - self.session = session() - return self + database_engine = self.create_engine() + return database_engine def __exit__(self, exc_type, exc_val, exc_tb): """ @@ -199,6 +210,9 @@ class DBHelper(BaseHelper): Raises: """ + if isinstance(exc_type, (AttributeError)): + raise SQLAlchemyError(exc_val) + self.session.close() @classmethod @@ -246,10 +260,8 @@ class DBHelper(BaseHelper): if entity is None: raise ContentNoneException( 'The added entity content cannot be empty') - try: self.session.add(entity) - except SQLAlchemyError as sql_error: self.session.rollback() raise Error(sql_error) diff --git a/packageship/packageship/libs/log/__init__.py b/packageship/packageship/libs/log/__init__.py index 3decd458bb39dd8edcf986def0957fc4fc6853c0..28f7f56c35380dccc39d199a9ed6fe65d6f32cdc 100644 --- a/packageship/packageship/libs/log/__init__.py +++ b/packageship/packageship/libs/log/__init__.py @@ -4,5 +4,6 @@ Common methods for logging """ from packageship.libs.log.loghelper import setup_log from packageship.libs.log.loghelper import Log +from packageship.libs.log.loghelper import LOGGER -__all__ = ['setup_log', 'Log'] +__all__ = ['setup_log', 'Log', 'LOGGER'] diff --git a/packageship/packageship/libs/log/loghelper.py b/packageship/packageship/libs/log/loghelper.py index fc53bb214b27863e8d015d29957b957cb33aa697..64d24fe9140bfa69bf0b9bbbf0cd581980adb492 100644 --- a/packageship/packageship/libs/log/loghelper.py +++ b/packageship/packageship/libs/log/loghelper.py @@ -6,38 +6,26 @@ import os import pathlib import logging from concurrent_log_handler import ConcurrentRotatingFileHandler -from packageship import system_config -from packageship.libs.configutils.readconfig import ReadConfig - - -READCONFIG = ReadConfig(system_config.SYS_CONFIG_PATH) +from packageship.libs.conf import configuration def setup_log(config=None): """ Log logging in the context of flask """ + _level = configuration.LOG_LEVEL if config: - logging.basicConfig(level=config.LOG_LEVEL) - else: - _level = READCONFIG.get_config('LOG', 'log_level') - if _level is None: - _level = 'INFO' - logging.basicConfig(level=_level) - path = READCONFIG.get_config('LOG', 'log_path') - log_name = READCONFIG.get_config('LOG', 'log_name') - backup_count = READCONFIG.get_config('LOG', 'backup_count') + _level = config.LOG_LEVEL + logging.basicConfig(level=_level) + backup_count = configuration.BACKUP_COUNT if not backup_count or not isinstance(backup_count, int): backup_count = 10 - max_bytes = READCONFIG.get_config('LOG', 'max_bytes') + max_bytes = configuration.MAX_BYTES if not max_bytes or not isinstance(max_bytes, int): max_bytes = 314572800 - if not log_name: - log_name = 'log_info.log' - if not path: - path = os.path.join(system_config.LOG_FOLDER_PATH, log_name) - else: - path = os.path.join(path, log_name) + + path = os.path.join(configuration.LOG_PATH, configuration.LOG_NAME) + if not os.path.exists(path): try: os.makedirs(os.path.split(path)[0]) @@ -65,33 +53,24 @@ class Log(): self.__file_handler = None - log_name = READCONFIG.get_config('LOG', 'log_name') - if not log_name: - log_name = 'log_info.log' + self.__path = os.path.join( + configuration.LOG_PATH, configuration.LOG_NAME) if path: - self.__path = os.path.join(system_config.LOG_FOLDER_PATH, path) - else: - self.__path = READCONFIG.get_config('LOG', 'log_path') - if not self.__path: - self.__path = os.path.join( - system_config.LOG_FOLDER_PATH, log_name) - else: - self.__path = os.path.join(self.__path, log_name) + self.__path = path if not os.path.exists(self.__path): try: os.makedirs(os.path.split(self.__path)[0]) except FileExistsError: pathlib.Path(self.__path).touch() - self.__level = READCONFIG.get_config('LOG', 'log_level') - if self.__level is None: - self.__level = 'INFO' + + self.__level = configuration.LOG_LEVEL self.__logger = logging.getLogger(self.__name) self.__logger.setLevel(self.__level) - self.backup_count = READCONFIG.get_config('LOG', 'backup_count') + self.backup_count = configuration.BACKUP_COUNT if not self.backup_count or not isinstance(self.backup_count, int): self.backup_count = 10 - self.max_bytes = READCONFIG.get_config('LOG', 'max_bytes') + self.max_bytes = configuration.MAX_BYTES if not self.max_bytes or not isinstance(self.max_bytes, int): self.max_bytes = 314572800 @@ -125,3 +104,6 @@ class Log(): self.__set_formatter() self.close_handler() return self.__logger + + +LOGGER = Log(__name__) diff --git a/packageship/packageship/manage.py b/packageship/packageship/manage.py index 25d8aa7762fd20e1d488a72a0d5943ba62fc2c99..f98f34aa806be480f90e83390054caec23e4fbfb 100644 --- a/packageship/packageship/manage.py +++ b/packageship/packageship/manage.py @@ -3,27 +3,20 @@ Description: Entry for project initialization and service startupc """ import os -from packageship.libs.exception import Error try: - from packageship import system_config - if not os.path.exists(system_config.SYS_CONFIG_PATH): - raise FileNotFoundError( - 'the system configuration file does not exist and the log cannot be started') -except FileNotFoundError as file_not_found: - from packageship.libs.log.loghelper import Log - Log(__name__).logger.error(file_not_found) - raise Exception( - 'the system configuration file does not exist and the log cannot be started') -else: - from packageship.libs.configutils.readconfig import ReadConfig - + os.environ['PERMISSION_SERVICE'] = 'write' +except RuntimeError: + pass from packageship.application import init_app -try: - app = init_app('write') -except Error as error: - raise Exception('Service failed to start') -else: - from packageship.application.app_global import identity_verification +from packageship.application.app_global import identity_verification + + +if not os.path.exists(os.environ.get('SETTINGS_FILE_PATH')): + raise FileNotFoundError( + 'System configuration file:%s' % os.environ.get('SETTINGS_FILE_PATH'), + 'does not exist, software service cannot be started') + +app = init_app() @app.before_request @@ -36,7 +29,4 @@ def before_request(): if __name__ == "__main__": - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - port = _readconfig.get_system('write_port') - addr = _readconfig.get_system('write_ip_addr') - app.run(port=port, host=addr) + app.run() diff --git a/packageship/packageship/package.ini b/packageship/packageship/package.ini index e0f4f47291711a305ea6dccaf5db579b604ec498..ee739ccff4eee83457435afa88cd1b4738c7c03d 100644 --- a/packageship/packageship/package.ini +++ b/packageship/packageship/package.ini @@ -4,7 +4,7 @@ init_conf_path=/etc/pkgship/conf.yaml ; Where to store data files when using sqlite database -; data_base_path=/var/run/pkgship_dbs +; database_folder_path=/var/run/pkgship_dbs ; Port managed by the administrator, with write permission @@ -16,11 +16,11 @@ query_port=8090 ; IP address path with write permission -write_ip_addr=127.0.0.1 +write_host=127.0.0.1 ; IP address path with permission to query data -query_ip_addr=127.0.0.1 +query_host=127.0.0.1 ; The address of the remote service, the command line can directly ; call the remote service to complete the data request @@ -29,7 +29,7 @@ remote_host=https://api.openeuler.org/pkgmanage [LOG] ; Custom log storage path -; log_path=/var/log/pkgship/ +log_path=/var/log/pkgship ; Logging level ; The log level option value can only be as follows diff --git a/packageship/packageship/pkgship.py b/packageship/packageship/pkgship.py index ea32b5276e37f7f2848ac4768668b000951c893e..fa66e8ac4d1cc124418efff40ab6fb2032448e8f 100644 --- a/packageship/packageship/pkgship.py +++ b/packageship/packageship/pkgship.py @@ -17,12 +17,9 @@ try: from requests.exceptions import HTTPError import prettytable from prettytable import PrettyTable - from packageship import system_config - from packageship.libs.log import Log + from packageship.libs.log import LOGGER from packageship.libs.exception import Error - from packageship.libs.configutils.readconfig import ReadConfig - - LOGGER = Log(__name__) + from packageship.libs.conf import configuration except ImportError as import_error: print('Error importing related dependencies, \ please check if related dependencies are installed') @@ -66,7 +63,6 @@ class BaseCommand(): Description: Class instance initialization """ - self._read_config = ReadConfig(system_config.SYS_CONFIG_PATH) self.write_host = None self.read_host = None self.__http = 'http://' @@ -85,9 +81,9 @@ class BaseCommand(): Raises: """ - wirte_port = self._read_config.get_system('write_port') + wirte_port = str(configuration.WRITE_PORT) - write_ip = self._read_config.get_system('write_ip_addr') + write_ip = configuration.WRITE_HOST if not all([write_ip, wirte_port]): raise Error( "The system does not configure the relevant port and ip correctly") @@ -103,9 +99,9 @@ class BaseCommand(): Raises: """ - read_port = self._read_config.get_system('query_port') + read_port = str(configuration.QUERY_PORT) - read_ip = self._read_config.get_system('query_ip_addr') + read_ip = configuration.QUERY_HOST if all([read_ip, read_port]): _read_host = self.__http + read_ip + ":" + read_port @@ -116,7 +112,7 @@ class BaseCommand(): Set read domain name """ if remote: - _host = self._read_config.get_system('remote_host') + _host = configuration.REMOTE_HOST self.read_host = _host if self.read_host is None: raise Error( diff --git a/packageship/packageship/pkgshipd b/packageship/packageship/pkgshipd index c6e742f0db3c86e1b0448b6e031cdab42163cc3b..7b4bdd30c319bd9ead1e41bd2372f2d93e80ea6c 100755 --- a/packageship/packageship/pkgshipd +++ b/packageship/packageship/pkgshipd @@ -24,7 +24,7 @@ function create_config_file(){ echo "[INFO] run packageship under path: $wsgi_file_path" if [ $service = "manage" -o $service = "all" ];then write_port=$(get_config "$service" "write_port") - write_ip_addr=$(get_config "$service" "write_ip_addr") + write_ip_addr=$(get_config "$service" "write_host") if [[ -z "$daemonize" ]] || [[ -z "$buffer_size" ]] || [[ -z "$write_ip_addr" ]] || [[ -z "$http_timeout" ]] || [[ -z "$harakiri" ]] || [[ -z "$write_port" ]]; then echo "[ERROR] CAN NOT find all config name in: $SYS_PATH/package.ini, Please check the file" @@ -51,7 +51,7 @@ daemonize=$daemonize" > $OUT_PATH/manage.ini if [ $service = "selfpkg" -o $service = "all" ];then query_port=$(get_config "$service" "query_port") - query_ip_addr=$(get_config "$service" "query_ip_addr") + query_ip_addr=$(get_config "$service" "query_host") if [[ -z "$daemonize" ]] || [[ -z "$buffer_size" ]] || [[ -z "$query_ip_addr" ]] || [[ -z "$http_timeout" ]] || [[ -z "$harakiri" ]] || [[ -z "$query_port" ]];then echo "[ERROR] CAN NOT find all config name in: $SYS_PATH/package.ini, Please check the file." diff --git a/packageship/packageship/selfpkg.py b/packageship/packageship/selfpkg.py index b889553111f93bf484fc05f97ecd58832ac339f6..0fad11365d443f00a427362efe107734a2b4d2ba 100644 --- a/packageship/packageship/selfpkg.py +++ b/packageship/packageship/selfpkg.py @@ -3,27 +3,20 @@ Description: Entry for project initialization and service startupc """ import os -from packageship.libs.exception import Error -from packageship.libs.configutils.readconfig import ReadConfig - try: - from packageship import system_config - if not os.path.exists(system_config.SYS_CONFIG_PATH): - raise FileNotFoundError( - 'the system configuration file does not exist and the log cannot be started') -except FileNotFoundError as file_not_found: - from packageship.libs.log.loghelper import Log - Log(__name__).logger.error(file_not_found) - raise Exception( - 'the system configuration file does not exist and the log cannot be started') - + os.environ['PERMISSION_SERVICE'] = 'query' +except RuntimeError: + pass from packageship.application import init_app -try: - app = init_app('query') -except Error as error: - raise Exception('Service failed to start') -else: - from packageship.application.app_global import identity_verification +from packageship.application.app_global import identity_verification + + +if not os.path.exists(os.environ.get('SETTINGS_FILE_PATH')): + raise FileNotFoundError( + 'System configuration file:%s' % os.environ.get('SETTINGS_FILE_PATH'), + 'does not exist, software service cannot be started') + +app = init_app() @app.before_request @@ -36,7 +29,4 @@ def before_request(): if __name__ == "__main__": - _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) - port = _readconfig.get_system('query_port') - addr = _readconfig.get_system('query_ip_addr') - app.run(port=port, host=addr) + app.run() diff --git a/packageship/setup.py b/packageship/setup.py index 346a0c62a8ccee68e48680cf0f8dbd78e18e96d4..f478d40584bf5f1e605a08f1de8cd5707ebee5c5 100644 --- a/packageship/setup.py +++ b/packageship/setup.py @@ -24,7 +24,6 @@ setup( 'packageship.application.apps.package.function.packages', 'packageship.application.apps.package.function.searchdb', 'packageship.application.apps.package.function.self_depend', - 'packageship.application.apps.lifecycle.function.base', 'packageship.application.apps.lifecycle.function.download_yaml', 'packageship.application.apps.lifecycle.function.gitee', 'packageship.application.apps.lifecycle.function.concurrent', @@ -35,6 +34,7 @@ setup( 'packageship.application.models.package', 'packageship.application.settings', 'packageship.libs.__init__', + 'packageship.libs.conf.global_config', 'packageship.libs.configutils.readconfig', 'packageship.libs.dbutils.sqlalchemy_helper', 'packageship.libs.exception.ext', diff --git a/packageship/test/base_code/operate_data_base.py b/packageship/test/base_code/operate_data_base.py index bbdee42123e3ad2d9b8a781ac2820d531cbd12c7..aac5fb9bbd26d396d24d0db645183e25fc378f25 100644 --- a/packageship/test/base_code/operate_data_base.py +++ b/packageship/test/base_code/operate_data_base.py @@ -5,11 +5,14 @@ OperateTestBase """ import os import unittest - +from packageship import system_config from packageship.libs.exception import Error try: - from packageship import system_config + os.environ["SETTINGS_FILE_PATH"] = os.path.join(os.path.dirname(system_config.BASE_PATH), + 'test', + 'common_files', + 'package.ini') system_config.SYS_CONFIG_PATH = os.path.join(os.path.dirname(system_config.BASE_PATH), 'test', @@ -27,8 +30,8 @@ try: from test.base_code.init_config_path import init_config from packageship.manage import app -except Error: - raise +except ImportError: + raise ImportError class OperateTestBase(unittest.TestCase): diff --git a/packageship/test/base_code/read_data_base.py b/packageship/test/base_code/read_data_base.py index 8e6a4c0fa996a961c49ffdfa941621f95b49597f..f76ac34b80f5c103eee7606aae78341f9fb5c7cc 100644 --- a/packageship/test/base_code/read_data_base.py +++ b/packageship/test/base_code/read_data_base.py @@ -4,10 +4,15 @@ import os import unittest import json from .basetest import TestBase - +from packageship import system_config from packageship.libs.exception import Error + try: - from packageship import system_config + + os.environ["SETTINGS_FILE_PATH"] = os.path.join(os.path.dirname(system_config.BASE_PATH), + 'test', + 'common_files', + 'package.ini') system_config.SYS_CONFIG_PATH = os.path.join(os.path.dirname(system_config.BASE_PATH), 'test', @@ -26,8 +31,8 @@ try: from test.base_code.init_config_path import init_config from packageship.selfpkg import app -except Error: - raise +except ImportError: + raise ImportError class ReadTestBase(TestBase): diff --git a/packageship/test/common_files/conf.yaml b/packageship/test/common_files/conf.yaml index 190b6f7fe6ade916373e89630fa61f1892ac102d..1a2d9df2e30de2c6b8cc11de89ffcc4508e55324 100644 --- a/packageship/test/common_files/conf.yaml +++ b/packageship/test/common_files/conf.yaml @@ -2,9 +2,9 @@ dbname: mainline priority: 1 src_db_file: - status: enable + lifecycle: enable - bin_db_file: dbname: fedora30 priority: 2 src_db_file: - status: enable + lifecycle: enable diff --git a/packageship/test/test_module/init_system_tests/test_importdata.py b/packageship/test/test_module/init_system_tests/test_importdata.py index 59beb9603eaf3725ae70faf706791e4d6fb60d57..7c0cdd92d4260c5ef572af28a086a849293f5dc8 100644 --- a/packageship/test/test_module/init_system_tests/test_importdata.py +++ b/packageship/test/test_module/init_system_tests/test_importdata.py @@ -11,9 +11,13 @@ from configparser import ConfigParser import yaml from packageship import system_config from packageship.libs.exception import Error - try: + os.environ["SETTINGS_FILE_PATH"] = os.path.join(os.path.dirname(system_config.BASE_PATH), + 'test', + 'common_files', + 'package.ini') + system_config.SYS_CONFIG_PATH = os.path.join( os.path.dirname( system_config.BASE_PATH), @@ -33,8 +37,8 @@ try: from test.base_code.init_config_path import init_config -except Error: - raise Error +except ImportError: + raise ImportError from packageship.application.initsystem.data_import import InitDataBase from packageship.libs.exception import ContentNoneException @@ -64,7 +68,8 @@ class ImportData(unittest.TestCase): # Yaml file exists but the content is empty try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'w', encoding='utf-8') as w_f: @@ -83,7 +88,8 @@ class ImportData(unittest.TestCase): # Yaml file exists but DB exists_ The same with name try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -118,7 +124,8 @@ class ImportData(unittest.TestCase): config.set("SYSTEM", "init_conf_path", "D:\\Users\\conf.yaml") config.write(open(system_config.SYS_CONFIG_PATH, "w")) - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') InitDataBase(config_file_path=_config_path).init_data() except FileNotFoundError as error: self.assertEqual( @@ -135,7 +142,8 @@ class ImportData(unittest.TestCase): def test_dbname(self): """test dbname""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -159,7 +167,8 @@ class ImportData(unittest.TestCase): def test_src_db_file(self): """test src db file""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -183,7 +192,8 @@ class ImportData(unittest.TestCase): def test_priority(self): """test priority""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -211,7 +221,8 @@ class ImportData(unittest.TestCase): Initialization of system data """ # Normal configuration - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') InitDataBase(config_file_path=_config_path).init_data() # In the correct case, an import will be generated under the initsystem