Moved folders #405
This commit is contained in:
4088
bot/tools/checks/UserJoinedVoiceChannel.json
Normal file
4088
bot/tools/checks/UserJoinedVoiceChannel.json
Normal file
File diff suppressed because it is too large
Load Diff
1
bot/tools/checks/__init__.py
Normal file
1
bot/tools/checks/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports
|
44
bot/tools/checks/checks.json
Normal file
44
bot/tools/checks/checks.json
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "checks",
|
||||
"Version": {
|
||||
"Major": "1",
|
||||
"Minor": "2",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "Sven Heidemann",
|
||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||
"Description": "CPL internal tool to set version from branch name",
|
||||
"LongDescription": "CPL internal tool to set version from branch name",
|
||||
"URL": "https://www.sh-edraft.de",
|
||||
"CopyrightDate": "2022",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||
"Dependencies": [
|
||||
"cpl-core==2022.12.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli==2022.12.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "library",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "checks.main",
|
||||
"EntryPoint": "checks",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
80
bot/tools/checks/get_ontime_overlaps.py
Normal file
80
bot/tools/checks/get_ontime_overlaps.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from cpl_query.extension import List
|
||||
|
||||
|
||||
class UserJoinedVoiceChannel:
|
||||
def __init__(self):
|
||||
self.JoinId = None
|
||||
self.UserId = None
|
||||
self.DiscordChannelId = None
|
||||
self.JoinedOn = None
|
||||
self.LeavedOn = None
|
||||
self.CreatedAt = None
|
||||
self.LastModifiedAt = None
|
||||
|
||||
@property
|
||||
def ontime(self) -> int:
|
||||
return round((self.LeavedOn - self.JoinedOn).total_seconds() / 3600, 2)
|
||||
|
||||
def from_dict(self, data: dict):
|
||||
self.JoinId = data["JoinId"]
|
||||
self.UserId = data["UserId"]
|
||||
self.DiscordChannelId = data["DiscordChannelId"]
|
||||
self.JoinedOn = datetime.strptime(data["JoinedOn"], "%Y-%m-%d %H:%M:%S.%f")
|
||||
self.LeavedOn = datetime.strptime(data["LeavedOn"], "%Y-%m-%d %H:%M:%S.%f")
|
||||
self.CreatedAt = data["CreatedAt"]
|
||||
self.LastModifiedAt = data["LastModifiedAt"]
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.JoinId}: {self.UserId} {self.JoinedOn} {self.LeavedOn}"
|
||||
|
||||
|
||||
def main():
|
||||
data = json.loads(open("UserJoinedVoiceChannel.json", "r").read())
|
||||
ujvcs = List(UserJoinedVoiceChannel)
|
||||
for x in data:
|
||||
ujvc = UserJoinedVoiceChannel()
|
||||
ujvc.from_dict(x)
|
||||
ujvcs.append(ujvc)
|
||||
|
||||
users = ujvcs.order_by(lambda x: x.UserId).select(lambda x: x.UserId).distinct()
|
||||
groups = {}
|
||||
for user in users:
|
||||
groups[user] = List(UserJoinedVoiceChannel)
|
||||
groups[user].extend(ujvcs.where(lambda x: x.UserId == user))
|
||||
|
||||
for group in groups:
|
||||
values: List[UserJoinedVoiceChannel] = groups[group]
|
||||
|
||||
def find_overlaps(x: UserJoinedVoiceChannel) -> bool:
|
||||
for y in values:
|
||||
if x == y:
|
||||
continue
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
Range = namedtuple("Range", ["start", "end"])
|
||||
r1 = Range(start=x.JoinedOn, end=x.LeavedOn)
|
||||
r2 = Range(start=y.LeavedOn, end=y.LeavedOn)
|
||||
latest_start = max(r1.start, r2.start)
|
||||
earliest_end = min(r1.end, r2.end)
|
||||
delta = (earliest_end - latest_start).days + 1
|
||||
overlap = max(0, delta)
|
||||
if overlap > 0:
|
||||
return True
|
||||
|
||||
overlaps = values.where(find_overlaps)
|
||||
if overlaps.count() == 0:
|
||||
continue
|
||||
|
||||
print(overlaps.select(lambda x: x.JoinId))
|
||||
print(f"{group} has {overlaps.count()} of {values.count()}")
|
||||
ontime = overlaps.sum(lambda x: x.ontime)
|
||||
print(f"ontime: {ontime}")
|
||||
print(f"xp: {ontime * 8}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
26
bot/tools/get_version/__init__.py
Normal file
26
bot/tools/get_version/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
get-version CPL internal tool to set version from branch name
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
CPL internal tool to set version from branch name
|
||||
|
||||
:copyright: (c) 2022 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = "get_version"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2022 sh-edraft.de"
|
||||
__version__ = "1.0.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="1", minor="0", micro="0")
|
18
bot/tools/get_version/application.py
Normal file
18
bot/tools/get_version/application.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from cpl_cli.configuration import ProjectSettings
|
||||
from cpl_core.application.application_abc import ApplicationABC
|
||||
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection.service_provider_abc import ServiceProviderABC
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
ApplicationABC.__init__(self, config, services)
|
||||
|
||||
self._ps: ProjectSettings = config.get_configuration(ProjectSettings)
|
||||
|
||||
def configure(self):
|
||||
pass
|
||||
|
||||
def main(self):
|
||||
Console.write_line(self._ps.version.to_str())
|
44
bot/tools/get_version/get-version.json
Normal file
44
bot/tools/get_version/get-version.json
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "get-version",
|
||||
"Version": {
|
||||
"Major": "1",
|
||||
"Minor": "2",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "Sven Heidemann",
|
||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||
"Description": "CPL internal tool to set version from branch name",
|
||||
"LongDescription": "CPL internal tool to set version from branch name",
|
||||
"URL": "https://www.sh-edraft.de",
|
||||
"CopyrightDate": "2022",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||
"Dependencies": [
|
||||
"cpl-core==2022.12.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli==2022.12.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "console",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "set_version.main",
|
||||
"EntryPoint": "set-version",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
14
bot/tools/get_version/main.py
Normal file
14
bot/tools/get_version/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from get_version.application import Application
|
||||
from get_version.startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
app_builder.build().run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
31
bot/tools/get_version/startup.py
Normal file
31
bot/tools/get_version/startup.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
|
||||
from cpl_cli.configuration import WorkspaceSettings
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
||||
from cpl_core.environment import ApplicationEnvironment
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
def configure_configuration(
|
||||
self, configuration: ConfigurationABC, environment: ApplicationEnvironment
|
||||
) -> ConfigurationABC:
|
||||
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:
|
||||
return services.build_service_provider()
|
26
bot/tools/post_build/__init__.py
Normal file
26
bot/tools/post_build/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
post-build KDB - post build
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tool project to clean build project files
|
||||
|
||||
:copyright: (c) 2022 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = "post_build"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2022 sh-edraft.de"
|
||||
__version__ = "1.0.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="1", minor="0", micro="0")
|
23
bot/tools/post_build/application.py
Normal file
23
bot/tools/post_build/application.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from cpl_core.application import ApplicationABC
|
||||
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("KDB Post-Build:")
|
||||
Console.spinner(f"Removing unnecessary configs", self._remove_config.remove)
|
||||
Console.spinner(f"Creating requirements file for pip", self._deps.create)
|
33
bot/tools/post_build/appsettings.json
Normal file
33
bot/tools/post_build/appsettings.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"TimeFormatSettings": {
|
||||
"DateFormat": "%Y-%m-%d",
|
||||
"TimeFormat": "%H:%M:%S",
|
||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||
},
|
||||
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/",
|
||||
"Filename": "log_$start_time.log",
|
||||
"ConsoleLogLevel": "ERROR",
|
||||
"FileLogLevel": "WARN"
|
||||
},
|
||||
|
||||
"PostBuild": {
|
||||
"KeepConfigs": [
|
||||
"appsettings.json",
|
||||
"appsettings.development.json",
|
||||
"appsettings.staging.json",
|
||||
"appsettings.production.json",
|
||||
"feature-flags.json",
|
||||
"apisettings.json",
|
||||
"apisettings.development.json",
|
||||
"apisettings.staging.json",
|
||||
"apisettings.production.json"
|
||||
],
|
||||
"ConfigPaths": [
|
||||
"bot/config/",
|
||||
"bot_api/config/"
|
||||
]
|
||||
}
|
||||
}
|
14
bot/tools/post_build/main.py
Normal file
14
bot/tools/post_build/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from post_build.application import Application
|
||||
from post_build.startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
app_builder.build().run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
44
bot/tools/post_build/post-build.json
Normal file
44
bot/tools/post_build/post-build.json
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "post-build",
|
||||
"Version": {
|
||||
"Major": "1",
|
||||
"Minor": "2",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "Sven Heidemann",
|
||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||
"Description": "KDB - post build",
|
||||
"LongDescription": "Tool project to clean build project files",
|
||||
"URL": "https://www.sh-edraft.de",
|
||||
"CopyrightDate": "2022",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||
"Dependencies": [
|
||||
"cpl-core==2022.12.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli==2022.12.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "console",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "post_build.main",
|
||||
"EntryPoint": "post-build",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
32
bot/tools/post_build/post_build_settings.py
Normal file
32
bot/tools/post_build/post_build_settings.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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()}"
|
||||
)
|
26
bot/tools/post_build/service/__init__.py
Normal file
26
bot/tools/post_build/service/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
post-build KDB - post build
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tool project to clean build project files
|
||||
|
||||
:copyright: (c) 2022 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = "post_build.service"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2022 sh-edraft.de"
|
||||
__version__ = "1.0.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
# imports
|
||||
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="1", minor="0", micro="0")
|
67
bot/tools/post_build/service/dependencies.py
Normal file
67
bot/tools/post_build/service/dependencies.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import os
|
||||
|
||||
from cpl_cli.configuration import WorkspaceSettings, ProjectSettings, BuildSettings
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
|
||||
from post_build.post_build_settings import PostBuildSettings
|
||||
|
||||
|
||||
class Dependencies:
|
||||
def __init__(
|
||||
self,
|
||||
config: ConfigurationABC,
|
||||
env: ApplicationEnvironmentABC,
|
||||
ws: WorkspaceSettings,
|
||||
project: ProjectSettings,
|
||||
build: BuildSettings,
|
||||
post_build: PostBuildSettings,
|
||||
):
|
||||
self._config = config
|
||||
self._env = env
|
||||
self._workspace = ws
|
||||
self._project = project
|
||||
self._build = build
|
||||
self._post_build = post_build
|
||||
|
||||
self._dependencies = {}
|
||||
|
||||
def _add_dependencies(self, deps: list[str]):
|
||||
for d in deps:
|
||||
d_name = d
|
||||
if ">=" in d:
|
||||
d_name = d.split(">=")[0]
|
||||
if "<=" in d:
|
||||
d_name = d.split("<=")[0]
|
||||
if "==" in d:
|
||||
d_name = d.split("==")[0]
|
||||
|
||||
if d_name in self._dependencies:
|
||||
continue
|
||||
|
||||
self._dependencies[d_name] = d
|
||||
|
||||
def create(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 project in self._workspace.projects:
|
||||
project_file = os.path.join(
|
||||
self._env.working_directory, self._workspace.projects[project]
|
||||
)
|
||||
self._config.add_json_file(project_file, output=False, optional=False)
|
||||
project: ProjectSettings = self._config.get_configuration(ProjectSettings)
|
||||
self._add_dependencies(project.dependencies)
|
||||
|
||||
with open(f"{dist_path}/requirements.txt", "w+", encoding="utf-8") as f:
|
||||
for dependency in self._dependencies:
|
||||
f.write(f"{self._dependencies[dependency]}\n")
|
||||
f.close()
|
43
bot/tools/post_build/service/remove_config.py
Normal file
43
bot/tools/post_build/service/remove_config.py
Normal file
@@ -0,0 +1,43 @@
|
||||
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)))
|
38
bot/tools/post_build/startup.py
Normal file
38
bot/tools/post_build/startup.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import os.path
|
||||
|
||||
from cpl_cli.configuration import WorkspaceSettings
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
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):
|
||||
def __init__(self):
|
||||
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()
|
26
bot/tools/set_version/__init__.py
Normal file
26
bot/tools/set_version/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
set-version CPL internal tool to set version from branch name
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
CPL internal tool to set version from branch name
|
||||
|
||||
:copyright: (c) 2022 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = "set_version"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2022 sh-edraft.de"
|
||||
__version__ = "2022.7.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="2022", minor="7", micro="0")
|
150
bot/tools/set_version/application.py
Normal file
150
bot/tools/set_version/application.py
Normal file
@@ -0,0 +1,150 @@
|
||||
import os
|
||||
import traceback
|
||||
|
||||
from cpl_cli.configuration import ProjectSettings
|
||||
from cpl_cli.configuration.version_settings_name_enum import VersionSettingsNameEnum
|
||||
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
|
||||
from cpl_core.application.application_abc import ApplicationABC
|
||||
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl_core.console.console import Console
|
||||
from cpl_core.dependency_injection.service_provider_abc import ServiceProviderABC
|
||||
from cpl_core.pipes.version_pipe import VersionPipe
|
||||
from cpl_core.utils import String
|
||||
|
||||
from set_version.git_service import GitService
|
||||
from set_version.version_setter_service import VersionSetterService
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
ApplicationABC.__init__(self, config, services)
|
||||
|
||||
self._workspace: WorkspaceSettings = config.get_configuration(WorkspaceSettings)
|
||||
|
||||
self._git_service: GitService = self._services.get_service(GitService)
|
||||
self._version_setter: VersionSetterService = self._services.get_service(
|
||||
VersionSetterService
|
||||
)
|
||||
self._version_pipe: VersionPipe = self._services.get_service(VersionPipe)
|
||||
|
||||
def configure(self):
|
||||
self._configuration.parse_console_arguments(self._services)
|
||||
|
||||
def main(self):
|
||||
Console.write_line("Set versions:")
|
||||
|
||||
args = self._configuration.additional_arguments
|
||||
version = {}
|
||||
branch = ""
|
||||
suffix = ""
|
||||
force = False
|
||||
if "--force" in args:
|
||||
args.remove("--force")
|
||||
force = True
|
||||
|
||||
if len(args) > 1:
|
||||
Console.error(f'Unexpected argument(s): {", ".join(args[1:])}')
|
||||
return
|
||||
|
||||
if len(args) == 1:
|
||||
suffix = args[0]
|
||||
|
||||
try:
|
||||
branch = self._git_service.get_active_branch_name()
|
||||
Console.write_line(f"Found branch: {branch}")
|
||||
except Exception as e:
|
||||
Console.error("Branch not found", traceback.format_exc())
|
||||
return
|
||||
|
||||
try:
|
||||
if branch.startswith("#"):
|
||||
self._configuration.add_json_file(
|
||||
self._workspace.projects[self._workspace.default_project],
|
||||
optional=False,
|
||||
output=False,
|
||||
)
|
||||
ps: ProjectSettings = self._configuration.get_configuration(
|
||||
ProjectSettings
|
||||
)
|
||||
|
||||
version[VersionSettingsNameEnum.major.value] = ps.version.major
|
||||
version[VersionSettingsNameEnum.minor.value] = ps.version.minor
|
||||
version[
|
||||
VersionSettingsNameEnum.micro.value
|
||||
] = f'dev{branch.split("#")[1]}'
|
||||
else:
|
||||
if "." not in branch and "." in suffix:
|
||||
branch = suffix
|
||||
suffix = ""
|
||||
|
||||
version[VersionSettingsNameEnum.major.value] = branch.split(".")[0]
|
||||
version[VersionSettingsNameEnum.minor.value] = branch.split(".")[1]
|
||||
if len(branch.split(".")) == 2:
|
||||
if suffix == "":
|
||||
suffix = "0"
|
||||
version[VersionSettingsNameEnum.micro.value] = f"{suffix}"
|
||||
else:
|
||||
if not suffix.startswith(".") and suffix != "":
|
||||
suffix = f".{suffix}"
|
||||
version[
|
||||
VersionSettingsNameEnum.micro.value
|
||||
] = f'{branch.split(".")[2]}{suffix}'
|
||||
except Exception as e:
|
||||
Console.error(f"Branch {branch} does not contain valid version")
|
||||
return
|
||||
|
||||
diff_paths = []
|
||||
for file in self._git_service.get_diff_files():
|
||||
if "kdb-bot/src" in file:
|
||||
file = file.replace("kdb-bot/src", "")
|
||||
|
||||
if file.startswith("tools") or "kdb-web/src" in file:
|
||||
continue
|
||||
|
||||
if "/" in file:
|
||||
file = file.split("/")[1]
|
||||
else:
|
||||
file = os.path.basename(os.path.dirname(file))
|
||||
|
||||
if file in diff_paths:
|
||||
continue
|
||||
|
||||
diff_paths.append(file)
|
||||
|
||||
try:
|
||||
skipped = []
|
||||
for project in self._workspace.projects:
|
||||
if (
|
||||
project not in diff_paths
|
||||
and String.convert_to_snake_case(project) not in diff_paths
|
||||
and not force
|
||||
):
|
||||
# Console.write_line(f'Skipping {project} due to missing changes')
|
||||
skipped.append(project)
|
||||
continue
|
||||
|
||||
Console.write_line(
|
||||
f"Set dependencies {self._version_pipe.transform(version)} for {project}"
|
||||
)
|
||||
self._version_setter.set_dependencies(
|
||||
self._workspace.projects[project],
|
||||
version,
|
||||
"Dependencies",
|
||||
skipped=skipped,
|
||||
)
|
||||
self._version_setter.set_dependencies(
|
||||
self._workspace.projects[project],
|
||||
version,
|
||||
"DevDependencies",
|
||||
skipped=skipped,
|
||||
)
|
||||
|
||||
Console.write_line(
|
||||
f"Set version {self._version_pipe.transform(version)} for {project}"
|
||||
)
|
||||
self._version_setter.set_version(
|
||||
self._workspace.projects[project], version
|
||||
)
|
||||
except Exception as e:
|
||||
Console.error("Version could not be set", traceback.format_exc())
|
||||
return
|
17
bot/tools/set_version/git_service.py
Normal file
17
bot/tools/set_version/git_service.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import os
|
||||
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from git import Repo
|
||||
|
||||
|
||||
class GitService:
|
||||
def __init__(self, env: ApplicationEnvironmentABC):
|
||||
self._env = env
|
||||
self._repo = Repo(os.path.abspath(os.path.join(env.working_directory, "../")))
|
||||
|
||||
def get_active_branch_name(self) -> str:
|
||||
branch = self._repo.active_branch
|
||||
return branch.name
|
||||
|
||||
def get_diff_files(self) -> list[str]:
|
||||
return [item.a_path for item in self._repo.index.diff(None)]
|
14
bot/tools/set_version/main.py
Normal file
14
bot/tools/set_version/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from set_version.application import Application
|
||||
from set_version.startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
app_builder.build().run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
45
bot/tools/set_version/set-version.json
Normal file
45
bot/tools/set_version/set-version.json
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "set-version",
|
||||
"Version": {
|
||||
"Major": "1",
|
||||
"Minor": "2",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "Sven Heidemann",
|
||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||
"Description": "CPL internal tool to set version from branch name",
|
||||
"LongDescription": "CPL internal tool to set version from branch name",
|
||||
"URL": "https://www.sh-edraft.de",
|
||||
"CopyrightDate": "2022",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||
"Dependencies": [
|
||||
"cpl-core==2022.12.0",
|
||||
"GitPython==3.1.27"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli==2022.12.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "console",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "set_version.main",
|
||||
"EntryPoint": "set-version",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
38
bot/tools/set_version/startup.py
Normal file
38
bot/tools/set_version/startup.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import os
|
||||
|
||||
from cpl_cli.configuration import WorkspaceSettings
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
||||
from cpl_core.environment import ApplicationEnvironment
|
||||
from cpl_core.pipes.version_pipe import VersionPipe
|
||||
from set_version.git_service import GitService
|
||||
from set_version.version_setter_service import VersionSetterService
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
def configure_configuration(
|
||||
self, configuration: ConfigurationABC, environment: ApplicationEnvironment
|
||||
) -> ConfigurationABC:
|
||||
configuration.add_json_file("cpl-workspace.json", optional=True, output=False)
|
||||
if configuration.get_configuration(WorkspaceSettings) is None:
|
||||
environment.set_working_directory(
|
||||
os.path.join(environment.working_directory, "../../")
|
||||
)
|
||||
configuration.add_json_file(
|
||||
"cpl-workspace.json", optional=False, output=False
|
||||
)
|
||||
|
||||
return configuration
|
||||
|
||||
def configure_services(
|
||||
self, services: ServiceCollectionABC, environment: ApplicationEnvironment
|
||||
) -> ServiceProviderABC:
|
||||
services.add_pipes()
|
||||
services.add_singleton(GitService)
|
||||
services.add_transient(VersionSetterService)
|
||||
|
||||
return services.build_service_provider()
|
72
bot/tools/set_version/version_setter_service.py
Normal file
72
bot/tools/set_version/version_setter_service.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import json
|
||||
import os
|
||||
from string import ascii_letters
|
||||
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.utils import String
|
||||
|
||||
|
||||
class VersionSetterService:
|
||||
def __init__(self, env: ApplicationEnvironmentABC):
|
||||
self._env = env
|
||||
|
||||
def _read_file(self, file: str) -> dict:
|
||||
project_json = {}
|
||||
with open(
|
||||
os.path.join(self._env.working_directory, file), "r", encoding="utf-8"
|
||||
) as f:
|
||||
# load json
|
||||
project_json = json.load(f)
|
||||
f.close()
|
||||
|
||||
return project_json
|
||||
|
||||
def _write_file(self, file: str, project_json: dict):
|
||||
with open(
|
||||
os.path.join(self._env.working_directory, file), "w", encoding="utf-8"
|
||||
) as f:
|
||||
f.write(json.dumps(project_json, indent=2))
|
||||
f.close()
|
||||
|
||||
def set_version(self, file: str, version: dict):
|
||||
project_json = self._read_file(file)
|
||||
project_json["ProjectSettings"]["Version"] = version
|
||||
self._write_file(file, project_json)
|
||||
|
||||
def set_dependencies(self, file: str, version: dict, key: str, skipped=None):
|
||||
project_json = self._read_file(file)
|
||||
if key not in project_json["ProjectSettings"]:
|
||||
project_json["ProjectSettings"][key] = []
|
||||
|
||||
dependencies = project_json["ProjectSettings"][key]
|
||||
new_deps = []
|
||||
for dependency in dependencies:
|
||||
if not dependency.startswith("cpl-"):
|
||||
new_deps.append(dependency)
|
||||
continue
|
||||
|
||||
dep_version = dependency.split("=")[1]
|
||||
dep_name = dependency.split("=")[0]
|
||||
if dep_name[len(dep_name) - 1] not in ascii_letters:
|
||||
dep_name = dep_name[: len(dep_name) - 1]
|
||||
|
||||
if (
|
||||
skipped is not None
|
||||
and (
|
||||
dep_name in skipped
|
||||
or String.convert_to_snake_case(dep_name) in skipped
|
||||
)
|
||||
or dep_version == ""
|
||||
):
|
||||
new_deps.append(dependency)
|
||||
continue
|
||||
|
||||
new_deps.append(
|
||||
dependency.replace(
|
||||
dep_version,
|
||||
f'{version["Major"]}.{version["Minor"]}.{version["Micro"]}',
|
||||
)
|
||||
)
|
||||
|
||||
project_json["ProjectSettings"][key] = new_deps
|
||||
self._write_file(file, project_json)
|
Reference in New Issue
Block a user