Skip to content

Commit

Permalink
refactor(linter): Improve linter configuration and add more tools (os…
Browse files Browse the repository at this point in the history
…l-incubator#50)

* refactor(linter): Improve linter configuration and add more tools
* fix poetry dev dep
* fix issues with new containers sugar
  • Loading branch information
xmnlab authored Jun 13, 2023
1 parent 161442d commit 7935f45
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .makim.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0
version: 1.0
groups:
default:
targets:
Expand Down
75 changes: 46 additions & 29 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
default_stages:
- commit

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
Expand All @@ -6,56 +9,70 @@ repos:

- repo: local
hooks:
- entry: blue
id: blue
- id: blue
name: blue
entry: blue
exclude: |
(?x)(
docs
)
files: ""
language: system
pass_filenames: true
stages:
- commit
types:
- python
- file
- python

- entry: flake8
exclude: ^$
files: ""
id: flake8
language: python
name: flake8
- id: ruff
name: ruff
entry: ruff check
language: system
exclude: "docs/"
pass_filenames: true
stages:
- commit
types:
- python

- entry: isort
exclude: "^.*/js/.*$"
files: ""
id: isort
language: python
- id: isort
name: isort
entry: isort
language: system
pass_filenames: true
types:
- python

- id: mypy
name: mypy
entry: mypy
language: system
files: "makim/"
pass_filenames: true
types:
- python


- id: bandit
name: bandit
entry: bandit
language: system
args: ['--configfile', 'pyproject.toml']
pass_filenames: true
stages:
- commit
types:
- python

- entry: mypy
- id: vulture
name: vulture
entry: vulture
language: system
files: "makim/"
description: Find unused Python code.
pass_filenames: true
types:
- python

- id: pydocstyle
name: pydocstyle
entry: pydocstyle
exclude: ^$
# note: it would be good to extend this to other apps as well
files: "makim/"
id: mypy
language: python
name: mypy
language: system
pass_filenames: true
stages:
- commit
types:
- python
1 change: 1 addition & 0 deletions makim/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Makim app to be called from `python -m`."""
from makim.cli import app

if __name__ == '__main__':
Expand Down
7 changes: 7 additions & 0 deletions makim/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Cli functions to define the arguments and to call Makim."""
import argparse
import os
import sys
Expand All @@ -21,6 +22,7 @@ def __init__(
width=None,
**kwargs,
):
"""Define the parameters for the argparse help text."""
super().__init__(
prog,
indent_increment=indent_increment,
Expand All @@ -35,6 +37,8 @@ def __init__(

def _get_args():
"""
Define the arguments for the CLI.
note: when added new flags, update the list of flags to be
skipped at extract_makim_args function.
"""
Expand Down Expand Up @@ -123,10 +127,12 @@ def _get_args():


def show_version():
"""Show version."""
print(__version__)


def extract_makim_args():
"""Extract makim arguments from the CLI call."""
makim_args = {}
index_to_remove = []
for ind, arg in enumerate(list(sys.argv)):
Expand Down Expand Up @@ -171,6 +177,7 @@ def extract_makim_args():


def app():
"""Call the makim program with the arguments defined by the user."""
makim_args = extract_makim_args()
args_parser = _get_args()
args = args_parser.parse_args()
Expand Down
3 changes: 3 additions & 0 deletions makim/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Classes and function for handling logs."""
from enum import Enum


class MakimError(Enum):
"""Type error class."""

SH_ERROR_RETURN_CODE = 1
SH_KEYBOARD_INTERRUPT = 2
MAKIM_CONFIG_FILE_NOT_FOUND = 3
Expand Down
18 changes: 16 additions & 2 deletions makim/makim.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Makim main class.
`MakIm` or just `makim` is based on `make` and focus on improve
the way to define targets and dependencies. Instead of using the
`Makefile` format, it uses `yaml` format.
Expand All @@ -15,22 +17,26 @@

import dotenv
import sh
import yaml
import yaml # type: ignore
from colorama import Fore
from jinja2 import Template

from makim.errors import MakimError


def escape_template_tag(v: str) -> str:
"""Escape template tag when processing the template config file."""
return v.replace('{{', r'\{\{').replace('}}', r'\}\}')


def unescape_template_tag(v: str) -> str:
"""Unescape template tag when processing the template config file."""
return v.replace(r'\{\{', '{{').replace(r'\}\}', '}}')


class PrintPlugin:
"""Logs class."""

def _print_error(self, message: str):
print(Fore.RED, message, Fore.RESET, file=sys.stderr)

Expand All @@ -42,6 +48,8 @@ def _print_warning(self, message: str):


class Makim(PrintPlugin):
"""Makim main class."""

makim_file: str = '.makim.yaml'
global_data: dict = {}
shell_app: sh.Command = sh.xonsh
Expand All @@ -56,6 +64,7 @@ class Makim(PrintPlugin):
target_data: dict = {}

def __init__(self):
"""Prepare the Makim class with the default configuration."""
os.environ['RAISE_SUBPROC_ERROR'] = '1'
os.environ['XONSH_SHOW_TRACEBACK'] = '0'

Expand Down Expand Up @@ -96,7 +105,9 @@ def _check_makim_file(self):
return Path(self.makim_file).exists()

def _verify_target_conditional(self, conditional):
...
# todo: implement verification
print(f'condition {conditional} not verified')
return False

def _verify_args(self):
if not self._check_makim_file():
Expand Down Expand Up @@ -272,6 +283,7 @@ def _load_target_args(self):

@property
def shell_args(self):
"""Return the arguments for the defined shell app."""
if self.shell_app.__dict__['__name__'].endswith('bash'):
return ['-e']
return []
Expand Down Expand Up @@ -421,13 +433,15 @@ def _run_command(self, args: dict):
# public methods

def load(self, makim_file: str):
"""Load makim configuration."""
self.makim_file = makim_file
self._load_config_data()
self._verify_config()
self._load_shell_app()
self.env = self._load_dotenv(self.global_data)

def run(self, args: dict):
"""Run makim target code."""
self.args = args

# setup
Expand Down
38 changes: 30 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,20 @@ exclude = [
[tool.poetry.dependencies]
python = "^3.8.1"
sh = "^2.0.0"
pyyaml = "<6.0"
pyyaml = ">=5.0"
jinja2 = "<3.0.3"
xonsh = "^0.14.0"
python-dotenv = "^0.21.1"
colorama = "^0.4.6"
urllib3 = "<2"

[tool.poetry.dev-dependencies]
containers-sugar = "1.5.0"
[tool.poetry.group.dev.dependencies]
containers-sugar = "1.7.0"
pytest = "^7"
blue = "^0.9.1"
types-pyyaml = "^6.0.12.3"
isort = "^5"
pre-commit = "^2"
flake8 = "^4"
mypy = "^0.931"
pre-commit = "^3"
mypy = "^1"
pytest-cov = "^3.0.0"
mkdocs = ">=1.2.3,<2"
mkdocs-exclude = ">=1.0.2,<2"
Expand All @@ -66,7 +64,11 @@ mkdocs-literate-nav = ">=0.4.1,<1"
mkdocs-macros-plugin = ">=0.6.3,<1"
mkdocs-material = ">=8.2.1,<9"
mkdocstrings = ">=0.17.0,<0.18.0"

ruff = "^0.0.272"
bandit = "^1.7.5"
vulture = "^2.7"
pydocstyle = "^6.3.0"
docker-compose = "^1.29.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand All @@ -89,3 +91,23 @@ skip_glob = ["docs/*", "*.egg-info"]

[tool.mypy]
ignore_missing_imports = true

[tool.ruff]
line-length = 79
exclude = [
"docs",
]

[tool.bandit]
exclude_dirs = ["tests"]
targets = "makim/"

[tool.vulture]
exclude = ["tests"]
ignore_decorators = []
ignore_names = []
make_whitelist = true
min_confidence = 80
paths = ["makim/"]
sort_by_size = true
verbose = false
8 changes: 5 additions & 3 deletions tests/.containers-sugar.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
version: 1.0.0
version: 1.0
compose-app: docker-compose
defaults:
group: group1
service-groups:
- name: default
- name: group1
project-name: makim-tests
compose-path: containers/compose.yaml
env-file: .env
services:
default: service1
list:
available:
- name: service1
2 changes: 1 addition & 1 deletion tests/.makim-bash-group-scope.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0
version: 1.0
env-file: .env
groups:
group-scope:
Expand Down
2 changes: 1 addition & 1 deletion tests/.makim-bash-main-scope.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0
version: 1.0
env-file: .env
shell: bash
groups:
Expand Down
2 changes: 1 addition & 1 deletion tests/.makim-bash-target-scope.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0
version: 1.0
env-file: .env
groups:
target-scope:
Expand Down
13 changes: 7 additions & 6 deletions tests/.makim-complex.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0
version: 1.0
env-file: .env
groups:
default:
Expand Down Expand Up @@ -194,14 +194,15 @@ groups:
setup:
help: build necessary docker image
run: |
containers-sugar pull --group default --all
containers-sugar build --group default --all
containers-sugar restart --group default --all --options -d
pwd
kxgr pull --all
kxgr build --all
kxgr ext restart --all --options -d
run:
help: test running a docker service using containers-sugar
dependencies:
- target: containers.setup
shell: bash
run: |
containers-sugar exec --group default --service service1 --options -T --cmd python -c "print(1)"
containers-sugar stop --all
kxgr exec --service service1 --options -T --cmd python -c "print(1)"
kxgr stop --all
2 changes: 1 addition & 1 deletion tests/.makim-env.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0
version: 1.0
env-file: .env
env:
GLOBAL_VAR_1: 1
Expand Down
2 changes: 1 addition & 1 deletion tests/.makim-simple.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0
version: 1.0
env-file: .env
groups:
default:
Expand Down
Loading

0 comments on commit 7935f45

Please sign in to comment.