1
0
Fork 0
mirror of synced 2024-12-04 13:25:34 -05:00

Compare commits

...

6 commits

Author SHA1 Message Date
Mike Hennessy
b8d3cd993b
Merge 202b8b3c70 into 8d94c6ec1a 2024-11-28 23:53:16 +00:00
Anish Athalye
8d94c6ec1a Disable coverage status checks 2024-11-21 17:23:21 +05:30
Anish Athalye
2294ac78f8 Update actions 2024-11-21 17:13:19 +05:30
Mike Hennessy
202b8b3c70
fix: remove duplicate import 2023-02-24 21:58:36 -05:00
Mike Hennessy
442b8483db
Merge remote-tracking branch 'upstream/master' into add-plugin-config-loading 2023-02-24 21:49:57 -05:00
Mike Hennessy
6dd426d71d
feat: add the ability to load plugins in config 2023-02-24 21:39:47 -05:00
9 changed files with 120 additions and 19 deletions

View file

@ -47,11 +47,14 @@ jobs:
run: | run: |
python -m tox python -m tox
python -m tox -e coverage_report python -m tox -e coverage_report
- uses: codecov/codecov-action@v3 - uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
fmt: fmt:
name: Format name: Format
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- uses: psf/black@stable - uses: psf/black@stable
- uses: isort/isort-action@v1 - uses: isort/isort-action@v1

View file

@ -1,5 +1,4 @@
Contributing # Contributing
============
All kinds of contributions to Dotbot are greatly appreciated. For someone All kinds of contributions to Dotbot are greatly appreciated. For someone
unfamiliar with the code base, the most efficient way to contribute is usually unfamiliar with the code base, the most efficient way to contribute is usually
@ -7,21 +6,19 @@ to submit a [feature request](#feature-requests) or [bug report](#bug-reports).
If you want to dive into the source code, you can submit a [patch](#patches) as If you want to dive into the source code, you can submit a [patch](#patches) as
well, either working on your own ideas or [existing issues][issues]. well, either working on your own ideas or [existing issues][issues].
Feature Requests ## Feature Requests
----------------
Do you have an idea for an awesome new feature for Dotbot? Please [submit a Do you have an idea for an awesome new feature for Dotbot? Please [submit a
feature request][issue]. It's great to hear about new ideas. feature request][issue]. It's great to hear about new ideas.
If you are inclined to do so, you're welcome to [fork][fork] Dotbot, work on If you are inclined to do so, you're welcome to [fork][fork] Dotbot, work on
implementing the feature yourself, and submit a patch. In this case, it's implementing the feature yourself, and submit a patch. In this case, it's
*highly recommended* that you first [open an issue][issue] describing your _highly recommended_ that you first [open an issue][issue] describing your
enhancement to get early feedback on the new feature that you are implementing. enhancement to get early feedback on the new feature that you are implementing.
This will help avoid wasted efforts and ensure that your work is incorporated This will help avoid wasted efforts and ensure that your work is incorporated
into the code base. into the code base.
Bug Reports ## Bug Reports
-----------
Did something go wrong with Dotbot? Sorry about that! Bug reports are greatly Did something go wrong with Dotbot? Sorry about that! Bug reports are greatly
appreciated! appreciated!
@ -31,15 +28,14 @@ as Dotbot version, operating system, configuration file, error messages, and
steps to reproduce the bug. The more details you can include, the easier it is steps to reproduce the bug. The more details you can include, the easier it is
to find and fix the bug. to find and fix the bug.
Patches ## Patches
-------
Want to hack on Dotbot? Awesome! Want to hack on Dotbot? Awesome!
If there are [open issues][issues], you're more than welcome to work on those - If there are [open issues][issues], you're more than welcome to work on those -
this is probably the best way to contribute to Dotbot. If you have your own this is probably the best way to contribute to Dotbot. If you have your own
ideas, that's great too! In that case, before working on substantial changes to ideas, that's great too! In that case, before working on substantial changes to
the code base, it is *highly recommended* that you first [open an issue][issue] the code base, it is _highly recommended_ that you first [open an issue][issue]
describing what you intend to work on. describing what you intend to work on.
**Patches are generally submitted as pull requests.** Patches are also **Patches are generally submitted as pull requests.** Patches are also
@ -82,6 +78,10 @@ do so with the following:
docker run -it --rm -v "${PWD}:/dotbot" -w /dotbot python:3.10-alpine /bin/sh docker run -it --rm -v "${PWD}:/dotbot" -w /dotbot python:3.10-alpine /bin/sh
``` ```
If the machine you are running Docker on has SELinux in the enforcing state, you
will have to disable that on the container. This can be done by adding
`--security-opt label:disable` to the above command.
After spawning the container, follow the same instructions as above (create a After spawning the container, follow the same instructions as above (create a
virtualenv, ..., run the tests). virtualenv, ..., run the tests).

View file

@ -428,11 +428,26 @@ should do something and return whether or not it completed successfully.
All built-in Dotbot directives are written as plugins that are loaded by All built-in Dotbot directives are written as plugins that are loaded by
default, so those can be used as a reference when writing custom plugins. default, so those can be used as a reference when writing custom plugins.
Plugins are loaded using the `--plugin` and `--plugin-dir` options, using See [here](plugins) for a current list of plugins.
either absolute paths or paths relative to the base directory. It is
recommended that these options are added directly to the `install` script.
See [here][plugins] for a current list of plugins. #### Format
Plugins can be loaded either by the command-line arguments `--plugin` or
`--plugin-dir` or by the `plugins` directive. Each of these take either
absolute paths or paths relative to the base directory.
When using command-line arguments to load multiple plugins you must add
one argument for each plugin to be loaded. It is recommended to place
these command-line arguments directly in the `install` script.
The `plugins` config directive is specified as an array of paths to load.
#### Example
```yaml
- plugins:
- dotbot-plugins/dotbot-template
```
## Command-line Arguments ## Command-line Arguments

4
codecov.yml Normal file
View file

@ -0,0 +1,4 @@
coverage:
status:
project: off
patch: off

View file

@ -9,7 +9,7 @@ import dotbot
from .config import ConfigReader, ReadingError from .config import ConfigReader, ReadingError
from .dispatcher import Dispatcher, DispatchError from .dispatcher import Dispatcher, DispatchError
from .messenger import Level, Messenger from .messenger import Level, Messenger
from .plugins import Clean, Create, Link, Shell from .plugins import Clean, Create, Link, Plugins, Shell
from .util import module from .util import module
@ -121,7 +121,7 @@ def main():
plugins = [] plugins = []
plugin_directories = list(options.plugin_dirs) plugin_directories = list(options.plugin_dirs)
if not options.disable_built_in_plugins: if not options.disable_built_in_plugins:
plugins.extend([Clean, Create, Link, Shell]) plugins.extend([Clean, Create, Link, Plugins, Shell])
plugin_paths = [] plugin_paths = []
for directory in plugin_directories: for directory in plugin_directories:
for plugin_path in glob.glob(os.path.join(directory, "*.py")): for plugin_path in glob.glob(os.path.join(directory, "*.py")):

View file

@ -42,11 +42,13 @@ class Dispatcher:
) and action != "defaults": ) and action != "defaults":
self._log.info("Skipping action %s" % action) self._log.info("Skipping action %s" % action)
continue continue
handled = False handled = False
if action == "defaults": if action == "defaults":
self._context.set_defaults(task[action]) # replace, not update self._context.set_defaults(task[action]) # replace, not update
handled = True handled = True
# keep going, let other plugins handle this if they want # keep going, let other plugins handle this if they want
for plugin in self._plugins: for plugin in self._plugins:
if plugin.can_handle(action): if plugin.can_handle(action):
try: try:
@ -63,14 +65,25 @@ class Dispatcher:
) )
self._log.debug(err) self._log.debug(err)
if self._exit: if self._exit:
# There was an execption exit # There was an exception exit
return False return False
if not handled: if not handled:
success = False success = False
self._log.error("Action %s not handled" % action) self._log.error("Action %s not handled" % action)
if self._exit: if self._exit:
# Invalid action exit # Invalid action exit
return False return False
if action == "plugins":
# Create a list of loaded plugin names
loaded_plugins = [plugin.__class__.__name__ for plugin in self._plugins]
# Load plugins that haven't been loaded yet
for plugin in Plugin.__subclasses__():
if plugin.__name__ not in loaded_plugins:
self._plugins.append(plugin(self._context))
return success return success

View file

@ -1,4 +1,5 @@
from .clean import Clean from .clean import Clean
from .create import Create from .create import Create
from .link import Link from .link import Link
from .plugins import Plugins
from .shell import Shell from .shell import Shell

46
dotbot/plugins/plugins.py Normal file
View file

@ -0,0 +1,46 @@
import glob
import os
from ..plugin import Plugin
from ..util import module
class Plugins(Plugin):
"""
Load plugins from a list of paths.
"""
_directive = "plugins"
_has_shown_override_message = False
def can_handle(self, directive):
return directive == self._directive
def handle(self, directive, data):
if directive != self._directive:
raise ValueError("plugins cannot handle directive %s" % directive)
return self._process_plugins(data)
def _process_plugins(self, data):
success = True
plugin_paths = []
for item in data:
self._log.lowinfo("Loading plugin from %s" % item)
plugin_path_globs = glob.glob(os.path.join(item, "*.py"))
if not plugin_path_globs:
success = False
self._log.warning("Failed to load plugin from %s" % item)
else:
for plugin_path in plugin_path_globs:
plugin_paths.append(plugin_path)
for plugin_path in plugin_paths:
abspath = os.path.abspath(plugin_path)
module.load(abspath)
if success:
self._log.info("All commands have been executed")
else:
self._log.error("Some commands were not successfully executed")
return success

View file

@ -0,0 +1,19 @@
"""Test that a plugin can be loaded by config file.
This file is copied to a location with the name "config_file.py",
and is then loaded from within the `test_cli.py` code.
"""
import os.path
import dotbot
class ConfigFile(dotbot.Plugin):
def can_handle(self, directive):
return directive == "plugin_config_file"
def handle(self, directive, data):
with open(os.path.abspath(os.path.expanduser("~/flag")), "w") as file:
file.write("config file plugin loading works")
return True