From 20534264c93ca791f9a5797e6d2f62b13a54d5a5 Mon Sep 17 00:00:00 2001 From: Romain Dorgueil Date: Wed, 25 Nov 2020 08:38:48 +0100 Subject: [PATCH] Adds ability to insert steps in a pipeline, add precommit parsing in our own release pipeline. --- .pre-commit-config.yaml | 8 -------- Makefile | 16 ++++++++-------- Projectfile | 6 +++++- medikit/_version.py | 2 +- medikit/commands/pipeline.py | 18 +++++++++++++++--- medikit/pipeline.py | 34 ++++++++++++++++++++++++++-------- setup.py | 2 +- 7 files changed, 56 insertions(+), 30 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 25c4b68..d06d09d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,5 @@ repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files - - repo: https://github.com/psf/black rev: 20.8b1 hooks: diff --git a/Makefile b/Makefile index d450f7e..37825e3 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# Generated by Medikit 0.7.5 on 2020-11-25. +# Generated by Medikit 0.8.0 on 2020-11-25. # All changes will be overriden. # Edit Projectfile and run “make update” (or “medikit update”) to regenerate. @@ -8,12 +8,12 @@ PYTHON ?= $(shell which python || echo python) PYTHON_BASENAME ?= $(shell basename $(PYTHON)) PYTHON_DIRNAME ?= $(shell dirname $(PYTHON)) PYTHON_REQUIREMENTS_FILE ?= requirements.txt -PYTHON_REQUIREMENTS_INLINE ?= +PYTHON_REQUIREMENTS_INLINE ?= PYTHON_REQUIREMENTS_DEV_FILE ?= requirements-dev.txt -PYTHON_REQUIREMENTS_DEV_INLINE ?= -QUICK ?= +PYTHON_REQUIREMENTS_DEV_INLINE ?= +QUICK ?= PIP ?= $(PYTHON) -m pip -PIP_INSTALL_OPTIONS ?= +PIP_INSTALL_OPTIONS ?= VERSION ?= $(shell git describe 2>/dev/null || git rev-parse --short HEAD) BLACK ?= $(shell which black || echo black) BLACK_OPTIONS ?= --line-length 120 @@ -22,13 +22,13 @@ ISORT_OPTIONS ?= --recursive --apply PYTEST ?= $(PYTHON_DIRNAME)/pytest PYTEST_OPTIONS ?= --capture=no --cov=$(PACKAGE) --cov-report html SPHINX_BUILD ?= $(PYTHON_DIRNAME)/sphinx-build -SPHINX_OPTIONS ?= +SPHINX_OPTIONS ?= SPHINX_SOURCEDIR ?= docs SPHINX_BUILDDIR ?= $(SPHINX_SOURCEDIR)/_build SPHINX_AUTOBUILD ?= $(PYTHON_DIRNAME)/sphinx-autobuild MEDIKIT ?= $(PYTHON) -m medikit -MEDIKIT_UPDATE_OPTIONS ?= -MEDIKIT_VERSION ?= 0.7.5 +MEDIKIT_UPDATE_OPTIONS ?= +MEDIKIT_VERSION ?= 0.8.0 .PHONY: $(SPHINX_SOURCEDIR) clean format help install install-dev medikit quick release test update update-requirements watch-$(SPHINX_SOURCEDIR) diff --git a/Projectfile b/Projectfile index 8628013..c65f525 100644 --- a/Projectfile +++ b/Projectfile @@ -1,6 +1,7 @@ # medikit (see github.com/python-edgy/medikit) -from medikit import listen, require +from medikit.steps.exec import System +from medikit import listen, require, pipeline sphinx = require("sphinx") @@ -89,4 +90,7 @@ with require("make") as make: ) +with pipeline("release") as release: + release.add(System('pre-commit run'), before="System('git add -p .', True)") + # vim: ft=python: diff --git a/medikit/_version.py b/medikit/_version.py index ab55bb1..777f190 100644 --- a/medikit/_version.py +++ b/medikit/_version.py @@ -1 +1 @@ -__version__ = "0.7.5" +__version__ = "0.8.0" diff --git a/medikit/commands/pipeline.py b/medikit/commands/pipeline.py index 86d6237..3f14247 100644 --- a/medikit/commands/pipeline.py +++ b/medikit/commands/pipeline.py @@ -6,18 +6,20 @@ from medikit.commands.base import Command from medikit.commands.utils import _read_configuration from medikit.events import LoggingDispatcher -from medikit.pipeline import ConfiguredPipeline, logger +from medikit.pipeline import ConfiguredPipeline, get_identity, logger START = "start" CONTINUE = "continue" ABORT = "abort" +SHOW = "show" COMPLETE = "complete" +QUIT = "quit" class PipelineCommand(Command): def add_arguments(self, parser): parser.add_argument("pipeline", default=None, nargs="?") - parser.add_argument("action", choices=(START, CONTINUE, ABORT), nargs="?") + parser.add_argument("action", choices=(START, CONTINUE, ABORT, SHOW), nargs="?") parser.add_argument("--force", "-f", action="store_true") @classmethod @@ -54,7 +56,9 @@ def handle(cls, config_filename, *, pipeline, action, force=False, verbose=False raise RuntimeError("Choose a pipeline action: start, continue, abort.") while action: - if action == START: + if action == SHOW: + action = cls._handle_show(pipeline, filename=pipeline_file) + elif action == START: action = cls._handle_start(pipeline, filename=pipeline_file, force=force) elif action == CONTINUE: action = cls._handle_continue(pipeline, filename=pipeline_file) @@ -68,10 +72,18 @@ def handle(cls, config_filename, *, pipeline, action, force=False, verbose=False os.rename(pipeline_file, os.path.join(path, target)) logger.info("Pipeline complete. State saved as “{}”.".format(target)) break + elif action == QUIT: + break else: raise ValueError("Invalid action “{}”.".format(action)) force = False + @classmethod + def _handle_show(cls, pipeline, *, filename): + for step in pipeline: + print(get_identity(step)) + return QUIT + @classmethod def _handle_start(cls, pipeline, *, filename, force=False): if os.path.exists(filename): diff --git a/medikit/pipeline.py b/medikit/pipeline.py index 59c9724..503ec5d 100644 --- a/medikit/pipeline.py +++ b/medikit/pipeline.py @@ -9,6 +9,10 @@ logger = logging.getLogger(__name__) +def get_identity(step): + return str(step) + + class Pipeline: """ Class to configure a pipeline. @@ -18,8 +22,20 @@ class Pipeline: def __init__(self): self.steps = [] - def add(self, step): - self.steps.append(step) + def add(self, step, *, before=None): + if before: + insert_at = None + for i, _step in enumerate(self.steps): + if before == get_identity(_step): + insert_at = i + break + if not insert_at: + raise ValueError( + 'Step with identity {!r} not found. Try "show" subcommand to list identities.'.format(before) + ) + self.steps = self.steps[:insert_at] + [step] + self.steps[insert_at:] + else: + self.steps.append(step) return self def remove(self, identity): @@ -28,9 +44,8 @@ def remove(self, identity): del self.steps[i] break - -def get_identity(step): - return str(step) + def __iter__(self): + yield from self.steps class ConfiguredPipeline: @@ -44,6 +59,12 @@ def __init__(self, name, pipeline, config=None): self.meta = {"created": str(datetime.datetime.now())} self.config = config + def __iter__(self): + yield from self.steps + + def __len__(self): + return len(self.steps) + def init(self): for step in self.steps: step.init() @@ -61,9 +82,6 @@ def current(self): return i + 1 return len(self) - def __len__(self): - return len(self.steps) - def abort(self): for step in self.steps: step.abort() diff --git a/setup.py b/setup.py index 39d8549..44e29c7 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -# Generated by Medikit 0.7.5 on 2020-11-25. +# Generated by Medikit 0.8.0 on 2020-11-25. # All changes will be overriden. # Edit Projectfile and run “make update” (or “medikit update”) to regenerate.