From 3b9ef0048e229e083a3d978949ff52a4ed59a589 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 20 Oct 2022 16:32:05 +0200 Subject: [PATCH] Remove unnecessary configs #70-1 --- kdb-bot/tools/post_build/__init__.py | 25 ++++++++++++ kdb-bot/tools/post_build/application.py | 12 +++++- kdb-bot/tools/post_build/appsettings.json | 15 +++++++ .../tools/post_build/post_build_settings.py | 29 ++++++++++++++ kdb-bot/tools/post_build/service/__init__.py | 26 +++++++++++++ .../tools/post_build/service/dependencies.py | 7 ++++ .../tools/post_build/service/remove_config.py | 39 +++++++++++++++++++ kdb-bot/tools/post_build/startup.py | 16 ++++++++ 8 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 kdb-bot/tools/post_build/post_build_settings.py create mode 100644 kdb-bot/tools/post_build/service/__init__.py create mode 100644 kdb-bot/tools/post_build/service/dependencies.py create mode 100644 kdb-bot/tools/post_build/service/remove_config.py diff --git a/kdb-bot/tools/post_build/__init__.py b/kdb-bot/tools/post_build/__init__.py index ad5eca3064..a9a935649b 100644 --- a/kdb-bot/tools/post_build/__init__.py +++ b/kdb-bot/tools/post_build/__init__.py @@ -1 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +post-build +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) +:license: + +""" + +__title__ = 'post_build' +__author__ = '' +__license__ = '' +__copyright__ = 'Copyright (c) ' +__version__ = '0.0.0' + +from collections import namedtuple + + # imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='0', micro='0') diff --git a/kdb-bot/tools/post_build/application.py b/kdb-bot/tools/post_build/application.py index 34a68c80b1..39c68b6358 100644 --- a/kdb-bot/tools/post_build/application.py +++ b/kdb-bot/tools/post_build/application.py @@ -3,14 +3,24 @@ from cpl_core.configuration import ConfigurationABC from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC +from post_build.service.dependencies import Dependencies +from post_build.service.remove_config import RemoveConfig + class Application(ApplicationABC): def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): ApplicationABC.__init__(self, config, services) + self._remove_config: RemoveConfig = services.get_service(RemoveConfig) + self._deps: Dependencies = services.get_service(Dependencies) + def configure(self): pass def main(self): - Console.write_line('Hello World') + Console.write_line('KDB Post-Build:') + Console.spinner(f'Removing unnecessary configs', self._remove_config.remove) + # Console.spinner(f'Creating requirements file for pip', self._deps.create) + + diff --git a/kdb-bot/tools/post_build/appsettings.json b/kdb-bot/tools/post_build/appsettings.json index 629e6ebd22..c8411f8e21 100644 --- a/kdb-bot/tools/post_build/appsettings.json +++ b/kdb-bot/tools/post_build/appsettings.json @@ -11,5 +11,20 @@ "Filename": "log_$start_time.log", "ConsoleLogLevel": "ERROR", "FileLogLevel": "WARN" + }, + + "PostBuild": { + "KeepConfigs": [ + "appsettings.json", + "appsettings.staging.json", + "appsettings.production.json", + "apisettings.json", + "apisettings.staging.json", + "apisettings.production.json" + ], + "ConfigPaths": [ + "bot/config/", + "bot_api/config/" + ] } } diff --git a/kdb-bot/tools/post_build/post_build_settings.py b/kdb-bot/tools/post_build/post_build_settings.py new file mode 100644 index 0000000000..e52ec53f25 --- /dev/null +++ b/kdb-bot/tools/post_build/post_build_settings.py @@ -0,0 +1,29 @@ +import traceback + +from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC +from cpl_core.console import Console + + +class PostBuildSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._keep_config = [] + self._config_paths = [] + + @property + def keep_config(self) -> list[str]: + return self._keep_config + + @property + def config_paths(self) -> list[str]: + return self._config_paths + + def from_dict(self, settings: dict): + try: + self._keep_config = settings['KeepConfigs'] + self._config_paths = settings['ConfigPaths'] + except Exception as e: + Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings') + Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/kdb-bot/tools/post_build/service/__init__.py b/kdb-bot/tools/post_build/service/__init__.py new file mode 100644 index 0000000000..aa80818a01 --- /dev/null +++ b/kdb-bot/tools/post_build/service/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +post-build +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) +:license: + +""" + +__title__ = 'post_build.service' +__author__ = '' +__license__ = '' +__copyright__ = 'Copyright (c) ' +__version__ = '0.0.0' + +from collections import namedtuple + + +# imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='0', micro='0') diff --git a/kdb-bot/tools/post_build/service/dependencies.py b/kdb-bot/tools/post_build/service/dependencies.py new file mode 100644 index 0000000000..aa10e0bac9 --- /dev/null +++ b/kdb-bot/tools/post_build/service/dependencies.py @@ -0,0 +1,7 @@ +class Dependencies: + + def __init__(self): + pass + + def create(self): + pass diff --git a/kdb-bot/tools/post_build/service/remove_config.py b/kdb-bot/tools/post_build/service/remove_config.py new file mode 100644 index 0000000000..07774e10a5 --- /dev/null +++ b/kdb-bot/tools/post_build/service/remove_config.py @@ -0,0 +1,39 @@ +import os + +from cpl_cli.configuration import ProjectSettings, BuildSettings, WorkspaceSettings +from cpl_core.console import Console +from cpl_core.environment import ApplicationEnvironmentABC + +from post_build.post_build_settings import PostBuildSettings + + +class RemoveConfig: + + def __init__(self, env: ApplicationEnvironmentABC, ws: WorkspaceSettings, project: ProjectSettings, build: BuildSettings, post_build: PostBuildSettings,): + + self._env = env + self._workspace = ws + self._project = project + self._build = build + self._post_build = post_build + + def remove(self): + dist_path = os.path.abspath(os.path.join( + self._env.working_directory, + os.path.dirname(self._workspace.projects[self._project.name]), + self._build.output_path, + self._project.name, + 'build' + )) + + for cfg_path in self._post_build.config_paths: + config_path = os.path.join( + dist_path, + cfg_path + ) + for r, d, f in os.walk(config_path): + for file in f: + if file in self._post_build.keep_config: + continue + + os.remove(os.path.abspath(os.path.join(config_path, file))) diff --git a/kdb-bot/tools/post_build/startup.py b/kdb-bot/tools/post_build/startup.py index 359c03d265..4191ced6d8 100644 --- a/kdb-bot/tools/post_build/startup.py +++ b/kdb-bot/tools/post_build/startup.py @@ -1,8 +1,15 @@ +import os.path + +from cpl_cli.configuration import WorkspaceSettings, ProjectSettings from cpl_core.application import StartupABC from cpl_core.configuration import ConfigurationABC +from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.environment import ApplicationEnvironment +from post_build.service.dependencies import Dependencies +from post_build.service.remove_config import RemoveConfig + class Startup(StartupABC): @@ -10,7 +17,16 @@ class Startup(StartupABC): StartupABC.__init__(self) def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: + configuration.add_json_file(f'appsettings.json', optional=False, output=False) + environment.set_working_directory(os.path.abspath(os.path.join(environment.working_directory, '../../'))) + configuration.add_json_file(f'cpl-workspace.json', optional=False, output=False) + ws: WorkspaceSettings = configuration.get_configuration(WorkspaceSettings) + configuration.add_json_file(ws.projects[ws.default_project], optional=False, output=False) + return configuration def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: + services.add_transient(RemoveConfig) + services.add_transient(Dependencies) + return services.build_service_provider()