Mercurial > piecrust2
comparison piecrust/commands/builtin/tasks.py @ 1114:8af2ea1f5c34
tasks: Add new `tasks` command and infrastructure, with `mention` task.
* The new command lets `chef` run tasks from a queue.
* The webmention endpoint now adds a mention task.
* Moved mention handling code to a task runner.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Thu, 22 Feb 2018 22:12:45 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1113:29c51b981c17 | 1114:8af2ea1f5c34 |
|---|---|
| 1 import os.path | |
| 2 import logging | |
| 3 from piecrust.commands.base import ChefCommand | |
| 4 | |
| 5 | |
| 6 logger = logging.getLogger(__name__) | |
| 7 | |
| 8 | |
| 9 class TasksCommand(ChefCommand): | |
| 10 """ Command for managing and running task queues. | |
| 11 """ | |
| 12 def __init__(self): | |
| 13 super().__init__() | |
| 14 self.name = 'tasks' | |
| 15 self.description = "Manages and runs various tasks." | |
| 16 | |
| 17 def setupParser(self, parser, app): | |
| 18 subparsers = parser.add_subparsers() | |
| 19 | |
| 20 p = subparsers.add_parser( | |
| 21 'list', | |
| 22 help="Show the list of tasks current in the queue.") | |
| 23 p.set_defaults(sub_func=self._listTasks) | |
| 24 | |
| 25 p = subparsers.add_parser( | |
| 26 'run', | |
| 27 help="Runs the current task queue.") | |
| 28 p.add_argument( | |
| 29 '-k', '--keep-queue', | |
| 30 action='store_true', | |
| 31 help="Don't delete the task queue files.") | |
| 32 p.add_argument( | |
| 33 '-t', '--task', | |
| 34 help="Specify which task to run.") | |
| 35 p.set_defaults(sub_func=self._runTasks) | |
| 36 | |
| 37 def run(self, ctx): | |
| 38 if hasattr(ctx.args, 'sub_func'): | |
| 39 ctx.args.sub_func(ctx) | |
| 40 | |
| 41 def _listTasks(self, ctx): | |
| 42 from piecrust.tasks.base import TaskManager | |
| 43 | |
| 44 root_dir = ctx.app.root_dir | |
| 45 tm = TaskManager(ctx.app) | |
| 46 tm.getTasks() | |
| 47 tasks = list(tm.getTasks()) | |
| 48 logger.info("Task queue contains %d tasks" % len(tasks)) | |
| 49 for path, task_type, task_data in tasks: | |
| 50 logger.info(" - [%s] %s" % | |
| 51 (task_type, os.path.relpath(path, root_dir))) | |
| 52 | |
| 53 def _runTasks(self, ctx): | |
| 54 from piecrust.tasks.base import TaskManager | |
| 55 | |
| 56 only_task = ctx.args.task | |
| 57 if only_task and os.path.isfile(only_task): | |
| 58 only_task, _ = os.path.splitext(os.path.basename(only_task)) | |
| 59 | |
| 60 tm = TaskManager(ctx.app) | |
| 61 tm.runQueue( | |
| 62 only_task=only_task, | |
| 63 clear_queue=False) # (not ctx.args.keep_queue)) | |
| 64 |
