Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.bumpversion.cfg
#	docker_printer/__init__.py
#	docs/conf.py
#	pyproject.toml
#	setup.cfg
  • Loading branch information
scnerd committed Jul 19, 2023
2 parents 91de00e + ffaf1ac commit 44e9801
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.2.3
current_version = 0.3.0
commit = True
tag = True

Expand Down
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ repos:
- id: check-yaml
args: ['--unsafe'] # Temporary workaround while we still have cloudformation files
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
additional_dependencies: ['click!=8.1.0']

- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.254'
hooks:
- id: ruff
args: ["--fix"]
49 changes: 49 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Enable Pyflakes `E` and `F` codes by default.
select = ["E", "F", "I", "UP", "RUF100"]
ignore = ["E501", "F722"]

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["A", "C", "D", "E", "F", "I", "UP"]
unfixable = []

# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"*/migrations/*",
"*/static/CACHE/*",
"docs",
"node_modules"
]

per-file-ignores = {}

# Same as Black.
line-length = 120

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# Assume Python 3.7.
target-version = "py37"

[mccabe]
# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10
4 changes: 3 additions & 1 deletion docker_printer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__version__ = "0.2.3"
"""Composer for dockerfiles"""

__version__ = "0.3.0"
4 changes: 2 additions & 2 deletions docker_printer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def main():
)
)

from docker_printer.cli import cli
from docker_printer.cli import app

cli()
app()


if __name__ == "__main__":
Expand Down
58 changes: 37 additions & 21 deletions docker_printer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,39 @@
import textwrap
from pathlib import Path

import click
import typer

from . import __version__
from .models import TargetCollection, BuildConfigCollection
from .models import BuildConfigCollection, TargetCollection
from .utils import (
base_dir,
builds_file,
jinja_env,
yml_load,
preload_modules,
targets_file,
builds_file,
base_dir,
yml_load,
)

app = typer.Typer()


def version_callback(value: bool):
if value:
typer.echo(__version__)
raise typer.Exit()

@click.group()
@click.version_option(__version__, message="%(version)s")
def cli():
pass

@app.callback()
def main(
version: bool = typer.Option(
None, "--version", callback=version_callback, is_eager=True
),
):
# Do other global stuff, handle other global options here
return

@cli.command()

@app.command()
def synth():
_synth()

Expand All @@ -36,7 +48,7 @@ def _synth():
dockerfile = targets.render_dockerfile(jinja_env())
dockerfile_path = base_dir() / "Dockerfile.synth"

click.echo(f"Saving to {dockerfile_path}")
typer.echo(f"Saving to {dockerfile_path}")
with open(dockerfile_path, "w", newline="\n") as f:
f.write(dockerfile)

Expand All @@ -45,31 +57,35 @@ def _synth():
bakefile = build_config.generate_bakefile(targets)
with open(bakefile_path, "w", newline="\n") as f:
f.write(bakefile + "\n")
click.echo(build_config.build_command)
typer.echo(build_config.build_command)

return targets, build_configs


@cli.command()
@click.argument("name")
def build(name):
@app.command()
def build(name: str):
_, build_configs = _synth()

try:
config = next(cfg for cfg in build_configs.__root__ if cfg.name == name)
except StopIteration:
raise click.Abort(f"No build config found with name '{name}'")
raise typer.Abort(f"No build config found with name '{name}'")

subprocess.run(config.build_command)


@cli.command()
@click.argument("path", default=Path(), type=click.Path(exists=True, dir_okay=True))
def init(path):
@app.command()
def init(
path: Path = typer.Argument(
...,
exists=True,
dir_okay=True,
)
):
base_dir = path / "docker-printer"
if base_dir.exists():
click.echo(f"{base_dir} already exists, cannot initialize new project")
raise click.Abort()
typer.echo(f"{base_dir} already exists, cannot initialize new project")
raise typer.Abort()

base_dir.mkdir(exist_ok=False, parents=False)
(base_dir / "modules").mkdir(exist_ok=False, parents=False)
Expand Down
28 changes: 16 additions & 12 deletions docker_printer/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json
import re
from collections import defaultdict
from typing import List, Optional, Set, Dict, Any, Hashable, Union, Iterable
from typing import Any, Dict, Hashable, Iterable, List, Optional, Set, Union

import jinja2
from pydantic import BaseModel, validator, PrivateAttr
from pydantic import BaseModel, PrivateAttr, validator
from rich import print
from rich.tree import Tree


class CommonListTree:
Expand All @@ -30,14 +32,14 @@ def terminal_labels(self):
lbl for child in self.children.values() for lbl in child.labels
}

def print_tree(self, _indent=0):
def tree(self, tree=None) -> Tree:
for value, child in self.children.items():
if child.terminal_labels:
suffix = f' [{", ".join(child.terminal_labels)}]'
else:
suffix = ""
print(" " * _indent + f"- {value}" + suffix)
child.print_tree(_indent + 1)
terminals = " ".join(
f"[code]{lbl}[/code]" for lbl in child.terminal_labels
)
text = f"{value} {terminals}"
subtree = tree.add(text.strip())
child.tree(subtree)

def visit(self, func):
for value, child in self.children.items():
Expand All @@ -50,8 +52,10 @@ def __init__(self):
def merge_list(self, *args, **kwargs):
self.root.merge_list(*args, **kwargs)

def print_tree(self):
self.root.print_tree()
def tree(self) -> Tree:
root = Tree("[dim]Dockerfile.synth[/dim]")
self.root.tree(root)
return root

def visit(self, func):
self.root.visit(func)
Expand Down Expand Up @@ -222,7 +226,7 @@ def render_dockerfile(
for target in targets:
module_tree.merge_list(target.all_modules(), target.name)

module_tree.print_tree()
print(module_tree.tree())
chunks: Dict[Union[CommonListTree.Node, str], str] = {}
names = {}
image_args = {}
Expand Down
3 changes: 1 addition & 2 deletions docker_printer/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import getpass
import platform
from functools import lru_cache
from importlib import resources
from pathlib import Path
from tempfile import TemporaryDirectory

import jinja2
from importlib import resources
import yaml
from yaml.scanner import ScannerError

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
project = "docker-printer"
copyright = "2022, David Maxson"
author = "David Maxson"
release = "0.2.3"
release = "0.3.0"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
11 changes: 3 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
[build-system]
requires = [
"setuptools>=61.2",
"build",
"wheel"
]
build-backend = "setuptools.build_meta"
requires = ["flit_core >=3.2,<4"]
build-backend = "flit_core.buildapi"

[project]
version = "0.2.2"
name = "docker-printer"
description = "Composer for dockerfiles"
dynamic = ["version", "description"]
authors = [
{ name = "Rearc Data", email = "[email protected]" },
]
Expand Down

0 comments on commit 44e9801

Please sign in to comment.