Skip to content

Commit

Permalink
Merge pull request #43 from elcaminoreal/use-cd-run
Browse files Browse the repository at this point in the history
use commander data's run abstractions
  • Loading branch information
moshez committed Jan 17, 2024
2 parents fac2047 + 479031a commit d5fe549
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 107 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -10,7 +10,7 @@ dynamic = ["version"]
description = "A gatherer"
readme = "README.rst"
authors = [{name = "Moshe Zadka", email = "moshez@zadka.club"}]
dependencies = ["attrs", "incremental", "venusian", "toolz"]
dependencies = ["attrs", "incremental", "venusian", "toolz", "commander_data"]
requires-python = ">=3.11"

[project.optional-dependencies]
Expand Down
32 changes: 14 additions & 18 deletions requirements-docs.txt
@@ -1,27 +1,29 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --extra=docs --output-file=requirements-docs.txt pyproject.toml
# pip-compile --output-file=- -
#
alabaster==0.7.16
# via sphinx
attrs==23.2.0
# via gather (pyproject.toml)
# via -r -
babel==2.14.0
# via sphinx
certifi==2023.11.17
# via requests
charset-normalizer==3.3.2
# via requests
commander-data==2024.1.17.82975
# via -r -
docutils==0.20.1
# via sphinx
idna==3.6
# via requests
imagesize==1.4.1
# via sphinx
incremental==22.10.0
# via gather (pyproject.toml)
# via -r -
jinja2==3.1.3
# via sphinx
markupsafe==2.1.3
Expand All @@ -35,28 +37,22 @@ requests==2.31.0
snowballstemmer==2.2.0
# via sphinx
sphinx==7.2.6
# via
# gather (pyproject.toml)
# sphinxcontrib-applehelp
# sphinxcontrib-devhelp
# sphinxcontrib-htmlhelp
# sphinxcontrib-qthelp
# sphinxcontrib-serializinghtml
sphinxcontrib-applehelp==1.0.7
# via -r -
sphinxcontrib-applehelp==1.0.8
# via sphinx
sphinxcontrib-devhelp==1.0.5
sphinxcontrib-devhelp==1.0.6
# via sphinx
sphinxcontrib-htmlhelp==2.0.4
sphinxcontrib-htmlhelp==2.0.5
# via sphinx
sphinxcontrib-jsmath==1.0.1
# via sphinx
sphinxcontrib-qthelp==1.0.6
sphinxcontrib-qthelp==1.0.7
# via sphinx
sphinxcontrib-serializinghtml==1.1.9
sphinxcontrib-serializinghtml==1.1.10
# via sphinx
toolz==0.12.0
# via gather (pyproject.toml)
# via -r -
urllib3==2.1.0
# via requests
venusian==3.1.0
# via gather (pyproject.toml)
# via -r -
20 changes: 11 additions & 9 deletions requirements-lint.txt
@@ -1,23 +1,25 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --extra=lint --output-file=requirements-lint.txt pyproject.toml
# pip-compile --output-file=- -
#
astroid==3.0.2
# via pylint
attrs==23.2.0
# via gather (pyproject.toml)
# via -r -
black==23.12.1
# via gather (pyproject.toml)
# via -r -
click==8.1.7
# via black
commander-data==2024.1.17.82975
# via -r -
dill==0.3.7
# via pylint
flake8==7.0.0
# via gather (pyproject.toml)
# via -r -
incremental==22.10.0
# via gather (pyproject.toml)
# via -r -
isort==5.13.2
# via pylint
mccabe==0.7.0
Expand All @@ -39,10 +41,10 @@ pycodestyle==2.11.1
pyflakes==3.2.0
# via flake8
pylint==3.0.3
# via gather (pyproject.toml)
# via -r -
tomlkit==0.12.3
# via pylint
toolz==0.12.0
# via gather (pyproject.toml)
# via -r -
venusian==3.1.0
# via gather (pyproject.toml)
# via -r -
16 changes: 9 additions & 7 deletions requirements-mypy.txt
@@ -1,20 +1,22 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --extra=mypy --output-file=requirements-mypy.txt pyproject.toml
# pip-compile --output-file=- -
#
attrs==23.2.0
# via gather (pyproject.toml)
# via -r -
commander-data==2024.1.17.82975
# via -r -
incremental==22.10.0
# via gather (pyproject.toml)
# via -r -
mypy==1.8.0
# via gather (pyproject.toml)
# via -r -
mypy-extensions==1.0.0
# via mypy
toolz==0.12.0
# via gather (pyproject.toml)
# via -r -
typing-extensions==4.9.0
# via mypy
venusian==3.1.0
# via gather (pyproject.toml)
# via -r -
20 changes: 11 additions & 9 deletions requirements-tests.txt
@@ -1,13 +1,13 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --extra=tests --output-file=requirements-tests.txt pyproject.toml
# pip-compile --output-file=- -
#
attrs==23.2.0
# via
# -r -
# automat
# gather (pyproject.toml)
# twisted
# virtue
automat==22.10.0
Expand All @@ -16,34 +16,36 @@ click==8.1.7
# via virtue
colorama==0.4.6
# via virtue
commander-data==2024.1.17.82975
# via -r -
constantly==23.10.4
# via twisted
coverage[toml]==7.4.0
# via gather (pyproject.toml)
# via -r -
hyperlink==21.0.0
# via twisted
idna==3.6
# via hyperlink
incremental==22.10.0
# via
# gather (pyproject.toml)
# -r -
# twisted
pyhamcrest==2.1.0
# via gather (pyproject.toml)
# via -r -
pyrsistent==0.20.0
# via virtue
six==1.16.0
# via automat
toolz==0.12.0
# via gather (pyproject.toml)
# via -r -
twisted==23.10.0
# via virtue
typing-extensions==4.9.0
# via twisted
venusian==3.1.0
# via gather (pyproject.toml)
# via -r -
virtue==2023.8.2
# via gather (pyproject.toml)
# via -r -
zope-interface==6.1
# via twisted

Expand Down
16 changes: 16 additions & 0 deletions requirements.txt
@@ -0,0 +1,16 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --output-file=- -
#
attrs==23.2.0
# via -r -
commander-data==2024.1.17.82975
# via -r -
incremental==22.10.0
# via -r -
toolz==0.12.0
# via -r -
venusian==3.1.0
# via -r -
43 changes: 6 additions & 37 deletions src/gather/commands.py
Expand Up @@ -2,14 +2,14 @@

from __future__ import annotations
import argparse
import functools
import logging
import os
import subprocess
import sys
import subprocess
from typing import Sequence, Any, Tuple

import attrs
from commander_data.run import Runner

from .api import Wrapper, unique

Expand Down Expand Up @@ -89,33 +89,6 @@ def set_parser(*, collected, parser=None):
return parser


def _make_safe_run(args):
no_dry_run = getattr(args, "no_dry_run", False)
orig_run = args.orig_run

@functools.wraps(orig_run)
def wrapped_run(cmdargs, **kwargs):
real_kwargs = dict(text=True, check=True, capture_output=True)
real_kwargs.update(kwargs)
LOGGER.info("Running: %s", cmdargs)
try:
return orig_run(cmdargs, **real_kwargs)
except subprocess.CalledProcessError as exc:
exc.add_note(f"STDERR: {exc.stderr}")
exc.add_note(f"STDOUT: {exc.stdout}")
raise

@functools.wraps(orig_run)
def wrapped_dry_run(cmdargs, **kwargs):
LOGGER.info("Running: %s", cmdargs)
LOGGER.info("Dry run, skipping")

unsafe_run = wrapped_run if no_dry_run else wrapped_dry_run
args.run = unsafe_run
args.safe_run = wrapped_run
args.orig_run = orig_run


def run_maybe_dry(
*,
parser,
Expand Down Expand Up @@ -150,15 +123,11 @@ def error(args):

args = parser.parse_args(argv[1:])
args.orig_run = sp_run
a_runner = Runner.from_args(args)
args.run = a_runner.run
args.safe_run = a_runner.safe_run
args.env = env
_make_safe_run(args)
try:
command = args.__gather_command__
except AttributeError:
command = error
return command(
args=args,
)
getattr(args, "__gather_command__", error)(args)


def run(*, parser, argv=sys.argv, env=os.environ, sp_run=subprocess.run):
Expand Down
7 changes: 7 additions & 0 deletions src/gather/example/__init__.py
@@ -0,0 +1,7 @@
"""
Example commands and plugins
"""

from gather import entry

ENTRY_DATA = entry.EntryData.create(__name__)
13 changes: 6 additions & 7 deletions src/gather/example/__main__.py
@@ -1,9 +1,8 @@
"""Run the example commands"""
from gather import entry
from . import ENTRY_DATA

if __name__ != "__main__":
raise ImportError("only run")

from . import main
from gather.commands import run

run(parser=main.get_parser())
entry.dunder_main(
globals_dct=globals(),
command_data=ENTRY_DATA,
)
7 changes: 5 additions & 2 deletions src/gather/example/breakfast.py
Expand Up @@ -3,12 +3,15 @@

import gather

from . import ENTRY_DATA

BREAKFAST = gather.Collector()


def breakfast(_args):
@ENTRY_DATA.register()
def breakfast(args):
"""Collect breakfast plugins, make breakfast"""
foods = [klass() for klass in BREAKFAST.collect().values()]
foods = [klass() for klass in gather.unique(BREAKFAST.collect()).values()]
for food in foods:
food.prepare()
for food in foods:
Expand Down
28 changes: 11 additions & 17 deletions src/gather/example/main.py
@@ -1,33 +1,27 @@
"""Example commands"""
import sys
import gather
from gather import commands
from gather.commands import add_argument

_COMMANDS_COLLECTOR = gather.Collector()
REGISTER = commands.make_command_register(_COMMANDS_COLLECTOR)
from commander_data.common import LOCAL_PYTHON as PYTHON

from gather.commands import add_argument

def get_parser():
"""Get parser dispatching to example commands"""
return commands.set_parser(collected=_COMMANDS_COLLECTOR.collect())
from . import ENTRY_DATA


@REGISTER(
@ENTRY_DATA.register(
add_argument("--value"),
name="do-something",
)
def _do_something(*, args, env, run):
def _do_something(args):
print(args.value)
print(env["SHELL"])
run([sys.executable, "-c", "print(1+1)"], check=True)
print(args.env["SHELL"])
args.safe_run(PYTHON(c="print(1+1)"), capture_output=False)


@REGISTER(
@ENTRY_DATA.register(
add_argument("--no-dry-run", action="store_true"),
name="do-something-else",
)
def _do_something_else(*, args, env, run):
def _do_something_else(args):
print(args.no_dry_run)
print(env["SHELL"])
run([sys.executable, "-c", "print(1+1)"], check=True)
print(args.env["SHELL"])
args.safe_run(PYTHON(c="print(1+1)"), capture_output=False)

0 comments on commit d5fe549

Please sign in to comment.