|
1 |
| -import os |
2 |
| - |
3 | 1 | import appdirs
|
| 2 | +import importlib |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from pathlib import Path |
4 | 7 |
|
5 | 8 | from ctfcli import __name__ as pkg_name
|
6 | 9 |
|
7 | 10 |
|
| 11 | +def load_plugins(commands: dict): |
| 12 | + plugin_dir = get_plugin_dir() |
| 13 | + sys.path.insert(0, plugin_dir) |
| 14 | + |
| 15 | + for plugin in sorted(os.listdir(plugin_dir)): |
| 16 | + plugin_path = Path(plugin_dir) / plugin / "__init__.py" |
| 17 | + |
| 18 | + logging.debug(f"Loading {plugin_path} as {plugin}") |
| 19 | + |
| 20 | + loaded = importlib.import_module(plugin) |
| 21 | + loaded.load(commands) |
| 22 | + |
| 23 | + sys.path.remove(str(plugin_dir)) |
| 24 | + |
| 25 | + |
8 | 26 | def get_plugin_dir():
|
9 |
| - plugins_path = os.path.join(get_data_dir(), "plugins") |
10 |
| - if not os.path.exists(plugins_path): |
| 27 | + if os.getenv("CTFCLI_PLUGIN_DIR"): |
| 28 | + plugins_path = get_custom_plugin_dir() |
| 29 | + else: |
| 30 | + plugins_path = get_data_dir() / "plugins" |
| 31 | + |
| 32 | + if not plugins_path.exists(): |
11 | 33 | os.makedirs(plugins_path)
|
12 |
| - return os.path.join(plugins_path) |
| 34 | + |
| 35 | + return str(plugins_path.absolute()) |
| 36 | + |
| 37 | + |
| 38 | +def get_custom_plugin_dir() -> Path: |
| 39 | + custom_plugins_path = Path(os.getenv("CTFCLI_PLUGIN_DIR")) |
| 40 | + |
| 41 | + if custom_plugins_path.is_absolute(): |
| 42 | + return custom_plugins_path |
| 43 | + |
| 44 | + base_dir = Path().parent.parent |
| 45 | + return base_dir / custom_plugins_path |
13 | 46 |
|
14 | 47 |
|
15 | 48 | def get_data_dir():
|
16 |
| - return appdirs.user_data_dir(appname=pkg_name) |
| 49 | + return Path(appdirs.user_data_dir(appname=pkg_name)) |
0 commit comments