Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ruff and mypy #57

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
// Set *default* container specific settings.json values on container create.
"settings": {
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
"editor.defaultFormatter": "charliermarsh.ruff"
},
"python.testing.pytestArgs": [
"--ignore=desired_state_generator/test/integration",
Expand All @@ -28,14 +33,11 @@
"python.testing.pytestEnabled": true,
"python.pythonPath": "/usr/bin/python3",
"python.defaultInterpreterPath": "/usr/bin/python3",
// Only Flake8 is used as linter and static code analyzer, as faster tool
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
// Style Formatter
"python.formatting.provider": "black",
// Security Linter
"python.linting.banditEnabled": true,
"python.disableInstallationCheck": true,
"python.analysis.typeCheckingMode": "basic",
// Strong Type Checker
"mypy.enabled": true,
"mypy.runUsingActiveInterpreter": true,
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": {
"zsh": {
Expand All @@ -48,14 +50,11 @@
"ms-azuretools.vscode-dapr",
"ms-azuretools.vscode-docker",
"ms-python.python",
"cschleiden.vscode-github-actions",
"rpdswtk.vsmqtt",
"dotjoshjohnson.xml",
"ms-kubernetes-tools.vscode-kubernetes-tools",
"matangover.mypy",
"augustocdias.tasks-shell-input",
"ms-python.isort",
"ms-python.flake8"
"ms-python.mypy-type-checker",
"charliermarsh.ruff"
]
}
},
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install required packages
run: |
pip install mypy

- name: Run Linters
uses: ./.github/actions/pre-commit-action

Expand Down
67 changes: 20 additions & 47 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -25,45 +25,28 @@ repos:
exclude: ^runtime_k3d/src/runtime/deployment/config/helm/templates/|^runtime_k3d/src/app_deployment/config/helm/templates/|^config/feedercan/
- id: check-added-large-files

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: isort
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/psf/black
rev: 23.3.0
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.8.0"
hooks:
- id: black

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-unused-arguments
args: ["--ignore=E203"]

- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: bandit
types: [python]
args: ["--ini", "setup.cfg"]

# Commented out because of mypy bug failing for "match case" syntax
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: "v1.2.0"
# hooks:
# - id: mypy
# args: [runtime_k3d, runtime_local]
# language: system
# pass_filenames: false

- repo: https://github.com/pycqa/pydocstyle
rev: 6.3.0
hooks:
- id: pydocstyle
- id: mypy
args:
[
"--config-file",
"setup.cfg",
runtime_k3d,
runtime_local,
runtime_kanto,
desired_state_generator,
]
language: system
pass_filenames: false

- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.4
Expand All @@ -80,16 +63,6 @@ repos:
- --use-current-year
- --allow-past-years
- --skip-license-insertion-comment=This file is maintained by velocitas CLI, do not modify manually.
- id: insert-license
files: '.*\.(cpp|c|cc|h)$'
args:
- --license-filepath
- license_header.txt
- --comment-style
- /**| *| */
- --use-current-year
- --allow-past-years
- --skip-license-insertion-comment=This file is maintained by velocitas CLI, do not modify manually.
- id: insert-license
files: "Dockerfile.*"
args:
Expand Down
2 changes: 1 addition & 1 deletion runtime_k3d/src/runtime/deployment/gen_podspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def generate_nodeport_service(service_id: str, port: str) -> dict[str, Any]:
service_config: The parsed configuration from the runtime.json
port: The port to be published
"""
nodeport_spec = {
nodeport_spec: dict[str, Any] = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"name": f"{service_id}-nodeport{port}"},
Expand Down
2 changes: 1 addition & 1 deletion runtime_k3d/src/runtime/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def is_runtime_installed(log_output: TextIOWrapper | int = subprocess.DEVNULL) -
if key == "STATUS" and value == "deployed":
return True

log_output.write(process.stdout)
log_output.write(process.stdout) # type: ignore
BjoernAtBosch marked this conversation as resolved.
Show resolved Hide resolved

return False

Expand Down
4 changes: 2 additions & 2 deletions runtime_k3d/test/integration/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def check_cluster_status_and_dapr(kubectl_regs: dict, dapr_regs: dict) -> bool:
)


def check_image_if_created(image_reg: Pattern) -> bool:
def check_image_if_created() -> bool:
command_get_images_logs = check_output(["docker", "images"]).decode()
return matches_any_regex(image_reg, command_get_images_logs)

Expand All @@ -87,7 +87,7 @@ def test_scripts_run_successfully():
assert run_command(f"{BASE_COMMAND_RUNTIME} up")
assert check_cluster_status_and_dapr(kubectl_regs, dapr_regs)
assert run_command(f"{BASE_COMMAND_DEPLOYMENT} build-vehicleapp")
assert check_image_if_created(image_reg)
assert check_image_if_created()
assert run_command(f"{BASE_COMMAND_DEPLOYMENT} deploy-vehicleapp")
assert check_pods(pods_regs)

Expand Down
8 changes: 4 additions & 4 deletions runtime_kanto/src/app_deployment/deploy_vehicleapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ def remove_vehicleapp(
log_output (TextIOWrapper | int): Logfile to write or DEVNULL by default.
"""
if is_vehicleapp_in_kanto(app_name):
log_output.write(f"Removing {app_name} container from Kanto\n")
log_output.write(f"Removing {app_name} container from Kanto\n") # type: ignore
subprocess.call(
["kanto-cm", "remove", "-f", "-n", app_name],
stdout=log_output,
stderr=log_output,
)

if is_vehicleapp_in_containerd(app_name):
log_output.write(f"Removing {app_name} container from containerd\n")
log_output.write(f"Removing {app_name} container from containerd\n") # type: ignore
ps = subprocess.Popen(
(
"sudo",
Expand Down Expand Up @@ -167,7 +167,7 @@ def create_container(
mqtt_port = get_service_port("mqtt-broker")
mqtt_address = "mqtt://127.0.0.1"

log_output.write(f"Creating new {app_name} container\n")
log_output.write(f"Creating new {app_name} container\n") # type: ignore
subprocess.check_call(
[
"kanto-cm",
Expand Down Expand Up @@ -201,7 +201,7 @@ def start_container(
log_output (TextIOWrapper | int): Logfile to write or DEVNULL by default.
"""

log_output.write(f"Starting {app_name} container\n")
log_output.write(f"Starting {app_name} container\n") # type: ignore
subprocess.check_call(
[
"kanto-cm",
Expand Down
14 changes: 7 additions & 7 deletions runtime_kanto/src/runtime/controlplane_kanto.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def create_and_start_registry(log_output: TextIOWrapper | int = subprocess.DEVNU
Args:
log_output (TextIOWrapper | int): Logfile to write or DEVNULL by default.
"""
log_output.write("Creating and starting Kanto registry container\n")
log_output.write("Creating and starting Kanto registry container\n") # type: ignore
subprocess.check_call(
[
"docker",
Expand All @@ -76,7 +76,7 @@ def start_registry(log_output: TextIOWrapper | int = subprocess.DEVNULL):
Args:
log_output (TextIOWrapper | int): Logfile to write or DEVNULL by default.
"""
log_output.write("Starting Kanto registry container\n")
log_output.write("Starting Kanto registry container\n") # type: ignore
subprocess.check_call(
["docker", "start", KANTO_REGISTRY_NAME],
stdout=log_output,
Expand All @@ -90,7 +90,7 @@ def stop_registry(log_output: TextIOWrapper | int = subprocess.DEVNULL):
Args:
log_output (TextIOWrapper | int): Logfile to write or DEVNULL by default.
"""
log_output.write("Stopping Kanto registry container\n")
log_output.write("Stopping Kanto registry container\n") # type: ignore
subprocess.check_call(
["docker", "stop", KANTO_REGISTRY_NAME],
stdout=log_output,
Expand Down Expand Up @@ -118,17 +118,17 @@ def configure_controlplane(
spinner.write(status + "starting registry.")
create_and_start_registry(log_output)
spinner.write(status + "started.")
log_output.write(status + "started.\n")
log_output.write(status + "started.\n") # type: ignore
else:
spinner.write(status + "registry already exists.")
if registry_running(log_output):
spinner.write(status + "registry already started.")
log_output.write(status + "registry already started.\n")
log_output.write(status + "registry already started.\n") # type: ignore
else:
spinner.write(status + "starting registry.")
start_registry(log_output)
spinner.write(status + "started.")
log_output.write(status + "started.\n")
log_output.write(status + "started.\n") # type: ignore


def reset_controlplane(
Expand All @@ -149,4 +149,4 @@ def reset_controlplane(
else:
status = status + "does not exist."
spinner.write(status)
log_output.write(status + "\n")
log_output.write(status + "\n") # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,38 @@ def remove_container(log_output: TextIOWrapper | int = subprocess.DEVNULL):
Args:
log_output (TextIOWrapper | int): Logfile to write or DEVNULL by default.
"""
log_output.write("Removing databroker container\n")
log_output.write("Removing databroker container\n") # type: ignore
subprocess.call(
["kanto-cm", "remove", "-f", "-n", "databroker"],
stdout=log_output,
stderr=log_output,
)
log_output.write("Removing mosquitto container\n")
log_output.write("Removing mosquitto container\n") # type: ignore
subprocess.call(
["kanto-cm", "remove", "-f", "-n", "mosquitto"],
stdout=log_output,
stderr=log_output,
)
log_output.write("Removing feedercan container\n")
log_output.write("Removing feedercan container\n") # type: ignore
subprocess.call(
["kanto-cm", "remove", "-f", "-n", "feedercan"],
stdout=log_output,
stderr=log_output,
)
log_output.write("Removing seatservice container\n")
log_output.write("Removing seatservice container\n") # type: ignore
subprocess.call(
["kanto-cm", "remove", "-f", "-n", "seatservice"],
stdout=log_output,
stderr=log_output,
)
log_output.write("Removing mockservice container\n")
log_output.write("Removing mockservice container\n") # type: ignore
subprocess.call(
["kanto-cm", "remove", "-f", "-n", "mockservice"],
stdout=log_output,
stderr=log_output,
)
app_name = get_app_manifest()["name"].lower()
log_output.write(f"Removing {app_name} container\n")
log_output.write(f"Removing {app_name} container\n") # type: ignore
remove_vehicleapp(app_name, log_output)


Expand Down Expand Up @@ -209,7 +209,7 @@ def start_kanto(spinner: Yaspin, log_output: TextIOWrapper | int = subprocess.DE
adapt_feedercan_deployment_file()
adapt_mockservice_deployment_file()
adapt_databroker_deployment_file()
log_output.write("Starting Kanto runtime\n")
log_output.write("Starting Kanto runtime\n") # type: ignore
kanto = subprocess.Popen(
[
"sudo",
Expand Down Expand Up @@ -252,7 +252,7 @@ def stop_kanto(log_output: TextIOWrapper | int = subprocess.DEVNULL):
Args:
log_output (TextIOWrapper | int): Logfile to write or DEVNULL by default.
"""
log_output.write("Stopping Kanto runtime\n")
log_output.write("Stopping Kanto runtime\n") # type: ignore
subprocess.check_call(
[
"sudo",
Expand Down
3 changes: 1 addition & 2 deletions runtime_kanto/src/runtime/runtime_down.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
# SPDX-License-Identifier: Apache-2.0

from controlplane_kanto import reset_controlplane
from runtime import stop_kanto, undeploy_runtime
from velocitas_lib import create_log_file
from yaspin import yaspin

from runtime_kanto import stop_kanto, undeploy_runtime


def runtime_down():
"""Stop the Kanto runtime."""
Expand Down
3 changes: 1 addition & 2 deletions runtime_kanto/src/runtime/runtime_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
import signal

from controlplane_kanto import configure_controlplane
from runtime import is_kanto_running, start_kanto
from runtime_down import runtime_down
from velocitas_lib import create_log_file
from yaspin import yaspin

from runtime_kanto import is_kanto_running, start_kanto


def runtime_up():
"""Start up the K3D runtime."""
Expand Down
2 changes: 2 additions & 0 deletions runtime_kanto/test/integration/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def run_command_until_logs_match(
)
timer: Timer = Timer(timeout_sec, proc.kill)
timer.start()
if not proc.stdout:
return False
for line in iter(proc.stdout.readline, b""):
if proc.poll() is not None:
print(f"Timeout reached after {timeout_sec} seconds, process killed!")
Expand Down
3 changes: 2 additions & 1 deletion runtime_local/src/local_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def spawn_process(
time.sleep(0.1)
continue
patterns[:] = filterfalse(
lambda pattern: pattern.search(line), patterns # noqa: B023
lambda pattern: pattern.search(line),
patterns,
)
if len(patterns) == 0:
timer.cancel()
Expand Down
4 changes: 3 additions & 1 deletion runtime_local/src/run_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def terminate_spawned_processes():
(service_id, process) = spawned_processes.popitem()
process.terminate()
stop_container(service_id, subprocess.DEVNULL)
spinner.write(f"> {process.args[0]} (service-id='{service_id}') terminated")
spinner.write(
f"> {[process.args][0]!r} (service-id='{service_id}') terminated"
)
spinner.ok("✅")


Expand Down
Loading
Loading