fix pylint

This commit is contained in:
Michael Jerger 2023-05-20 16:14:35 +02:00
parent ce35bc2d54
commit b44751149a
19 changed files with 82 additions and 126 deletions

View file

@ -30,7 +30,7 @@ pylint:
stage: lint&test stage: lint&test
script: script:
- pip install -r dev_requirements.txt - pip install -r dev_requirements.txt
- pylint -d W0511,R0903,C0301,W0614,C0114,C0115,C0116,similarities,W1203,W0702,W0702,R0913,R0902,R0914,R1732 src/main/python/ddadevops/ - pylint -d W0511,R0903,C0301,W0614,C0114,C0115,C0116,similarities,W1203,W0702,W0702,R0913,R0902,R0914,R1732,R1705,W0707,C0123,W0703,C0103 src/main/python/ddadevops/
pytest: pytest:
stage: lint&test stage: lint&test

View file

@ -1,5 +1,5 @@
from pathlib import Path from pathlib import Path
from ..domain import Image, Devops, BuildType from ..domain import Devops, BuildType
from ..infrastructure import FileApi, ResourceApi, ImageApi from ..infrastructure import FileApi, ResourceApi, ImageApi

View file

@ -1,4 +1,4 @@
from typing import Optional, List from typing import List
from pathlib import Path from pathlib import Path
from ..infrastructure import GitApi, BuildFileRepository from ..infrastructure import GitApi, BuildFileRepository
from ..domain import Version, Release, ReleaseType from ..domain import Version, Release, ReleaseType
@ -53,8 +53,8 @@ class ReleaseService:
def __set_version_and_commit__( def __set_version_and_commit__(
self, version: Version, build_file_ids: List[str], message: str self, version: Version, build_file_ids: List[str], message: str
): ):
for id in build_file_ids: for build_file_id in build_file_ids:
build_file = self.build_file_repository.get(Path(id)) build_file = self.build_file_repository.get(Path(build_file_id))
build_file.set_version(version) build_file.set_version(version)
self.build_file_repository.write(build_file) self.build_file_repository.write(build_file)
self.git_api.add_file(build_file.file_path) self.git_api.add_file(build_file.file_path)

View file

@ -49,7 +49,7 @@ class C4kBuild(DevopsBuild):
self.execution_api = ExecutionApi() self.execution_api = ExecutionApi()
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)
if BuildType.C4K not in devops.specialized_builds: if BuildType.C4K not in devops.specialized_builds:
raise ValueError(f"C4kBuild requires BuildType.C4K") raise ValueError("C4kBuild requires BuildType.C4K")
def update_runtime_config(self, dns_record: DnsRecord): def update_runtime_config(self, dns_record: DnsRecord):
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)

View file

@ -1,6 +1,5 @@
from typing import Optional
import deprecation import deprecation
from .domain import Devops, InitService from .domain import InitService
from .infrastructure import DevopsRepository, FileApi from .infrastructure import DevopsRepository, FileApi
@ -21,12 +20,12 @@ def get_devops_build(project):
class DevopsBuild: class DevopsBuild:
def __init__(self, project, input: dict): def __init__(self, project, inp: dict):
self.project = project self.project = project
self.file_api = FileApi() self.file_api = FileApi()
self.init_service = InitService.prod(project.basedir) self.init_service = InitService.prod(project.basedir)
self.devops_repo = DevopsRepository() self.devops_repo = DevopsRepository()
devops = self.init_service.initialize(input) devops = self.init_service.initialize(inp)
self.devops_repo.set_devops(self.project, devops) self.devops_repo.set_devops(self.project, devops)
self.project.set_property("build", self) self.project.set_property("build", self)

View file

@ -1,4 +1,3 @@
from typing import Optional
import deprecation import deprecation
from .domain import BuildType from .domain import BuildType
from .application import ImageBuildService from .application import ImageBuildService
@ -33,12 +32,12 @@ def create_devops_docker_build_config(
class DevopsImageBuild(DevopsBuild): class DevopsImageBuild(DevopsBuild):
def __init__(self, project, input: dict): def __init__(self, project, inp: dict):
super().__init__(project, input) super().__init__(project, inp)
self.image_build_service = ImageBuildService.prod() self.image_build_service = ImageBuildService.prod()
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)
if BuildType.IMAGE not in devops.specialized_builds: if BuildType.IMAGE not in devops.specialized_builds:
raise ValueError(f"ImageBuild requires BuildType.IMAGE") raise ValueError("ImageBuild requires BuildType.IMAGE")
def initialize_build_dir(self): def initialize_build_dir(self):
super().initialize_build_dir() super().initialize_build_dir()

View file

@ -1,16 +1,9 @@
from enum import Enum
from typing import Optional
from pathlib import Path
import re
import json import json
from .common import ( import re
Validateable, from enum import Enum
Devops, from pathlib import Path
ReleaseType, from .common import Validateable
) from .version import Version
from .version import (
Version,
)
class BuildFileType(Enum): class BuildFileType(Enum):
@ -110,18 +103,12 @@ class BuildFile(Validateable):
) )
self.content = substitute self.content = substitute
case BuildFileType.JAVA_CLOJURE: case BuildFileType.JAVA_CLOJURE:
version_line = re.search("\\(defproject .*\n", self.content)
version_line_group = version_line.group()
# TODO: we should stick here on defproject instead of first line! # TODO: we should stick here on defproject instead of first line!
version_string = re.search(
"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group
)
version_str = version_string.group()
substitute = re.sub( substitute = re.sub(
'"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', '"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"',
f'"{new_version.to_string()}"', f'"{new_version.to_string()}"',
self.content, self.content,
1 1,
) )
self.content = substitute self.content = substitute
except: except:

View file

@ -7,18 +7,18 @@ from .common import (
class C4k(Validateable): class C4k(Validateable):
def __init__(self, input: dict): def __init__(self, inp: dict):
self.module = input.get("module") self.module = inp.get("module")
self.stage = input.get("stage") self.stage = inp.get("stage")
self.c4k_executable_name = input.get("c4k_executable_name", input.get("module")) self.c4k_executable_name = inp.get("c4k_executable_name", inp.get("module"))
self.c4k_config = input.get("c4k_config", {}) self.c4k_config = inp.get("c4k_config", {})
self.c4k_grafana_cloud_url = input.get( self.c4k_grafana_cloud_url = inp.get(
"c4k_grafana_cloud_url", "c4k_grafana_cloud_url",
"https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push", "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push",
) )
self.c4k_auth = input.get("c4k_auth", {}) self.c4k_auth = inp.get("c4k_auth", {})
self.c4k_grafana_cloud_user = input.get('c4k_grafana_cloud_user') self.c4k_grafana_cloud_user = inp.get('c4k_grafana_cloud_user')
self.c4k_grafana_cloud_password = input.get('c4k_grafana_cloud_password') self.c4k_grafana_cloud_password = inp.get('c4k_grafana_cloud_password')
self.dns_record: Optional[DnsRecord] = None self.dns_record: Optional[DnsRecord] = None
# TODO: these functions should be located at TerraformBuild later on. # TODO: these functions should be located at TerraformBuild later on.

View file

@ -1,7 +1,5 @@
import deprecation
from enum import Enum from enum import Enum
from typing import List, TypedDict from typing import List
import deprecation
def filter_none(list_to_filter): def filter_none(list_to_filter):
@ -70,15 +68,15 @@ class DnsRecord(Validateable):
class Devops(Validateable): class Devops(Validateable):
def __init__( def __init__(
self, self,
input: dict, inp: dict,
specialized_builds: dict[BuildType, Validateable], specialized_builds: dict[BuildType, Validateable],
mixins: dict[MixinType, Validateable], mixins: dict[MixinType, Validateable],
): ):
self.stage = input.get("stage") self.stage = inp.get("stage")
self.project_root_path = input.get("project_root_path") self.project_root_path = inp.get("project_root_path")
self.module = input.get("module") self.module = inp.get("module")
self.name = input.get("name", self.module) self.name = inp.get("name", self.module)
self.build_dir_name = input.get("build_dir_name", "target") self.build_dir_name = inp.get("build_dir_name", "target")
self.specialized_builds = specialized_builds self.specialized_builds = specialized_builds
self.mixins = mixins self.mixins = mixins
@ -99,15 +97,3 @@ class Devops(Validateable):
for mixin in self.mixins: for mixin in self.mixins:
result += self.mixins[mixin].validate() result += self.mixins[mixin].validate()
return result return result
def __put__(self, key, value):
self.stack[key] = value
def __get(self, key):
return self.stack[key]
def __get_keys__(self, keys):
result = {}
for key in keys:
result[key] = self.__get(key)
return result

View file

@ -1,8 +1,6 @@
import deprecation
from enum import Enum from enum import Enum
from typing import List, TypedDict from typing import List
from inflection import underscore from inflection import underscore
import deprecation
from .common import ( from .common import (
Validateable, Validateable,
) )
@ -23,7 +21,7 @@ class CredentialMapping(Validateable):
result = [] result = []
result += self.__validate_is_not_empty__("gopass_path") result += self.__validate_is_not_empty__("gopass_path")
if not self.name and not self.gopass_field: if not self.name and not self.gopass_field:
result.append(f"Either name or gopass field has to be defined.") result.append("Either name or gopass field has to be defined.")
return result return result
def gopass_type(self): def gopass_type(self):
@ -46,14 +44,14 @@ class CredentialMapping(Validateable):
class Credentials(Validateable): class Credentials(Validateable):
def __init__(self, input: dict, default_mappings: list = []): def __init__(self, inp: dict, default_mappings: list = []):
input_mappings = input.get("credentials_mapping", []) inp_mappings = inp.get("credentials_mapping", [])
self.mappings = {} self.mappings = {}
for input_mapping in default_mappings: for inp_mapping in default_mappings:
mapping = CredentialMapping(input_mapping) mapping = CredentialMapping(inp_mapping)
self.mappings[mapping.name_for_input()] = mapping self.mappings[mapping.name_for_input()] = mapping
for input_mapping in input_mappings: for inp_mapping in inp_mappings:
mapping = CredentialMapping(input_mapping) mapping = CredentialMapping(inp_mapping)
self.mappings[mapping.name_for_input()] = mapping self.mappings[mapping.name_for_input()] = mapping
def validate(self) -> List[str]: def validate(self) -> List[str]:

View file

@ -1,5 +1,3 @@
import deprecation
from enum import Enum
from typing import List, Optional, Dict from typing import List, Optional, Dict
from .common import Validateable, Devops, BuildType, MixinType from .common import Validateable, Devops, BuildType, MixinType
from .image import Image from .image import Image
@ -12,28 +10,28 @@ class DevopsFactory:
def __init__(self): def __init__(self):
pass pass
def build_devops(self, input: dict, version: Optional[Version] = None) -> Devops: def build_devops(self, inp: dict, version: Optional[Version] = None) -> Devops:
build_types = self.__parse_build_types__(input["build_types"]) build_types = self.__parse_build_types__(inp["build_types"])
mixin_types = self.__parse_mixin_types__(input["mixin_types"]) mixin_types = self.__parse_mixin_types__(inp["mixin_types"])
specialized_builds: Dict[BuildType, Validateable] = {} specialized_builds: Dict[BuildType, Validateable] = {}
if BuildType.IMAGE in build_types: if BuildType.IMAGE in build_types:
specialized_builds[BuildType.IMAGE] = Image(input) specialized_builds[BuildType.IMAGE] = Image(inp)
if BuildType.C4K in build_types: if BuildType.C4K in build_types:
specialized_builds[BuildType.C4K] = C4k(input) specialized_builds[BuildType.C4K] = C4k(inp)
mixins: Dict[MixinType, Validateable] = {} mixins: Dict[MixinType, Validateable] = {}
if MixinType.RELEASE in mixin_types: if MixinType.RELEASE in mixin_types:
mixins[MixinType.RELEASE] = Release(input, version) mixins[MixinType.RELEASE] = Release(inp, version)
devops = Devops(input, specialized_builds=specialized_builds, mixins=mixins) devops = Devops(inp, specialized_builds=specialized_builds, mixins=mixins)
devops.throw_if_invalid() devops.throw_if_invalid()
return devops return devops
def merge(self, input: dict, context: dict, authorization: dict) -> dict: def merge(self, inp: dict, context: dict, authorization: dict) -> dict:
return {} | context | authorization | input return {} | context | authorization | inp
def __parse_build_types__(self, build_types: List[str]) -> List[BuildType]: def __parse_build_types__(self, build_types: List[str]) -> List[BuildType]:
result = [] result = []

View file

@ -1,4 +1,4 @@
from typing import Optional, List from typing import List
from .common import ( from .common import (
filter_none, filter_none,
Validateable, Validateable,
@ -8,16 +8,16 @@ from .common import (
class Image(Validateable): class Image(Validateable):
def __init__( def __init__(
self, self,
input: dict, inp: dict,
): ):
self.image_dockerhub_user = input.get("image_dockerhub_user") self.image_dockerhub_user = inp.get("image_dockerhub_user")
self.image_dockerhub_password = input.get("image_dockerhub_password") self.image_dockerhub_password = inp.get("image_dockerhub_password")
self.image_tag = input.get("image_tag") self.image_tag = inp.get("image_tag")
self.image_build_commons_path = input.get("image_build_commons_path") self.image_build_commons_path = inp.get("image_build_commons_path")
self.image_use_package_common_files = input.get( self.image_use_package_common_files = inp.get(
"image_use_package_common_files", True "image_use_package_common_files", True
) )
self.image_build_commons_dir_name = input.get( self.image_build_commons_dir_name = inp.get(
"image_build_commons_dir_name", "docker" "image_build_commons_dir_name", "docker"
) )

View file

@ -1,9 +1,8 @@
from pathlib import Path from pathlib import Path
from typing import List, Dict from typing import Dict
from .common import Devops, MixinType, BuildType from .common import Devops, MixinType, BuildType
from .credentials import CredentialMapping, Credentials, GopassType from .credentials import CredentialMapping, Credentials, GopassType
from .devops_factory import DevopsFactory from .devops_factory import DevopsFactory
from .version import Version
from .release import ReleaseType from .release import ReleaseType
from ..infrastructure import BuildFileRepository, CredentialsApi, EnvironmentApi, GitApi from ..infrastructure import BuildFileRepository, CredentialsApi, EnvironmentApi, GitApi
@ -33,9 +32,9 @@ class InitService:
GitApi(), GitApi(),
) )
def initialize(self, input: dict) -> Devops: def initialize(self, inp: dict) -> Devops:
build_types = self.devops_factory.__parse_build_types__(input["build_types"]) build_types = self.devops_factory.__parse_build_types__(inp["build_types"])
mixin_types = self.devops_factory.__parse_mixin_types__(input["mixin_types"]) mixin_types = self.devops_factory.__parse_mixin_types__(inp["mixin_types"])
version = None version = None
default_mappings = [] default_mappings = []
@ -66,7 +65,7 @@ class InitService:
] ]
if MixinType.RELEASE in mixin_types: if MixinType.RELEASE in mixin_types:
primary_build_file_id = input.get( primary_build_file_id = inp.get(
"release_primary_build_file", "./project.clj" "release_primary_build_file", "./project.clj"
) )
primary_build_file = self.build_file_repository.get( primary_build_file = self.build_file_repository.get(
@ -74,12 +73,12 @@ class InitService:
) )
version = primary_build_file.get_version() version = primary_build_file.get_version()
credentials = Credentials(input, default_mappings) credentials = Credentials(inp, default_mappings)
authorization = self.authorization(credentials) authorization = self.authorization(credentials)
context = self.context(mixin_types, version) context = self.context(mixin_types, version)
merged = self.devops_factory.merge(input, context, authorization) merged = self.devops_factory.merge(inp, context, authorization)
return self.devops_factory.build_devops(merged, version=version) return self.devops_factory.build_devops(merged, version=version)

View file

@ -1,9 +1,7 @@
from enum import Enum
from typing import Optional, List from typing import Optional, List
from pathlib import Path from pathlib import Path
from .common import ( from .common import (
Validateable, Validateable,
Devops,
ReleaseType, ReleaseType,
) )
from .version import ( from .version import (
@ -12,14 +10,14 @@ from .version import (
class Release(Validateable): class Release(Validateable):
def __init__(self, input: dict, version: Optional[Version]): def __init__(self, inp: dict, version: Optional[Version]):
self.release_type = ReleaseType[input.get("release_type", "NONE")] self.release_type = ReleaseType[inp.get("release_type", "NONE")]
self.release_main_branch = input.get("release_main_branch", "main") self.release_main_branch = inp.get("release_main_branch", "main")
self.release_current_branch = input.get("release_current_branch") self.release_current_branch = inp.get("release_current_branch")
self.release_primary_build_file = input.get( self.release_primary_build_file = inp.get(
"release_primary_build_file", "./project.clj" "release_primary_build_file", "./project.clj"
) )
self.release_secondary_build_files = input.get( self.release_secondary_build_files = inp.get(
"release_secondary_build_files", [] "release_secondary_build_files", []
) )
self.version = version self.version = version

View file

@ -1,4 +1,3 @@
from enum import Enum
from typing import Optional from typing import Optional
from .common import ( from .common import (
Validateable, Validateable,
@ -37,7 +36,7 @@ class Version(Validateable):
return self.to_string().__hash__() return self.to_string().__hash__()
def is_snapshot(self): def is_snapshot(self):
return not self.snapshot_suffix == None return not self.snapshot_suffix is None
def to_string(self) -> str: def to_string(self) -> str:
version_no = ".".join([str(x) for x in self.version_list]) version_no = ".".join([str(x) for x in self.version_list])
@ -49,7 +48,7 @@ class Version(Validateable):
result = [] result = []
result += self.__validate_is_not_empty__("version_list") result += self.__validate_is_not_empty__("version_list")
if self.version_list and len(self.version_list) < 3: if self.version_list and len(self.version_list) < 3:
result += [f"version_list must have at least 3 levels."] result += ["version_list must have at least 3 levels."]
if ( if (
self.version_list self.version_list
and self.version_string and self.version_string

View file

@ -1,10 +1,9 @@
from subprocess import check_output, Popen, PIPE, run
from pathlib import Path from pathlib import Path
from sys import stdout from sys import stdout
from os import chmod, environ from os import chmod, environ
from pkg_resources import resource_string from pkg_resources import resource_string
import yaml import yaml
from subprocess import check_output, Popen, PIPE, run
from ..domain import Devops, Image, C4k, Release, BuildFile
class ResourceApi: class ResourceApi:
@ -99,7 +98,7 @@ class ExecutionApi:
output = output.rstrip() output = output.rstrip()
return output return output
def execute_live(command): def execute_live(self, command):
process = Popen(command, stdout=PIPE) process = Popen(command, stdout=PIPE)
for line in iter(process.stdout.readline, b""): for line in iter(process.stdout.readline, b""):
print(line.decode("utf-8"), end="") print(line.decode("utf-8"), end="")

View file

@ -1,12 +1,6 @@
from pathlib import Path from pathlib import Path
from sys import stdout from ..domain.common import Devops
from os import chmod from ..domain.build_file import BuildFile
from subprocess import run
from pkg_resources import resource_string
import yaml
import deprecation
from ..domain import Devops, Image, C4k, Release, BuildFile
from ..python_util import execute
class DevopsRepository: class DevopsRepository:

View file

@ -5,12 +5,12 @@ from .domain import MixinType
class ReleaseMixin(DevopsBuild): class ReleaseMixin(DevopsBuild):
def __init__(self, project: Project, input: dict): def __init__(self, project: Project, inp: dict):
super().__init__(project, input) super().__init__(project, inp)
self.release_service = ReleaseService.prod(project.basedir) self.release_service = ReleaseService.prod(project.basedir)
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)
if MixinType.RELEASE not in devops.mixins: if MixinType.RELEASE not in devops.mixins:
raise ValueError(f"ReleaseMixin requires MixinType.RELEASE") raise ValueError("ReleaseMixin requires MixinType.RELEASE")
def prepare_release(self): def prepare_release(self):
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)