Skip to content

Commit

Permalink
Adds ability to insert steps in a pipeline, add precommit parsing in …
Browse files Browse the repository at this point in the history
…our own release pipeline.
  • Loading branch information
hartym committed Nov 25, 2020
1 parent c931569 commit 2053426
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 30 deletions.
8 changes: 0 additions & 8 deletions .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:
Expand Down
16 changes: 8 additions & 8 deletions 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.

Expand All @@ -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
Expand All @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion 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")

Expand Down Expand Up @@ -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:
2 changes: 1 addition & 1 deletion medikit/_version.py
@@ -1 +1 @@
__version__ = "0.7.5"
__version__ = "0.8.0"
18 changes: 15 additions & 3 deletions medikit/commands/pipeline.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
34 changes: 26 additions & 8 deletions medikit/pipeline.py
Expand Up @@ -9,6 +9,10 @@
logger = logging.getLogger(__name__)


def get_identity(step):
return str(step)


class Pipeline:
"""
Class to configure a pipeline.
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion 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.

Expand Down

0 comments on commit 2053426

Please sign in to comment.